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

Commit

Permalink
Apply PR tweaks to comments and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Martz committed Dec 29, 2016
1 parent adcd94a commit 3b1ccbb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
17 changes: 9 additions & 8 deletions django/climate_change_api/climate_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions django/climate_change_api/indicators/abstract_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
27 changes: 15 additions & 12 deletions django/climate_change_api/indicators/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion django/climate_change_api/indicators/query_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')]
Expand All @@ -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()

0 comments on commit 3b1ccbb

Please sign in to comment.