library("tidyverse")
library("latex2exp")

## |--------------------------------+---------------------+-------------+------------------------------+--------------------------------------------+------------------------------------------------------------------|
## | Monitored Hardware             | Alim                | I2C Address | Notes                        | OS                                         | sha256sum (.img file)                                            |
## |--------------------------------+---------------------+-------------+------------------------------+--------------------------------------------+------------------------------------------------------------------|
## | 1Kohm resistor                 | some random 5V alim |        0x40 |                              |                                            |                                                                  |
## | Raspberry Pi 3 model B (2016)  | 5.1V rpi alim       |        0x41 | Powered through GPIO 2 and 6 | 2023-05-03-raspios-bullseye-arm64-lite.img | 9cd68ff450bc0fe0ac5b60e32723bc403ebca89d1214714e8e74361370d25204 |
## | Raspberry Pi 3 model B+ (2017) | 5.1V rpi alim       |        0x42 | Powered through GPIO 2 and 6 | 2023-05-03-raspios-bullseye-arm64-lite.img | 9cd68ff450bc0fe0ac5b60e32723bc403ebca89d1214714e8e74361370d25204 |
## | Raspberry Pi 4 model B  (2018) | 5.1V rpi alim       |        0x43 | Powered through GPIO 2 and 6 | 2023-05-03-raspios-bullseye-arm64-lite.img | 9cd68ff450bc0fe0ac5b60e32723bc403ebca89d1214714e8e74361370d25204 |
## |--------------------------------+---------------------+-------------+------------------------------+--------------------------------------------+------------------------------------------------------------------|


pure_read=read_csv("results/pure_read.csv")
zmq=read_csv("results/zmq.csv")

GG_DELAY_READ=function(exp,for_inmem=0){
    delays=exp%>%filter(inmem==for_inmem)%>%group_by(usen,deviceid)%>%mutate(tsnsec=(nsecs*10e-10)+timestamp)
    delays=delays%>%mutate(delay=tsnsec-lag(tsnsec,1,default=NA))%>%filter(!is.na(delay))%>%mutate(time=timestamp-startat)
    delays=delays%>%group_by(usen)%>%mutate(duration=max(time))
    delays=delays%>%group_by(usen,deviceid,time)%>%summarize(delay_sd=sd(delay),delay_mean=mean(delay),duration=mean(duration),delay_max=max(delay),delay_min=min(delay))
##### Store in file
    f=paste0("figures/delay_inmem",for_inmem,"_stddev.txt")
    window=0
    ina1<<-delays%>%filter(usen==1,time>window,time<duration-window)
    ina2<<-delays%>%filter(usen==2,time>window,time<duration-window)
    ina3<<-delays%>%filter(usen==3,time>window,time<duration-window)
    ina4<<-delays%>%filter(usen==4,time>window,time<duration-window)
    write(paste0("With 1 INA260 sd=",mean(ina1$delay_sd)," min=",mean(ina1$delay_min)," max=",mean(ina1$delay_max)),f)
    write(paste0("With 2 INA260 sd=",mean(ina2$delay_sd)," min=",mean(ina2$delay_min)," max=",mean(ina2$delay_max)),f,append=TRUE)
    write(paste0("With 3 INA260 sd=",mean(ina3$delay_sd)," min=",mean(ina3$delay_min)," max=",mean(ina3$delay_max)),f,append=TRUE)
    write(paste0("With 4 INA260 sd=",mean(ina4$delay_sd)," min=",mean(ina4$delay_min)," max=",mean(ina4$delay_max)),f,append=TRUE)
    ##### Compute additional metrics
    durations=delays%>%group_by(usen,deviceid)%>%summarise(duration=max(time))
    med=delays%>%group_by(usen)%>%summarize(median=round(median(delay_mean),digits=9),duration=mean(duration))
#### End
    label_at <- function(n) function(x) ifelse(x %% n == 0, x, ifelse (x==0,x,""))
    ggplot(delays,aes(time,delay_mean,color=deviceid))+geom_point()+
        facet_wrap(~usen,labeller = labeller(usen = c("1" = "With 1 ina260 in use",
                                                      "2" = "With 2 ina260 in use",
                                                      "3" = "With 3 ina260 in use",
                                                      "4" = "With 4 ina260 in use")))+
        ylab(TeX(r'(Average delay per seconds $\bar{d}$)'))+
        xlab("Time (s)")+
        labs(color="Device id")+
        scale_y_continuous(breaks = seq(0, 0.1,by=0.0001),labels=seq(0, 0.1,by=0.0001))+
        geom_text(data=med,aes(x=max(med$duration)/2,y=median,label=median),inherit.aes=FALSE,nudge_y=0.00004)+
        geom_hline(data=med,aes(yintercept=median),size=0.9,show.legend = FALSE,size=0.3)+
        theme(legend.position="top",legend.margin=margin(b = -0.2, unit='cm'))
    ggsave(paste0("figures/pure_read_inmem",for_inmem!=0,"_delay.pdf"),width=6,height=5.5)
}



