Ticket #10218: ptcc-time.R

File ptcc-time.R, 2.3 KB (added by dcf, 4 years ago)
Line 
1library(ggplot2)
2library(scales)
3
4transports <- c("obfs3", "meek", "fte", "<OR>")
5countries <- c("ru", "cn", "ir", "us", "de", "gb")
6
7x <- read.csv("merged-2016-02.csv.xz")
8x <- x[x$node=="bridge" & x$metric=="responses" & x$version=="", ]
9# Get just the date from stats_start.
10x$date <- as.Date(substr(x$stats_start, 1, 10))
11# Ignore last (incomplete) day.
12x <- x[x$date < max(x$date), ]
13# Sum multiple rows per date.
14x <- aggregate(list(val=x$val), list(date=x$date, fingerprint=x$fingerprint, country=x$country, transport=x$transport), sum)
15
16# Total responses per fingerprint.
17x.total <- x[x$country=="" & x$transport=="", c("date", "fingerprint", "val")]
18colnames(x.total)[colnames(x.total)=="val"] <- "val_total"
19# Total responses per fingerprint and transport.
20x.transport <- x[x$transport!="", c("date", "fingerprint", "transport", "val")]
21colnames(x.transport)[colnames(x.transport)=="val"] <- "val_transport"
22# Total responses per fingerprint and country.
23x.country <- x[x$country!="" & x$country %in% countries, c("date", "fingerprint", "country", "val")]
24colnames(x.country)[colnames(x.country)=="val"] <- "val_country"
25
26tmp <- expand.grid(date=unique(x.total$date), fingerprint=unique(x.total$fingerprint), country=unique(x.country$country), transport=unique(x.transport$transport))
27tmp <- merge(tmp, x.total, by=c("date", "fingerprint"), all.x=T)
28tmp$val_total[is.na(tmp$val_total)] <- 0
29tmp <- merge(tmp, x.transport, by=c("date", "fingerprint", "transport"), all.x=T)
30tmp$val_transport[is.na(tmp$val_transport)] <- 0
31tmp <- merge(tmp, x.country, by=c("date", "fingerprint", "country"), all.x=T)
32tmp$val_country[is.na(tmp$val_country)] <- 0
33
34tmp$val_lower <- pmax(0, tmp$val_transport + tmp$val_country - tmp$val_total)
35tmp$val_upper <- pmin(tmp$val_transport, tmp$val_country)
36
37# Sum by transport and country.
38x <- aggregate(tmp[, c("val_transport", "val_lower", "val_upper")], list(date=tmp$date, transport=tmp$transport, country=tmp$country), sum)
39
40for (transport in transports) {
41        y <- x[x$transport==transport & x$country %in% countries, ]
42
43        p <- ggplot(y, aes(date, ymin=val_lower, ymax=val_upper, y=(val_lower+val_upper)/2, color=country))
44        p <- p + geom_linerange(size=2, alpha=0.6)
45        p <- p + geom_line(alpha=0.6)
46        p <- p + ylab("responses") + ggtitle(transport)
47        p <- p + theme_minimal()
48        ggsave(sprintf("ptcc-time-%s.png", transport), p, width=5, height=4, dpi=120)
49}