-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsuite_license_reporting.r
88 lines (74 loc) · 3.27 KB
/
gsuite_license_reporting.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
library(tidyverse)
library(lubridate)
library(janitor)
library(scales)
library(plotly)
# This script leverages data extracted by GAM
# from GSuite / Google Workspace to show daily trends
# of GSuite Business License sku net additions - subtractions and
# cumulative trends. It starts with a seed
# base_log file of initial six month gsuite log license assignment events
# then this 'last 30 day' event report extraction script runs daily to append
# and dedupe the base log:
# gam.exe report admin event "USER_LICENSE_ASSIGNMENT"
# start -30d > "license_assignment_30d.csv"
# The data is used to build a ggplotly plot for a
# Shiny reporting app
base_log <- read_csv("license_assignment_base.csv",
col_types = cols(.default = "c"))
last_30_log <- read_csv("license_assignment_30d.csv",
col_types = cols(.default = "c"))
admin_license_log <- bind_rows(base_log, last_30_log) %>%
distinct(id.uniqueQualifier, .keep_all = TRUE)
write_csv(admin_license_log, "license_assignment_base.csv")
admin_license_log$id.time <- admin_license_log$id.time %>%
strftime(format = "%Y-%m-%d")
admin_license_log <- admin_license_log %>%
distinct(USER_EMAIL, .keep_all = TRUE) %>%
# trim back to start week of oldest log data
filter(id.time >= "2020-08-30") %>%
select(NEW_VALUE, USER_EMAIL, id.time) %>%
mutate(
license_type = case_when(
NEW_VALUE == "G Suite Business - Archived User" ~ "archive",
NEW_VALUE == "G Suite Business" ~ "gsuite")) %>%
clean_names()
daily_net <- admin_license_log %>%
group_by(theday = id_time) %>%
count(license_type) %>%
pivot_wider(names_from = license_type, values_from = n) %>%
mutate(archive = replace_na(archive, 0)) %>%
mutate(gsuite = replace_na(gsuite, 0)) %>%
mutate(net_change = gsuite - archive)
daily_net$cumulative <- cumsum(daily_net$net_change)
daily_net$past_days <- as.integer(rownames(daily_net))
fig <-
daily_net %>%
ggplot() +
geom_bar(mapping = aes(x = past_days, y = net_change),
stat = "identity", fill = ifelse(daily_net$net_change <0, "red","green")) +
geom_line(mapping = aes(x = past_days, y = cumulative), colour = "skyblue", size = 1.1) +
geom_point(aes(x = past_days, y = cumulative),
shape = 21, size = 1.75, stroke = 1.0, color = "skyblue", fill = "white") +
scale_y_continuous(name = "Net Daily Change", breaks = breaks_width(50)) +
scale_x_continuous(name = "Days", breaks = breaks_width(30)) +
theme_bw() +
ggtitle("GSuite Business License SKU Trend") +
annotate(geom = "text", x = 0.5*max(daily_net$past_days),
y = 0.52*max(daily_net$cumulative),
label = "Cumulative Change", hjust = 0, colour = "skyblue", size = 5) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 14),
axis.text.y = element_text(size = 14),
plot.title = element_text(hjust = 0.5)
# plotly ignores vertical title adjustment: , vjust = -10)
)
daily_trend_plotly <-
ggplotly(fig) %>%
plotly::config(displayModeBar = FALSE)
saveRDS(daily_trend_plotly,"daily_trend_plotly.rds")
# daily_trend_plotly Plotly plot can be loaded into Shiny