Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ambiorix and React example #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 14_ambiorix_and_react/.gitignore
Binary file not shown.
47 changes: 47 additions & 0 deletions 14_ambiorix_and_react/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "R-Debugger",
"name": "Launch R-Workspace",
"request": "launch",
"debugMode": "workspace",
"workingDirectory": "${workspaceFolder}/Backend"
},
{
"type": "R-Debugger",
"name": "Debug R-File",
"request": "launch",
"debugMode": "file",
"workingDirectory": "${workspaceFolder}/Backend",
"file": "${file}"
},
{
"type": "R-Debugger",
"name": "Debug R-Function",
"request": "launch",
"debugMode": "function",
"workingDirectory": "${workspaceFolder}/Backend",
"file": "${file}",
"mainFunction": "main",
"allowGlobalDebugging": false
},
{
"type": "R-Debugger",
"name": "Debug R-Package",
"request": "launch",
"debugMode": "workspace",
"workingDirectory": "${workspaceFolder}/Backend",
"includePackageScopes": true,
"loadPackages": [
"."
]
},
{
"type": "R-Debugger",
"request": "attach",
"name": "Attach to R process",
"splitOverwrittenOutput": true
}
]
}
1 change: 1 addition & 0 deletions 14_ambiorix_and_react/Backend/BELGIC_CONFIG.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BELGIC_CONFIG="config.json"
54 changes: 54 additions & 0 deletions 14_ambiorix_and_react/Backend/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(future)
library(ambiorix)
library(coro)
library(httr)
library(RPostgres)
library(promises)
library(yaml)
library(dplyr)
library(tidyr)
library(lubridate)
library(jsonlite)
library(rworldmap)
library(sp)


plan(multisession)


source("./router/user-info.R")
source("./helpers/connect_db.R")
source("./jobs/job.R")
source("./jobs/fetchAndStoreData.R")
source("./jobs/setupDatabase.R")



PORT <- 1000L
localhost <- "127.0.0.1"
app <- Ambiorix$new()

app$use(function(req, res) {
res$header("Access-Control-Allow-Origin", "*")
res$header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
res$header("Access-Control-Allow-Headers", "Content-Type, Authorization")
})

app$options("*", function(req, res) {
res$header("Access-Control-Allow-Origin", "*")
res$header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
res$header("Access-Control-Allow-Headers", "Content-Type, Authorization")
res$send()
})

# Routers to get user-info
app$use(router)

promises::promise_all(job())



app$start(port = PORT, host = localhost)



