Commit 57b3aff0 authored by Christophe Benz's avatar Christophe Benz
Browse files

Merge branch '407-enhance-series-names' into 'master'

Enhance series names generation when labels are duplicated, add tests

See merge request !23
parents aecc596c e44063a4
......@@ -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'], {})
>>> generate_series_name({'FREQ': 'A'}, ['FREQ'], {'FREQ': {'A': 'Annual'}})
>>> generate_series_name({'FREQ': 'M'}, ['FREQ'], {'FREQ': {'A': 'Annual'}})
>>> 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
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment