Bearloga has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250879

Change subject: Add time frame selection (WIP) + Moves global smoothing 
selector into 'Global Settings' menu + Refactors code to be more time frame & 
smoothing friendly
......................................................................

Add time frame selection (WIP)
+ Moves global smoothing selector into 'Global Settings' menu
+ Refactors code to be more time frame & smoothing friendly

Bug: T116782

Change-Id: I06c8e56f98f077ea48234923650aeea4d140f13f
---
M server.R
M ui.R
M utils.R
3 files changed, 261 insertions(+), 175 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/discovery/rainbow 
refs/changes/79/250879/1

diff --git a/server.R b/server.R
index d91a6ab..706d75c 100644
--- a/server.R
+++ b/server.R
@@ -16,6 +16,24 @@
     existing_date <<- Sys.Date()
   }
 
+  # This is used to subset for getting the bounds for 
polloi::subset_by_date_range():
+  time_frame_range <- function(tf_selector) {
+    tf_setting <- input[[tf_selector]]
+    if ( tf_setting == 'global' ) {
+      if ( input$timeframe_global == 'custom' ) {
+        return(input$daterange_global)
+      } else {
+        tf_setting <- input$timeframe_global
+      }
+    }
+    return(switch(tf_setting,
+                  all = c(as.Date("2015-04-14"), Sys.Date()-1),
+                  week = c(Sys.Date()-8, Sys.Date()-1),
+                  month = c(Sys.Date()-31, Sys.Date()-1),
+                  quarter = c(Sys.Date()-91, Sys.Date()-1),
+                  custom = input[[paste(tf_selector, "daterange", sep = 
"_")]]))
+  }
+
   ## Desktop value boxes
   output$desktop_event_searches <- renderValueBox(
     valueBox(
@@ -46,18 +64,17 @@
 
   ## The dynamic graphs of events on desktop
   output$desktop_event_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(desktop_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_desktop_event)),
-                         xlab = "Date", ylab = "Events", title = "Desktop 
search events, by day")
+    desktop_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_desktop_event)) 
%>%
+      
polloi::subset_by_date_range(time_frame_range("desktop_event_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "Desktop 
search events, by day")
   })
 
   output$desktop_load_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(desktop_load_data,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_desktop_load)),
-                         xlab = "Date", ylab = "Load time (ms)", title = 
"Desktop load times, by day",
-                         use_si = FALSE)
+    desktop_load_data %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_desktop_load)) %>%
+      polloi::subset_by_date_range(time_frame_range("desktop_load_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Load time (ms)", title = 
"Desktop load times, by day", use_si = FALSE)
   })
 
   ## Mobile value boxes
@@ -90,18 +107,17 @@
 
   ## Mobile plots
   output$mobile_event_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(mobile_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_mobile_event)),
-                         xlab = "Date", ylab = "Events", title = "Mobile 
search events, by day")
+    mobile_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_mobile_event)) %>%
+      polloi::subset_by_date_range(time_frame_range("mobile_event_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "Mobile 
search events, by day")
   })
 
   output$mobile_load_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(mobile_load_data,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_mobile_load)),
-                         xlab = "Date", ylab = "Load time (ms)", "Mobile 
result load times, by day",
-                         use_si = FALSE)
+    mobile_load_data %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_mobile_load)) %>%
+      polloi::subset_by_date_range(time_frame_range("mobile_load_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Load time (ms)", title = 
"Mobile search events, by day", use_si = FALSE)
   })
 
   ## App value boxes
@@ -134,112 +150,103 @@
 
   ## App plots
   output$android_event_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(android_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_app_event)),
-                         xlab = "Date", ylab = "Events", "Android mobile app 
search events, by day")
+    android_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_app_event)) %>%
+      polloi::subset_by_date_range(time_frame_range("app_event_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "Android 
mobile app search events, by day")
   })
 
   output$android_load_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(android_load_data,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_app_load)),
-                         xlab = "Date", ylab = "Load time (ms)", "Android 
result load times, by day",
-                         use_si = FALSE)
+    android_load_data %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_app_load)) %>%
+      polloi::subset_by_date_range(time_frame_range("app_load_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Load time (ms)", title = 
"Android result load times, by day", use_si = FALSE)
   })
 
   output$ios_event_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(ios_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_app_event)),
-                         xlab = "Date", ylab = "Events", "iOS mobile app 
search events, by day")
+    ios_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_app_event)) %>%
+      polloi::subset_by_date_range(time_frame_range("app_event_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Events", title = "iOS mobile 
app search events, by day")
   })
 
   output$ios_load_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(ios_load_data,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_app_load)),
