Skip to content

Commit

Permalink
Hotfix update column aliases for new data format (#28)
Browse files Browse the repository at this point in the history
* Update column aliases for new data format

* include old names for backward compatibility
  • Loading branch information
phoebeheywood committed Jan 8, 2024
1 parent 4a41387 commit 031f827
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions db_interface/interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,13 @@ DBInterface <- R6::R6Class(
get_circuit_details_build_query = function(circuit_details) {
column_names <- names(read.csv(circuit_details, nrows = 3, header = TRUE))

column_aliases <- list(c_id = "_c_id", site_id = "_site_id", con_type = "_con_type", polarity = "_polarity")
column_aliases <- list(
c_id = "_c_id",
site_id = "_site_id",
con_type = "_con_type",
polarity = "_polarity",
X250ms_voltage = "_250ms_voltage"
)

query <- "REPLACE INTO circuit_details_raw
SELECT cast(_c_id AS integer) AS c_id, cast(_site_id AS integer) AS site_id, _con_type AS con_type,
Expand All @@ -396,6 +402,17 @@ DBInterface <- R6::R6Class(
column_names <- names(read.csv(site_details, nrows = 3, header = TRUE))

column_aliases <- list(
site_id = "_site_id",
s_state = "_s_state",
ac_cap_w = "_ac",
dc_cap_w = "_dc",
inverter_manufacturer = "_manufacturer",
inverter_model = "_model",
s_postcode = "_s_postcode",
pv_install_date = "_pv_installation_year_month"
)

column_aliases_old <- list(
site_id = "_site_id",
s_state = "_s_state",
ac = "_ac",
Expand All @@ -405,25 +422,34 @@ DBInterface <- R6::R6Class(
s_postcode = "_s_postcode",
pv_installation_year_month = "_pv_installation_year_month"
)

query <- "REPLACE INTO site_details_raw
SELECT cast(_site_id AS integer) AS site_id, cast(_s_postcode AS integer) AS s_postcode, _s_state AS s_state,
_ac AS ac, _dc AS dc, _manufacturer AS manufacturer, _model AS model,
_pv_installation_year_month AS pv_installation_year_month FROM site_details"

for (name in column_names) {
if (name %in% names(column_aliases)) {
if (all(column_names %in% names(column_aliases))) {
query <- "REPLACE INTO site_details_raw
SELECT cast(_site_id AS integer) AS site_id, cast(_s_postcode AS integer) AS s_postcode, _s_state AS s_state,
_ac / 1000 AS ac, _dc AS dc, _manufacturer AS manufacturer, _model AS model,
_pv_installation_year_month AS pv_installation_year_month FROM site_details"
for (name in column_names) {
query <- gsub(column_aliases[[name]], name, query)
} else {
stop(
paste0(
"The provided site details file should have the columns site_id, s_postcode, s_state, ac, dc,
manufacturer, model and pv_installation_year_month. The ac column should be in kW and the the dc in W.
Please check this file and try again. Currently cannot find ",
name
)
)
}
} else if (all(column_names %in% names(column_aliases_old))) {
query <- "REPLACE INTO site_details_raw
SELECT cast(_site_id AS integer) AS site_id, cast(_s_postcode AS integer) AS s_postcode, _s_state AS s_state,
_ac AS ac, _dc AS dc, _manufacturer AS manufacturer, _model AS model,
_pv_installation_year_month AS pv_installation_year_month FROM site_details"

for (name in column_names) {
query <- gsub(column_aliases_old[[name]], name, query)
}
} else {
stop(
paste0(
"The provided site details file should have the columns site_id, s_postcode, s_state, ac_cap_w, dc_cap_w,
inverter_manufacturer, inverter_model and pv_install_date. The ac and dc columns should both be in W.
Please check this file and try again. Currently cannot find ",
paste0(column_names[!column_names %in% names(column_aliases)], collapse = ", ")
)
)
}
return(query)
},
Expand Down

0 comments on commit 031f827

Please sign in to comment.