Skip to content

Commit

Permalink
initial commit for docker compose blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasalcock committed Jul 15, 2020
1 parent 5563ca0 commit 38fe5f5
Show file tree
Hide file tree
Showing 226 changed files with 126,171 additions and 0 deletions.
1 change: 1 addition & 0 deletions compose/bookings/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
3 changes: 3 additions & 0 deletions compose/bookings/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ignore these files during build
.Rhistory
.Rproj.user
39 changes: 39 additions & 0 deletions compose/bookings/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM openanalytics/r-base:3.6.1

RUN mkdir -p root/bookings_api/code
RUN mkdir -p root/bookings_api/data

COPY code /root/bookings_api/code
#COPY data /root/bookings_api/data

COPY renv.lock /root/bookings_api/renv.lock
COPY renv /root/bookings_api/renv

WORKDIR /root/bookings_api

RUN apt-get update -qq && apt-get install -y \
git-core \
libcurl4-gnutls-dev \
libxml2-dev \
libpq-dev \
sudo \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev \
libssl1.0.0 \
libpng-dev \
xtail \
wget

RUN R -e "install.packages('renv')"
RUN R -e "library('renv'); renv::consent(provided=TRUE)"
RUN R -e "renv::restore(confirm=FALSE)"
RUN R -e "install.packages('PKI')"

EXPOSE 6060

ENTRYPOINT ["R", "-e", "source('code/run.R')"]
49 changes: 49 additions & 0 deletions compose/bookings/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
IMAGE=bookings-api
VERSION=1.0
CONTAINER=api
TAR_NAME=../tars/bookings_api.tar
NETWORK=smst_network

build:
@echo 'Building Docker image'
DOCKER_BUILDKIT=1 docker build -t ${IMAGE}:${VERSION} .

cleanup:
@echo 'Removing untagged images'
@docker rmi $$(docker images -f "dangling=true" -q)

run:
@echo 'Running Container from API image'
@docker run --rm \
-d \
-v $$(pwd)/data:/root/bookings_api/data \
-p 6060:6060 \
--name ${CONTAINER} \
--net ${NETWORK} \
${IMAGE}:${VERSION}

ts:
docker run -it \
-v $$(pwd)/data:/root/bookings/data \
--entrypoint /bin/bash \
--rm \
--name ${CONTAINER}
${IMAGE}:${VERSION}

stop:
@echo 'Stopping API'
@docker stop ${CONTAINER}

stopall:
@echo 'Stopping all containers'
@docker stop $$(docker ps -a -q)

removeall:
@echo 'Removing all containers'
@docker container rm $$(docker ps -a -q)

view:
@docker ps -a

save:
@docker save -o ${TAR_NAME} ${IMAGE}:${VERSION}
98 changes: 98 additions & 0 deletions compose/bookings/code/api.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
library(dplyr)
library(jsonlite)
library(httr)
library(knitr)
library(ggplot2)
library(kableExtra)
library(forcats)

# load data
bookings <- read.csv("data/hotel_bookings.csv")

# get total number of bookings by hotel and country
no_bookings <- bookings %>%
dplyr::filter(is_canceled == 0) %>%
dplyr::group_by(hotel, country) %>%
dplyr::count() %>%
dplyr::ungroup() %>%
dplyr::rename(bookings = n)

# total revenue table by country
revenue_table <- bookings %>%
dplyr::filter(is_canceled == 0 & country != "NULL") %>%
dplyr::group_by(hotel, market_segment, distribution_channel,
deposit_type, customer_type, country) %>%
dplyr::summarise(revenue = sum(adr, na.rm = TRUE)) %>%
dplyr::ungroup()

#' Log request information
#' @filter log
function(req){
cat(
"Time of request:", as.character(Sys.time()),"\n",
"Method:", req$REQUEST_METHOD, "\n",
"Path info:", req$PATH_INFO, "\n",
"User agent:", req$HTTP_USER_AGENT, "@", req$REMOTE_ADDR, "\n"
)
plumber::forward()
}

#' Barchart
#' @png (width=800, height=600)
#' @param min_bookings
#' @param my_country
#' @get /plot
function(req, res, min_bookings, my_country){

# throw error
if(missing(min_bookings)){
stop(message("min_bookings is missing"))
}

# cast input to numeric
if(!is.numeric(min_bookings)){
min_bookings <- as.numeric(min_bookings)
}

# generate barchart
p1 <- no_bookings %>%
dplyr::filter(bookings >= min_bookings) %>%
ggplot2::ggplot(
mapping = aes(
x = forcats::fct_reorder(country, bookings),
y = bookings,
fill = hotel
)
) +
ggplot2::geom_bar(stat="identity", position = "dodge") +
ggplot2::coord_flip() +
ggplot2::theme_minimal() +
ggplot2::labs(
y = "Number of bookings",
x = "Country",
fill = "Hotel type",
title = paste("Countries with at least ", min_bookings, "bookings")
)

# use print to render plot
print(p1)
}


#' Filter revenue table
#' @serializer html
#' @param mycountry
#' @get /revenue
#' @post /revenue
function(req, res, mycountry){

# throw error
if(missing(mycountry)){
stop(message("country is missing"))
}

# render html table
revenue_table %>%
dplyr::filter(country == mycountry) %>%
knitr::kable(format = "html")
}
7 changes: 7 additions & 0 deletions compose/bookings/code/run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(plumber)

# create api
hotel_api <- plumber::plumb("code/api.R")

# run api
hotel_api$run(host = "0.0.0.0", port = 6060)
Loading

0 comments on commit 38fe5f5

Please sign in to comment.