library(dplyr)
library(dbplyr)
library(ggplot2)
library(lubridate)
library(tidyr)

# Access to our smartmeter DB
con <- DBI::dbConnect(RMariaDB::MariaDB(), host="db", dbname="smartmeter", user="root", password="rootme")

DBI::dbListTables(con)

# get some records
res <- DBI::dbSendQuery(con, "SELECT * FROM readings LIMIT 10")
DBI::dbFetch(res)

# dplyr style of reading data from mysql
# con %>% tbl("readings") %>% show_query()

# get all records from 2022
energy <- con %>% tbl("readings") %>% 
        select(total_m3_gas_consumed, total_kwh_consumed_high, total_kwh_consumed_low, 
                total_kwh_produced_high, total_kwh_produced_low, high_tarif, created_at) %>% 
        filter(created_at > "2021-09-01", created_at < "2021-12-31") %>%
        mutate(date = as.Date(created_at)) %>%
        collect()

# add hour column
energy <- energy %>% mutate(hour = format(strptime(created_at,"%Y-%m-%d %H:%M:%S"),'%H'))

# group by hour
energy_per_hour <- energy %>% 
                mutate(total_usage_kwh = (total_kwh_consumed_high+total_kwh_consumed_low)) %>%
                mutate(total_return_kwh = (total_kwh_produced_high+total_kwh_produced_low)) %>%
                group_by(date,hour) %>% 
                summarize(max_m3_gas_consumed = max(total_m3_gas_consumed), 
                            min_m3_gas_consumed = min(total_m3_gas_consumed), 
                            max_usage_kwh = max(total_usage_kwh), 
                            min_usage_kwh = min(total_usage_kwh), 
                            max_return_kwh = max(total_return_kwh), 
                            min_return_kwh = min(total_return_kwh)) %>%
                mutate(usage_m3 = max_m3_gas_consumed-min_m3_gas_consumed) %>%
                mutate(usage_kwh = max_usage_kwh-min_usage_kwh) %>%
                mutate(return_kwh = max_return_kwh-min_return_kwh) %>%
                select(-max_m3_gas_consumed, -min_m3_gas_consumed, -max_usage_kwh, -min_usage_kwh, -max_return_kwh, -min_return_kwh )

# mutate(usage_kwh = round(usage_kwh,1), return_kwh = round(return_kwh,1)) %>%
# and again, group by day
energy_per_day <- energy_per_hour %>% 
                  group_by(date) %>% 
                  summarize(usage_m3=round(sum(usage_m3),2), 
                            usage_kwh=round(sum(usage_kwh),2),
                            return_kwh=round(sum(return_kwh),2))

# some plots
energy_per_hour %>% 
            mutate(usage_kwh = round(usage_kwh,1), return_kwh = round(return_kwh,1)) %>%
            ggplot( aes(x=date, fill=hour, y=usage_kwh, text=as.character(date))) +
            geom_bar(stat="identity") +
            theme_bw() +  
            labs(x="Date", y="kwh") 

# daily usage/return of electricity
energy_per_day %>% pivot_longer(cols = usage_kwh:return_kwh) %>%
            ggplot( aes(x=date, y=value, fill=name, text=as.character(date))) +
            geom_bar(position="dodge", stat="identity") +
            geom_text(aes(label=value), vjust=-0.3, hjust=1.2, size=2.5) +
            theme_bw() +  
            labs(x="Date", y="kwh") 

# daily usage/return of gas
energy_per_day %>% pivot_longer(cols = usage_m3) %>%
            ggplot( aes(x=date, y=value, fill=name, text=as.character(date))) +
            geom_bar(position="dodge", stat="identity") +
            geom_text(aes(label=value), vjust=-0.3, hjust=1.2, size=2.5) +
            theme_bw() +  
            labs(x="Date", y="m3") 

