Skip to content

Commit

Permalink
fixes in kernel computation (#33)
Browse files Browse the repository at this point in the history
* fix: timezone diff issue

* fix: userKernel -> kernelComputation
  • Loading branch information
bruceyyu authored Oct 1, 2024
1 parent 980498a commit 46960c5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions R/duckdb_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ my_env <- new.env()

duckdb_register_con <- function(df) {
my_env$con <- dbConnect(duckdb::duckdb(), ":memory:")
dbExecute(my_env$con, "INSTALL icu")
dbExecute(my_env$con, "LOAD icu")
dbExecute(my_env$con, "SET GLOBAL TimeZone = 'UTC'")
DBI::dbWriteTable(my_env$con, "gwalkr_mid_table", as.data.frame(df), overwrite = FALSE)
}

Expand Down
5 changes: 3 additions & 2 deletions R/gwalkr.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#' @param visConfig An optional config string to reproduce your chart. You can copy the string by clicking "export config" button on the GWalkR interface.
#' @param visConfigFile An optional config file path to reproduce your chart. You can download the file by clicking "export config" button then "download" button on the GWalkR interface.
#' @param toolbarExclude An optional list of strings to exclude the tools from toolbar UI. However, Kanaries brand info is not allowed to be removed or changed unless you are granted with special permission.
#' @param kernelComputation An optional boolean to enable the kernel mode computation which is much more efficient. Default is FALSE.
#'
#' @return An \code{htmlwidget} object that can be rendered in R environments
#'
Expand All @@ -33,7 +34,7 @@
#' gwalkr(mtcars)
#'
#' @export
gwalkr <- function(data, lang = "en", dark = "light", columnSpecs = list(), visConfig = NULL, visConfigFile = NULL, toolbarExclude = list(), useKernel = FALSE) {
gwalkr <- function(data, lang = "en", dark = "light", columnSpecs = list(), visConfig = NULL, visConfigFile = NULL, toolbarExclude = list(), kernelComputation = FALSE) {
if (!is.data.frame(data)) stop("data must be a data frame")
if (!is.null(visConfig) && !is.null(visConfigFile)) stop("visConfig and visConfigFile are mutually exclusive")
lang <- match.arg(lang, choices = c("en", "ja", "zh"))
Expand All @@ -45,7 +46,7 @@ gwalkr <- function(data, lang = "en", dark = "light", columnSpecs = list(), visC
visConfig <- readLines(visConfigFile, warn=FALSE)
}

if (useKernel) {
if (kernelComputation) {
gwalkr_kernel(data, lang, dark, rawFields, visConfig, toolbarExclude)
} else {
x = list(
Expand Down
15 changes: 13 additions & 2 deletions R/gwalkr_kernel.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
convert_timestamps_in_df <- function(df) {
for (colname in colnames(df)) {
if (inherits(df[[colname]], "POSIXt")) {
df[[colname]] <- as.numeric(as.POSIXct(df[[colname]], tz = "UTC")) * 1000
}
}
return(df)
}

gwalkr_kernel <- function(data, lang, dark, rawFields, visConfig, toolbarExclude) {
cat("GWalkR kernel mode init...")
cat("GWalkR kernel mode initialized...\n")
cat("Note: The console is unavailable while running a Shiny app. You can stop the app to use the console, or press Ctrl + C to terminate.\n")

filter_func <- function(data, req) {
query <- parseQueryString(req$QUERY_STRING)

res <- duckdb_get_data(query$sql)

res <- convert_timestamps_in_df(res)

json <- toJSON(
res,
auto_unbox = TRUE
Expand Down
4 changes: 3 additions & 1 deletion man/gwalkr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web_app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const App: React.FC<IAppProps> = observer((propsIn) => {
fields={rawFields}
chart={specList}
computation={getDataFromKernelBySql(fieldMetas, endpointPath)}
defaultConfig={{ config: { timezoneDisplayOffset: 0 } }}
/>
</div>
</React.StrictMode>
Expand Down

0 comments on commit 46960c5

Please sign in to comment.