learnapp.py
23.6 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
'''
Learn application.
This is the main controller of the application.
'''
# python standard library
import asyncio
from collections import defaultdict
from datetime import datetime
import logging
from random import random
from os.path import join, exists
from typing import Any, Dict, Iterable, List, Optional, Tuple, Set, DefaultDict
# third party libraries
import bcrypt
import networkx as nx
from sqlalchemy import create_engine, select, func
from sqlalchemy.orm import Session
from sqlalchemy.exc import NoResultFound
# this project
from aprendizations.models import Student, Answer, Topic, StudentTopic
from aprendizations.questions import Question, QFactory, QDict, QuestionException
from aprendizations.student import StudentState
from aprendizations.tools import load_yaml
# setup logger for this module
logger = logging.getLogger(__name__)
# ============================================================================
class LearnException(Exception):
'''Exceptions raised from the LearnApp class'''
class DatabaseUnusableError(LearnException):
'''Exception raised if the database fails in the initialization'''
# ============================================================================
class LearnApp():
'''
LearnApp - application logic
self.deps = networkx topic dependencies
self.courses = {
course_id: {
'title': ...,
'description': ...,
'goals': ...,
}, ...
}
self.factory = { qref: QFactory() }
self.online = {
student_id: {
'number': ...,
'name': ...,
'state': StudentState(),
'counter': ...
}, ...
}
'''
def __init__(self,
courses: str, # filename with course configurations
prefix: str, # path to topics
dbase: str, # database filename
check: bool = False) -> None:
self._db_setup(dbase) # setup database and check students
self.online: Dict[str, Dict] = {} # online students
try:
config: Dict[str, Any] = load_yaml(courses)
except Exception as exc:
msg = f'Failed to load yaml file "{courses}"'
logger.error(msg)
raise LearnException(msg) from exc
# --- topic dependencies are shared between all courses
self.deps = nx.DiGraph(prefix=prefix)
logger.info('Populating topic graph:')
# topics defined directly in the courses file, usually empty
base_topics = config.get('topics', {})
self._populate_graph(base_topics)
logger.info('%6d topics in %s', len(base_topics), courses)
# load other course files with the topics the their deps
for course_file in config.get('topics_from', []):
course_conf = load_yaml(course_file) # course configuration
logger.info('%6d topics from %s',
len(course_conf["topics"]), course_file)
self._populate_graph(course_conf)
logger.info('Graph has %d topics', len(self.deps))
# --- courses dict
self.courses = config['courses']
logger.info('Courses: %s', ', '.join(self.courses.keys()))
for cid, course in self.courses.items():
course.setdefault('title', cid) # course title undefined
for goal in course['goals']:
if goal not in self.deps.nodes():
msg = f'Goal "{goal}" from course "{cid}" does not exist'
logger.error(msg)
raise LearnException(msg)
if self.deps.nodes[goal]['type'] == 'chapter':
course['goals'] += [g for g in self.deps.predecessors(goal)
if g not in course['goals']]
# --- factory is a dict with question generators for all topics
self.factory: Dict[str, QFactory] = self._make_factory()
# if graph has topics that are not in the database, add them
self._add_missing_topics(self.deps.nodes())
if check:
self._sanity_check_questions()
def _sanity_check_questions(self) -> None:
'''
Unit tests for all questions
Generates all questions, give right and wrong answers and corrects.
'''
logger.info('Starting sanity checks (may take a while...)')
errors: int = 0
for qref, qfactory in self.factory.items():
logger.debug('checking %s...', qref)
try:
question = qfactory.generate()
except QuestionException as exc:
logger.error(exc)
errors += 1
continue # to next question
if 'tests_right' in question:
for right_answer in question['tests_right']:
question['answer'] = right_answer
question.correct()
if question['grade'] < 1.0:
logger.error('Failed right answer in "%s".', qref)
errors += 1
continue # to next test
elif question['type'] == 'textarea':
msg = f'- consider adding tests to {question["ref"]}'
logger.warning(msg)
if 'tests_wrong' in question:
for wrong_answer in question['tests_wrong']:
question['answer'] = wrong_answer
question.correct()
if question['grade'] >= 1.0:
logger.error('Failed wrong answer in "%s".', qref)
errors += 1
continue # to next test
if errors > 0:
logger.error('%6d error(s) found.', errors)
raise LearnException('Sanity checks')
logger.info(' 0 errors found.')
async def login(self, uid: str, password: str) -> bool:
'''user login'''
# wait random time to minimize timing attacks
await asyncio.sleep(random())
query = select(Student).where(Student.id == uid)
try:
with Session(self._engine, future=True) as session:
student = session.execute(query).scalar_one()
except NoResultFound:
logger.info('User "%s" does not exist', uid)
return False
loop = asyncio.get_running_loop()
pw_ok: bool = await loop.run_in_executor(None,
bcrypt.checkpw,
password.encode('utf-8'),
student.password)
if pw_ok:
if uid in self.online:
logger.warning('User "%s" already logged in', uid)
counter = self.online[uid]['counter']
else:
logger.info('User "%s" logged in', uid)
counter = 0
# get topics for this student and set its current state
query = select(StudentTopic).where(StudentTopic.student_id == uid)
with Session(self._engine, future=True) as session:
student_topics = session.execute(query).scalars().all()
state = {t.topic_id: {
'level': t.level,
'date': datetime.strptime(t.date, "%Y-%m-%d %H:%M:%S.%f")
} for t in student_topics}
self.online[uid] = {
'number': uid,
'name': student.name,
'state': StudentState(uid=uid, state=state,
courses=self.courses, deps=self.deps,
factory=self.factory),
'counter': counter + 1, # counts simultaneous logins
}
else:
logger.info('User "%s" wrong password', uid)
return pw_ok
def logout(self, uid: str) -> None:
'''User logout'''
del self.online[uid]
logger.info('User "%s" logged out', uid)
async def change_password(self, uid: str, password: str) -> bool:
'''
Change user Password.
Returns True if password is successfully changed
'''
if not password:
return False
loop = asyncio.get_running_loop()
hashed_pw = await loop.run_in_executor(None,
bcrypt.hashpw,
password.encode('utf-8'),
bcrypt.gensalt())
with Session(self._engine, future=True) as session:
query = select(Student).where(Student.id == uid)
user = session.execute(query).scalar_one()
user.password = hashed_pw
session.commit()
logger.info('User "%s" changed password', uid)
return True
async def check_answer(self, uid: str, answer) -> Question:
'''
Checks answer and update database.
Returns corrected question.
'''
student = self.online[uid]['state']
await student.check_answer(answer)
topic_id = student.get_current_topic()
question: Question = student.get_current_question()
grade = question["grade"]
ref = question["ref"]
logger.info('User "%s" got %.2f in "%s"', uid, grade, ref)
# always save grade of answered question
answer = Answer(ref=ref,
grade=grade,
starttime=str(question['start_time']),
finishtime=str(question['finish_time']),
student_id=uid,
topic_id=topic_id)
with Session(self._engine, future=True) as session:
session.add(answer)
session.commit()
return question
async def get_question(self, uid: str) -> Optional[Question]:
'''
Get the question to show (current or new one)
If no more questions, save/update level in database
'''
student_state = self.online[uid]['state']
question: Optional[Question] = await student_state.get_question()
# save topic to database if finished
if student_state.topic_has_finished():
topic_id: str = student_state.get_previous_topic()
level: float = student_state.get_topic_level(topic_id)
date: str = str(student_state.get_topic_date(topic_id))
logger.info('User "%s" finished "%s" (level=%.2f)',
uid, topic_id, level)
query = select(StudentTopic) \
.where(StudentTopic.student_id == uid) \
.where(StudentTopic.topic_id == topic_id)
with Session(self._engine, future=True) as session:
student_topic = session.execute(query).scalar_one_or_none()
if student_topic is None:
# insert new studenttopic into database
logger.debug('db insert studenttopic')
query_topic = select(Topic).where(Topic.id == topic_id)
query_student = select(Student).where(Student.id == uid)
topic = session.execute(query_topic).scalar_one()
student = session.execute(query_student).scalar_one()
# association object
student_topic = StudentTopic(level=level,
date=date,
topic=topic,
student=student)
student.topics.append(student_topic)
else:
# update studenttopic in database
logger.debug('db update studenttopic to level %f', level)
student_topic.level = level
student_topic.date = date
session.add(student_topic)
session.commit()
return question
def start_course(self, uid: str, course_id: str) -> None:
'''Start course'''
student_state = self.online[uid]['state']
try:
student_state.start_course(course_id)
except Exception as exc:
logger.warning('"%s" could not start course "%s"', uid, course_id)
raise LearnException() from exc
else:
logger.info('User "%s" course "%s"', uid, course_id)
async def start_topic(self, uid: str, topic: str) -> None:
'''Start new topic'''
student = self.online[uid]['state']
try:
await student.start_topic(topic)
except Exception as exc:
logger.warning('User "%s" could not start "%s": %s',
uid, topic, str(exc))
else:
logger.info('User "%s" started topic "%s"', uid, topic)
def _add_missing_topics(self, topics: Iterable[str]) -> None:
'''
Fill db table 'Topic' with topics from the graph, if new
'''
with Session(self._engine, future=True) as session:
db_topics = session.execute(select(Topic.id)).scalars().all()
new = [Topic(id=t) for t in topics if t not in db_topics]
if new:
session.add_all(new)
session.commit()
logger.info('Added %d new topic(s) to the database', len(new))
def _db_setup(self, database: str) -> None:
'''
Setup and check database contents
'''
logger.info('Checking database "%s":', database)
if not exists(database):
msg = 'Database does not exist.'
logger.error(msg)
raise LearnException(msg)
self._engine = create_engine(f'sqlite:///{database}', future=True)
try:
query_students = select(func.count(Student.id))
query_topics = select(func.count(Topic.id))
query_answers = select(func.count(Answer.id))
with Session(self._engine, future=True) as session:
count_students = session.execute(query_students).scalar()
count_topics = session.execute(query_topics).scalar()
count_answers = session.execute(query_answers).scalar()
except Exception as exc:
logger.error('Database "%s" not usable!', database)
raise DatabaseUnusableError() from exc
else:
logger.info('%6d students', count_students)
logger.info('%6d topics', count_topics)
logger.info('%6d answers', count_answers)
def _populate_graph(self, config: Dict[str, Any]) -> None:
'''
Populates a digraph.
Nodes are the topic references e.g. 'my/topic'
g.nodes['my/topic']['name'] name of the topic
g.nodes['my/topic']['questions'] list of question refs
Edges are obtained from the deps defined in the YAML file for each topic.
'''
defaults = {
'type': 'topic', # chapter
'file': 'questions.yaml',
'shuffle_questions': True,
'choose': 99,
'forgetting_factor': 1.0, # no forgetting
'max_tries': 1, # in every question
'append_wrong': True,
'min_level': 0.01, # to unlock topic
}
defaults.update(config.get('defaults', {}))
# iterate over topics and populate graph
topics: Dict[str, Dict] = config.get('topics', {})
self.deps.add_nodes_from(topics.keys())
for tref, attr in topics.items():
logger.debug(' + %s', tref)
for dep in attr.get('deps', []):
self.deps.add_edge(dep, tref)
topic = self.deps.nodes[tref] # get current topic node
topic['name'] = attr.get('name', tref)
topic['questions'] = attr.get('questions', [])
for k, default in defaults.items():
topic[k] = attr.get(k, default)
# prefix/topic
topic['path'] = join(self.deps.graph['prefix'], tref)
# ------------------------------------------------------------------------
# methods that do not change state (pure functions)
# ------------------------------------------------------------------------
def _make_factory(self) -> Dict[str, QFactory]:
'''
Buils dictionary of question factories
- visits each topic in the graph,
- adds factory for each topic.
'''
logger.info('Building questions factory:')
factory = {}
for tref in self.deps.nodes:
factory.update(self._factory_for(tref))
logger.info('Factory has %s questions', len(factory))
return factory
# ------------------------------------------------------------------------
# makes factory for a single topic
# ------------------------------------------------------------------------
def _factory_for(self, tref: str) -> Dict[str, QFactory]:
factory: Dict[str, QFactory] = {}
topic = self.deps.nodes[tref] # get node
# load questions as list of dicts
try:
fullpath: str = join(topic['path'], topic['file'])
except Exception as exc:
msg = f'Invalid topic "{tref}". Check dependencies of: ' + \
', '.join(self.deps.successors(tref))
logger.error(msg)
raise LearnException(msg) from exc
logger.debug(' Loading %s', fullpath)
try:
questions: List[QDict] = load_yaml(fullpath)
except Exception as exc:
if topic['type'] == 'chapter':
return factory # chapters may have no "questions"
msg = f'Failed to load "{fullpath}"'
logger.error(msg)
raise LearnException(msg) from exc
if not isinstance(questions, list):
msg = f'File "{fullpath}" must be a list of questions'
logger.error(msg)
raise LearnException(msg)
# 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
localrefs: Set[str] = set() # refs in current file
for i, question in enumerate(questions):
qref = question.get('ref', str(i)) # ref or number
if qref in localrefs:
msg = f'Duplicate ref "{qref}" in "{topic["path"]}"'
logger.error(msg)
raise LearnException(msg)
localrefs.add(qref)
question['ref'] = f'{tref}:{qref}'
question['path'] = topic['path']
question.setdefault('append_wrong', topic['append_wrong'])
# if questions are left undefined, include all.
if not topic['questions']:
topic['questions'] = [q['ref'] for q in questions]
topic['choose'] = min(topic['choose'], len(topic['questions']))
for question in questions:
if question['ref'] in topic['questions']:
factory[question['ref']] = QFactory(question)
logger.debug(' + %s', question["ref"])
logger.info('%6d questions in %s', len(topic["questions"]), tref)
return factory
def get_login_counter(self, uid: str) -> int:
'''login counter'''
return int(self.online[uid]['counter'])
def get_student_name(self, uid: str) -> str:
'''Get the username'''
return self.online[uid].get('name', '')
def get_student_state(self, uid: str) -> List[Dict[str, Any]]:
'''Get the knowledge state of a given user'''
return self.online[uid]['state'].get_knowledge_state()
def get_student_progress(self, uid: str) -> float:
'''Get the current topic progress of a given user'''
return float(self.online[uid]['state'].get_topic_progress())
def get_current_question(self, uid: str) -> Optional[Question]:
'''Get the current question of a given user'''
question: Optional[Question] = self.online[uid]['state'].get_current_question()
return question
def get_current_question_id(self, uid: str) -> str:
'''Get id of the current question for a given user'''
return str(self.online[uid]['state'].get_current_question()['qid'])
def get_student_question_type(self, uid: str) -> str:
'''Get type of the current question for a given user'''
return str(self.online[uid]['state'].get_current_question()['type'])
# ------------------------------------------------------------------------
# def get_student_topic(self, uid: str) -> str:
# return str(self.online[uid]['state'].get_current_topic())
def get_student_course_title(self, uid: str) -> str:
'''get the title of the current course for a given user'''
return str(self.online[uid]['state'].get_current_course_title())
def get_current_course_id(self, uid: str) -> Optional[str]:
'''get the current course (id) of a given user'''
cid: Optional[str] = self.online[uid]['state'].get_current_course_id()
return cid
# ------------------------------------------------------------------------
# def get_topic_name(self, ref: str) -> str:
# return str(self.deps.nodes[ref]['name'])
def get_current_public_dir(self, uid: str) -> str:
'''
Get the path for the 'public' directory of the current topic of the
given user.
E.g. if the user has the active topic 'xpto',
then returns 'path/to/xpto/public'.
'''
topic: str = self.online[uid]['state'].get_current_topic()
prefix: str = self.deps.graph['prefix']
return join(prefix, topic, 'public')
def get_courses(self) -> Dict[str, Dict[str, Any]]:
'''
Get dictionary with all courses {'course1': {...}, 'course2': {...}}
'''
return self.courses
def get_course(self, course_id: str) -> Dict[str, Any]:
'''
Get dictionary {'title': ..., 'description':..., 'goals':...}
'''
return self.courses[course_id]
def get_rankings(self, uid: str, cid: str) -> List[Tuple[str, str, float]]:
'''
Returns rankings for a certain cid (course_id).
User where uid have <=2 chars are considered ghosts are hidden from
the rankings. This is so that there can be users for development or
testing purposes, which are not real users.
The user_id of real students must have >2 chars.
This should be modified to have a "visible" flag
'''
logger.info('User "%s" rankings for "%s"', uid, cid)
query_students = select(Student.id, Student.name)
query_student_topics = select(StudentTopic.student_id,
StudentTopic.topic_id,
StudentTopic.level,
StudentTopic.date)
with Session(self._engine, future=True) as session:
# all students in the database FIXME: only with answers of this course
students = session.execute(query_students).all()
# topic levels FIXME: only topics of this course
student_topics = session.execute(query_student_topics).all()
# compute topic progress
progress: DefaultDict[str, float] = defaultdict(int)
goals = self.courses[cid]['goals']
num_goals = len(goals)
now = datetime.now()
for student, topic, level, datestr in student_topics:
if topic in goals:
date = datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S.%f")
elapsed_days = (now - date).days
progress[student] += level**elapsed_days / num_goals
return sorted(((u, name, progress[u])
for u, name in students
if u in progress and (len(u) > 2 or len(uid) <= 2)),
key=lambda x: x[2], reverse=True)