diff --git a/scidash/main/settings.py b/scidash/main/settings.py index 9d0b0b9c..0fd219bc 100644 --- a/scidash/main/settings.py +++ b/scidash/main/settings.py @@ -249,3 +249,7 @@ CELERY_RESULT_BACKEND = 'django-db' NO_IMPORT_TAG = 'unschedulable' + +# SCIDASH +# Initial search number of quarters to search back in time. +SCIDASH_INITIAL_SEARCH_QUARTERS = 12 diff --git a/scidash/sciunittests/migrations/0047_auto_20191219_1217.py b/scidash/sciunittests/migrations/0047_auto_20191219_1217.py new file mode 100644 index 00000000..3d8f5f1b --- /dev/null +++ b/scidash/sciunittests/migrations/0047_auto_20191219_1217.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2019-12-19 12:17 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sciunittests', '0046_auto_20190827_0221'), + ] + + operations = [ + migrations.AddIndex( + model_name='scoreinstance', + index=models.Index(fields=['-timestamp'], name='sciunittest_timesta_aa7614_idx'), + ), + ] diff --git a/scidash/sciunittests/models.py b/scidash/sciunittests/models.py index 2f17f852..f3094fb1 100644 --- a/scidash/sciunittests/models.py +++ b/scidash/sciunittests/models.py @@ -243,6 +243,9 @@ def prediction(self): class Meta: ordering = ['-timestamp'] + indexes = [ + models.Index(fields=['-timestamp',]), + ] def __str__(self): return "Score for {0} in {1} test instance".format( diff --git a/scidash/sciunittests/views.py b/scidash/sciunittests/views.py index 99e91b50..e3109d9d 100644 --- a/scidash/sciunittests/views.py +++ b/scidash/sciunittests/views.py @@ -9,7 +9,21 @@ class DateRangeView(APIView): def get(self, request, *args, **kwargs): - three_month_period = datetime.timedelta(3 * 365 / 12) + """Returns the initial search period (acceptable_period). + for the first N (settings.ACCEPTABLE_SCORE_INSTANCES_AMOUNT) scores. + + Parameters + ---------- + - + + Returns + ------- + JSON object + { + "current_date": "", + "acceptable_period": "" + } + """ current_date = datetime.date.today() + datetime.timedelta(days=1) current_date_iso = datetime.datetime( year=current_date.year, @@ -17,20 +31,20 @@ def get(self, request, *args, **kwargs): day=current_date.day ) - acceptable_period = None - - for quarter in range(1, s.SCIDASH_INITIAL_SEARCH_QUARTERS + 1): - period = current_date_iso - quarter*three_month_period - count = ScoreInstance.objects.filter( - timestamp__gte=period, timestamp__lt=current_date_iso - ).count() - - if count > s.ACCEPTABLE_SCORE_INSTANCES_AMOUNT: - acceptable_period = period - break - - if acceptable_period is None: - acceptable_period = period + scores = ScoreInstance.objects.filter( + timestamp__lt=current_date_iso).order_by('-timestamp') \ + [:s.ACCEPTABLE_SCORE_INSTANCES_AMOUNT] + if scores: + # found scores, acceptable period is scores last.timestamp + # because sorting is DESC timestamp + acceptable_period = scores.reverse()[0].timestamp + else: + # acceptable period defaults to current date - 1 year + acceptable_period = datetime.datetime( + year=current_date.year-1, + month=current_date.month, + day=current_date.day + ) return Response( {