Skip to content
4 changes: 4 additions & 0 deletions scidash/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions scidash/sciunittests/migrations/0047_auto_20191219_1217.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
3 changes: 3 additions & 0 deletions scidash/sciunittests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 29 additions & 15 deletions scidash/sciunittests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,42 @@

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": "<yyyy-mm-ddThh:mi:ss>",
"acceptable_period": "<yyyy-mm-ddThh:mi:ss>"
}
"""
current_date = datetime.date.today() + datetime.timedelta(days=1)
current_date_iso = datetime.datetime(
year=current_date.year,
month=current_date.month,
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(
{
Expand Down