Skip to content

Commit 8d7445d

Browse files
authored
most used content (plot edition) (#36)
* add content usage plot * fix date range formatting for API
1 parent 3b0b840 commit 8d7445d

File tree

3 files changed

+848
-17
lines changed

3 files changed

+848
-17
lines changed

extensions/most-used-content/app.R

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ library(connectapi)
55
library(dplyr)
66
library(purrr)
77
library(lubridate)
8+
library(ggplot2)
9+
library(plotly)
10+
library(tidyr)
811

912
shinyOptions(
1013
cache = cachem::cache_disk("./app_cache/cache/", max_age = 60 * 60 * 8)
@@ -24,6 +27,18 @@ ui <- page_fillable(
2427

2528
actionButton("clear_cache", "Clear Cache", icon = icon("refresh"))
2629
),
30+
31+
tabsetPanel(
32+
tabPanel(
33+
"Plot of Daily Views",
34+
plotlyOutput("daily_views_plot")
35+
),
36+
tabPanel(
37+
"Plot of Unique Daily Visitors",
38+
plotlyOutput("unique_daily_visitor_plot")
39+
)
40+
),
41+
2742
card(
2843
DTOutput(
2944
"content_usage_table"
@@ -84,25 +99,64 @@ server <- function(input, output, session) {
8499
arrange(desc(total_views))
85100
}) |> bindCache(date_range()$from_date, date_range()$to_date)
86101

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+
# Compute daily usage numbers for plots
103+
usage_by_date <- reactive({
104+
all_dates <- seq.Date(date_range()$from_date, date_range()$to_date, by = "day")
105+
106+
usage_data() |>
107+
mutate(date = date(timestamp)) |>
108+
group_by(date) |>
109+
summarize(
110+
daily_views = n(),
111+
unique_daily_visitors = n_distinct(user_guid, na.rm = TRUE)
102112
) |>
103-
formatDate(columns = "Last Viewed At", method = "toLocaleString")
113+
full_join(tibble(date = all_dates)) |>
114+
mutate(across(c(unique_daily_visitors, daily_views), ~ replace_na(.x, 0)))
115+
})
116+
117+
# Render table
118+
output$content_usage_table <- renderDT({
119+
datatable(
120+
content_usage_data(),
121+
options = list(
122+
order = list(list(4, "desc")),
123+
paging = FALSE
124+
),
125+
colnames = c(
126+
"Content Title" = "title",
127+
"Content GUID" = "content_guid",
128+
"Owner Username" = "owner_username",
129+
"Total Views" = "total_views",
130+
"Unique Logged-in Viewers" = "unique_viewers",
131+
"Last Viewed At" = "last_viewed_at"
132+
)
133+
) |>
134+
formatStyle(
135+
columns = "Content Title",
136+
`white-space` = "nowrap",
137+
`overflow` = "hidden",
138+
`text-overflow` = "ellipsis",
139+
`max-width` = "300px"
140+
) |>
141+
formatDate(columns = "Last Viewed At", method = "toLocaleString")
142+
})
143+
144+
output$daily_views_plot <- renderPlotly(
145+
ggplotly(
146+
ggplot(usage_by_date()) +
147+
geom_col(aes(x = date, y = daily_views)) +
148+
labs(x = "Date", y = "Daily Views")
149+
)
150+
)
151+
152+
output$unique_daily_visitor_plot <- renderPlotly(
153+
ggplotly(
154+
ggplot(usage_by_date()) +
155+
geom_col(aes(x = date, y = unique_daily_visitors)) +
156+
labs(x = "Date", y = "Unique Daily Visitors (Logged-in)")
157+
)
158+
)
104159

105-
})
106160
}
107161

108162
shinyApp(ui, server)

extensions/most-used-content/get_usage.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ get_usage_legacy <- function(client, from = NULL, to = NULL) {
4545
}
4646

4747
get_usage <- function(client, from = NULL, to = NULL) {
48+
from <- format(from, "%Y-%m-%dT%H:%M:%SZ")
49+
to <- format(to, "%Y-%m-%dT%H:%M:%SZ")
4850
tryCatch(
4951
{
5052
print("Trying firehose usage endpoint.")

0 commit comments

Comments
 (0)