Compare commits

..
1 Commits
Author SHA1 Message Date
gmp 2d9aa5d1cd Initial commit. 2020-08-27 10:47:13 -04:00
2 changed files with 29 additions and 62 deletions
+16 -36
View File
@@ -20,8 +20,7 @@ import (
) )
type Config struct { type Config struct {
MailHost, Username, Password, From string MailHost, Username, Password string
Recipients []string
MailPort int MailPort int
Len int64 Len int64
} }
@@ -53,14 +52,11 @@ func getConf() Config {
if _, err := toml.DecodeFile(confFile, &conf); err != nil { if _, err := toml.DecodeFile(confFile, &conf); err != nil {
log.Fatal("Error reading config file: ", err) log.Fatal("Error reading config file: ", err)
} }
if conf.From == "" {
conf.From = conf.Username + "@" + conf.MailHost
}
of, err := os.Create(path.Join(confDir, "c.R")) of, err := os.Create(path.Join(confDir, "c.R"))
if err != nil { if err != nil {
log.Fatal("Error creating R source file: ", err) log.Fatal("Error creating R source file: ", err)
} }
_, err = of.WriteString(rCode()) _, err = of.WriteString(rCode)
if err != nil { if err != nil {
log.Fatal("Error writing R source code: ", err) log.Fatal("Error writing R source code: ", err)
} }
@@ -103,50 +99,34 @@ func fetch() {
log.Printf("Length differs: body = %d", len(body)) log.Printf("Length differs: body = %d", len(body))
conf.Len = int64(len(body)) conf.Len = int64(len(body))
save() save()
of, err := os.Create(path.Join(path.Dir(confFile), "rows.csv"))
if err != nil {
log.Fatal("Cannot create rows.csv: ", err)
}
_, err = of.Write(body)
if err != nil {
log.Fatal("Error writing rows.csv: ", err)
}
confDir := path.Dir(confFile) confDir := path.Dir(confFile)
cmd := exec.Command("R", "--no-save", "-f", path.Join(confDir, "c.R")) cmd := exec.Command("R", "--no-save", "-f", path.Join(confDir, "c.R"))
log.Printf("Running R") log.Printf("Running R")
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
log.Print("Error running R: ", err) log.Fatal("Error running R: ", err)
return
} }
log.Printf("R completed") log.Printf("R completed")
if conf.MailHost == "" { if conf.MailHost == "" {
log.Print("No mail host set, returning") log.Print("No mail host set, returning")
return return
} }
f, err := os.Open("Rplot001.svg") f, err := os.Open("Rplots.pdf")
if err != nil { if err != nil {
log.Fatal("Error opening Rplot001.svg: ", err) log.Fatal("Error opening Rplots.pdf: ", err)
} }
defer f.Close() defer f.Close()
for _, r := range conf.Recipients { email := mailyak.New(fmt.Sprintf("%s:%d", conf.MailHost, conf.MailPort),
email := mailyak.New(fmt.Sprintf("%s:%d", conf.MailHost, conf.MailPort), smtp.PlainAuth("", conf.Username, conf.Password, conf.MailHost))
smtp.PlainAuth("", conf.Username, conf.Password, conf.MailHost)) email.To("gmp@wow.st")
email.To(r) email.From("covid@wow.st")
email.From(conf.From) msg := "Covid dashboard update for " + time.Now().Format("Monday, January 2 2006")
msg := "Covid dashboard update for " + time.Now().Format("Monday, January 2 2006") email.Subject(msg)
email.Subject(msg) email.Plain().Set(msg)
email.Plain().Set(msg) email.Attach("dashboard.pdf", f)
email.HTML().Set(`<html><body><img src="dashboard.svg"/></body></html>`) err = email.Send()
email.AttachInlineWithMimeType("dashboard.svg", f, "image/svg+xml") if err != nil {
err = email.Send() log.Print("Error sending email: ", err)
if err != nil {
log.Print("Error sending email: ", err)
} else {
log.Print("Sent email to ", r)
}
} }
} }
+13 -26
View File
@@ -1,19 +1,16 @@
package main package main
import ( var rCode string = `
"path"
)
func rCode() string {
return `
library(tidyverse) library(tidyverse)
library(ggplot2) library(ggplot2)
library(gridExtra) library(gridExtra)
library(lubridate) library(lubridate)
gc()
options(scipen=100000) options(scipen=100000)
d <- read_csv('`+ path.Dir(confFile) + `/rows.csv') d <- read_csv('~/prog/covid/nyopendata/rows.csv')
d <- pivot_longer(d, c('New Positives', 'Total Number of Tests Performed')) d <- pivot_longer(d, c('New Positives', 'Total Number of Tests Performed'))
d <- transmute(d, date=mdy(d$'Test Date'), county=County, name=name, value=value) d <- transmute(d, date=mdy(d$'Test Date'), county=County, name=name, value=value)
queens <- subset(d, county=='Queens') queens <- subset(d, county=='Queens')
@@ -24,26 +21,16 @@ trate <- pivot_wider(totals, names_from='name') %>% transmute(date=date,county=c
d2 <- rbind(queens, totals) d2 <- rbind(queens, totals)
p1 <- ggplot(queens, aes(x=date, y=value, group=name, color=name))+geom_line(color='black',alpha=1/3)+scale_y_log10(n.breaks=6, position='right')+labs(y='',color='')+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom')+geom_smooth(se=FALSE,method='gam') p1 <- ggplot(queens, aes(x=date, y=value, color=name))+geom_line()+scale_y_log10(n.breaks=6)+labs(y='',color='')+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom')
p2 <- ggplot(totals, aes(x=date, y=value, group=name, color=name))+geom_line(color='black',alpha=1/3)+stat_smooth(geom='line', linetype='dotted', method='gam', se=FALSE)+scale_y_log10(n.breaks=6, position='right')+labs(y='')+labs(color='')+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom')+geom_smooth(se=FALSE,method='gam') #p2 <- ggplot(d, aes(x=date, y=value, color=name))+geom_line(data=queens)+stat_smooth(geom='line', linetype='dotted', data=queens, method='gam', se=FALSE)+geom_line(alpha=1/3, data=totals)+stat_smooth(geom='line', linetype='dotted', data=totals, method='gam', se=FALSE)+scale_y_log10(n.breaks=6)+labs(y='')+labs(color='')+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom')
p2 <- ggplot(totals, aes(x=date, y=value, color=name))+geom_line()+stat_smooth(geom='line', linetype='dotted', method='gam', se=FALSE)+scale_y_log10(n.breaks=6)+labs(y='')+labs(color='')+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom')
pr1 <- ggplot(qrate, aes(x=date, y=value)) + geom_line(color='black',alpha=1/3)+geom_smooth(method='gam',formula=y~s(x, bs="cs"),se=FALSE)+scale_y_continuous(labels = scales::percent, position='right')+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.10))+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5)) pr1 <- ggplot(qrate, aes(x=date, y=value)) + geom_line()+geom_smooth(method='gam',formula=y~s(x, bs="cs"),se=FALSE)+scale_y_continuous(labels = scales::percent,)+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.04))+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5))
pr2 <- ggplot(trate, aes(x=date, y=value)) + geom_line(color='black',alpha=1/3)+geom_smooth(method='gam',formula=y~s(x, bs="cs"),se=FALSE)+scale_y_continuous(labels = scales::percent, position='right')+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.10))+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5)) pr2 <- ggplot(trate, aes(x=date, y=value)) + geom_line()+geom_smooth(method='gam',formula=y~s(x, bs="cs"),se=FALSE)+scale_y_continuous(labels = scales::percent)+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.04))+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))
queens <- tail(queens,n=32) p <- grid.arrange(p1,p2,pr1,pr2,ncol=2)
totals <- tail(totals,n=32)
qrate <- tail(qrate,n=16)
trate <- tail(trate,n=16)
p3 <- ggplot(queens, aes(x=date, y=value, color=name))+geom_line()+scale_y_log10(n.breaks=12, position='right')+labs(y='',color='')+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom') zip <- read_csv('~/prog/covid/nyopendata/timeseries.csv')
p4 <- ggplot(totals, aes(x=date, y=value, color=name))+geom_line()+scale_y_log10(n.breaks=12, position='right')+labs(y='')+labs(color='')+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))+theme(legend.position='bottom') zip <- pivot_longer(zip, c('COVID_CASE_COUNT','COVID_DEATH_COUNT','TOTAL_COVID_TESTS'))
zip$date <- mdy(zip$date)
pr3 <- ggplot(qrate, aes(x=date, y=value)) + geom_line()+scale_y_continuous(labels = scales::percent, position='right')+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.10))+ggtitle('Queens')+theme(plot.title=element_text(hjust = 0.5))
pr4 <- ggplot(trate, aes(x=date, y=value)) + geom_line()+scale_y_continuous(labels = scales::percent, position='right')+labs(y='Positive Rate')+coord_cartesian(ylim=c(0,0.10))+ggtitle('New York')+theme(plot.title=element_text(hjust = 0.5))
svg(width=8,height=16)
grid.arrange(p1, p2, pr1, pr2, p3, p4, pr3, pr4, ncol=2, top=format(d[[nrow(d),1]], "%B %d %Y"))
dev.off()
` `
}