forked from learningequality/sushi-chef-khan-academy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkolibridb.py
executable file
·408 lines (325 loc) · 13.1 KB
/
kolibridb.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python
"""
Helpers for downloding Kolibri databases and printing topic trees:
./kolibridb.py --channel_id=95a52b386f2c485cb97dd60901674a98
or to get the same result as HTML (assuming you have `pandoc` installed):
./kolibridb.py --channel_id=95a52b386f2c485cb97dd60901674a98 --htmlexport
"""
import argparse
from collections import defaultdict
from contextlib import redirect_stdout
from itertools import groupby
from operator import itemgetter
import os
import io
import json
import requests
import sqlite3
import subprocess
import uuid
# DATABASE
################################################################################
DATABASES_DIR = 'chefdata/databases'
STUDIO_SERVER_LOOKUP = {
'production': 'https://studio.learningequality.org',
'develop': 'https://develop.studio.learningequality.org',
'local': 'http://localhost:8080',
}
def download_db_file(channel_id, server='production', update=False):
"""
Download DB file for Kolibri channel `channel_id` from a Studio server.
"""
os.makedirs(DATABASES_DIR, exist_ok=True)
db_file_path = os.path.join(DATABASES_DIR, channel_id + '.sqlite3')
if os.path.exists(db_file_path) and not update:
return db_file_path
if server in STUDIO_SERVER_LOOKUP.keys():
base_url = STUDIO_SERVER_LOOKUP[server]
elif 'http' in server:
base_url = server.rstrip('/')
else:
raise ValueError('Unrecognized arg', server)
db_file_url = base_url + '/content/databases/' + channel_id + '.sqlite3'
response = requests.get(db_file_url)
if response.ok:
with open(db_file_path, 'wb') as db_file:
for chunk in response:
db_file.write(chunk)
return db_file_path
else:
print(response.status_code, response.content)
raise ConnectionError('Failed to download DB file from', db_file_url)
def dbconnect(db_file_path):
conn = sqlite3.connect(db_file_path)
return conn
def dbex(conn, query):
"""
Execure a DB query and return results as a list of dicts.
"""
cursor = conn.cursor()
print('Running DB query', query)
cursor.execute(query)
results = [dict(zip([col[0] for col in cursor.description], row)) for row in cursor.fetchall()]
return results
# BASIC ORM
################################################################################
def dbfilter(rows, **kwargs):
"""
Return all the `rows` that match the `key=value` conditions, where keys are DB column
names and value is a row's value.
"""
selected = []
for row in rows:
accept = True
for key, value in kwargs.items():
if key not in row or row[key] != value:
accept = False
if accept:
selected.append(row)
return selected
def filter_key_in_values(rows, key, values):
"""
Return all the `rows` whose value for `key` is in the list `values`.
"""
if isinstance(values, str):
values = [values]
return list(filter(lambda r: r[key] in values, rows))
def dbget(rows, **kwargs):
"""
Return all the `rows` that match the `key=value` conditions, where keys are DB column
names and value is a row's value.
"""
selected = dbfilter(rows, **kwargs)
assert len(selected) < 2, 'mulitple results found'
if selected:
return selected[0]
else:
return None
def dbvalues_list(rows, *args, flat=False):
results = []
for row in rows:
result = []
for arg in args:
result.append(row[arg])
results.append(result)
if flat:
return [result[0] for result in results]
else:
return results
# UTILS
################################################################################
def sane_group_by(items, key):
"""
Wrapper for itertools.groupby to make it easier to use.
Returns a dict with keys = possible values of key in items
and corresponding values being lists of items that have that key.
"""
sorted_items = sorted(items, key=itemgetter(key))
return dict((k, list(g)) for k, g in groupby(sorted_items, key=itemgetter(key)))
def count_values_for_attr(rows, *attrs):
counts = {}
for attr in attrs:
counts[attr] = defaultdict(int)
for row in rows:
val = row[attr]
counts[attr][val] += 1
return counts
# KOLIBRI CHANNEL
################################################################################
def get_channel(channel_id):
db_file_path = download_db_file(channel_id)
conn = sqlite3.connect(db_file_path)
return dbex(conn, "SELECT * FROM content_channelmetadata;")[0]
def get_nodes_by_id(conn, attach_files=True, attach_assessments=True):
nodes = dbex(conn, "SELECT * FROM content_contentnode;")
# TODO: load tags from content_contentnode_tags and content_contenttag
# TODO: load content_contentnode_has_prerequisite, content_contentnode_related
nodes_by_id = {}
for node in nodes:
nodes_by_id[node['id']] = node
if attach_files:
# attach all the files associated with each node under the key "files"
files = get_files(conn)
local_files = get_local_files(conn)
local_file_lookup = {}
for local_file in local_files:
local_file_lookup[local_file["id"]] = local_file
for file in files:
node_id = file['contentnode_id']
node = nodes_by_id[node_id]
local_file = local_file_lookup[file["local_file_id"]]
file["extension"] = local_file["extension"]
file["checksum"] = local_file["id"]
if 'files' in node:
node['files'].append(file)
else:
node['files'] = [file]
if attach_assessments:
assessmentmetadata = get_assessmentmetadata(conn)
for aim in assessmentmetadata:
node = nodes_by_id[aim['contentnode_id']]
# attach assesment_ids direclty to node to imitate ricecooker/studio
node['assessment_item_ids'] = json.loads(aim['assessment_item_ids'])
node['assessmentmetadata'] = {
'number_of_assessments': aim['number_of_assessments'],
'mastery_model': aim['mastery_model'],
'randomize': aim['randomize'],
'is_manipulable': aim['is_manipulable'],
}
return nodes_by_id
def get_nodes_for_remote_files(channel_id):
try:
db_file_path = download_db_file(channel_id)
conn = sqlite3.connect(db_file_path)
return get_nodes_by_id(conn, attach_files=True, attach_assessments=False)
except Exception:
return {}
def get_files(conn):
files = dbex(conn, "SELECT * FROM content_file;")
return files
def get_local_files(conn):
localfiles = dbex(conn, "SELECT * FROM content_localfile;")
return localfiles
def get_assessmentmetadata(conn):
assessmentmetadata = dbex(conn, "SELECT * FROM content_assessmentmetadata;")
return assessmentmetadata
def get_tree(conn):
"""
Return a complete JSON tree of the entire channel.
"""
nodes_by_id = get_nodes_by_id(conn)
nodes = nodes_by_id.values()
sorted_nodes = sorted(nodes, key=lambda n: (n['parent_id'] or '0'*32, n['sort_order']))
root = sorted_nodes[0]
for node in sorted_nodes[1:]:
parent = nodes_by_id[node['parent_id']]
if 'children' in parent:
parent['children'].append(node)
else:
parent['children'] = [node]
return root
# NODE_ID UTILS
################################################################################
def node_id_from_source_ids(source_domain, channel_source_id, source_ids):
"""
Compute the node_id (str) for the node whose path is `source_ids` (list)
in a channel identified by `source_domain` and `channel_source_id`.
"""
domain_namespace = uuid.uuid5(uuid.NAMESPACE_DNS, source_domain)
content_ids = [uuid.uuid5(domain_namespace, source_id).hex for source_id in source_ids]
print('computed content_ids =', content_ids)
channel_id = uuid.uuid5(domain_namespace, channel_source_id)
print('Computed channel_id =', channel_id.hex)
node_id = channel_id
for content_id in content_ids:
node_id = uuid.uuid5(node_id, content_id)
return node_id.hex
# TREE PRINTING
################################################################################
CONTENT_KINDS = ['topic', 'video', 'audio', 'exercise', 'document', 'slideshow', 'h5p', 'html5']
def get_stats(subtree):
"""
Recusively compute kind-counts and total file_size (non-deduplicated).
"""
if 'children' in subtree and subtree['children']:
stats = dict((kind, 0) for kind in CONTENT_KINDS)
stats['topic'] = 1 # count self
stats['size'] = 0
for child in subtree['children']:
child_stats = get_stats(child)
for k, v in child_stats.items():
stats[k] += v
return stats
else:
size = sum([f['file_size'] for f in subtree['files']])
return {subtree['kind']: 1, 'size': size}
def stats_to_str(stats):
stats_str = ' '
for key in CONTENT_KINDS:
if key in stats and stats[key]:
if stats[key] > 1:
stats_str += str(stats[key]) + ' ' + key + 's, '
else:
stats_str += str(stats[key]) + ' ' + key + ', '
size_mb_str = "%.2f" % (float(stats['size'])/1024/1024) + 'MB'
stats_str += size_mb_str
return stats_str
def print_subtree(subtree, level=0, extrakeys=None, maxlevel=2, printstats=True):
extra = ''
if level > maxlevel:
return
if extrakeys:
for key in extrakeys:
extra = extra + ' ' + key + '=' + subtree[key]
if printstats:
stats = get_stats(subtree)
extra += stats_to_str(stats)
title = subtree['title'].replace('\n', ' ')
print(' '*2*level + ' -', title + ' (' + subtree['id'] + ')', extra)
if 'children' in subtree:
for child in subtree['children']:
print_subtree(child, level=level+1, extrakeys=extrakeys, maxlevel=maxlevel, printstats=printstats)
# TREE EXPORT
################################################################################
def export_kolibri_json_tree(channel_id=None, db_file_path=None, suffix='', server='production', update=False):
"""
Convert a channel from Kolibri database file to a JSON tree.
"""
if channel_id is None and db_file_path is None:
raise ValueError("Need to specify either channel_id or db_file_path")
if db_file_path:
conn = dbconnect(db_file_path)
else:
db_file_path = download_db_file(channel_id, server=server, update=update)
conn = dbconnect(db_file_path)
kolibri_tree = get_tree(conn)
conn.close()
if db_file_path:
pre_filename = db_file_path.split(os.pathsep)[-1].replace('.sqlite3', '')
json_filename = pre_filename + suffix + '.json'
else:
json_filename = channel_id + suffix + '.json'
with open(json_filename, 'w') as jsonf:
json.dump(kolibri_tree, jsonf, indent=2, ensure_ascii=False, sort_keys=True)
print('Channel exported as Kolibri JSON Tree in ' + json_filename)
# HTML EXPORTS
################################################################################
KOLIBRI_TREE_HTMLEXPORT_DIR = 'reports/kolibrihtmltrees'
def export_kolibritree_as_html(kolibritree, maxlevel=7):
"""
Export `kolibritree` as HTML for inspection of contents.
"""
basedir = KOLIBRI_TREE_HTMLEXPORT_DIR
if not os.path.exists(basedir):
os.makedirs(basedir, exist_ok=True)
channel_id = kolibritree['id']
path_md = os.path.join(basedir, 'channel_{}_tree.md'.format(channel_id))
path_html = os.path.join(basedir, 'channel_{}_tree.html'.format(channel_id))
with io.StringIO() as buf, redirect_stdout(buf):
print('# Kolibri Topic Tree for channel', channel_id)
print('')
print_subtree(kolibritree, maxlevel=maxlevel)
output_md = buf.getvalue()
with open(path_md, 'w') as mdfile:
mdfile.write(output_md)
subprocess.call(['pandoc', '--from', 'gfm', path_md, '-o', path_html])
print('Saved', path_html)
os.remove(path_md)
# CLI
################################################################################
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Kolibri channel topic tree viewer')
parser.add_argument('--channel_id', required=True, help="Channel ID")
parser.add_argument('--printmaxlevel', type=int, default=2, help='print tree depth')
parser.add_argument('--htmlexport', action='store_true', help='save topic tree as html')
parser.add_argument('--htmlmaxlevel', type=int, default=7, help='html tree depth')
parser.add_argument('--update', action='store_true', help='Force re-download of DB file')
args = parser.parse_args()
db_file_path = download_db_file(args.channel_id, update=args.update)
conn = dbconnect(db_file_path)
kolibritree = get_tree(conn)
# PRINT IN TERMINAL
print_subtree(kolibritree, maxlevel=args.printmaxlevel)
# HTML TREE EXPORT
if args.htmlexport:
export_kolibritree_as_html(kolibritree, maxlevel=args.htmlmaxlevel)