1# Copyright 2016 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.
4from telemetry.value import failure
5from telemetry.value import improvement_direction
6from telemetry.value import scalar
7
8
9def TranslateMreFailure(mre_failure, page):
10  return failure.FailureValue.FromMessage(page, mre_failure.stack)
11
12
13def TranslateScalarValue(scalar_value, page):
14  assert (scalar_value['type'] == 'numeric' and
15          scalar_value['numeric']['type'] == 'scalar')
16  scalar_value['value'] = scalar_value['numeric']['value']
17
18  name = scalar_value['grouping_keys']['name']
19
20  unit_parts = scalar_value['numeric']['unit'].split('_')
21  if len(unit_parts) != 2:
22    raise ValueError('Must specify improvement direction for value ' + name)
23
24  scalar_value['units'] = unit_parts[0]
25
26  if unit_parts[1] == 'biggerIsBetter':
27    scalar_value['improvement_direction'] = improvement_direction.UP
28  else:
29    assert unit_parts[1] == 'smallerIsBetter'
30    scalar_value['improvement_direction'] = improvement_direction.DOWN
31
32  scalar_value['page_id'] = page.id
33  scalar_value['name'] = name
34  del scalar_value['grouping_keys']['name']
35  return scalar.ScalarValue.FromDict(scalar_value, {page.id: page})
36