From 810970cdf404a81ae1146e1eaf7f9f453f3f8b80 Mon Sep 17 00:00:00 2001 From: Wolfgang Ludger Hottgenroth Date: Wed, 11 Aug 2021 16:07:48 +0200 Subject: [PATCH] changes --- secondDbSteps.r | 51 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/secondDbSteps.r b/secondDbSteps.r index 5e89b49..8c90f36 100644 --- a/secondDbSteps.r +++ b/secondDbSteps.r @@ -9,18 +9,20 @@ get_freq_df <- function(con, startDate, endDate) { startStr <- strftime(startDate, "%Y-%m-%d %H:%M:%S", tz="UTC") endStr <- strftime(endDate, "%Y-%m-%d %H:%M:%S", tz="UTC") + # get from database res <-dbSendQuery(con, "select time, location, freq from mainsfrequency where valid=1 and time >= $1 and time < $2") dbBind(res, list(startStr, endStr)) frequencies <- dbFetch(res) dbClearResult(res) + # get values from all location at one time in a row freq_wide <- frequencies %>% pivot_wider(names_from = location, values_from = freq, values_fn = mean) + # remove measurement error (frequency gradient greater than THRESHOLD) THRESHOLD <- 0.5 - for (colIdx in 2:length(freq_wide)) { last <- freq_wide[[1, colIdx]] for (rowIdx in 1:length(freq_wide[[colIdx]])) { @@ -37,37 +39,56 @@ get_freq_df <- function(con, startDate, endDate) { con <- dbConnect(RPostgres::Postgres(), dbname='mainscnt', - host='db.mainscnt.eu', + host='172.16.10.27', user='wn') -START <- "2021-08-03 07:00:00" +START <- "2021-08-02 00:00:00" INTERVAL <- 3600 -for (offset in 0:0) { +freq_deviation_integrals <- data.frame() + +for (offset in 0:23) { startDate <- ymd_hms(START) + INTERVAL * offset endDate <- startDate + INTERVAL - freq_wide <- get_freq_df(con, startDate, endDate) - freq_wide_names <- names(freq_wide) - for (colIdx in 2:length(freq_wide)) { - colName.mean <- paste("mean.w.o.", freq_wide_names[colIdx], sep="") - colName.diff <- paste(freq_wide_names[colIdx], ".to.mean", sep="") + # get prepared and sanitized data from database + freq_wide <- get_freq_df(con, startDate, endDate) + # + location_names <- names(freq_wide)[-1] + + for (colIdx in 1:length(location_names)) { + colName.mean <- paste("mean.w.o.", location_names[colIdx], sep="") + colName.diff <- paste(location_names[colIdx], ".to.mean", sep="") freq_wide <- freq_wide %>% rowwise() %>% - mutate(!!colName.mean := mean(c_across(freq_wide_names[c(-1, - colIdx)]), na.rm=TRUE)) %>% - mutate(!!colName.diff := abs(eval(as.name(colName.mean)) - eval(as.name(freq_wide_names[colIdx]))) * 100) + mutate(!!colName.mean := mean(c_across(location_names[- colIdx]), na.rm=TRUE)) %>% + mutate(!!colName.diff := abs(eval(as.name(colName.mean)) - eval(as.name(location_names[colIdx]))) * 100) } means <- freq_wide %>% select(ends_with(".to.mean")) - means.means <- apply(means, 2, mean, na.rm=TRUE) + sum.means <- apply(means, 2, sum, na.rm=TRUE) + #printf("start: %s, end: %s\n", startDate, endDate) + #print(sum.means) + #printf("\n") + + next.row.no <- nrow(freq_deviation_integrals) + 1 + freq_deviation_integrals[next.row.no, c(1, 2)] <- c(strftime(startDate, "%Y-%m-%d %H:%M:%S", tz="UTC"), strftime(endDate, "%Y-%m-%d %H:%M:%S", tz="UTC")) + freq_deviation_integrals[next.row.no, c(3:(2 + length(sum.means)))] <- sum.means[order(names(sum.means))] - - # print(summary(freq_wide)) } - + +names(freq_deviation_integrals) <- c("startDate", "endDate", sort(location_names)) + + +for (colIdx in 1:length(location_names)) { + freq_deviation_integrals[,ncol(freq_deviation_integrals)+1] <- c(0, diff(freq_deviation_integrals[,2+colIdx],1)) + names(freq_deviation_integrals)[length(location_names)+2+colIdx] = paste("diff", sort(location_names)[colIdx], sep=".") +} + + dbDisconnect(con)