Skip to content

Commit

Permalink
Fixed alerts in report
Browse files Browse the repository at this point in the history
  • Loading branch information
rogergithub3 committed Aug 29, 2023
1 parent 0700b8f commit 4d73ccf
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 10 deletions.
50 changes: 50 additions & 0 deletions src/tesla_ce/apps/api/v2/serializers/institution/alert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2020 Xavier Baró
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
""" Alert serializer module """
import json

from rest_framework import serializers
from tesla_ce.models import Alert, Message


class MessageSerializer(serializers.ModelSerializer):
"""Message serialize model module."""
class Meta:
model = Message
fields = "__all__"


class AlertSerializer(serializers.ModelSerializer):
"""Alert serialize model module."""
data = serializers.SerializerMethodField()
message = MessageSerializer(read_only=True, many=False, source='message_code', required=False)

class Meta:
model = Alert
fields = "__all__"

@staticmethod
def get_data(obj):
""" Get data """
content = {}

Check warning on line 41 in src/tesla_ce/apps/api/v2/serializers/institution/alert.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/apps/api/v2/serializers/institution/alert.py#L41

Added line #L41 was not covered by tests

with obj.data.open() as f:
try:
content_json = f.read().decode('utf-8')
content = json.loads(content_json)
except Exception:
pass

Check warning on line 48 in src/tesla_ce/apps/api/v2/serializers/institution/alert.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/apps/api/v2/serializers/institution/alert.py#L43-L48

Added lines #L43 - L48 were not covered by tests

return content

Check warning on line 50 in src/tesla_ce/apps/api/v2/serializers/institution/alert.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/apps/api/v2/serializers/institution/alert.py#L50

Added line #L50 was not covered by tests
2 changes: 1 addition & 1 deletion src/tesla_ce/lib/data/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8
1.0.9
7 changes: 7 additions & 0 deletions src/tesla_ce/models/report_activity_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _

from .alert import Alert
from .base_model import BaseModel
from .instrument import Instrument
from .report_activity import REPORT_ALERT_LEVEL
Expand All @@ -43,6 +44,7 @@ class ResultFacts(Enum):
NEGATIVE_LEARNER_RESULT_BAD_ACTIVITY = -3
NEGATIVE_LEARNER_RESULT_NOT_FREQUENT = -4
NEGATIVE_CONFIDENCE_LOW = -5
NEGATIVE_WITH_ALERTS = -6


class ReportActivityInstrument(BaseModel):
Expand Down Expand Up @@ -222,6 +224,11 @@ def get_facts(self):
if self.get_prob_learner() > 0.8:
facts['positive'].append(ResultFacts.POSITIVE_LEARNER_RESULT_FREQUENT.name)

# check if there are any alerts
if Alert.objects.filter(session__activity=self.report.activity,
session__learner=self.report.learner).count() > 0:
facts['negative'].append(ResultFacts.NEGATIVE_WITH_ALERTS.name)

Check warning on line 230 in src/tesla_ce/models/report_activity_instrument.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/models/report_activity_instrument.py#L230

Added line #L230 was not covered by tests

return facts

def update_audit(self):
Expand Down
58 changes: 49 additions & 9 deletions src/tesla_ce/tasks/reports/results.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 Xavier Baró
# Copyright (c) 2020 Xavier Baró
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -121,13 +121,15 @@ def update_learner_activity_session_report(self, learner_id, activity_id, sessio
list(results_qs.values_list('created_at', 'result').order_by('created_at')),
cls=DjangoJSONEncoder)

from tesla_ce.apps.api.v2.serializers.institution.alert import AlertSerializer
session_data = {
'instruments': list(results_qs.values_list('instrument', flat=True).distinct()),
'alerts': json.dumps(
list(models.Alert.objects.filter(session_id=session_id).order_by('created_at')),
cls=DjangoJSONEncoder),
'data': data
'alerts': list(AlertSerializer(models.Alert.objects.filter(session_id=session_id).order_by('created_at'),
many=True).data),
'data': data,
'session_id': session_id
}

data_path = '{}/results/{}/{}/{}/session_{}.json'.format(
session_report.session.activity.vle.institution_id,
session_report.session.learner.learner_id,
Expand All @@ -138,6 +140,31 @@ def update_learner_activity_session_report(self, learner_id, activity_id, sessio
session_report.data.save(data_path,
ContentFile(json.dumps(session_data, cls=DjangoJSONEncoder).encode('utf-8')))

max_level = 2 # Ok

# Update session_report levels with alerts information
if len(session_data['alerts']) > 0:
if models.Alert.objects.filter(session_id=session_id, level=2).count() > 0:
max_level = 4 # Alert
elif models.Alert.objects.filter(session_id=session_id, level__in=[1, 3]).count() > 0:
max_level = 3 # Warning

Check warning on line 150 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L147-L150

Added lines #L147 - L150 were not covered by tests

# todo: Update session_report levels with provider results
session_report.identity_level = 1
session_report.content_level = 1
session_report.integrity_level = 1

for activity_instrument in session_report.session.activity.get_learner_instruments(
session_report.session.learner):
if activity_instrument.instrument.identity:
session_report.identity_level = max_level

if activity_instrument.instrument.originality or activity_instrument.instrument.authorship:
session_report.content_level = max_level

if activity_instrument.instrument.integrity:
session_report.integrity_level = max_level

Check warning on line 166 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L166

Added line #L166 was not covered by tests

session_report.save()


Expand Down Expand Up @@ -310,6 +337,15 @@ def update_learner_activity_report(learner_id, activity_id, force_update=False):
'data': session_data
})

if session.identity_level > report.identity_level:
report.identity_level = session.identity_level

Check warning on line 341 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L341

Added line #L341 was not covered by tests

if session.integrity_level > report.integrity_level:
report.integrity_level = session.integrity_level

Check warning on line 344 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L344

Added line #L344 was not covered by tests

if session.content_level > report.content_level:
report.content_level = session.content_level

Check warning on line 347 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L347

Added line #L347 was not covered by tests

# Add results for attachments without session
documents = models.Request.objects.filter(learner_id=learner_id,
activity_id=activity_id,
Expand Down Expand Up @@ -342,10 +378,14 @@ def update_learner_activity_report(learner_id, activity_id, force_update=False):


@celery_app.task(ignore_result=True)
def update_activity_report(activity_id):
pass
def update_activity_report(activity_id, force_update=False):
activity = models.Activity.objects.get(id=activity_id)

Check warning on line 382 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L382

Added line #L382 was not covered by tests

for learner in activity.course.learners.all():
update_learner_activity_report(activity_id=activity_id, learner_id=learner.id, force_update=force_update)

Check warning on line 385 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L384-L385

Added lines #L384 - L385 were not covered by tests


@celery_app.task(ignore_result=True)
def update_course_report(course_id):
pass
def update_course_report(course_id, force_update=False):
for activity in models.Activity.objects.filter(course_id=course_id):
update_activity_report(activity.id, force_update=False)

Check warning on line 391 in src/tesla_ce/tasks/reports/results.py

View check run for this annotation

Codecov / codecov/patch

src/tesla_ce/tasks/reports/results.py#L390-L391

Added lines #L390 - L391 were not covered by tests

0 comments on commit 4d73ccf

Please sign in to comment.