Skip to content

Commit

Permalink
parse dates on first upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Sep 16, 2024
1 parent 2e0b656 commit 92cb94a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
31 changes: 18 additions & 13 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,25 @@ target_post_dataset <- function(req, res) {
return(bad_request_response(msg))
}

if (suppressWarnings(all(is.na(as.numeric(file_body[, xcol]))))) {
xtype <- "date"
file_body[, xcol] <- parse_date(file_body[, xcol])
if (suppressWarnings(all(is.na(file_body[, xcol])))) {
msg <- paste("Invalid x column values:",
"these should be numbers or dates in a standard format")
return(bad_request_response(msg))

Check warning on line 55 in R/api.R

View check run for this annotation

Codecov / codecov/patch

R/api.R#L53-L55

Added lines #L53 - L55 were not covered by tests
}
logger::log_info("Detected date values in x column")
} else {
logger::log_info("Detected numeric values in x column")
xtype <- "number"
}

logger::log_info(paste("Saving dataset", filename, "to disk"))
dir.create(path, recursive = TRUE)
utils::write.csv(file_body, file.path(path, "data"), row.names = FALSE)
write(xcol, file.path(path, "xcol"))
write(xtype, file.path(path, "xtype"))
response_success(jsonlite::unbox(filename))
}

Expand Down Expand Up @@ -160,20 +175,10 @@ read_dataset <- function(req, name, scale) {
dat$value <- log2(dat$value)
}
xcol <- readLines(file.path(path, "xcol"))
xtype <- readLines(file.path(path, "xtype"))
logger::log_info("Parsing x column values")
if (suppressWarnings(all(is.na(as.numeric(dat[, xcol]))))) {
xtype <- "date"
dat[, xcol] <- as.Date(lubridate::parse_date_time(dat[, xcol],
c("dmy", "mdy", "ymd", "ydm")))
if (suppressWarnings(all(is.na(dat[, xcol])))) {
msg <- paste("Invalid x column values:",
"these should be numbers or dates in a standard format")
porcelain::porcelain_stop(msg)
}
logger::log_info("Detected date values in x column")
} else {
logger::log_info("Deteced numeric values in x column")
xtype <- "number"
if (xtype == "date") {
dat[, xcol] <- as.Date(dat[, xcol], origin = "1970-01-01")
}
list(data = dat, xcol = xcol, xtype = xtype)
}
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ validate_scale <- function(scale) {
)
}
}

parse_date <- function(dat) {
as.Date(lubridate::parse_date_time(dat,
c("dmy", "mdy", "ymd", "ydm")))
}
1 change: 1 addition & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local_add_dataset <- function(dat, name, session = session_id, env = parent.fram
dir.create(filepath, recursive = TRUE)
write.csv(dat, file.path(filepath, "data"), row.names = FALSE)
write("day", file.path(filepath, "xcol"))
write("number", file.path(filepath, "xtype"))
withr::defer(fs::dir_delete(filepath), envir = env)
name
}
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ test_that("can get dataset with dates", {
value = 1:5,
day = dates)
router <- build_routes(cookie_key)
local_add_dataset(dat, name = "testdataset")
post_request <- local_POST_dataset_request(dat,
"testdataset",
cookie = cookie)
expect_equal(router$call(post_request)$status, 200)
res <- router$call(make_req("GET",
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = cookie))
Expand Down

0 comments on commit 92cb94a

Please sign in to comment.