Skip to content

Commit

Permalink
Merge pull request #8207 from rtibbles/batch_notifications
Browse files Browse the repository at this point in the history
Add basic batch processing of notifications with tests.
  • Loading branch information
rtibbles committed Jul 19, 2021
2 parents 53aa0e6 + 7fb6e9e commit e5b4fa0
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 15 deletions.
7 changes: 3 additions & 4 deletions kolibri/core/logger/test/factory_logger.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import datetime

import factory

from .. import models
from kolibri.core.auth.test.test_api import FacilityUserFactory
from kolibri.utils.time_utils import local_now


class ContentSessionLogFactory(factory.DjangoModelFactory):
class Meta:
model = models.ContentSessionLog

user = factory.SubFactory(FacilityUserFactory)
start_timestamp = datetime.datetime.now()
start_timestamp = local_now()


class ContentSummaryLogFactory(factory.DjangoModelFactory):
class Meta:
model = models.ContentSummaryLog

user = factory.SubFactory(FacilityUserFactory)
start_timestamp = datetime.datetime.now()
start_timestamp = local_now()


class UserSessionLogFactory(factory.DjangoModelFactory):
Expand Down
31 changes: 28 additions & 3 deletions kolibri/core/notifications/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from kolibri.core.lessons.models import Lesson
from kolibri.core.logger.models import AttemptLog
from kolibri.core.logger.models import ContentSummaryLog
from kolibri.core.logger.models import ExamAttemptLog
from kolibri.core.logger.models import ExamLog
from kolibri.core.query import annotate_array_aggregate


Expand Down Expand Up @@ -186,7 +188,7 @@ def check_and_created_answered_lesson(lesson, user_id, contentnode_id, timestamp
classroom_id=lesson["classroom_id"],
timestamp=timestamp,
).exists():
# Let's create an Lesson Completion notification
# Let's create an Lesson Answered notification
notification = create_notification(
NotificationObjectType.Resource,
NotificationEventType.Answered,
Expand Down Expand Up @@ -258,7 +260,7 @@ def create_summarylog(summarylog):
notifications = []
for lesson, contentnode_id in lessons:
notifications_started = check_and_created_started(
lesson, summarylog.user_id, contentnode_id, summarylog.end_timestamp
lesson, summarylog.user_id, contentnode_id, summarylog.start_timestamp
)
notifications += notifications_started

Expand Down Expand Up @@ -345,7 +347,7 @@ def num_answered(examlog):


def created_quiz_notification(examlog, event_type, timestamp):
assigned_collections = (
assigned_collections = list(
ExamAssignment.objects.filter(
exam_id=examlog.exam_id,
collection_id__in=examlog.user.memberships.all().values_list(
Expand Down Expand Up @@ -483,3 +485,26 @@ def parse_attemptslog(attemptlog):

if notifications:
save_notifications(notifications)


def batch_process_attemptlogs(attemptlog_ids):
for attemptlog in AttemptLog.objects.filter(id__in=attemptlog_ids):
parse_attemptslog(attemptlog)


def batch_process_examlogs(examlog_ids, examattemptlog_ids):
for examattemptlog in (
ExamAttemptLog.objects.filter(id__in=examattemptlog_ids)
.select_related("examlog")
.order_by("start_timestamp")
):
create_examlog(examattemptlog.examlog, examattemptlog.start_timestamp)
create_examattemptslog(examattemptlog.examlog, examattemptlog.start_timestamp)
for examlog in ExamLog.objects.filter(id__in=examlog_ids):
parse_examlog(examlog, examlog.completion_timestamp)


def batch_process_summarylogs(summarylog_ids):
for summarylog in ContentSummaryLog.objects.filter(id__in=summarylog_ids):
create_summarylog(summarylog)
parse_summarylog(summarylog)

0 comments on commit e5b4fa0

Please sign in to comment.