Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. library("RMySQL")
  2. library("ggplot2")
  3. library("dplyr")
  4. dbDisconnect(mydb)
  5. all_cons <- dbListConnections(MySQL())
  6. for (con in all_cons)
  7. dbDisconnect(con)
  8. mydb = dbConnect(MySQL(), user='threader', password='<PASSWORD>', dbname='threads', host='localhost')
  9.  
  10. query <- "
  11. SELECT
  12. visits.*,
  13. people.first_name,
  14. people.last_name,
  15. SUBSTR(visits.clienttime, 1, 10) date,
  16. IF(orgs.name IS NOT NULL, orgs.name, IF (people.email like '%projectspac%', 'project spaces', 'other')) org_name
  17. FROM
  18. page_visitation visits
  19. JOIN
  20. people people
  21. ON
  22. people.uid = visits.user_id
  23. LEFT OUTER JOIN
  24. orgs orgs
  25. ON
  26. orgs.uid = people.org_id AND
  27. orgs.uid IN ('34360266575', '34360211026', '34360210992') AND SUBSTR(visits.clienttime, 1, 10) IN ('2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18')
  28. "
  29.  
  30. rs = dbSendQuery(mydb, query)
  31. data = fetch(rs, n=-1)
  32.  
  33. by_org <- data %>% group_by(org_name, date) %>%
  34. summarize(visits = n(), dau = n_distinct(user_id)) %>%
  35. filter(date %in% c('2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18')) %>%
  36. filter(org_name %in% c('Albert', 'Chirps', 'project spaces', 'Recharge'))
  37.  
  38. ggplot(by_org, aes(x = as.Date(date, format="%Y-%m-%d"), y = visits, color=org_name)) +
  39. geom_line() +
  40. ggtitle("page visits") +
  41. theme(plot.title = element_text(size=16, hjust=0.5)) +
  42. xlab("date")
  43.  
  44. ggplot(by_org, aes(x = as.Date(date, format="%Y-%m-%d"), y = dau, color=org_name)) +
  45. geom_line() +
  46. ggtitle("page visiting dau") +
  47. theme(plot.title = element_text(size=16, hjust=0.5)) +
  48. xlab("date")
  49.  
  50. query <- "
  51. SELECT
  52. contents.*,
  53. people.first_name,
  54. people.last_name,
  55. SUBSTR(contents.created_timestamp, 1, 10) date,
  56. IF(orgs.name IS NOT NULL, orgs.name, IF (people.email like '%projectspac%', 'project spaces', 'other')) org_name
  57. FROM
  58. contents contents
  59. JOIN
  60. people people
  61. ON
  62. people.uid = contents.actor_id AND
  63. contents.content_type IN ('comment', 'reply', 'thread')
  64. LEFT OUTER JOIN
  65. orgs orgs
  66. ON
  67. orgs.uid = people.org_id AND
  68. orgs.uid IN ('34360266575', '34360211026', '34360210992') AND SUBSTR(contents.created_timestamp, 1, 10) IN ('2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18')
  69. "
  70.  
  71. rs = dbSendQuery(mydb, query)
  72. data = fetch(rs, n=-1)
  73.  
  74. by_org <- data %>% group_by(org_name, date) %>%
  75. summarize(writes = n(), writes_dau = n_distinct(actor_id)) %>%
  76. filter(date %in% c('2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18')) %>%
  77. filter(org_name %in% c('Albert', 'Chirps', 'project spaces', 'Recharge'))
  78.  
  79. ggplot(by_org, aes(x = as.Date(date, format="%Y-%m-%d"), y = writes, color=org_name)) +
  80. geom_line() +
  81. ggtitle("writes") +
  82. theme(plot.title = element_text(size=16, hjust=0.5)) +
  83. xlab("date")
  84.  
  85. ggplot(by_org, aes(x = as.Date(date, format="%Y-%m-%d"), y = writes_dau, color=org_name)) +
  86. geom_line() +
  87. ggtitle("writes dau") +
  88. theme(plot.title = element_text(size=16, hjust=0.5)) +
  89. xlab("date")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement