-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit for docker compose blog post
- Loading branch information
1 parent
5563ca0
commit 38fe5f5
Showing
226 changed files
with
126,171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
source("renv/activate.R") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ignore these files during build | ||
.Rhistory | ||
.Rproj.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.