-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: kernel computation feature (#27)
* fix: improve data parser * feat: init kernel computing feature powered by duckdb
- Loading branch information
Showing
12 changed files
with
284 additions
and
48 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: GWalkR | ||
Title: Interactive Exploratory Data Analysis Tool | ||
Version: 0.1.5 | ||
Version: 0.2.0 | ||
Authors@R: c( | ||
person("Yue", "Yu", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0002-9302-0793")), | ||
|
@@ -17,4 +17,7 @@ Imports: | |
htmlwidgets, | ||
jsonlite, | ||
openssl, | ||
shiny | ||
shiny, | ||
shinycssloaders, | ||
DBI, | ||
duckdb |
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
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,56 @@ | ||
library(DBI) | ||
library(duckdb) | ||
|
||
my_env <- new.env() | ||
|
||
duckdb_register_con <- function(df) { | ||
my_env$con <- dbConnect(duckdb::duckdb(), ":memory:") | ||
DBI::dbWriteTable(my_env$con, "gwalkr_mid_table", as.data.frame(df), overwrite = FALSE) | ||
} | ||
|
||
duckdb_unregister_con <- function(df) { | ||
if (!is.null(my_env$con)) { | ||
dbDisconnect(my_env$con) | ||
my_env$con <- NULL # Set to NULL after disconnecting | ||
} | ||
} | ||
|
||
duckdb_get_field_meta <- function() { | ||
if (exists("con", envir = my_env)) { | ||
result <- dbGetQuery(my_env$con, 'SELECT * FROM gwalkr_mid_table LIMIT 1') | ||
if (nrow(result) > 0) { | ||
return(get_data_meta_type(result)) | ||
} | ||
} else { | ||
stop("Database connection not found.") | ||
} | ||
} | ||
|
||
duckdb_get_data <- function(sql) { | ||
if (exists("con", envir = my_env)) { | ||
result <- dbGetQuery(my_env$con, sql) | ||
if (nrow(result) > 0) { | ||
return(result) | ||
} | ||
} else { | ||
stop("Database connection not found.") | ||
} | ||
} | ||
|
||
get_data_meta_type <- function(data) { | ||
meta_types <- list() | ||
|
||
for (key in names(data)) { | ||
value <- data[[key]] | ||
field_meta_type <- if (inherits(value, "POSIXct")) { | ||
if (!is.null(attr(value, "tzone"))) "datetime_tz" else "datetime" | ||
} else if (is.numeric(value)) { | ||
"number" | ||
} else { | ||
"string" | ||
} | ||
meta_types <- append(meta_types, list(list(key = key, type = field_meta_type))) | ||
} | ||
|
||
return(meta_types) | ||
} |
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
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,69 @@ | ||
gwalkr_kernel <- function(data, lang, dark, rawFields, visConfig, toolbarExclude) { | ||
cat("GWalkR kernel mode init...") | ||
|
||
filter_func <- function(data, req) { | ||
query <- parseQueryString(req$QUERY_STRING) | ||
|
||
res <- duckdb_get_data(query$sql) | ||
|
||
json <- toJSON( | ||
res, | ||
auto_unbox = TRUE | ||
) | ||
|
||
httpResponse( | ||
status = 200L, | ||
content_type = "application/json", | ||
content = json | ||
) | ||
} | ||
|
||
app <- shinyApp( | ||
ui = fluidPage( | ||
shinycssloaders::withSpinner( | ||
gwalkrOutput("gwalkr_kernel"), | ||
proxy.height="400px" | ||
) | ||
), | ||
|
||
server = function(input, output, session) { | ||
path <- session$registerDataObj( | ||
"GWALKR", | ||
NULL, | ||
filter_func | ||
) | ||
|
||
duckdb_register_con(data) | ||
fieldMetas <- duckdb_get_field_meta() | ||
|
||
x = list( | ||
rawFields = rawFields, | ||
i18nLang = lang, | ||
visSpec = visConfig, | ||
dark = dark, | ||
toolbarExclude = toolbarExclude, | ||
useKernel = TRUE, | ||
fieldMetas = fieldMetas, | ||
endpointPath = path | ||
) | ||
|
||
output$gwalkr_kernel = renderGwalkr({ | ||
htmlwidgets::createWidget( | ||
name = 'gwalkr', | ||
x, | ||
package = 'GWalkR', | ||
width='100%', | ||
height='100%' | ||
) | ||
}) | ||
session$onSessionEnded(function() { | ||
cat("GwalkR closed") | ||
duckdb_unregister_con() | ||
}) | ||
}, | ||
|
||
options=c(launch.browser = .rs.invokeShinyPaneViewer) | ||
) | ||
|
||
if (interactive()) app | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,31 @@ | ||
import type { IDataQueryPayload, IRow } from "@kanaries/graphic-walker/interfaces"; | ||
import { parser_dsl_with_meta } from "@kanaries/gw-dsl-parser"; | ||
|
||
const DEFAULT_LIMIT = 50_000; | ||
|
||
const sendHTTPData = (sql: string, endpointPath: string) => { | ||
return new Promise((resolve, reject) => { | ||
fetch(`${endpointPath}&sql=${encodeURIComponent(sql)}`) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
console.log("Processed data from R:", data); | ||
resolve(data); | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
|
||
export function getDataFromKernelBySql(fieldMetas: { key: string; type: string }[], endpointPath: string) { | ||
return async (payload: IDataQueryPayload) => { | ||
const sql = parser_dsl_with_meta( | ||
"gwalkr_mid_table", | ||
JSON.stringify({ ...payload, limit: payload.limit ?? DEFAULT_LIMIT }), | ||
JSON.stringify({ gwalkr_mid_table: fieldMetas }) | ||
); | ||
const result = (await sendHTTPData(sql, endpointPath)) ?? []; | ||
return result as IRow[]; | ||
}; | ||
} |
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
Oops, something went wrong.