1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Backward compatibility for old results API.
5
6This module helps convert the old PageMeasurementResults API into the new
7style one. This exists as a bridging solution so we can change the underlying
8implementation and update the PageMeasurementResults API once we know the
9underlying implementation is solid.
10"""
11from telemetry import value as value_module
12from telemetry.value import histogram
13from telemetry.value import list_of_scalar_values
14from telemetry.value import scalar
15
16
17def ConvertOldCallingConventionToValue(page, trace_name, units,
18                                       value, chart_name, data_type):
19  value_name = value_module.ValueNameFromTraceAndChartName(
20      trace_name, chart_name)
21  if data_type == 'default':
22    if isinstance(value, list):
23      return list_of_scalar_values.ListOfScalarValues(
24          page, value_name, units, value, important=True)
25    else:
26      return scalar.ScalarValue(page, value_name, units,
27                                value, important=True)
28  elif data_type == 'unimportant':
29    if isinstance(value, list):
30      return list_of_scalar_values.ListOfScalarValues(
31          page, value_name, units, value, important=False)
32    else:
33      return scalar.ScalarValue(page, value_name, units,
34                                value, important=False)
35  elif data_type == 'histogram':
36    assert isinstance(value, basestring)
37    return histogram.HistogramValue(
38        page, value_name, units, raw_value_json=value, important=True)
39  elif data_type == 'unimportant-histogram':
40    assert isinstance(value, basestring)
41    return histogram.HistogramValue(
42        page, value_name, units, raw_value_json=value, important=False)
43  elif data_type == 'informational':
44    raise NotImplementedError()
45  else:
46    raise ValueError('Unrecognized data type %s', data_type)
47