GG_READ_PERF=function(exp,for_inmem=0){
##### Analyze
    exp=exp%>%filter(inmem==for_inmem)%>%mutate(time=timestamp-startat)
    data=exp%>%group_by(usen,deviceid)%>%group_by(usen,deviceid,timestamp,startat,endat)%>%summarize(nperdev=n())%>%mutate(time=timestamp-startat,duration=endat-startat)
##### Now complete missing measurements (say no value on at time 5 then a row with a zero at that time must be added)
    data=data%>%group_by(usen,deviceid)%>%complete(timestamp=seq(min(startat),min(endat),by=1),fill=list(nperdev=0))%>%mutate(startat=min(startat,na.rm=TRUE),endat=max(startat,na.rm=TRUE),time=timestamp-startat,duration=max(duration,na.rm=TRUE))
##### Compute additional metrics
    durations=exp%>%group_by(usen,deviceid)%>%summarise(duration=max(endat)-max(startat))
    med=data%>%group_by(usen)%>%summarize(median=round(median(nperdev)),duration=mean(duration))
#### End
    
    # Plot
    msg=paste0("(inmem=",for_inmem,")")
    nread=max(exp$nread)
    label_at <- function(n) function(x) ifelse(x %% n == 0, x, ifelse (x==0,x,""))
    ggplot(data,aes(x=time,y=nperdev,color=deviceid)) + geom_point(size=0.3) +geom_line() + xlab("Time (s)") + ylab(TeX(r'(Sample read frequency $f_r$)')) +
        facet_wrap(~usen,labeller = labeller(usen = c("1" = "With 1 ina260 in use",
                                                      "2" = "With 2 ina260 in use",
                                                      "3" = "With 3 ina260 in use",
                                                      "4" = "With 4 ina260 in use")))+
        scale_x_continuous(breaks = seq(0,3600,by=60),labels=label_at(60))+
        scale_y_continuous(breaks = seq(0, max(data$nperdev), by = 1000),expand = expansion(mult = 0.1))+
        #ggtitle(paste("Stop until",nread,"measurements are done", msg))+
        labs(color="Device id")+
        geom_vline(data=durations,aes(xintercept=duration,color=deviceid),linetype="dashed",show.legend = FALSE)+
        geom_hline(data=med,aes(yintercept=median),show.legend = FALSE,size=0.3)+
        geom_text(data=med,aes(x=duration/2,y=median,label=median),inherit.aes=FALSE,vjust=-0.55)+
        theme(legend.position="top",legend.margin=margin(b = -0.2, unit='cm'))
    ggsave(paste0("figures/pure_read_inmem",for_inmem!=0,".pdf"),width=6,height=5.5)
}

TABLE_DELAY=function(exp,for_inmem=0){
    delays=exp%>%filter(inmem==for_inmem)%>%group_by(usen,deviceid)%>%mutate(tsnsec=(nsecs*10e-10)+timestamp)
    delays=delays%>%group_by(usen,deviceid)%>%mutate(delay=tsnsec-lag(tsnsec,1,default=NA))%>%filter(!is.na(delay))%>%mutate(time=timestamp-startat)
    f=paste0("figures/fp_inmem",for_inmem,".txt")
    write("", f)
    delays%>%group_by(usen)%>%group_walk(function(data,grp){
        cur_usen=grp$usen
        cdf=ecdf(data$delay) # Get CDF
#### For fd
        fdefault=454.54
        percent=1-cdf(1/fdefault)
        write(paste0("inmem=",for_inmem," usen=",cur_usen,":     f=",fdefault,"Hz percent=",round(percent*100,digits=2),"%", " missings=",percent*length(data$delay)),f, append=TRUE)
#### For fmax
        fmax=3571.429
        percent=1-cdf(1/fmax)
        write(paste0("inmem=",for_inmem," usen=",cur_usen,":     f=",fmax,"Hz percent=",round(percent*100,digits=2),"%", " missings=",percent*length(data$delay),"\n----"),f, append=TRUE)
       # plot(cdf,xlim=c(0,0.001))
    })
}

