library(knitr)
library(jsonlite)
library(httr)
library(magrittr)
library(dplyr)
library(lubridate)
library(ggplot2)
library(stringr)
urls <- c("http://api2.datascienceandr.org:3000/api/getManyRecords",
         "http://api2.datascienceandr.org:3000/api/getManyRecords")
fetch_df <- function(url){
  res <- POST(url, body = list(num = 1000), encode = "json")
  res %>%
  content(as = "text") %>%
  fromJSON()
}

combined_df <- lapply(urls, fetch_df) %>% do.call(rbind, .)

df <- combined_df %>%
  filter(user_id != "") %>%
  mutate(
    user_id = factor(user_id),
    course_name = str_split_fixed(course, ":", 2)[, 1] %>% factor(),
    lesson_name = str_split_fixed(course, ":", 2)[, 2] %>% factor(),
    type = factor(type, levels = c(0,1), labels = c("進入", "完成")),
    created_at = ymd_hms(created_at)
  )

平台每日使用量

df %>%
  mutate(date = as.Date(created_at)) %>%
  count(date, type) %>%
  ggplot(aes(x = date, y = n, fill = type)) + 
  geom_bar(stat = "identity", position = "dodge")

最用功的人

df %>% count(user_id, sort = T) %>% top_n(10) %>% kable()
user_id n
author 26
GU 20
Leo 18
Ted 16
Bruce 14
leo 14
aa 12
pearl 10
Sypan 10
tester 10

歡迎的課程

df %>% count(lesson_name, type) %>%
  ggplot(aes(x = type, y = n, fill = type)) +
  facet_wrap(~lesson_name, ncol = 4) +
  geom_bar(stat = "identity")