|
| 1 | +library(shiny) |
| 2 | +library(bslib) |
| 3 | +library(DT) |
| 4 | +library(connectapi) |
| 5 | +library(dplyr) |
| 6 | +library(purrr) |
| 7 | +library(lubridate) |
| 8 | + |
| 9 | +shinyOptions( |
| 10 | + cache = cachem::cache_disk("./app_cache/cache/", max_age = 60 * 60 * 8) |
| 11 | +) |
| 12 | + |
| 13 | +source("get_usage.R") |
| 14 | + |
| 15 | +ui <- page_fillable( |
| 16 | + theme = bs_theme(version = 5), |
| 17 | + |
| 18 | + card( |
| 19 | + card_header("Most Used Content"), |
| 20 | + layout_sidebar( |
| 21 | + sidebar = sidebar( |
| 22 | + title = "No Filters Yet", |
| 23 | + open = FALSE, |
| 24 | + |
| 25 | + actionButton("clear_cache", "Clear Cache", icon = icon("refresh")) |
| 26 | + ), |
| 27 | + card( |
| 28 | + DTOutput( |
| 29 | + "content_usage_table" |
| 30 | + ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + ) |
| 34 | +) |
| 35 | + |
| 36 | +server <- function(input, output, session) { |
| 37 | + # Cache invalidation button ---- |
| 38 | + cache <- cachem::cache_disk("./app_cache/cache/") |
| 39 | + observeEvent(input$clear_cache, { |
| 40 | + print("Cache cleared!") |
| 41 | + cache$reset() # Clears all cached data |
| 42 | + session$reload() # Reload the app to ensure fresh data |
| 43 | + }) |
| 44 | + |
| 45 | + # Loading and processing data ---- |
| 46 | + client <- connect() |
| 47 | + |
| 48 | + # Default dates. "This week" is best "common sense" best represented by six |
| 49 | + # days ago thru the end of today. Without these, content takes too long to |
| 50 | + # display on some servers. |
| 51 | + date_range <- reactive({ |
| 52 | + list( |
| 53 | + from_date = today() - ddays(6), |
| 54 | + to_date = today() |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + content <- reactive({ |
| 59 | + get_content(client) |
| 60 | + }) |> bindCache("static_key") |
| 61 | + |
| 62 | + usage_data <- reactive({ |
| 63 | + get_usage( |
| 64 | + client, |
| 65 | + from = date_range()$from_date, |
| 66 | + to = date_range()$to_date + hours(23) + minutes(59) + seconds(59) |
| 67 | + ) |
| 68 | + }) |> bindCache(date_range()$from_date, date_range()$to_date) |
| 69 | + |
| 70 | + # Compute basic usage stats |
| 71 | + content_usage_data <- reactive({ |
| 72 | + usage_summary <- usage_data() |> |
| 73 | + group_by(content_guid) |> |
| 74 | + summarize( |
| 75 | + total_views = n(), |
| 76 | + unique_viewers = n_distinct(user_guid, na.rm = TRUE), |
| 77 | + last_viewed_at = max(timestamp, na.rm = TRUE) |
| 78 | + ) |
| 79 | + |
| 80 | + content() |> |
| 81 | + mutate(owner_username = map_chr(owner, "username")) |> |
| 82 | + select(title, content_guid = guid, owner_username) |> |
| 83 | + right_join(usage_summary, by = "content_guid") |> |
| 84 | + arrange(desc(total_views)) |
| 85 | + }) |> bindCache(date_range()$from_date, date_range()$to_date) |
| 86 | + |
| 87 | + output$content_usage_table <- renderDT({ |
| 88 | + datatable( |
| 89 | + content_usage_data(), |
| 90 | + options = list( |
| 91 | + order = list(list(4, "desc")), |
| 92 | + paging = FALSE |
| 93 | + ), |
| 94 | + colnames = c( |
| 95 | + "Content Title" = "title", |
| 96 | + "Content GUID" = "content_guid", |
| 97 | + "Owner Username" = "owner_username", |
| 98 | + "Total Views" = "total_views", |
| 99 | + "Unique Logged-in Viewers" = "unique_viewers", |
| 100 | + "Last Viewed At" = "last_viewed_at" |
| 101 | + ) |
| 102 | + ) |> |
| 103 | + formatDate(columns = "Last Viewed At", method = "toLocaleString") |
| 104 | + |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +shinyApp(ui, server) |
0 commit comments