GG_READ_POWER=function(exp,for_inmem=0){
    # Analyze
    exp=exp%>%filter(inmem==for_inmem)%>%mutate(time=timestamp-startat+nsecs*1e-9)
    nread=max(exp$nread)
    msg=paste0("(inmem=",for_inmem,")")
    data=exp%>%group_by(usen,deviceid)%>%mutate(duration=endat-startat)
    # Plot
    label_at <- function(n) function(x) ifelse(x %% n == 0, x, ifelse (x==0,x,""))
    ggplot(data,aes(x=time,y=power,color=deviceid)) + geom_point(size=0.3) +geom_line() + xlab("Time (s)") + ylab("Power consumption (W)") +
        facet_wrap(~usen,labeller = labeller(usen = c("1" = "With 1 ina260 in use",
                                                      "2" = "With 2 ina260 in use",
                                                      "3" = "With 3 ina260 in use",
                                                      "4" = "With 4 ina260 in use")))+
        scale_x_continuous(breaks = seq(0,3600,by=60),labels=label_at(60)) + ggtitle(paste("Stop until",nread,"measurements are done", msg))
    ggsave(paste0("figures/pure_read_inmem",for_inmem!=0,"_power.pdf"),width=10)
}

TABLE_READ_POWER=function(exp){
    power_0x40<<-median((exp%>%filter(addr=="0x40"))$power)
    power_0x41<<-median((exp%>%filter(addr=="0x41"))$power)
    power_0x42<<-median((exp%>%filter(addr=="0x42"))$power)
    power_0x43<<-median((exp%>%filter(addr=="0x43"))$power)
    power_0x40_std<<-round(sd((exp%>%filter(addr=="0x40"))$power),digits=2)
    power_0x41_std<<-round(sd((exp%>%filter(addr=="0x41"))$power),digits=2)
    power_0x42_std<<-round(sd((exp%>%filter(addr=="0x42"))$power),digits=2)
    power_0x43_std<<-round(sd((exp%>%filter(addr=="0x43"))$power),digits=2)
    f="figures/power_median.txt"
    write(paste0("1Kohm resistor                   0x40: ",power_0x40,"W(",power_0x40_std,")"), f)
    write(paste0("Raspberri Pi 3 model B (2016)    0x41: ",power_0x41,"W(",power_0x41_std,")"), f, append=TRUE)
    write(paste0("Raspberri Pi 3 model B+ (2017)   0x42: ",power_0x42,"W(",power_0x42_std,")"), f, append=TRUE)
    write(paste0("Raspberry Pi 4 model B  (2018)   0x43: ",power_0x43,"W(",power_0x43_std,")"), f, append=TRUE)
}

GG_ZMQ_PERF=function(exp){
##### Analyze
    data=exp%>%group_by(usen,deviceid,timestamp)%>%summarize(nperdev=n(),duration=max(duration))%>%mutate(time=timestamp-min(timestamp))
    data=data%>%group_by(usen,deviceid)%>%mutate(startat=min(timestamp),endat=max(timestamp))
    jean <<- data
##### Now complete missing measurements (say no value on at time 5 then a row with a zero at that time must be added)
    data=data%>%group_by(usen,deviceid)%>%complete(timestamp=seq(min(startat),min(startat)+max(duration),by=1),fill=list(nperdev=0))%>%mutate(startat=min(startat,na.rm=TRUE),endat=max(startat,na.rm=TRUE),time=timestamp-startat)
#### Now compute additional metrics
    med=data%>%group_by(usen)%>%summarize(median=round(median(nperdev)),duration_real=max(timestamp)-min(timestamp))
#### Plot
    label_at <- function(n) function(x) ifelse(x %% n == 0, x, ifelse (x==0,x,""))
    ggplot(data,aes(x=time,y=nperdev,color=deviceid))+geom_point()+geom_line()+
        facet_wrap(~usen,labeller = labeller(usen = c("1" = "With 1 ina260 in use",
                                                      "2" = "With 2 ina260 in use",
                                                      "3" = "With 3 ina260 in use",
                                                      "4" = "With 4 ina260 in use")))+
        scale_x_continuous(breaks = seq(0,3600,by=60),labels=label_at(60))+
        scale_y_continuous(breaks = seq(0, max(data$nperdev), by = 1000),expand = expansion(mult = 0.1))+
        geom_hline(data=med,aes(yintercept=median),show.legend = FALSE,size=0.3)+
        geom_text(data=med,aes(x=duration_real/2,y=median,label=median),inherit.aes=FALSE,vjust=-0.55)+
        #ggtitle(paste("Running zmq experiment for",unique(exp$duration),"s"))+
        labs(color="Device id")+
        xlab("Time (s)") + ylab(TeX(r'(Sample read frequency $f_r$)'))+
        theme(legend.position="top",legend.margin=margin(b = -0.2, unit='cm'))
    ggsave("figures/zmq.pdf")

}


TABLE_DELAY(pure_read,0)
TABLE_DELAY(pure_read,1)
GG_DELAY_READ(pure_read,0)
GG_DELAY_READ(pure_read,1)
GG_READ_POWER(pure_read,0)
GG_READ_POWER(pure_read,1)
GG_READ_PERF(pure_read,0)
GG_READ_PERF(pure_read,1)
GG_ZMQ_PERF(zmq)
TABLE_READ_POWER(pure_read)
