-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from CDU-data-science-team/fix-data-upload
temporarily Fix data upload
- Loading branch information
Showing
10 changed files
with
175 additions
and
22 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
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,91 @@ | ||
--- | ||
title: "Write the predictions for all completed jobs to Database" | ||
author: "Experiences dashboard" | ||
date: 2023/11/14 | ||
format: | ||
html: | ||
embed-resources: true | ||
--- | ||
|
||
```{r} | ||
#| include: false | ||
library(DBI) | ||
library(odbc) | ||
library(dplyr) | ||
library(pins) | ||
``` | ||
|
||
|
||
|
||
## Intro | ||
|
||
Use this Script to manually write the prediction for all completed jobs that couldn't be auto written to the database by the scheduled API_url_tracker on Connect. | ||
This Script won't be needed if the [issue with the database upload](https://github.com/CDU-data-science-team/experiencesdashboard/issues/200) has been resolved. | ||
|
||
```{r} | ||
#| message: false | ||
conn <- odbc::dbConnect( | ||
drv = odbc::odbc(), | ||
driver = Sys.getenv("odbc_driver"), | ||
server = Sys.getenv("HOST_NAME"), | ||
UID = Sys.getenv("DB_USER"), | ||
PWD = Sys.getenv("MYSQL_PASSWORD"), | ||
database = "TEXT_MINING", | ||
Port = 3306, | ||
encoding = "UTF-8" | ||
) | ||
# connect to strategy unit Connect server | ||
board <- pins::board_connect() | ||
pending_jobs <- dplyr::tbl( | ||
conn, | ||
dbplyr::in_schema( | ||
"TEXT_MINING", | ||
"api_jobs" | ||
) | ||
) |> | ||
dplyr::filter(status == "completed") |> | ||
dplyr::collect() | ||
``` | ||
|
||
|
||
```{r} | ||
if (nrow(pending_jobs) > 0) { | ||
for (i in 1:nrow(pending_jobs)) { | ||
job <- pending_jobs[i, ] | ||
job_id <- as.character(job["job_id"]) | ||
trust_id <- as.character(job["trust_id"]) | ||
board_path <- as.character(job["pin_path"]) | ||
# get the prediction from the board | ||
prediction <- pins::pin_read(board, board_path) | ||
# update the main table on the database | ||
dplyr::rows_update( | ||
dplyr::tbl(conn, trust_id), | ||
prediction, | ||
by = "comment_id", | ||
unmatched = "ignore", | ||
copy = TRUE, | ||
in_place = TRUE | ||
) | ||
# update the job status as uploaded (successfully write prediction to main table) | ||
DBI::dbExecute(conn, paste("UPDATE api_jobs SET status='uploaded' WHERE job_id =", job_id)) | ||
# delete the prediction from the board | ||
pins::pin_delete(board, board_path) | ||
DBI::dbExecute( | ||
conn, | ||
sprintf("UPDATE api_jobs SET pin_path ='%s' WHERE job_id = %s", NA, job_id) | ||
) | ||
cat("Job", job_id, "prediction has been successfully written to database \n") | ||
} | ||
} else { | ||
cat("No uncompleted job") | ||
} | ||
``` |
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
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
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
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