Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Filter partial years from offset results
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Martz committed Feb 10, 2017
1 parent 3b5c3aa commit 4b9e84b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
9 changes: 3 additions & 6 deletions django/climate_change_api/indicators/abstract_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,13 @@ def get_queryset(self):

queryset = range_config.create_queryset(
years=self.params.years.value,
models=self.params.models.value,
scenario=self.scenario,
key_params=key_params
).filter(
map_cell=self.city.map_cell,
data_source__scenario=self.scenario
map_cell=self.city.map_cell
)

if self.params.models.value:
filter_set = ClimateDataFilterSet()
queryset = filter_set.filter_models(queryset, self.params.models.value)

if self.filters is not None:
queryset = queryset.filter(**self.filters)

Expand Down
45 changes: 32 additions & 13 deletions django/climate_change_api/indicators/query_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import calendar

from django.db.models.functions import Concat
from django.db.models import Case, When, CharField, Value, F
from climate_data.models import ClimateData, ClimateDataSource
from django.db.models import Case, When, CharField, Value, F, Max, Min, Q
from climate_data.models import ClimateData, ClimateDataSource, Scenario
from climate_data.filters import ClimateDataFilterSet


Expand Down Expand Up @@ -33,23 +33,25 @@ def get_leap_year_sets():
]

@classmethod
def create_queryset(cls, years=None, key_params=None):
def create_queryset(cls, scenario, years=None, models=None, key_params=None):
if key_params is None:
key_params = {}

queryset = (ClimateData.objects.all()
.annotate(agg_key=cls.keys(**key_params))
.filter(agg_key__isnull=False))
.filter(agg_key__isnull=False,
data_source__scenario=scenario))

if years is not None:
queryset = cls.filter_years(queryset, years)
if years is not None or models is not None:
filterset = cls.get_filter_set()
queryset = filterset.filter_years(queryset, years)
queryset = filterset.filter_models(queryset, models)

return queryset

@classmethod
def filter_years(cls, queryset, years):
filter_set = ClimateDataFilterSet()
return filter_set.filter_years(queryset, years)
def get_filter_set(cls):
return ClimateDataFilterSet()

@classmethod
def get_interval_key(cls, index):
Expand Down Expand Up @@ -216,11 +218,28 @@ def make_ranges(cls, label):
]

@classmethod
def filter_years(cls, queryset, years):
queryset.annotate(offset_year=Case(
def create_queryset(cls, *args, **kwargs):
queryset = super(OffsetYearQuerysetGenerator, cls).create_queryset(*args, **kwargs)
queryset = queryset.annotate(offset_year=Case(
When(day_of_year__lt=cls.custom_offset,
then=F('data_source__year') - 1),
default=F('data_source__year')))

filter_set = ClimateDataFilterSet(year_col='offset_year')
return filter_set.filter_years(queryset, years)
scenario = kwargs['scenario']
maxYear, minYear = (Scenario.objects
.annotate(maxYear=Max('climatedatasource__year'),
minYear=Min('climatedatasource__year'))
.values_list('maxYear', 'minYear')
.get(name=scenario))
queryset = queryset.exclude(
(Q(data_source__year=minYear) & Q(day_of_year__lt=cls.custom_offset)) |
(Q(data_source__year=maxYear) & Q(day_of_year__gte=cls.custom_offset))
)

query, params = queryset.query.sql_with_params()
raise Exception(query % params)
return queryset

@classmethod
def get_filter_set(cls):
return ClimateDataFilterSet(year_col='offset_year')

0 comments on commit 4b9e84b

Please sign in to comment.