diff --git a/django/climate_change_api/climate_data/views.py b/django/climate_change_api/climate_data/views.py index 3b1a14cc..1c7bebeb 100644 --- a/django/climate_change_api/climate_data/views.py +++ b/django/climate_change_api/climate_data/views.py @@ -260,17 +260,18 @@ def climate_indicator(request, *args, **kwargs): paramType: query - name: time_aggregation description: Time granularity to group data by for result structure. Valid aggregations - depend on indicator. Can be 'yearly', 'monthly' or 'daily'. Defaults to - 'yearly'. + depend on indicator. Can be 'yearly', 'quarterly', 'monthly', 'daily' or + 'custom'. Defaults to 'yearly'. If 'custom', 'custom_time_agg' parameter must be + set. required: false type: string paramType: query - - name: intervals - description: A list of comma separated month-day pairs defining the time intervals to - aggregate within. Data points will only be assigned to one aggregation, and for - overlapping intervals the interval defined first will take precedence. Dates - are formmatted MM-DD and pairs are formatted 'start:end'. For example, '3-1:5-31', - '1-1:6-31,7-1:12-31' + - name: custom_time_agg + description: Used in conjunction with the 'custom' time_aggregation value. A list of comma + separated month-day pairs defining the time intervals to aggregate within. Data + points will only be assigned to one aggregation, and for overlapping intervals + the interval defined first will take precedence. Dates are formmatted MM-DD and + pairs are formatted 'start:end'. For example, '3-1:5-31', '1-1:6-30,7-1:12-31' required: false type: string paramType: query diff --git a/django/climate_change_api/indicators/abstract_indicators.py b/django/climate_change_api/indicators/abstract_indicators.py index f7da93ef..9c044052 100644 --- a/django/climate_change_api/indicators/abstract_indicators.py +++ b/django/climate_change_api/indicators/abstract_indicators.py @@ -113,18 +113,18 @@ def get_queryset(self): queryset = queryset.filter(**self.filters) # For certain time aggregations, add a field to track which interval a data point is in - interval_configs = { + time_aggregation_configs = { 'monthly': MonthRangeConfig, 'quarterly': QuarterRangeConfig, 'custom': CustomRangeConfig } - if self.params.time_aggregation.value in interval_configs: - config = interval_configs[self.params.time_aggregation.value] + if self.params.time_aggregation.value in time_aggregation_configs: + config = time_aggregation_configs[self.params.time_aggregation.value] params = {} # The custom range config accepts a user-defined parameter to pick which dates to use - if self.params.intervals.value is not None: - params['intervals'] = self.params.intervals.value + if self.params.custom_time_agg.value is not None: + params['custom_time_agg'] = self.params.custom_time_agg.value queryset = (queryset .annotate(interval=config.cases(**params)) diff --git a/django/climate_change_api/indicators/params.py b/django/climate_change_api/indicators/params.py index 63da88f8..b0a1bf90 100644 --- a/django/climate_change_api/indicators/params.py +++ b/django/climate_change_api/indicators/params.py @@ -22,18 +22,21 @@ "'stdev' is an alias to 'stddev'. Defaults to 'min,max,avg'.") TIME_AGGREGATION_PARAM_DOCSTRING = ("Time granularity to group data by for result structure. Valid " - "aggregations depend on indicator. Can be 'yearly', 'monthly', " - "'daily' or 'custom'. Defaults to 'yearly'. If 'custom', " - "'intervals' parameter must be set.") + "aggregations depend on indicator. Can be 'yearly', " + "'quarterly', 'monthly', 'daily' or 'custom'. Defaults to " + "'yearly'. If 'custom', 'custom_time_agg' parameter must be " + "set.") UNITS_PARAM_DOCSTRING = ("Units in which to return the data. Defaults to Imperial units (Fahrenheit" " for temperature indicators and inches for precipitation).") -INTERVALS_PARAM_DOCSTRING = ("A list of comma separated month-day pairs defining the time intervals" - " to aggregate within. Data points will only be assigned to one " - "aggregation, and for overlapping intervals the interval defined first" - " will take precedence. Dates are formmatted MM-DD and pairs are " - "formatted 'start:end'. Examples: '3-1:5-31', '1-1:6-31,7-1:12-31'") +CUSTOM_TIME_AGG_PARAM_DOCSTRING = ("Used in conjunction with the 'custom' time_aggregation value. " + "A list of comma separated month-day pairs defining the time " + "intervals to aggregate within. Data points will only be " + "assigned to one aggregation, and for overlapping intervals the" + " interval defined first will take precedence. Dates are " + "formmatted MM-DD and pairs are formatted 'start:end'. Examples:" + " '3-1:5-31', '1-1:6-30,7-1:12-31'") PERCENTILE_PARAM_DOCSTRING = ("The percentile threshold used to calculate the number of exceeding " "events compared to historic levels. Must be an integer in the range " @@ -125,10 +128,10 @@ class IndicatorParams(object): default='min,max,avg', validators=None) - intervals = IndicatorParam('intervals', - description=INTERVALS_PARAM_DOCSTRING, - required=False, - validators=None) + custom_time_agg = IndicatorParam('custom_time_agg', + description=CUSTOM_TIME_AGG_PARAM_DOCSTRING, + required=False, + validators=None) def __init__(self, default_units, available_units, valid_aggregations): """ Initialize additional params that are instance specific diff --git a/django/climate_change_api/indicators/query_ranges.py b/django/climate_change_api/indicators/query_ranges.py index 5826ac01..8dabada9 100644 --- a/django/climate_change_api/indicators/query_ranges.py +++ b/django/climate_change_api/indicators/query_ranges.py @@ -130,6 +130,7 @@ def day_of_year_from_date(cls, date, label): @classmethod def get_intervals(cls, label): + # Spans are in the format MM-DD:MM-DD, so break those into nested tuples spans = [tuple(tuple(int(v) for v in date.split('-')) for date in span.split(':')) for span in cls.custom_spans.split(',')] @@ -147,7 +148,6 @@ def cases(cls, intervals): # Check if that happened, and if it did clear the cached config if cls.custom_spans != intervals: cls.range_config = None - # Spans are in the format MM-DD:MM-DD, so break those into nested tuples cls.custom_spans = intervals return super(CustomRangeConfig, cls).cases()