Binary file added 14_ambiorix_and_react/Backend/belgic.exe
Binary file not shown.
6 changes: 6 additions & 0 deletions 14_ambiorix_and_react/Backend/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"path": "./",
"port": "8080",
"backends": "2",
"attempts": 3
}
7 changes: 7 additions & 0 deletions 14_ambiorix_and_react/Backend/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default:
postgres:
user: "postgres"
host: "localhost"
password: "sa"
port: "5432"
dbname: "ambiorix_test1"
57 changes: 57 additions & 0 deletions 14_ambiorix_and_react/Backend/controller/all_user_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
get_all_user_info <- function(req, res) {
cat("ui_request_handler called\n")

future({
con <- connectToDatabase()

if (is.list(con) && isTRUE(con$error)) {
return(list(status = 500, data = list(error = con$message)))
}

on.exit(dbDisconnect(con), add = TRUE)
cat("Database connection established\n")

result <- tryCatch({
dbGetQuery(con, "SELECT * FROM users ORDER BY id DESC LIMIT 200")
}, error = function(e) {
cat("Failed to execute query:", conditionMessage(e), "\n")
return(list(error = TRUE, message = paste("Failed to execute query:", conditionMessage(e))))
})

if (is.list(result) && isTRUE(result$error)) {
cat(result$message, "\n")
return(list(status = 500, data = list(error = result$message)))
}

if (is.null(result) || nrow(result) == 0) {
cat("No users found\n")
return(list(status = 404, data = list(message = "No users found.")))
} else {
cat("Users found\n")
return(list(status = 200, data = result))
}
}) %...>% {
if (.$status == 500) {
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else if (.$status == 404) {
res$set_status(404)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else {
json_data <- toJSON(.$data, pretty = TRUE)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(json_data)
}
} %...!% {
cat("Unexpected error:", conditionMessage(.), "\n")
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(list(error = "Unexpected server error")))
}
}
67 changes: 67 additions & 0 deletions 14_ambiorix_and_react/Backend/controller/user_count_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
get_user_count_info <- function(req, res){

future({
con <- connectToDatabase()

if (is.list(con) && isTRUE(con$error)) {
return(list(status = 500, data = list(error = con$message)))
}

on.exit(dbDisconnect(con), add = TRUE)
cat("Database connection established\n")

result <- tryCatch({
dbGetQuery(con, "SELECT * FROM users ORDER BY id DESC LIMIT 100")
}, error = function(e) {
cat("Failed to execute query:", conditionMessage(e), "\n")
return(list(error = TRUE, message = paste("Failed to execute query:", conditionMessage(e))))
})

if (is.list(result) && isTRUE(result$error)) {
cat(result$message, "\n")
return(list(status = 500, data = list(error = result$message)))
}

if (is.null(result) || nrow(result) == 0) {
cat("No users found\n")
return(list(status = 404, data = list(message = "No users found.")))
} else {
userCount <- userCounterPerYear(result)
return(list(status = 200, data = userCount))
}
}) %...>% {
if (.$status == 500) {
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else if (.$status == 404) {
res$set_status(404)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else {
json_data <- toJSON(.$data, pretty = TRUE)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(json_data)
}
} %...!% {
cat("Unexpected error:", conditionMessage(.), "\n")
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(list(error = "Unexpected server error")))
}
}


userCounterPerYear <- function(userData) {

result <- userData %>%
mutate(Year = year(registered_date)) %>%
group_by(Year) %>%
summarise(countOfUsers = n())
print(result)
return(result)
}
72 changes: 72 additions & 0 deletions 14_ambiorix_and_react/Backend/controller/user_gender_count.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
get_user_gender_info <- function(req, res) {
year <- req$params$year
print(year)
future({
con <- connectToDatabase()

if (is.list(con) && isTRUE(con$error)) {
return(list(status = 500, data = list(error = con$message)))
}

on.exit(dbDisconnect(con), add = TRUE)
cat("Database connection established\n")

getQuery <- sprintf("SELECT gender FROM users WHERE EXTRACT(YEAR FROM registered_date) = '%s'", year)
result <- tryCatch(
{
dbGetQuery(con, getQuery)
},
error = function(e) {
cat("Failed to execute query:", conditionMessage(e), "\n")
return(list(error = TRUE, message = paste("Failed to execute getQuery:", conditionMessage(e))))
}
)

if (is.list(result) && isTRUE(result$error)) {
cat(result$message, "\n")
return(list(status = 500, data = list(error = result$message)))
}

if (is.null(result) || nrow(result) == 0) {
cat("No users found\n")
return(list(status = 404, data = list(message = "No users found.")))
} else {
userGenderCount <- userGenderCount(result)
return(list(status = 200, data = userGenderCount))
}
}) %...>%
{
if (.$status == 500) {
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else if (.$status == 404) {
res$set_status(404)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(.$data))
} else {
json_data <- toJSON(.$data, pretty = TRUE)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(json_data)
}
} %...!% {
cat("Unexpected error:", conditionMessage(.), "\n")
res$set_status(500)
res$header("Access-Control-Allow-Origin", "*")
res$header("Content-Type", "application/json")
res$send(toJSON(list(error = "Unexpected server error")))
}
}


userGenderCount <- function(userData) {
genderDistribution <- userData %>%
group_by(gender) %>%
summarize(count = n()) %>%
mutate(percentage = (count / sum(count)) * 100)

return(genderDistribution)
}
Loading