Add code for web logs analysis (#20008).
[tor-metrics-tasks.git] / task-4030 / bridge-blockings.R
blob4435354cff8a020916b3aa238c3c97058bf7fefc
1 library(ggplot2)
2 library(scales)
4 u <- read.csv("bridge-users.csv", stringsAsFactors = FALSE)
5 u <- u[u$date >= "2009-07-01" & u$date < "2011-08-01", c("date", "cn")]
6 ggplot(u, aes(x = as.Date(date), y = cn)) +
7 geom_line(size = 0.75) +
8 geom_rect(aes(NULL, NULL, xmin = as.Date("2010-01-01"),
9     xmax = as.Date("2010-06-30"), ymin = -Inf, ymax = Inf, fill = TRUE)) +
10 scale_fill_manual(name = "", breaks = TRUE,
11   values = alpha("purple", 0.005)) +
12 scale_x_date("", breaks = "6 months", minor_breaks = "1 month",
13   labels = date_format("%b %Y")) +
14 scale_y_continuous(name = "", limits = c(0, max(u$cn, na.rm = TRUE))) +
15 opts(legend.position = "none")
16 ggsave("bridge-users.pdf", width = 8, height = 4, dpi = 150)
18 b <- read.csv("bridge-blockings.csv", stringsAsFactors = FALSE)
19 b <- b[b$date >= '2010-01-01' & b$date <= '2010-06-30', ]
20 fingerprints <- unique(b[b$ips >= 100, "fingerprint"])
21 b <- b[b$fingerprint %in% fingerprints, ]
23 d <- data.frame(date = b$date, blocked = ifelse(b$ips < 40, 1, NA))
24 d <- na.omit(d)
25 d <- aggregate(d$blocked, by = list(date = d$date), sum)
26 e <- as.Date(setdiff(seq(from = as.Date("2010-01-01"),
27     to = as.Date("2010-06-30"), by = "1 day"), as.Date(d$date)),
28     origin = "1970-01-01")
29 u <- u[u$date >= '2010-01-01' & u$date <= '2010-06-30', ]
30 d <- rbind(data.frame(date = u$date, value = u$cn, variable = "Users"),
31     data.frame(date = d$date, value = d$x, variable = "Blocked Bridges"),
32     data.frame(date = as.character(e), value = 0,
33     variable = "Blocked Bridges"))
34 ggplot(d, aes(x = as.Date(date), y = value)) +
35 geom_line(size = 0.75) +
36 facet_grid(variable ~ ., scales = "free_y") +
37 scale_x_date("", breaks = "1 months", minor_breaks = "1 month",
38   labels = date_format("%b %Y")) +
39 scale_y_continuous(name = "")
40 ggsave("bridge-users-blockings.pdf", width = 8, height = 4, dpi = 150)
42 b <- data.frame(date = as.Date(b$date), ips = b$ips,
43     fingerprint = substr(b$fingerprint, 1, 4),
44     blocked = ifelse(b$blocked, "red", "black"))
45 bb <- b
46 bb[!is.na(b$ips) & b$ips >= 36, "ips"] <- NA
47 ggplot(b, aes(x = date, y = ips)) +
48 facet_wrap(~ fingerprint, ncol = 4) +
49 geom_line(size = 0.75) +
50 geom_point(size = 0.75) +
51 geom_hline(yintercept = 32, linetype = 2, size = 0.25) +
52 geom_point(data = bb, aes(x = date, y = ips), colour = "red", size = 3,
53     alpha = 0.25) +
54 scale_x_date("", breaks = "2 months", minor_breaks = "1 month",
55   labels = date_format("%b")) +
56 scale_y_continuous(name = "", breaks = c(0, 500, 1000)) +
57 scale_colour_manual(breaks = c("red", "black"),
58     values = c("red", "black")) +
59 opts(legend.position = "none")
60 ggsave("bridge-blockings.pdf", height = 9, width = 8)