-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.Rmd
235 lines (168 loc) · 5.98 KB
/
main.Rmd
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
title: "EdX data parsing"
author: "Ivan Tsarev, [email protected]"
---
```{r}
library(dplyr)
library(magrittr)
library(purrr)
library(jsonlite)
library(stringr)
library(furrr)
```
```{r}
# Neither jsonlite::fromJSON nor tidyjson::read_json(format = "jsonl") can deal with this format
# so gonna read it by lines
# Also it can speed us up due to possible parallel parsing
raw_data = list.files(pattern = "2017") %>%
readLines() %>%
str_split_fixed(pattern = ":", n = 2)
```
```{r}
# create filter function to apply to report inside parsing process
# user_expresion should be valid R expression resulting in logical()
create_filter_function = function(user_expression){
#TODO add function testing
function(report){
eval(parse(text = user_expression), report)
}
}
# Below "report" means valid json part from one row,
# took that name to avoid confusion with "event" field
# Function extract report from input json,
# @param unnest_to_df is useful for flat data without nested arrays and staff
# @param require_fileds preserve selected fields by dots notation like event.user_id
# returns NULL if report does not have all required fields
# @ user_filter takes function from create_filter_function() to filter reports
parse_report = function(report,
unnest_to_df = FALSE,
required_fields = NULL,
user_filter = NULL) {
report %<>% fromJSON()
# some reports have nested (?) json in "event" object
#TODO not only event can be nested json, make unnesting user-controlled
try({
report$event = fromJSON(report$event)
}, silent = TRUE)
if(unnest_to_df){
report %<>%
unlist() %>%
as.list() %>%
as.data.frame(stringsAsFactors = FALSE)
}
# preserve only reports with full set of required fields
# tons of "undefine column" error are expected, so just tryCatch it to NULL
if (!is.null(required_fields)) {
report = tryCatch({
report[, required_fields, drop = F]
},
error = function(err) {
NULL
})
}
# return report if filter succeded
# error here are not so expected and gonna be catched by safely()
if (!is.null(user_filter)) {
if (!user_filter(report)) {
report = NULL
}
}
report
}
# even more safety - rerunning map() over 1m elements is not fun =\
safely_parse_report = safely(parse_report, quiet = TRUE)
```
```{r}
# Let`s check if we can save some time with multiprocessing
plan(multisession)
system.time(
future_map(raw_data[1:10000,2],
safely_parse_report
)
)
plan(sequential)
system.time(
map(raw_data[1:10000,2],
safely_parse_report
)
)
```
```{r}
plan(multisession)
# actually without fields arg we can get parsed list of all event and subset it later for both output table
# but i suppose it gonna bottleneck future_* function due to coping HUGE object into workers
parsed_data = future_map(raw_data[,2], safely_parse_report, TRUE, c("context.user_id","event.id","event_type","time"))
# check for some unexpected errors
errors_list = parsed_data %>%
future_map(~.x %>%
extract2("error"))
errors_list %>%
future_map_lgl(is_null) %>%
table()
# no errors, going on
parsed_video_event_df = parsed_data %>%
future_map_dfr(., ~.x %>%
extract2("result"))
plan(sequential)
```
```{r}
# just to avoid copy-pasting :)
parsed_video_event_df$event_type %>%
unique() %>%
dput()
# get some other, non-video events with the specified field set
# so clear ot out
# select mentioned in https://edx.readthedocs.io/projects/devdata/en/stable/internal_data_formats/tracking_logs.html#video-interaction-events types
video_related_event_types = c("load_video",
"pause_video",
"play_video",
"stop_video",
"seek_video",
"speed_change_video",
"show_transcript",
"hide_transcript"
)
parsed_video_event_df %<>% extract(.$event_type %in% video_related_event_types,)
write.csv(parsed_video_event_df,
"video_events.csv",
row.names = F)
```
```{r}
# https://edx.readthedocs.io/projects/devdata/en/stable/internal_data_formats/tracking_logs.html#problem-check-server
# browser-sourced event contain hard-to-parse http-requests and duplicate data so screw them
server_problem_check_type_filter = create_filter_function("event_type == 'problem_check' & event_source == 'server'")
plan(multisession)
problem_check_data = future_map(raw_data[,2],
~safely_parse_report(.x, FALSE, NULL, server_problem_check_type_filter))
# check for some unexpected errors
errors_list = problem_check_data %>%
future_map(~.x %>%
extract2("error"))
errors_list %>%
future_map_lgl(is_null) %>%
table()
plan(sequential)
```
```{r}
# problem_check events are slightly more complicated with multiple questions and answer inside
# so the function below designed to extract data from it to a pretty df
extract_submission_data_from_check = function(parsed_report){
stopifnot(parsed_report$event_type == 'problem_check' & parsed_report$event_source == 'server')
tmp = map(parsed_report$event$submission,
~.x[c("question", "answer")]) %>%
map(data.frame, stringsAsFactors = F) %>%
bind_rows()
tmp$user_id = parsed_report$context$user_id
tmp$problem_id = parsed_report$event$problem_id
tmp$time = parsed_report$time
tmp[,c( "user_id", "problem_id","question", "answer", "time")]
}
```
```{r}
problem_check_data = problem_check_data[map_lgl(problem_check_data, ~!is.null(.x$result))]
parsed_problem_check_data_df = map_dfr(problem_check_data,
~extract_submission_data_from_check(.x$result))
write.csv(parsed_problem_check_data_df,
"problem_check_events.csv",
row.names = F)
```