def transform_data_row_index(cls, index_values, dimension_display_values, dimension_hyperlink_templates): # Add the index to the row row = {} for key, value in index_values.items(): if key is None: continue data = {RAW_VALUE: metric_value(value)} # Try to find a display value for the item. If this is a metric the raw value is replaced with the # display value because there is no raw value for a metric label display = getdeepattr(dimension_display_values, (key, value)) if display is not None: data['display'] = display # If the dimension has a hyperlink template, then apply the template by formatting it with the dimension # values for this row. The values contained in `index_values` will always contain all of the required values # at this point, otherwise the hyperlink template will not be included. if key in dimension_hyperlink_templates: data['hyperlink'] = dimension_hyperlink_templates[key].format( **index_values) row[key] = data return row
def transform_data_row_values(cls, series, item_map): # Add the values to the row row = {} for key, value in series.iteritems(): key = wrap_list(key) value = metric_value(value) data = {RAW_VALUE: metric_value(value)} # Try to find a display value for the item item = item_map.get(key[0]) display = metric_display(value, getattr(item, 'prefix', None), getattr(item, 'suffix', None), getattr(item, 'precision', None)) if display is not None: data['display'] = display setdeepattr(row, key, data) return row
def _render_timeseries_data(group_df, metric_key): series = [] for dimension_values, y in group_df[metric_key].iteritems(): first_dimension_value = utils.wrap_list(dimension_values)[0] if pd.isnull(first_dimension_value): # Ignore totals on the x-axis. continue series.append((formats.date_as_millis(first_dimension_value), formats.metric_value(y))) return series
def _render_category_data(group_df, metric_key): categories = list(group_df.index.levels[0]) \ if isinstance(group_df.index, pd.MultiIndex) \ else list(group_df.index) series = [] for labels, y in group_df[metric_key].iteritems(): label = labels[0] if isinstance(labels, tuple) else labels series.append({ 'x': categories.index(label), 'y': formats.metric_value(y) }) return series
def _format_metric_cell(value, metric): """ Renders a table cell in a metric column for non-pivoted tables. :param value: The raw value of the metric. :param metric: A reference to the slicer metric to access the display formatting. :return: A dict containing the keys value and display with the raw and display metric values. """ raw_value = formats.metric_value(value) return { 'value': raw_value, 'display': formats.metric_display(raw_value, prefix=metric.prefix, suffix=metric.suffix, precision=metric.precision) if raw_value is not None else None }
def _render_pie_series(self, series, reference, data_frame, render_series_label): metric = series.metric name = reference_label(metric, reference) df_key = utils.format_metric_key(series.metric.key) data = [] for dimension_values, y in data_frame[df_key].sort_values(ascending=False).iteritems(): data.append({ "name": render_series_label(dimension_values) if dimension_values else name, "y": formats.metric_value(y), }) return { "name": name, "type": series.type, "data": data, 'tooltip': { 'pointFormat': '<span style="color:{point.color}">\u25CF</span> {series.name}: ' '<b>{point.y} ({point.percentage:.1f}%)</b><br/>', 'valueDecimals': metric.precision, 'valuePrefix': reference_prefix(metric, reference), 'valueSuffix': reference_suffix(metric, reference), }, }
def test_timestamp_data_point_is_returned_as_string_iso_no_time(self): # Needs to be converted to milliseconds result = formats.metric_value(pd.Timestamp(datetime(2000, 1, 1, 1))) self.assertEqual('2000-01-01T01:00:00', result)
def test_int64_data_point_is_returned_as_py_int(self): # Needs to be cast to python int result = formats.metric_value(np.int64(1)) self.assertEqual(int(1), result)
def test_str_data_point_is_returned_unchanged(self): result = formats.metric_value(u'abc') self.assertEqual('abc', result)
def test_that_neg_inf_data_point_is_converted_to_none(self): # np.nan is converted to None result = formats.metric_value(-np.inf) self.assertIsNone(result)
def test_that_literal_strings_are_not_converted(self): for value in ['NAN', 'INF', 'nan', 'inf']: with self.subTest(value): result = formats.metric_value(value) self.assertEqual(result, value)