Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic batch processing of notifications with tests. #8207

Merged
merged 4 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appart from the tests, I don't see any place in the code where these batch_process_xxx functions are used. I guess there's another PR or issue were this is going to be worked, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - the issue this is fixing is dependent on the new morango version being merged into Kolibri, as it relies on being able to add a listener for syncing to get these updated ids.

I just wanted to get the code in first, and seems like it was useful. Will continue to chase down the Windows issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
then, ready to merge as soon as the Windows™ issues are solved

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)