-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.R
197 lines (169 loc) · 5.48 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
library(shiny)
library(shinydashboard)
library(readr)
library(dplyr)
library(ggplot2)
library(DT)
library(glue)
library(lubridate)
library(gdata) # for gdata::humanReadable
source("random-names.R")
source("modules/detail.R")
ui <- dashboardPage(
dashboardHeader(
title = "CRAN whales"
),
dashboardSidebar(
dateInput("date", "Date", value = Sys.Date() - 3),
numericInput("count", "Show top N downloaders:", 6)
),
dashboardBody(
fluidRow(
tabBox(id = "tab", width = 12,
tabPanel("All traffic",
fluidRow(
valueBoxOutput("total_size", width = 4),
valueBoxOutput("total_count", width = 4),
valueBoxOutput("total_downloaders", width = 4)
),
plotOutput("all_hour")
),
tabPanel("Biggest whales",
plotOutput("downloaders", height = 500)
),
tabPanel("Whales by hour",
plotOutput("downloaders_hour", height = 500)
),
tabPanel("Detail view",
detailViewUI("details")
)
)
)
)
)
server <- function(input, output, session) {
### Reactive expressions ============================================
# Downloads data from cran-logs.rstudio.com, and parses it.
# Successful downloads are stored in the data_cache dir.
data <- eventReactive(input$date, ignoreNULL = FALSE, {
date <- input$date
validate(need(is.Date(date), "Invalid date"))
year <- lubridate::year(date)
url <- glue("http://cran-logs.rstudio.com/{year}/{date}.csv.gz")
path <- file.path("data_cache", paste0(date, ".csv.gz"))
withProgress(value = NULL, {
# Download to a temporary file path, then rename to the real
# path when the download is complete. We do this so other
# processes/sessions don't use partially downloaded files.
if (!file.exists(path)) {
tmppath <- paste0(path, "-", Sys.getpid())
setProgress(message = "Downloading data...")
download.file(url, tmppath)
if (!file.exists(path)) {
file.rename(tmppath, path)
} else {
file.remove(tmppath)
}
}
setProgress(message = "Parsing data...")
read_csv(path, col_types = "Dti---c-ci", progress = FALSE) %>%
filter(!is.na(package))
})
})
# Returns a data frame of just the top `input$count` downloaders of the day,
# with the columns:
# ip_id - an arbitrary integer that's used in place of the real IP address
# ip_name - the same as ip_id but using easier-to-remember labels like
# "quant_weasel" or "nutritious_lovebird".
# n - the number of downloads performed by this IP on this day
whales <- reactive({
validate(
need(is.numeric(input$count), "Invalid top downloader count"),
need(input$count > 0, "Too few downloaders"),
need(input$count <= 25, "Too many downloaders; 25 or fewer please")
)
data() %>%
count(ip_id, country) %>%
arrange(desc(n)) %>%
head(input$count) %>%
mutate(ip_name = factor(ip_id, levels = ip_id,
labels = glue("{random_name(length(ip_id), input$date)} [{country}]"))) %>%
select(-country)
})
# data(), filtered down to the downloads that are by the top `input$count`
# downloaders
whale_downloads <- reactive({
data() %>%
inner_join(whales(), "ip_id") %>%
select(-n)
})
### Outputs =========================================================
#### "All traffic" tab ----------------------------------------
output$total_size <- renderValueBox({
data() %>%
pull(size) %>%
as.numeric() %>% # Cast from integer to numeric to avoid overflow warning
sum() %>%
humanReadable() %>%
valueBox("bandwidth consumed")
})
output$total_count <- renderValueBox({
data() %>%
nrow() %>%
format(big.mark = ",") %>%
valueBox("files downloaded")
})
output$total_uniques <- renderValueBox({
data() %>%
pull(package) %>%
unique() %>%
length() %>%
format(big.mark = ",") %>%
valueBox("unique packages")
})
output$total_downloaders <- renderValueBox({
data() %>%
pull(ip_id) %>%
unique() %>%
length() %>%
format(big.mark = ",") %>%
valueBox("unique downloaders")
})
output$all_hour <- renderPlot({
whale_ip <- whales()$ip_id
data() %>%
mutate(
time = hms::trunc_hms(time, 60*60),
is_whale = ip_id %in% whale_ip
) %>%
count(time, is_whale) %>%
ggplot(aes(time, n, fill = is_whale)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("#666666", "#88FF99"),
labels = c("no", "yes")) +
ylab("Downloads") +
xlab("Hour") +
scale_y_continuous(labels = scales::comma)
})
#### "Biggest whales" tab -------------------------------------
output$downloaders <- renderPlot({
whales() %>%
ggplot(aes(ip_name, n)) +
geom_bar(stat = "identity") +
ylab("Downloads on this day")
})
#### "Whales by hour" tab -------------------------------------
output$downloaders_hour <- renderPlot({
whale_downloads() %>%
mutate(time = hms::trunc_hms(time, 60*60)) %>%
count(time, ip_name) %>%
ggplot(aes(time, n)) +
geom_bar(stat = "identity") +
facet_wrap(~ip_name) +
ylab("Downloads") +
xlab("Hour")
})
#### "Detail view" tab ----------------------------------------
callModule(detailView, "details", whales, whale_downloads)
}
shinyApp(ui, server)