Skip to content

Commit

Permalink
adding vns script, adding db update action
Browse files Browse the repository at this point in the history
  • Loading branch information
brycefrank committed Sep 25, 2024
1 parent b940da3 commit 0fcf65f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/db.update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: db-update

on:
push:
branches: [ mongo_upsert ]
pull_request:
branches: mongo_upsert

jobs:
build:
runs-on: ${{ matrix.config.os }}
strategy:
matrix:
config:
- {os: windows-latest, r: 'release'}
permissions:
contents: write

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
needs: check

- name: Clone allometric/allometric.
run: |
git clone https://github.com/allometric/allometric.git
- name: Update database
env:
MONGODB_URL_DEV: ${{secrets.MONGODB_URL_DEV}}
run: |
devtools::install("./allometric", reload = FALSE);
library(allometric);
devtools::load_all(".");
url <- Sys.getenv("MONGODB_URL_DEV");
model_con <- get_model_con(url);
pub_con <- get_pub_con(url);
update_con <- get_update_con(url);
update_db(pub_con, model_con, update_con)
warnings();
shell: Rscript {0}

- uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: main
commit_message: Updating models.RDS
file_pattern: '*.RDS'
39 changes: 39 additions & 0 deletions scripts/vns.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This script writes the local variable naming system into a json representation
library(mongolite)
library(dplyr)
library(tidyr)
dotenv::load_dot_env()

db_url <- Sys.getenv("MONGODB_URL_DEV")

variables_con <- mongo(url = db_url, db = "allodev", collection = "variables")
meta_con <- mongo(url = db_url, db = "allodev", collection = "meta")

var_defs_dir <- system.file("variable_defs", package = "allometric")

csv_paths <- list.files(var_defs_dir, full.names = TRUE)
exceptions <- c("components", "measures", "model_types_defined", "prefix", "suffix")

for(i in seq_along(csv_paths)) {
path <- csv_paths[[i]]
filename <- tools::file_path_sans_ext(basename(path))

if(!filename %in% exceptions) {

data <- read.csv(path) |>
unite("variable", -c(description), sep = "", remove = F)

variables_con$insert(data)
} else {
data <- read.csv(path)

data_list <- list(
filename = data
)

names(data_list) <- filename

json <- data_list |> jsonlite::toJSON()
meta_con$insert(json)
}
}

0 comments on commit 0fcf65f

Please sign in to comment.