Skip to content

Commit

Permalink
fix(release): Fix daily stats runner (#1514) (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmessary authored Jan 31, 2024
2 parents bb1be21 + 547688b commit 9d5e67f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
31 changes: 23 additions & 8 deletions CodeListLibrary_project/clinicalcode/entity_utils/stats_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
from ..models import GenericEntity, Template, Statistics, Brand, CodingSystem, DataSource, PublishedGenericEntity, Tag
from . import template_utils, constants, model_utils, entity_db_utils, concept_utils

class MockStatsUser:
"""
Fake user for use within a RequestFactory
to simulate the request used for computation of statistics
"""
is_active = True
is_superuser = False
is_authenticated = True
is_superuser = True

def sort_by_count(a, b):
"""
Used to sort filter statistics in descending order
Expand Down Expand Up @@ -274,13 +284,13 @@ def clear_statistics_history():
cursor.execute(sql)


def compute_homepage_stats(request, brand):
def compute_homepage_stats(request, brand, is_mock=False):
stat = get_homepage_stats(request, brand)

if Statistics.objects.all().filter(org__iexact=brand, type__iexact='landing-page').exists():
stats = Statistics.objects.get(org__iexact=brand, type__iexact='landing-page')
stats.stat = stat
stats.updated_by = [None, request.user][request.user.is_authenticated]
stats.updated_by = [None, request.user][request.user.is_authenticated] if not is_mock else None
stats.modified = datetime.datetime.now()
stats.save()

Expand All @@ -291,22 +301,27 @@ def compute_homepage_stats(request, brand):
org=brand,
type='landing-page',
stat=stat,
created_by=[None, request.user][request.user.is_authenticated]
created_by=[None, request.user][request.user.is_authenticated] if not is_mock else None
)

clear_statistics_history()
return [stat, obj.id]


def save_homepage_stats(request, brand=None):
def save_homepage_stats(request, brand=None, is_mock=False):
if brand is not None:
return compute_homepage_stats(request, brand)
return compute_homepage_stats(request, brand, is_mock)

brands = Brand.objects.all()
result = [ ]
for brand in brands:
result.append(compute_homepage_stats(request, brand.name))
result.append(compute_homepage_stats(request, 'ALL'))
if is_mock:
setattr(request, 'CURRENT_BRAND', brand)
result.append(compute_homepage_stats(request, brand.name, is_mock))

if is_mock:
setattr(request, 'CURRENT_BRAND', None)
result.append(compute_homepage_stats(request, 'ALL', is_mock))
return result


Expand All @@ -317,7 +332,7 @@ def get_homepage_stats(request, brand=None):

if brand is None:
brand = request.CURRENT_BRAND if request.CURRENT_BRAND is not None and request.CURRENT_BRAND != '' else 'ALL'

collection_ids = [ ]
if brand == 'ALL':
collection_ids = Tag.objects.filter(tag_type=2)
Expand Down
11 changes: 9 additions & 2 deletions CodeListLibrary_project/clinicalcode/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from celery.utils.log import get_task_logger
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, BadHeaderError
from django.test.client import RequestFactory
from django.core import management

import time
Expand Down Expand Up @@ -56,12 +57,18 @@ def run_daily_statistics(self):
'''
logger = get_task_logger('cll')
try:
stats_utils.collect_statistics()
stats_utils.collect_statistics(None)

request = RequestFactory().get('/')
request.user = stats_utils.MockStatsUser()
# setattr(request, 'CURRENT_BRAND', )
stats_utils.save_homepage_stats(request, is_mock=True)
except Exception as e:
logger.warning(f'Unable to run daily statistics job, got error {e}')
return False
else:
logger.info(f'Successfully updated statistics')
return True
return True

@shared_task(bind=True)
def run_weekly_cleanup(self):
Expand Down

0 comments on commit 9d5e67f

Please sign in to comment.