Skip to content

Commit a74e9d7

Browse files
committed
initial draft of separated aws
1 parent 77d7e5e commit a74e9d7

File tree

9 files changed

+282
-192
lines changed

9 files changed

+282
-192
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ run:
77

88
run-nohup:
99
nohup Rscript run.R &
10+
11+
sync:
12+
Rscript sync.R

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export(make_shared_ensembles)
2727
export(make_shared_grids)
2828
export(make_target_ensemble_grid)
2929
export(make_target_param_grid)
30+
export(manage_S3_forecast_cache)
3031
export(overprediction)
3132
export(perform_sanity_checks)
3233
export(read_external_predictions_data)
@@ -39,6 +40,8 @@ export(slide_forecaster)
3940
export(underprediction)
4041
export(weighted_interval_score)
4142
importFrom(assertthat,assert_that)
43+
importFrom(aws.s3,get_bucket)
44+
importFrom(aws.s3,s3sync)
4245
importFrom(cli,cli_abort)
4346
importFrom(cli,hash_animal)
4447
importFrom(dplyr,across)
@@ -82,6 +85,7 @@ importFrom(epipredict,step_population_scaling)
8285
importFrom(epipredict,step_training_window)
8386
importFrom(epiprocess,as_epi_df)
8487
importFrom(epiprocess,epix_slide)
88+
importFrom(here,here)
8589
importFrom(magrittr,"%<>%")
8690
importFrom(magrittr,"%>%")
8791
importFrom(purrr,imap)

R/manage_S3.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#' Manage S3 cache
2+
#' @param rel_cache_dir The relative path to the cache directory, e.g.
3+
#' "data-processed/2021-09-01". Default is `"{tar_project}/objects"`
4+
#' @param bucket_name The name of the S3 bucket to sync.
5+
#' @param direction Set 'download' to download files, 'upload' to upload files,
6+
#' and `sync` to do both
7+
#' @param verbose Set to TRUE to print the files being synced.
8+
#' @param prefix specify the prefix for `s3sync`, which filters down which files
9+
#' to sync to those starting with `prefix`
10+
#'
11+
#' @importFrom aws.s3 s3sync get_bucket
12+
#' @importFrom here here
13+
#' @export
14+
manage_S3_forecast_cache <- function(rel_cache_dir = NULL,
15+
bucket_name = "forecasting-team-data",
16+
direction = "sync",
17+
verbose = FALSE,
18+
prefix = Sys.getenv("AWS_S3_PREFIX", "exploration"),
19+
tar_project = Sys.getenv("TAR_PROJECT", "exploration")) {
20+
if (is.null(rel_cache_dir)) {
21+
cache_path <- tar_project
22+
} else {
23+
cache_path <- here(rel_cache_dir)
24+
}
25+
if (!dir.exists(cache_path)) dir.create(cache_path)
26+
27+
prefix <- paste0(prefix, "/", tar_project, "/")
28+
s3b <- get_bucket(bucket_name, prefix = prefix)
29+
if (direction == "sync") {
30+
if (verbose) {
31+
s3sync(cache_path, s3b, prefix = prefix)
32+
} else {
33+
sink("/dev/null")
34+
s3sync(cache_path, s3b, prefix = prefix, verbose = FALSE)
35+
sink()
36+
}
37+
} else {
38+
if (verbose) {
39+
s3sync(cache_path, s3b, prefix = prefix, direction = direction)
40+
} else {
41+
sink("/dev/null")
42+
s3sync(cache_path, s3b, prefix = prefix, direction = direction, verbose = FALSE)
43+
sink()
44+
}
45+
}
46+
return(TRUE)
47+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ DEBUG_MODE=true
1616
USE_SHINY=false
1717
TAR_PROJECT=covid_hosp_explore
1818
EXTERNAL_SCORES_PATH=
19-
USE_AWS_S3=false
2019
AWS_S3_PREFIX=2023/exploration
2120

21+
# Pull from the bucket
22+
make sync
23+
2224
# Run the pipeline wrapper run.R.
2325
make run
2426
```
@@ -28,7 +30,6 @@ make run
2830
- `USE_SHINY` controls whether we start a Shiny server after producing the targets.
2931
- `TAR_PROJECT` controls which `targets` project is run by `run.R`.
3032
- `EXTERNAL_SCORES_PATH` controls where external scores are loaded from. If not set, external scores are not used.
31-
- `USE_AWS_S3` controls whether we use AWS S3 to store the cache.
3233
- `AWS_S3_PREFIX` controls the prefix to use in the AWS S3 bucket (a prefix is a pseudo-directory in a bucket).
3334

3435
## Development

extras/targets-common.R

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,6 @@ tar_option_set(
5050
)
5151
)
5252

53-
use_aws_s3 <- as.logical(Sys.getenv("USE_AWS_S3", FALSE))
54-
tar_project <- Sys.getenv("TAR_PROJECT", "covid_hosp_explore")
55-
aws_s3_prefix <- Sys.getenv("AWS_S3_PREFIX", "exploration")
56-
aws_s3_prefix <- paste0(aws_s3_prefix, "/", tar_project)
57-
if (use_aws_s3) {
58-
tar_option_set(
59-
repository = "aws",
60-
resources = tar_resources(
61-
aws = tar_resources_aws(
62-
bucket = "forecasting-team-data",
63-
prefix = aws_s3_prefix,
64-
region = "us-east-1"
65-
)
66-
)
67-
)
68-
}
69-
7053
linreg <- parsnip::linear_reg()
7154
quantreg <- epipredict::quantile_reg()
7255
ONE_AHEAD_FORECAST_NAME <- "forecast_by_ahead"

man/manage_S3_forecast_cache.Rd

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)