-                         xlab = "Date", ylab = "Load time (ms)", "iOS result 
load times, by day",
-                         use_si = FALSE)
+    ios_load_data %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_app_load)) %>%
+      polloi::subset_by_date_range(time_frame_range("app_load_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Load time (ms)", title = 
"iOS result load times, by day", use_si = FALSE)
   })
 
   ## API plots
   output$cirrus_aggregate <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(split_dataset$cirrus[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_fulltext_search)),
-                         xlab = "Date", ylab = "Searches", "Full-text via API 
usage by day",
-                         legend_name = "Searches")
+    split_dataset$cirrus[, c(1, 3)] %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_fulltext_search)) 
%>%
+      
polloi::subset_by_date_range(time_frame_range("fulltext_search_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Searches", title = 
"Full-text via API usage by day", legend_name = "Searches")
   })
 
   output$open_aggregate <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(split_dataset$open[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_open_search)),
-                         xlab = "Date", ylab = "Searches", "OpenSearch API 
usage by day",
-                         legend_name = "Searches")
+    split_dataset$open[, c(1, 3)] %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_open_search)) %>%
+      polloi::subset_by_date_range(time_frame_range("open_search_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Searches", title = 
"OpenSearch API usage by day", legend_name = "Searches")
   })
 
   output$geo_aggregate <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(split_dataset$geo[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_geo_search)),
-                         xlab = "Date", ylab = "Searches", "Geo Search API 
usage by day",
-                         legend_name = "Searches")
+    split_dataset$geo[, c(1, 3)] %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_geo_search)) %>%
+      polloi::subset_by_date_range(time_frame_range("geo_search_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Searches", title = "Geo 
Search API usage by day", legend_name = "Searches")
   })
 
   output$language_aggregate <- renderDygraph({
-    polloi::make_dygraph(data = 
polloi::smoother(split_dataset$language[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_language_search)),
-                         xlab = "Date", ylab = "Searches", "Language Search 
API usage by day",
-                         legend_name = "Searches")
+    split_dataset$language[, c(1, 3)] %>%
+      
polloi::subset_by_date_range(time_frame_range("language_search_timeframe")) %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_language_search)) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Searches", title = "Language 
Search API usage by day", legend_name = "Searches")
   })
 
   output$prefix_aggregate <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(split_dataset$prefix[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_prefix_search)),
-                         xlab = "Date", ylab = "Searches", "Prefix Search API 
usage by day",
-                         legend_name = "Searches")
+    split_dataset$prefix[, c(1, 3)] %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_prefix_search)) 
%>%
+      
polloi::subset_by_date_range(time_frame_range("prefix_search_timeframe")) %>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Searches", title = "Prefix 
Search API usage by day", legend_name = "Searches")
   })
 
   # Failure plots
   output$failure_rate_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(failure_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_failure_rate)),
-                         xlab = "Date", ylab = "Queries", "Search Queries with 
Zero Results, by day")
+    failure_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_failure_rate)) %>%
+      polloi::subset_by_date_range(time_frame_range("failure_rate_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Queries", title = "Search 
Queries with Zero Results, by day")
   })
 
   output$failure_rate_change_plot <- renderDygraph({
-    polloi::make_dygraph(data = 
polloi::smoother(failure_roc_dygraph_set[,c(1,3)],
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_failure_rate)),
-                         xlab = "Date", ylab = "Change (%)", "Zero Results 
rate change, by day",
-                         legend_name = "Change")
+    failure_roc_dygraph_set[, c(1, 3)] %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, input$smoothing_failure_rate)) %>%
+      polloi::subset_by_date_range(time_frame_range("failure_rate_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Change (%)", title = "Zero 
Results rate change, by day", legend_name = "Change")
   })
 
   output$failure_breakdown_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(failure_breakdown_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_failure_breakdown)),
