...
 
Commits (2)
......@@ -26,15 +26,43 @@ log = logging.getLogger(__name__)
def generate_series_name(dimensions, dimensions_codes_order, dimensions_values_labels):
"""Generate a name for a time series based on dimensions values labels."""
"""Generate a name for a time series based on dimensions values labels.
>>> generate_series_name({}, [], {})
''
>>> generate_series_name({'FREQ': 'A'}, [], {})
''
>>> generate_series_name({}, [], {'FREQ': {'A': 'Annual'}})
''
>>> generate_series_name({}, ['FREQ'], {})
'FREQ (MISSING CODE)'
>>> generate_series_name({'FREQ': 'A'}, ['FREQ'], {'FREQ': {'A': 'Annual'}})
'Annual'
>>> generate_series_name({'FREQ': 'M'}, ['FREQ'], {'FREQ': {'A': 'Annual'}})
'M'
>>> generate_series_name({'FREQ': 'A', 'INDICATOR': 'X'}, ['FREQ', 'INDICATOR'], {'FREQ': {'A': 'Annual'}, 'INDICATOR': {'X': 'Files'}})
'Annual – Files'
# Duplicate dimensions values labels
>>> generate_series_name({'FREQ': 'A', 'INDICATOR': 'X'}, ['FREQ', 'INDICATOR'], {'FREQ': {'A': 'Annual'}, 'INDICATOR': {'X': 'Files', 'Y': 'Files'}})
'Annual – Files (X)'
>>> generate_series_name({'FREQ': 'A', 'INDICATOR': 'Z'}, ['FREQ', 'INDICATOR'], {'FREQ': {'A': 'Annual'}, 'INDICATOR': {'X': 'Files', 'Y': 'Files'}})
'Annual – Z'
"""
def get_dimension_value_label(dimension_code):
dimension_value_code = dimensions.get(dimension_code)
if dimension_value_code is None:
log.error("Missing dimension value code in Solr series for dimension_code {}: {}".format(
dimension_code, dimensions))
return "{} (MISSING CODE)".format(dimension_code)
dimension_value_label = dimensions_values_labels.get(dimension_code, {}).get(dimension_value_code) \
or dimension_value_code
dimension_values_labels = dimensions_values_labels.get(dimension_code, {})
dimension_value_label = dimension_values_labels.get(dimension_value_code)
if dimension_value_label is None:
dimension_value_label = dimension_value_code
else:
dimension_value_labels = dimension_values_labels.keys()
if len(dimension_value_labels) > 1:
dimension_value_label += ' ({})'.format(dimension_value_code)
return dimension_value_label
series_name = " – ".join(map(get_dimension_value_label, dimensions_codes_order))
return series_name
......