Skip to content

Commit

Permalink
CONCD-824 change the threshold from 2 to 3 for both submits and revie…
Browse files Browse the repository at this point in the history
…ws (#2448)

* CONCD-824 change the threshold from 2 to 3 for both submits and reviews

* CONCD-824 updated unit tests
  • Loading branch information
rasarkar authored Jul 12, 2024
1 parent e4e7ac3 commit 8ae13b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
47 changes: 34 additions & 13 deletions concordia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

ONE_DAY = datetime.timedelta(days=1)
ONE_DAY_AGO = timezone.now() - ONE_DAY
WINDOW = 60


def resource_file_upload_path(instance, filename):
Expand Down Expand Up @@ -796,36 +795,58 @@ def recent_review_actions(self, days=1):
def reviewing_too_quickly(self, start=ONE_DAY_AGO):
with connection.cursor() as cursor:
cursor.execute(
"""SELECT u.id, u.username, COUNT(*)
f"""SELECT u.id, u.username, COUNT(*)
FROM concordia_transcription t1
JOIN concordia_transcription t2
ON t1.id < t2.id
JOIN concordia_transcription t3
ON t2.id < t3.id
AND t1.reviewed_by_id = t2.reviewed_by_id
AND t1.accepted >= %s
AND t2.accepted >= %s
AND ABS(EXTRACT(EPOCH FROM (t1.updated_on - t2.updated_on))) < %s
AND t2.reviewed_by_id = t3.reviewed_by_id
AND t1.accepted >= '{start}'
AND t2.accepted >= '{start}'
AND t3.accepted >= '{start}'
AND ABS(
EXTRACT(EPOCH FROM (t1.updated_on - t2.updated_on))
) < 60
AND ABS(
EXTRACT(EPOCH FROM (t1.updated_on - t3.updated_on))
) < 60
AND ABS(EXTRACT(
EPOCH FROM (t2.updated_on - t3.updated_on))
) < 60
JOIN auth_user u on t1.reviewed_by_id = u.id
WHERE u.is_superuser = FALSE and u.is_staff = False
GROUP BY u.id, u.username""",
[start, start, WINDOW],
GROUP BY u.id, u.username""" # nosec B608
)
return cursor.fetchall()

def transcribing_too_quickly(self, start=ONE_DAY_AGO):
with connection.cursor() as cursor:
cursor.execute(
"""SELECT u.id, u.username, COUNT(*)
f"""SELECT u.id, u.username, COUNT(*)
FROM concordia_transcription t1
JOIN concordia_transcription t2
ON t1.id < t2.id
JOIN concordia_transcription t3
ON t2.id < t3.id
AND t1.user_id = t2.user_id
AND t1.submitted >= %s
AND t2.submitted >= %s
AND ABS(EXTRACT(EPOCH FROM (t1.created_on - t2.created_on))) < %s
AND t2.user_id = t3.user_id
AND t1.submitted >= '{start}'
AND t2.submitted >= '{start}'
AND t3.submitted >= '{start}'
AND ABS(
EXTRACT(EPOCH FROM (t1.created_on - t2.created_on))
) < 60
AND ABS(
EXTRACT(EPOCH FROM (t1.created_on - t3.created_on))
) < 60
AND ABS(
EXTRACT(EPOCH FROM (t2.created_on - t3.created_on))
) < 60
JOIN auth_user u on t1.user_id = u.id
WHERE u.is_superuser = FALSE and u.is_staff = False
GROUP BY u.id, u.username""",
[start, start, WINDOW],
GROUP BY u.id, u.username""" # nosec B608
)
return cursor.fetchall()

Expand Down
18 changes: 8 additions & 10 deletions concordia/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,12 @@ def fix_storage_images(campaign_slug=None, asset_start_id=None):
logger.debug("%s / %s (%s%%)", count, full_count, str(count / full_count * 100))


def transcribing_too_quickly(start):
return Transcription.objects.transcribing_too_quickly(start)
def transcribing_too_quickly():
return Transcription.objects.transcribing_too_quickly()


def reviewing_too_quickly(start):
return Transcription.objects.reviewing_too_quickly(start)
def reviewing_too_quickly():
return Transcription.objects.reviewing_too_quickly()


@celery_app.task(ignore_result=True)
Expand All @@ -1058,19 +1058,17 @@ def clear_sessions():


@celery_app.task
def unusual_activity(days=1):
def unusual_activity():
"""
Locate pages that were improperly transcribed or reviewed.
"""
now = timezone.now()
WINDOW = now - datetime.timedelta(days=days)
site = Site.objects.get_current()
context = {
"title": "Unusual User Activity Report for "
+ now.strftime("%b %d %Y, %I:%M %p"),
+ timezone.now().strftime("%b %d %Y, %I:%M %p"),
"domain": "https://" + site.domain,
"transcriptions": transcribing_too_quickly(WINDOW),
"reviews": reviewing_too_quickly(WINDOW),
"transcriptions": transcribing_too_quickly(),
"reviews": reviewing_too_quickly(),
}

text_body_template = loader.get_template("emails/unusual_activity.txt")
Expand Down
16 changes: 11 additions & 5 deletions concordia/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,15 @@ def test_status(self):
)

def test_reviewing_too_quickly(self):
transcriptions = Transcription.objects.reviewing_too_quickly()
self.assertEqual(len(transcriptions), 0)

self.transcription1.accepted = timezone.now()
self.transcription1.reviewed_by = self.create_user(username="tester2")
self.transcription1.save()
self.transcription2.accepted = self.transcription1.accepted
self.transcription2.reviewed_by = self.transcription1.reviewed_by
self.transcription2.save()
transcriptions = Transcription.objects.reviewing_too_quickly()
self.assertEqual(len(transcriptions), 0)

transcription3 = create_transcription(
asset=self.transcription1.asset,
user=self.transcription1.user,
Expand All @@ -194,11 +197,14 @@ def test_reviewing_too_quickly(self):
self.assertEqual(transcriptions[0][0], transcription3.reviewed_by.id)

def test_transcribing_too_quickly(self):
self.transcription1.submitted = timezone.now()
self.transcription1.save()
self.transcription2.submitted = self.transcription1.submitted
self.transcription2.user = self.transcription1.user
self.transcription2.save()
transcriptions = Transcription.objects.transcribing_too_quickly()
self.assertEqual(len(transcriptions), 0)

self.transcription1.submitted = timezone.now()
self.transcription1.save()
transcription3 = create_transcription(
asset=self.transcription1.asset,
user=self.transcription1.user,
Expand Down

0 comments on commit 8ae13b0

Please sign in to comment.