-                         xlab = "Date", ylab = "Zero Results Rate (%)", "Zero 
result rate by search type")
+    failure_breakdown_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, 
input$smoothing_failure_breakdown)) %>%
+      
polloi::subset_by_date_range(time_frame_range("failure_breakdown_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Zero Results Rate (%)", 
title = "Zero result rate by search type")
   })
 
   output$suggestion_dygraph_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(suggestion_dygraph_set,
-                                         smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                              
input$smoothing_failure_suggestions)),
-                         xlab = "Date", ylab = "Zero Results Rate (%)", "Zero 
Result Rates with Search Suggestions")
+    suggestion_dygraph_set %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, 
input$smoothing_failure_suggestions)) %>%
+      
polloi::subset_by_date_range(time_frame_range("failure_suggestions_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "Date", ylab = "Zero Results Rate (%)", 
title = "Zero Result Rates with Search Suggestions")
   })
 
   output$lethal_dose_plot <- renderDygraph({
-    polloi::make_dygraph(data = polloi::smoother(user_page_visit_dataset,
-                                                 smooth_level = 
polloi::smooth_switch(input$smoothing_global,
-                                                                               
       input$smoothing_lethal_dose_plot)),
-                         xlab = "", ylab = "Time (s)",
-                         title = "Time at which we have lost N% of the users") 
%>%
+    user_page_visit_dataset %>%
+      polloi::smoother(smooth_level = 
polloi::smooth_switch(input$smoothing_global, 
input$smoothing_lethal_dose_plot)) %>%
+      polloi::subset_by_date_range(time_frame_range("lethal_dose_timeframe")) 
%>%
+      polloi::make_dygraph(xlab = "", ylab = "Time (s)", title = "Time at 
which we have lost N% of the users") %>%
       dyAxis("x", ticker = "Dygraph.dateTicker", axisLabelFormatter = 
CustomAxisFormatter,
              axisLabelWidth = 100, pixelsPerLabel = 80) %>%
       dyLegend(labelsDiv = "lethal_dose_plot_legend")
@@ -250,54 +257,61 @@
     date_range <- input$kpi_summary_date_range_selector
     switch(date_range,
            daily = {
-             temp <- polloi::safe_tail(desktop_load_data, 2)$timestamp %>% {
-               paste0(as.character(., "%A, %b "),
-                      sub("([a-z]{2})", "<sup>\\1</sup>",
-                          sapply(as.numeric(as.character(., "%e")), 
toOrdinal)))
-             }
+             dates <- Sys.Date() - c(1, 2)
+             temp <- dates %>%
+               as.character("%e") %>%
+               as.numeric %>%
+               sapply(toOrdinal) %>%
+               sub("([a-z]{2})", "<sup>\\1</sup>", .) %>%
+               paste0(as.character(dates, "%A, %b "), .)
            },
            weekly = {
-             date_range_index <- c(1, 7, 8, 14)
-             temp <- polloi::safe_tail(desktop_load_data, 
date_range_index[4])$timestamp %>% {
-               paste0(as.character(.[date_range_index], "%b "),
-                      sub("([a-z]{2})", "<sup>\\1</sup>",
-                          sapply(as.numeric(as.character(.[date_range_index], 
"%e")), toOrdinal)))
-             } %>% {
-               c(paste(.[1:2], collapse = "-"), paste(.[3:4], collapse = "-"))
-             }
+             dates <- Sys.Date() - c(1, 8, 9, 15)
+             temp <- dates %>%
+               as.character("%e") %>%
+               as.numeric %>%
+               sapply(toOrdinal) %>%
+               sub("([a-z]{2})", "<sup>\\1</sup>", .) %>%
+               paste0(as.character(dates, "%b "), .) %>%
+               {
+                 c(paste(.[1:2], collapse = "-"), paste(.[3:4], collapse = 
"-"))
+               }
            },
            monthly = {
-             date_range_index <- c(1, 31, 31, 60)
-             temp <- polloi::safe_tail(desktop_load_data, 
date_range_index[4])$timestamp %>% {
-               paste0(as.character(.[date_range_index], "%b "),
-                      sub("([a-z]{2})", "<sup>\\1</sup>",
-                          sapply(as.numeric(as.character(.[date_range_index], 
"%e")), toOrdinal)))
-             } %>% {
-               c(paste(.[1:2], collapse = "-"), paste(.[3:4], collapse = "-"))
-             }
+             dates <- Sys.Date() - c(1, 31, 32, 61)
+             temp <- dates %>%
+               as.character("%e") %>%
+               as.numeric %>%
+               sapply(toOrdinal) %>%
+               sub("([a-z]{2})", "<sup>\\1</sup>", .) %>%
+               paste0(as.character(dates, "%b "), .) %>%
+               {
+                 c(paste(.[1:2], collapse = "-"), paste(.[3:4], collapse = 
"-"))
+               }
            },
            quarterly = {
-             date_range_index <- c(1, 90)
-             temp <- polloi::safe_tail(desktop_load_data, 
date_range_index[2])$timestamp %>% {
-               paste0(as.character(.[date_range_index], "%B "),
-                      sub("([a-z]{2})", "<sup>\\1</sup>",
-                          sapply(as.numeric(as.character(.[date_range_index], 
"%e")), toOrdinal)))
-             } %>% paste0(collapse = "-")
-             return(HTML("<h3 class='kpi_date'>KPI summary for ", temp, 
":</h3>"))
+             dates <- Sys.Date() - c(1, 91)
+             return(dates %>%
+               as.character("%e") %>%
+               as.numeric %>%
+               sapply(toOrdinal) %>%
+               sub("([a-z]{2})", "<sup>\\1</sup>", .) %>%
+               paste0(as.character(dates, "%B "), .) %>%
+               paste0(collapse = "-") %>%
+               HTML("<h3 class='kpi_date'>KPI summary for ", ., ":</h3>"))
            })
     return(HTML("<h3 class='kpi_date'>KPI summary for ", temp[2], ", and % 
change from ", temp[1], ":</h3>"))
   })
   output$kpi_summary_box_load_time <- renderValueBox({
     date_range <- input$kpi_summary_date_range_selector
-    x <- lapply(list(desktop_load_data, mobile_load_data,
-                     android_load_data, ios_load_data),
-                polloi::safe_tail, n = date_range_switch(date_range)) %>%
+    x <- list(desktop_load_data, mobile_load_data, android_load_data, 
ios_load_data) %>%
+      lapply(polloi::subset_by_date_range, from = start_date(date_range)) %>%
       lapply(function(data_tail) return(data_tail$Median))
     if ( date_range == "quarterly" ) {
-      y <- median(apply(do.call(cbind, x), 1, median))
+      y <- median(apply(do.call(cbind_fill, x), 1, median))
       return(valueBox(subtitle = "Load time", value = sprintf("%.0fms", y), 
color = "orange"))
     }
-    x %<>% do.call(cbind, .) %>% apply(MARGIN = 1, FUN = median)
+    x %<>% do.call(cbind_fill, .) %>% apply(MARGIN = 1, FUN = median)
     y1 <- median(polloi::half(x)); y2 <- median(polloi::half(x, FALSE)); z <- 
100 * (y2 - y1) / y1
     if (abs(z) > 0) {
       return(valueBox(subtitle = sprintf("Load time (%.1f%%)", z),
@@ -308,7 +322,7 @@
   })
   output$kpi_summary_box_zero_results <- renderValueBox({
     date_range <- input$kpi_summary_date_range_selector
-    x <- polloi::safe_tail(failure_dygraph_set, date_range_switch(date_range))
+    x <- polloi::subset_by_date_range(failure_dygraph_set, from = 
start_date(date_range))
     x <- transform(x, Rate = `Zero Result Queries` / `Search Queries`)$Rate
     if (date_range == "quarterly") {
       return(valueBox(subtitle = "Zero results rate", color = "orange",
@@ -328,8 +342,8 @@
   output$kpi_summary_box_api_usage <- renderValueBox({
     date_range <- input$kpi_summary_date_range_selector
     x <- lapply(split_dataset, function(x) {
-      polloi::safe_tail(x, date_range_switch(date_range))$events
-    }) %>% do.call(cbind, .) %>%
+      polloi::subset_by_date_range(x, from = start_date(date_range))$events
+    }) %>% do.call(cbind_fill, .) %>%
       transform(total = cirrus + geo + language + open + prefix) %>%
       { .$total }
     if (date_range == "quarterly") {
@@ -351,7 +365,7 @@
       return(valueBox(subtitle = "User engagement", color = "black", value = 
"NA"))
     }
     #=========================================================================
-    x <- polloi::safe_tail(augmented_clickthroughs, 
date_range_switch(date_range))
+    x <- polloi::subset_by_date_range(augmented_clickthroughs, from = 
start_date(date_range))
     if (date_range == "quarterly") {
       return(valueBox(subtitle = "User engagement", color = "orange",
                       value = sprintf("%.1f%%", median(x$user_engagement))))
@@ -370,12 +384,12 @@
                     value = sprintf("%.1f%%", y2), color = "orange"))
   })
   output$kpi_summary_api_usage_proportions <- renderPlot({
-    n <- date_range_switch(input$kpi_summary_date_range_selector, 1, 7, 30, 90)
-    api_latest <- cbind("Full-text via API" = 
polloi::safe_tail(split_dataset$cirrus, n)$events,
-                        "Geo Search" = polloi::safe_tail(split_dataset$geo, 
n)$events,
-                        "OpenSearch" = polloi::safe_tail(split_dataset$open, 
n)$events,
-                        "Language" = polloi::safe_tail(split_dataset$language, 
n)$events,
-                        "Prefix" = polloi::safe_tail(split_dataset$prefix, 
n)$events) %>%
+    start_date <- Sys.Date() - (switch(input$kpi_summary_date_range_selector, 
daily = 1, weekly = 7, monthly = 30, quarterly = 90) + 1)
+    api_latest <- cbind_fill("Full-text via API" = 
polloi::subset_by_date_range(split_dataset$cirrus, from = start_date)$events,
+                             "Geo Search" = 
polloi::subset_by_date_range(split_dataset$geo, from = start_date)$events,
+                             "OpenSearch" = 
polloi::subset_by_date_range(split_dataset$open, from = start_date)$events,
+                             "Language" = 
polloi::subset_by_date_range(split_dataset$language, from = start_date)$events,
+                             "Prefix" = 
polloi::subset_by_date_range(split_dataset$prefix, from = start_date)$events) 
%>%
       apply(2, median) %>% round
     api_latest <- data.frame(API = names(api_latest),
                              Events = api_latest,
@@ -402,7 +416,7 @@
         Median = apply(., 1, median)
         cbind(Median = Median, .)
       } %>%
-      cbind(timestamp = polloi::safe_tail(desktop_load_data, 
num_of_days_in_common)$timestamp, .) %>%
+      cbind(date = polloi::safe_tail(desktop_load_data, 
num_of_days_in_common)$date, .) %>%
       polloi::smoother(smooth_level = ifelse(smooth_level == "global", 
input$smoothing_global, smooth_level), rename = FALSE) %>%
       { xts::xts(.[, -1], order.by = .[, 1]) }
     return(dygraph(load_times,
@@ -449,7 +463,7 @@
   })
   output$kpi_api_usage_series <- renderDygraph({
     smooth_level <- input$smoothing_kpi_api_usage
-    api_usage <- cbind(timestamp = split_dataset$cirrus$timestamp, 
as.data.frame(lapply(split_dataset, function(x) x$events)))
+    api_usage <- cbind(date = split_dataset$cirrus$date, 
as.data.frame(lapply(split_dataset, function(x) x$events)))
     if ( input$kpi_api_usage_series_include_open ) {
       api_usage <- transform(api_usage, all = cirrus + geo + language + open + 
prefix)
     } else {
diff --git a/ui.R b/ui.R
index 0d40b60..4f4a487 100644
--- a/ui.R
+++ b/ui.R
@@ -3,6 +3,25 @@
 library(dygraphs)
 options(scipen = 500)
 
+# Standardised input selector for smoothing
+smooth_select <- function(input_id, label = "Smoothing") {
+  return(selectInput(inputId = input_id, label = label, selectize = TRUE,
+                     selected = "global", choices = c("Use Global Setting" = 
"global",
+                                                      "No Smoothing" = "day", 
"Weekly Median" = "week", "Monthly Median" = "month")))
+}
+
+# Standardized selectors for time frame
+timeframe_select <- function(input_id, label = "Time Frame") {
+  return(selectInput(inputId = input_id, label = label, selectize = TRUE, 
selected = "global",
+                     choices = c("Use Global Setting" = "global", "Last 7 
days" = "week",
+                                 "Last 30 days" = "month", "Last 90 days" = 
"quarter", "Custom" = "custom")))
+}
+timeframe_daterange <- function(select_input_id, label = "Custom Date Range") {
+  return(conditionalPanel(paste0("input.", select_input_id," == 'custom'"),
+                          dateRangeInput(paste(select_input_id, "daterange", 
sep = "_"), label = label,
+                                         start = Sys.Date()-11, end = 
Sys.Date()-1, min = "2015-04-14")))
+}
+
 #Header elements for the visualisation
 header <- dashboardHeader(title = "Search Metrics", disable = FALSE)
 
@@ -14,11 +33,17 @@
   ),
   sidebarMenu(
     menuItem(text = "KPIs",
+             div(selectInput("kpi_summary_date_range_selector",
+                             label = "KPI data range", multiple = FALSE, 
selected = "weekly",
+                             choices = list("Yesterday" = "daily", "Last 7 
days" = "weekly",
+                                            "Last 30 days" = "monthly", "Last 
90 days" = "quarterly")),
+                 style = "margin-bottom:-10px;"),
              menuSubItem(text = "Summary", tabName = "kpis_summary"),
              menuSubItem(text = "Load times", tabName = "kpi_load_time"),
              menuSubItem(text = "Zero results", tabName = "kpi_zero_results"),
              menuSubItem(text = "API usage", tabName = "kpi_api_usage"),
-             menuSubItem(text = "Augmented Clickthrough", tabName = 
"kpi_augmented_clickthroughs")),
+             menuSubItem(text = "Augmented Clickthrough", tabName = 
"kpi_augmented_clickthroughs"),
+             icon = icon("star", lib = "glyphicon")),
     menuItem(text = "Desktop",
              menuSubItem(text = "Events", tabName = "desktop_events"),
              menuSubItem(text = "Load times", tabName = "desktop_load")),
@@ -38,37 +63,31 @@
              menuSubItem(text = "Summary", tabName = "failure_rate"),
              menuSubItem(text = "Search Type Breakdown", tabName = 
"failure_breakdown"),
              menuSubItem(text = "Search Suggestions", tabName = 
"failure_suggestions")),
-    menuItem(text = "Page Visit Times", tabName = "survival",
-             badgeLabel = "new", badgeColor = "fuchsia"),
-    selectInput(inputId = "smoothing_global", label = "Smoothing (Global 
Setting)", selectize = TRUE, selected = "day",
-                choices = c("No Smoothing" = "day", "Moving Average" = 
"moving_avg",
-                            "Weekly Median" = "week", "Monthly Median" = 
"month"))
+    menuItem(text = "Page Visit Times", tabName = "survival"),
+    menuItem(text = "Global Settings",
+             selectInput(inputId = "smoothing_global", label = "Smoothing", 
selectize = TRUE, selected = "day",
+                         choices = c("No Smoothing" = "day", "Weekly Median" = 
"week", "Monthly Median" = "month")),
+             selectInput(inputId = "timeframe_global", label = "Time Frame", 
selectize = TRUE, selected = "",
+                         choices = c("All available data" = "all", "Last 7 
days" = "week", "Last 30 days" = "month",
+                                     "Last 90 days" = "quarter", "Custom" = 
"custom")),
+             conditionalPanel("input.timeframe_global == 'custom'",
+                              dateRangeInput("daterange_global", label = 
"Custom Date Range",
+                                             start = Sys.Date()-11, end = 
Sys.Date()-1, min = "2015-04-14")),
+             icon = icon("cog", lib = "glyphicon"))
   )
 )
-
-# Standardised input selector for smoothing
-smooth_select <- function(input_id, label = "Smoothing") {
-  return(selectInput(inputId = input_id, label = label, selectize = TRUE,
-                     selected = "global", choices = c("Use Global Setting" = 
"global",
-                     "No Smoothing" = "day", "Moving Average" = "moving_avg",
-                     "Weekly Median" = "week", "Monthly Median" = "month")))
-}
 
 #Body elements for the search visualisations.
 body <- dashboardBody(
   tabItems(
     tabItem(tabName = "kpis_summary",
-            selectInput("kpi_summary_date_range_selector", label = "Data 
range", multiple = FALSE, selected = "weekly",
-                        choices = list("Yesterday" = "daily", "Last 7 days" = 
"weekly",
-                                       "Last 30 days" = "monthly", "Last 90 
days" = "quarterly")),
             htmlOutput("kpi_summary_date_range"),
             fluidRow(valueBoxOutput("kpi_summary_box_load_time", width = 3),
                      valueBoxOutput("kpi_summary_box_zero_results", width = 3),
                      valueBoxOutput("kpi_summary_box_api_usage", width = 3),
                      valueBoxOutput("kpi_summary_box_augmented_clickthroughs", 
width = 3)),
             plotOutput("kpi_summary_api_usage_proportions", height = "30px"),
-            includeMarkdown("./tab_documentation/kpis_summary.md")
-            ),
+            includeMarkdown("./tab_documentation/kpis_summary.md")),
     tabItem(tabName = "kpi_load_time",
             smooth_select("smoothing_kpi_load_time"),
             dygraphOutput("kpi_load_time_series"),
@@ -105,27 +124,37 @@
             fluidRow(
               valueBoxOutput("desktop_event_searches"),
               valueBoxOutput("desktop_event_resultsets"),
-              valueBoxOutput("desktop_event_clickthroughs")
-            ),
-            smooth_select("smoothing_desktop_event"),
+              valueBoxOutput("desktop_event_clickthroughs")),
+            fluidRow(
+              column(smooth_select("smoothing_desktop_event"), width = 4),
+              column(timeframe_select("desktop_event_timeframe"), width = 4),
+              column(timeframe_daterange("desktop_event_timeframe"), width = 
4)),
             dygraphOutput("desktop_event_plot"),
             includeMarkdown("./tab_documentation/desktop_events.md")),
     tabItem(tabName = "desktop_load",
-            smooth_select("smoothing_desktop_load"),
+            fluidRow(
+              column(smooth_select("smoothing_desktop_load"), width = 4),
+              column(timeframe_select("desktop_load_timeframe"), width = 4),
+              column(timeframe_daterange("desktop_load_timeframe"), width = 
4)),
             dygraphOutput("desktop_load_plot"),
             includeMarkdown("./tab_documentation/desktop_load.md")),
     tabItem(tabName = "mobile_events",
             fluidRow(
               valueBoxOutput("mobile_event_searches"),
               valueBoxOutput("mobile_event_resultsets"),
-              valueBoxOutput("mobile_event_clickthroughs")
-            ),
-            smooth_select("smoothing_mobile_event"),
+              valueBoxOutput("mobile_event_clickthroughs")),
+            fluidRow(
+              column(smooth_select("smoothing_mobile_event"), width = 4),
+              column(timeframe_select("mobile_event_timeframe"), width = 4),
+              column(timeframe_daterange("mobile_event_timeframe"), width = 
4)),
             dygraphOutput("mobile_event_plot"),
             includeMarkdown("./tab_documentation/mobile_events.md")
     ),
     tabItem(tabName = "mobile_load",
-            smooth_select("smoothing_mobile_load"),
+            fluidRow(
+              column(smooth_select("smoothing_mobile_load"), width = 4),
+              column(timeframe_select("mobile_load_timeframe"), width = 4),
+              column(timeframe_daterange("mobile_load_timeframe"), width = 4)),
             dygraphOutput("mobile_load_plot"),
             includeMarkdown("./tab_documentation/mobile_load.md")
     ),
@@ -133,65 +162,95 @@
             fluidRow(
               valueBoxOutput("app_event_searches"),
               valueBoxOutput("app_event_resultsets"),
-              valueBoxOutput("app_event_clickthroughs")
-            ),
-            smooth_select("smoothing_app_event"),
+              valueBoxOutput("app_event_clickthroughs")),
+            fluidRow(
+              column(smooth_select("smoothing_app_event"), width = 4),
+              column(timeframe_select("app_event_timeframe"), width = 4),
+              column(timeframe_daterange("app_event_timeframe"), width = 4)),
             dygraphOutput("android_event_plot"),
             dygraphOutput("ios_event_plot"),
             includeMarkdown("./tab_documentation/app_events.md")
     ),
     tabItem(tabName = "app_load",
-            smooth_select("smoothing_app_load"),
+            fluidRow(
+              column(smooth_select("smoothing_app_load"), width = 4),
+              column(timeframe_select("app_load_timeframe"), width = 4),
+              column(timeframe_daterange("app_load_timeframe"), width = 4)),
             dygraphOutput("android_load_plot"),
             dygraphOutput("ios_load_plot"),
             includeMarkdown("./tab_documentation/app_load.md")
     ),
     tabItem(tabName = "fulltext_search",
-            smooth_select("smoothing_fulltext_search"),
+            fluidRow(
+              column(smooth_select("smoothing_fulltext_search"), width = 4),
+              column(timeframe_select("fulltext_search_timeframe"), width = 4),
+              column(timeframe_daterange("fulltext_search_timeframe"), width = 
4)),
             dygraphOutput("cirrus_aggregate"),
             includeMarkdown("./tab_documentation/fulltext_basic.md")
     ),
     tabItem(tabName = "open_search",
-            smooth_select("smoothing_open_search"),
+            fluidRow(
+              column(smooth_select("smoothing_open_search"), width = 4),
+              column(timeframe_select("open_search_timeframe"), width = 4),
+              column(timeframe_daterange("open_search_timeframe"), width = 4)),
             dygraphOutput("open_aggregate"),
             includeMarkdown("./tab_documentation/open_basic.md")
     ),
     tabItem(tabName = "geo_search",
-            smooth_select("smoothing_geo_search"),
+            fluidRow(
+              column(smooth_select("smoothing_geo_search"), width = 4),
+              column(timeframe_select("geo_search_timeframe"), width = 4),
+              column(timeframe_daterange("geo_search_timeframe"), width = 4)),
             dygraphOutput("geo_aggregate"),
             includeMarkdown("./tab_documentation/geo_basic.md")
     ),
     tabItem(tabName = "prefix_search",
-            smooth_select("smoothing_prefix_search"),
+            fluidRow(
+              column(smooth_select("smoothing_prefix_search"), width = 4),
+              column(timeframe_select("prefix_search_timeframe"), width = 4),
+              column(timeframe_daterange("prefix_search_timeframe"), width = 
4)),
             dygraphOutput("prefix_aggregate"),
             includeMarkdown("./tab_documentation/prefix_basic.md")
     ),
     tabItem(tabName = "language_search",
-            smooth_select("smoothing_language_search"),
+            fluidRow(
+              column(smooth_select("smoothing_language_search"), width = 4),
+              column(timeframe_select("language_search_timeframe"), width = 4),
+              column(timeframe_daterange("language_search_timeframe"), width = 
4)),
             dygraphOutput("language_aggregate"),
             includeMarkdown("./tab_documentation/language_basic.md")
     ),
     tabItem(tabName = "failure_rate",
-            smooth_select("smoothing_failure_rate"),
+            fluidRow(
+              column(smooth_select("smoothing_failure_rate"), width = 4),
+              column(timeframe_select("failure_rate_timeframe"), width = 4),
+              column(timeframe_daterange("failure_rate_timeframe"), width = 
4)),
             dygraphOutput("failure_rate_plot"),
             dygraphOutput("failure_rate_change_plot"),
             includeMarkdown("./tab_documentation/failure_rate.md")
     ),
     tabItem(tabName = "failure_breakdown",
-            smooth_select("smoothing_failure_breakdown"),
+            fluidRow(
+              column(smooth_select("smoothing_failure_breakdown"), width = 4),
+              column(timeframe_select("failure_breakdown_timeframe"), width = 
4),
+              column(timeframe_daterange("failure_breakdown_timeframe"), width 
= 4)),
             dygraphOutput("failure_breakdown_plot"),
             includeMarkdown("./tab_documentation/failure_breakdown.md")
     ),
     tabItem(tabName = "failure_suggestions",
-            smooth_select("smoothing_failure_suggestions"),
+            fluidRow(
+              column(smooth_select("smoothing_failure_suggestions"), width = 
4),
+              column(timeframe_select("failure_suggestions_timeframe"), width 
= 4),
+              column(timeframe_daterange("failure_suggestions_timeframe"), 
width = 4)),
             dygraphOutput("suggestion_dygraph_plot"),
             includeMarkdown("./tab_documentation/failure_suggests.md")
     ),
     tabItem(tabName = "survival",
             fluidRow(
               column(smooth_select("smoothing_lethal_dose_plot"), width = 4),
-              column(div(id = "lethal_dose_plot_legend"), width = 8)
-            ),
+              column(timeframe_select("lethal_dose_timeframe"), width = 4),
+              column(timeframe_daterange("lethal_dose_timeframe"), width = 4)),
+            div(id = "lethal_dose_plot_legend"),
             dygraphOutput("lethal_dose_plot"),
             includeMarkdown("./tab_documentation/survival.md")
     )
diff --git a/utils.R b/utils.R
index 3391e39..64c7c97 100644
--- a/utils.R
+++ b/utils.R
@@ -10,7 +10,7 @@
 ## dygraphs.
 read_desktop <- function() {
   data <- polloi::read_dataset("search/desktop_event_counts.tsv")
-  interim <- reshape2::dcast(data, formula = timestamp ~ action, fun.aggregate 
= sum)
+  interim <- reshape2::dcast(data, formula = date ~ action, fun.aggregate = 
sum)
   interim[is.na(interim)] <- 0
   desktop_dygraph_set <<- interim
   desktop_dygraph_means <<- round(colMeans(desktop_dygraph_set[,2:5]))
@@ -19,7 +19,7 @@
 
 read_web <- function() {
   data <- polloi::read_dataset("search/mobile_event_counts.tsv")
-  interim <- reshape2::dcast(data, formula = timestamp ~ action, fun.aggregate 
= sum)
+  interim <- reshape2::dcast(data, formula = date ~ action, fun.aggregate = 
sum)
   interim[is.na(interim)] <- 0
   mobile_dygraph_set <<- interim
   mobile_dygraph_means <<- round(colMeans(mobile_dygraph_set[,2:4]))
@@ -30,8 +30,8 @@
 
   data <- polloi::read_dataset("search/app_event_counts.tsv")
 
-  ios <- reshape2::dcast(data[data$platform == "iOS",], formula = timestamp ~ 
action, fun.aggregate = sum)
-  android <- reshape2::dcast(data[data$platform == "Android",], formula = 
timestamp ~ action, fun.aggregate = sum)
+  ios <- reshape2::dcast(data[data$platform == "iOS",], formula = date ~ 
action, fun.aggregate = sum)
+  android <- reshape2::dcast(data[data$platform == "Android",], formula = date 
~ action, fun.aggregate = sum)
   ios_dygraph_set <<- ios
   ios_dygraph_means <<- round(colMeans(ios[,2:4]))
 
@@ -120,8 +120,21 @@
                   x = 1))
 }
 
-date_range_switch <- function(date_range, daily = 2, weekly = 14, monthly = 
60, quarterly = 90) {
-  return(switch(date_range, daily = daily, weekly = weekly, monthly = monthly, 
quarterly = quarterly))
+start_date <- function(date_range) {
+  return(Sys.Date() - (switch(date_range, daily = 2, weekly = 14, monthly = 
60, quarterly = 90) + 1))
+}
+
+# Usage: date_range %>% name(c('start', 'end'))
+name <- function(x, labels) {
+  names(x) <- labels
+  return(x)
+}
+
+# From: 
http://r.789695.n4.nabble.com/How-to-join-matrices-of-different-row-length-from-a-list-td3177212.html
+cbind_fill <- function(...) {
+  nm <- lapply(list(...), as.matrix)
+  n <- max(sapply(nm, nrow))
+  do.call(cbind, lapply(nm, function (x) rbind(x, matrix(, n-nrow(x), 
ncol(x)))))
 }
 
 CustomAxisFormatter <- 'function (d, gran) {

-- 
To view, visit https://gerrit.wikimedia.org/r/250879
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I06c8e56f98f077ea48234923650aeea4d140f13f
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/discovery/rainbow
Gerrit-Branch: master
Gerrit-Owner: Bearloga <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to