learnapp.py
18.8 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# python standard library
from os import path
import logging
from contextlib import contextmanager # `with` statement in db sessions
import asyncio
from datetime import datetime
from random import random
from typing import Dict
# third party libraries
import bcrypt
import sqlalchemy as sa
import networkx as nx
# this project
from .models import Student, Answer, Topic, StudentTopic
from .student import StudentState
from .questions import QFactory
from .tools import load_yaml
# setup logger for this module
logger = logging.getLogger(__name__)
# ============================================================================
class LearnException(Exception):
pass
class DatabaseUnusableError(LearnException):
pass
# ============================================================================
# LearnApp - application logic
# ============================================================================
class LearnApp(object):
# ------------------------------------------------------------------------
# helper to manage db sessions using the `with` statement, for example
# with self.db_session() as s: s.query(...)
# ------------------------------------------------------------------------
@contextmanager
def db_session(self, **kw):
session = self.Session(**kw)
try:
yield session
session.commit()
except Exception:
logger.error('!!! Database rollback !!!')
session.rollback()
raise
finally:
session.close()
# ------------------------------------------------------------------------
# init
# ------------------------------------------------------------------------
def __init__(self, config_files, prefix, db, check=False):
self.db_setup(db) # setup database and check students
self.online = dict() # online students
self.deps = nx.DiGraph(prefix=prefix)
for c in config_files:
self.populate_graph(c)
# factory is a dict with question generators for all topics
self.factory = self.make_factory() # {'qref': QFactory()}
# if graph has topics that are not in the database, add them
self.db_add_missing_topics(self.deps.nodes())
if check:
self.sanity_check_questions()
# ------------------------------------------------------------------------
def sanity_check_questions(self):
logger.info('Starting sanity checks (may take a while...)')
errors = 0
for qref in self.factory:
logger.debug(f'[sanity_check_questions] Checking "{qref}"...')
try:
q = self.factory[qref].generate()
except Exception:
logger.error(f'Failed to generate "{qref}".')
errors += 1
continue # to next question
if 'tests_right' in q:
for t in q['tests_right']:
q['answer'] = t
q.correct()
if q['grade'] < 1.0:
logger.error(f'Failed right answer in "{qref}".')
errors += 1
continue # to next test
if 'tests_wrong' in q:
for t in q['tests_wrong']:
q['answer'] = t
q.correct()
if q['grade'] >= 1.0:
logger.error(f'Failed wrong answer in "{qref}".')
errors += 1
continue # to next test
if errors > 0:
logger.error(f'{errors:>6} errors found.')
raise LearnException('Sanity checks')
else:
logger.info(' 0 errors found.')
# ------------------------------------------------------------------------
# login
# ------------------------------------------------------------------------
async def login(self, uid, pw):
with self.db_session() as s:
found = s.query(Student.name, Student.password) \
.filter_by(id=uid) \
.one_or_none()
# wait random time to minimize timing attacks
await asyncio.sleep(random())
loop = asyncio.get_running_loop()
if found is None:
logger.info(f'User "{uid}" does not exist')
await loop.run_in_executor(None, bcrypt.hashpw, b'',
bcrypt.gensalt()) # just spend time
return False
else:
name, hashed_pw = found
pw_ok = await loop.run_in_executor(None, bcrypt.checkpw,
pw.encode('utf-8'), hashed_pw)
if pw_ok:
if uid in self.online:
logger.warning(f'User "{uid}" already logged in')
counter = self.online[uid]['counter']
else:
logger.info(f'User "{uid}" logged in')
counter = 0
# get topics of this student and set its current state
with self.db_session() as s:
tt = s.query(StudentTopic).filter_by(student_id=uid)
state = {t.topic_id: {
'level': t.level,
'date': datetime.strptime(t.date, "%Y-%m-%d %H:%M:%S.%f")
} for t in tt}
self.online[uid] = {
'number': uid,
'name': name,
'state': StudentState(deps=self.deps, factory=self.factory,
state=state),
'counter': counter + 1, # counts simultaneous logins
}
else:
logger.info(f'User "{uid}" wrong password')
return pw_ok
# ------------------------------------------------------------------------
# logout
# ------------------------------------------------------------------------
def logout(self, uid):
del self.online[uid]
logger.info(f'User "{uid}" logged out')
# ------------------------------------------------------------------------
# change_password. returns True if password is successfully changed.
# ------------------------------------------------------------------------
async def change_password(self, uid, pw):
if not pw:
return False
loop = asyncio.get_running_loop()
pw = await loop.run_in_executor(None, bcrypt.hashpw,
pw.encode('utf-8'), bcrypt.gensalt())
with self.db_session() as s:
u = s.query(Student).get(uid)
u.password = pw
logger.info(f'User "{uid}" changed password')
return True
# ------------------------------------------------------------------------
# checks answer (updating student state) and returns grade.
# ------------------------------------------------------------------------
async def check_answer(self, uid, answer):
knowledge = self.online[uid]['state']
q, action = await knowledge.check_answer(answer) # may move questions
logger.info(f'User "{uid}" got {q["grade"]:.2} in "{q["ref"]}"')
topic = knowledge.get_current_topic()
# always save grade of answered question
with self.db_session() as s:
s.add(Answer(
ref=q['ref'],
grade=q['grade'],
starttime=str(q['start_time']),
finishtime=str(q['finish_time']),
student_id=uid,
topic_id=topic))
logger.debug(f'[check_answer] Saved "{q["ref"]}" into database')
if knowledge.topic_has_finished():
# finished topic, save into database
logger.info(f'User "{uid}" finished "{topic}"')
level = knowledge.get_topic_level(topic)
date = str(knowledge.get_topic_date(topic))
with self.db_session() as s:
a = s.query(StudentTopic) \
.filter_by(student_id=uid, topic_id=topic) \
.one_or_none()
if a is None:
# insert new studenttopic into database
logger.debug('[check_answer] Database insert studenttopic')
t = s.query(Topic).get(topic)
u = s.query(Student).get(uid)
# association object
a = StudentTopic(level=level, date=date, topic=t,
student=u)
u.topics.append(a)
else:
# update studenttopic in database
logger.debug('[check_answer] Database update studenttopic')
a.level = level
a.date = date
s.add(a)
logger.debug(f'[check_answer] Saved topic "{topic}" into database')
return q, action
# ------------------------------------------------------------------------
# Start new topic
# ------------------------------------------------------------------------
async def start_topic(self, uid, topic):
student = self.online[uid]['state']
try:
await student.start_topic(topic)
except Exception as e:
logger.warning(f'User "{uid}" couldn\'t start "{topic}": {e}')
else:
logger.info(f'User "{uid}" started topic "{topic}"')
# ------------------------------------------------------------------------
# Fill db table 'Topic' with topics from the graph if not already there.
# ------------------------------------------------------------------------
def db_add_missing_topics(self, topics):
with self.db_session() as s:
new_topics = [Topic(id=t) for t in topics
if (t,) not in s.query(Topic.id)]
if new_topics:
s.add_all(new_topics)
logger.info(f'Added {len(new_topics)} new topic(s) to the '
f'database')
# ------------------------------------------------------------------------
# setup and check database contents
# ------------------------------------------------------------------------
def db_setup(self, db):
logger.info(f'Checking database "{db}":')
engine = sa.create_engine(f'sqlite:///{db}', echo=False)
self.Session = sa.orm.sessionmaker(bind=engine)
try:
with self.db_session() as s:
n = s.query(Student).count()
m = s.query(Topic).count()
q = s.query(Answer).count()
except Exception:
logger.error(f'Database "{db}" not usable!')
raise DatabaseUnusableError()
else:
logger.info(f'{n:6} students')
logger.info(f'{m:6} topics')
logger.info(f'{q:6} answers')
# ============================================================================
# Populates a digraph.
#
# Nodes are the topic references e.g. 'my/topic'
# g.node['my/topic']['name'] name of the topic
# g.node['my/topic']['questions'] list of question refs
#
# Edges are obtained from the deps defined in the YAML file for each topic.
# ------------------------------------------------------------------------
def populate_graph(self, conffile: str):
logger.info(f'Populating graph from: {conffile}...')
config = load_yaml(conffile) # course configuration
# default attributes that apply to the topics
default_file = config.get('file', 'questions.yaml')
default_shuffle_questions = config.get('shuffle_questions', True)
default_choose = config.get('choose', 9999)
default_forgetting_factor = config.get('forgetting_factor', 1.0)
default_maxtries = config.get('max_tries', 3)
default_append_wrong = config.get('append_wrong', True)
default_min_level = config.get('min_level', 0.01) # to unlock topic
# iterate over topics and populate graph
topics = config.get('topics', {})
g = self.deps # dependency graph
g.add_nodes_from(topics.keys())
for tref, attr in topics.items():
for d in attr.get('deps', []):
if d not in g.nodes():
logger.error(f'Topic "{tref}" depends on "{d}" but it '
f'does not exist')
raise LearnException()
else:
g.add_edge(d, tref)
t = g.node[tref] # get current topic node
t['type'] = attr.get('type', 'topic')
t['name'] = attr.get('name', tref)
t['path'] = path.join(g.graph['prefix'], tref) # prefix/topic
t['file'] = attr.get('file', default_file) # questions.yaml
t['shuffle_questions'] = attr.get('shuffle_questions',
default_shuffle_questions)
t['max_tries'] = attr.get('max_tries', default_maxtries)
t['forgetting_factor'] = attr.get('forgetting_factor',
default_forgetting_factor)
t['min_level'] = attr.get('min_level', default_min_level)
t['choose'] = attr.get('choose', default_choose)
t['append_wrong'] = attr.get('append_wrong', default_append_wrong)
t['questions'] = attr.get('questions', [])
logger.info(f'Loaded {g.number_of_nodes()} topics')
# ========================================================================
# methods that do not change state (pure functions)
# ========================================================================
# ------------------------------------------------------------------------
# Buils dictionary of question factories
# ------------------------------------------------------------------------
def make_factory(self) -> Dict[str, QFactory]:
logger.info('Building questions factory...')
factory = {} # {'qref': QFactory()}
g = self.deps
for tref in g.nodes():
t = g.node[tref]
# load questions as list of dicts
topicpath = path.join(g.graph['prefix'], tref)
questions = load_yaml(path.join(topicpath, t['file']), default=[])
# update refs to include topic as prefix.
# refs are required to be unique only within the file.
# undefined are set to topic:n, where n is the question number
# within the file
for i, q in enumerate(questions):
qref = q.get('ref', str(i)) # ref or number
q['ref'] = tref + ':' + qref
q['path'] = topicpath
q.setdefault('append_wrong', t['append_wrong'])
# if questions are left undefined, include all.
if not t['questions']:
t['questions'] = [q['ref'] for q in questions]
t['choose'] = min(t['choose'], len(t['questions']))
for q in questions:
if q['ref'] in t['questions']:
factory[q['ref']] = QFactory(q)
logger.info(f'{len(t["questions"]):6} {tref}')
logger.info(f'Factory contains {len(factory)} questions')
return factory
# ------------------------------------------------------------------------
def get_login_counter(self, uid: str) -> int:
return self.online[uid]['counter']
# ------------------------------------------------------------------------
def get_student_name(self, uid: str) -> str:
return self.online[uid].get('name', '')
# ------------------------------------------------------------------------
def get_student_state(self, uid: str):
return self.online[uid]['state'].get_knowledge_state()
# ------------------------------------------------------------------------
def get_student_progress(self, uid: str):
return self.online[uid]['state'].get_topic_progress()
# ------------------------------------------------------------------------
def get_current_question(self, uid: str):
return self.online[uid]['state'].get_current_question() # dict
# ------------------------------------------------------------------------
def get_current_question_id(self, uid: str) -> str:
return self.online[uid]['state'].get_current_question()['qid']
# ------------------------------------------------------------------------
def get_student_question_type(self, uid: str) -> str:
return self.online[uid]['state'].get_current_question()['type']
# ------------------------------------------------------------------------
def get_student_topic(self, uid: str) -> str:
return self.online[uid]['state'].get_current_topic() # str
# ------------------------------------------------------------------------
def get_title(self) -> str:
return self.deps.graph.get('title', '') # FIXME
# ------------------------------------------------------------------------
def get_topic_name(self, ref: str) -> str:
return self.deps.node[ref]['name']
# ------------------------------------------------------------------------
def get_current_public_dir(self, uid):
topic = self.online[uid]['state'].get_current_topic()
prefix = self.deps.graph['prefix']
return path.join(prefix, topic, 'public')
# ------------------------------------------------------------------------
def get_rankings(self, uid):
logger.info(f'User "{uid}" get rankings')
with self.db_session() as s:
students = s.query(Student.id, Student.name).all()
# topic progress
student_topics = s.query(StudentTopic.student_id,
StudentTopic.topic_id,
StudentTopic.level,
StudentTopic.date).all()
total_topics = s.query(Topic).count()
# answer performance
total = dict(s.query(Answer.student_id, sa.func.count(Answer.ref)).
group_by(Answer.student_id).
all())
right = dict(s.query(Answer.student_id, sa.func.count(Answer.ref)).
filter(Answer.grade == 1.0).
group_by(Answer.student_id).
all())
# compute percentage of right answers
perf = {uid: right.get(uid, 0.0)/total[uid] for uid in total}
# compute topic progress
prog = {s[0]: 0.0 for s in students}
now = datetime.now()
for uid, topic, level, date in student_topics:
date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")
prog[uid] += level**(now - date).days / total_topics
rankings = [(uid, name, prog[uid], perf.get(uid, 0.0))
for uid, name in students if uid != '0']
return sorted(rankings, key=lambda x: x[2], reverse=True)
# ------------------------------------------------------------------------