• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2015 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
5# Simplified version of telemetry Value system, just enough for us to get
6# us up and running.
7
8
9class Value(object):
10
11  def __init__(self, canonical_url, name, units, description=None,
12               important=False, ir_stable_id=None):
13    self.canonical_url = canonical_url
14    self.name = name
15    self.units = units
16    self.description = description
17    self.important = important
18    self.ir_stable_id = ir_stable_id
19
20  def AsDict(self):
21    d = {
22        'canonical_url': self.canonical_url,
23        'name': self.name,
24        'important': self.important
25    }
26    # Only dump values if they're non-None, because Python json-ification turns
27    # these to null, instead of leaving them out.
28    if self.units is not None:
29      d['units'] = self.units
30
31    if self.description is not None:
32      d['description'] = self.description
33
34    if self.ir_stable_id is not None:
35      d['ir_stable_id'] = self.ir_stable_id
36
37    self._AsDictInto(d)
38    assert 'type' in d
39
40    return d
41
42  def _AsDictInto(self, d):
43    raise NotImplementedError()
44
45  @classmethod
46  def FromDict(cls, d):
47    if d['type'] == 'dict':
48      return DictValue.FromDict(d)
49    elif d['type'] == 'scalar':
50      return ScalarValue.FromDict(d)
51    elif d['type'] == 'failure':
52      return FailureValue.FromDict(d)
53    elif d['type'] == 'skip':
54      return SkipValue.FromDict(d)
55    else:
56      raise NotImplementedError()
57
58
59# TODO(eakuefner): Change to NumericValue after porting Unit
60# (https://github.com/catapult-project/catapult/issues/2049)
61class ScalarValue(Value):
62
63  def __init__(self, canonical_url, name, value, description=None,
64               important=False, ir_stable_id=None):
65    assert isinstance(value, dict)
66    super(ScalarValue, self).__init__(canonical_url, name, units=None,
67                                      description=description,
68                                      important=important,
69                                      ir_stable_id=ir_stable_id)
70    self._value = value
71
72  def __repr__(self):
73    return '%s("%s", "%s")' % (self.__class__.__name__,
74                               self.name, self.value)
75
76  def _AsDictInto(self, d):
77    d['type'] = 'scalar'
78    d['value'] = self._value
79
80  @classmethod
81  def FromDict(cls, d):
82    assert d.get('units', None) == None
83    return cls(d['canonical_url'], name=d['name'],
84               description=d.get('description', None),
85               value=d['value'],
86               important=d['important'],
87               ir_stable_id=d.get('ir_stable_id', None))
88
89  @property
90  def value(self):
91    return self._value
92
93  def __getitem__(self, key):
94    return self._value[key]
95
96
97class DictValue(Value):
98
99  def __init__(self, canonical_url, name, value, description=None,
100               important=False, ir_stable_id=None):
101    assert isinstance(value, dict)
102    super(DictValue, self).__init__(canonical_url, name, units=None,
103                                    description=description,
104                                    important=important,
105                                    ir_stable_id=ir_stable_id)
106    self._value = value
107
108  def __repr__(self):
109    return '%s("%s", "%s")' % (self.__class__.__name__,
110                               self.name, self.value)
111
112  def _AsDictInto(self, d):
113    d['type'] = 'dict'
114    d['value'] = self._value
115
116  @classmethod
117  def FromDict(cls, d):
118    assert d.get('units', None) == None
119    return cls(d['canonical_url'], name=d['name'],
120               description=d.get('description', None),
121               value=d['value'],
122               important=d['important'],
123               ir_stable_id=d.get('ir_stable_id', None))
124
125  @property
126  def value(self):
127    return self._value
128
129  def __getitem__(self, key):
130    return self._value[key]
131
132class FailureValue(Value):
133
134  def __init__(self, canonical_url, failure_type_name, description, stack,
135               important=False, ir_stable_id=None):
136    super(FailureValue, self).__init__(canonical_url,
137                                       name=failure_type_name,
138                                       units=None,
139                                       description=description,
140                                       important=important,
141                                       ir_stable_id=ir_stable_id)
142    assert isinstance(stack, basestring)
143    self.stack = stack
144
145  def __repr__(self):
146    return '%s("%s", "%s")' % (self.__class__.__name__,
147                               self.name, self.description)
148
149  def _AsDictInto(self, d):
150    d['type'] = 'failure'
151    d['stack_str'] = self.stack
152
153  @classmethod
154  def FromDict(cls, d):
155    assert d.get('units', None) == None
156    return cls(d['canonical_url'],
157               failure_type_name=d['name'],
158               description=d.get('description', None),
159               stack=d['stack_str'],
160               important=d.get('important', False),
161               ir_stable_id=d.get('ir_stable_id', None))
162
163  def GetGTestPrintString(self):
164    return self.stack
165
166
167class SkipValue(Value):
168
169  def __init__(self, canonical_url, skipped_result_name,
170               description=None, important=False, ir_stable_id=None):
171    super(SkipValue, self).__init__(canonical_url,
172                                    name=skipped_result_name,
173                                    units=None,
174                                    description=description,
175                                    important=important,
176                                    ir_stable_id=ir_stable_id)
177
178  def __repr__(self):
179    return '%s("%s", "%s")' % (self.__class__.__name__,
180                               self.name, self.description)
181
182  def _AsDictInto(self, d):
183    d['type'] = 'skip'
184
185  @classmethod
186  def FromDict(cls, d):
187    assert d.get('units', None) == None
188    return cls(d['canonical_url'],
189               skipped_result_name=d['name'],
190               description=d.get('description', None),
191               important=d.get('important', False),
192               ir_stable_id=d.get('ir_stable_id', None))
193