summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/value/__init__.py
blob: c134637dac26a17f34d201dd661c03aaf3ad4851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Simplified version of telemetry Value system, just enough for us to get
# us up and running.


class Value(object):

  def __init__(self, canonical_url, name, units, description=None,
               important=False, ir_stable_id=None):
    self.canonical_url = canonical_url
    self.name = name
    self.units = units
    self.description = description
    self.important = important
    self.ir_stable_id = ir_stable_id

  def AsDict(self):
    d = {
        'canonical_url': self.canonical_url,
        'name': self.name,
        'important': self.important
    }
    # Only dump values if they're non-None, because Python json-ification turns
    # these to null, instead of leaving them out.
    if self.units is not None:
      d['units'] = self.units

    if self.description is not None:
      d['description'] = self.description

    if self.ir_stable_id is not None:
      d['ir_stable_id'] = self.ir_stable_id

    self._AsDictInto(d)
    assert 'type' in d

    return d

  def _AsDictInto(self, d):
    raise NotImplementedError()

  @classmethod
  def FromDict(cls, d):
    if d['type'] == 'dict':
      return DictValue.FromDict(d)
    elif d['type'] == 'scalar':
      return ScalarValue.FromDict(d)
    elif d['type'] == 'failure':
      return FailureValue.FromDict(d)
    elif d['type'] == 'skip':
      return SkipValue.FromDict(d)
    else:
      raise NotImplementedError()


# TODO(eakuefner): Change to NumericValue after porting Unit
# (https://github.com/catapult-project/catapult/issues/2049)
class ScalarValue(Value):

  def __init__(self, canonical_url, name, value, description=None,
               important=False, ir_stable_id=None):
    assert isinstance(value, dict)
    super(ScalarValue, self).__init__(canonical_url, name, units=None,
                                      description=description,
                                      important=important,
                                      ir_stable_id=ir_stable_id)
    self._value = value

  def __repr__(self):
    return '%s("%s", "%s")' % (self.__class__.__name__,
                               self.name, self.value)

  def _AsDictInto(self, d):
    d['type'] = 'scalar'
    d['value'] = self._value

  @classmethod
  def FromDict(cls, d):
    assert d.get('units', None) == None
    return cls(d['canonical_url'], name=d['name'],
               description=d.get('description', None),
               value=d['value'],
               important=d['important'],
               ir_stable_id=d.get('ir_stable_id', None))

  @property
  def value(self):
    return self._value

  def __getitem__(self, key):
    return self._value[key]


class DictValue(Value):

  def __init__(self, canonical_url, name, value, description=None,
               important=False, ir_stable_id=None):
    assert isinstance(value, dict)
    super(DictValue, self).__init__(canonical_url, name, units=None,
                                    description=description,
                                    important=important,
                                    ir_stable_id=ir_stable_id)
    self._value = value

  def __repr__(self):
    return '%s("%s", "%s")' % (self.__class__.__name__,
                               self.name, self.value)

  def _AsDictInto(self, d):
    d['type'] = 'dict'
    d['value'] = self._value

  @classmethod
  def FromDict(cls, d):
    assert d.get('units', None) == None
    return cls(d['canonical_url'], name=d['name'],
               description=d.get('description', None),
               value=d['value'],
               important=d['important'],
               ir_stable_id=d.get('ir_stable_id', None))

  @property
  def value(self):
    return self._value

  def __getitem__(self, key):
    return self._value[key]

class FailureValue(Value):

  def __init__(self, canonical_url, failure_type_name, description, stack,
               important=False, ir_stable_id=None):
    super(FailureValue, self).__init__(canonical_url,
                                       name=failure_type_name,
                                       units=None,
                                       description=description,
                                       important=important,
                                       ir_stable_id=ir_stable_id)
    assert isinstance(stack, basestring)
    self.stack = stack

  def __repr__(self):
    return '%s("%s", "%s")' % (self.__class__.__name__,
                               self.name, self.description)

  def _AsDictInto(self, d):
    d['type'] = 'failure'
    d['stack_str'] = self.stack

  @classmethod
  def FromDict(cls, d):
    assert d.get('units', None) == None
    return cls(d['canonical_url'],
               failure_type_name=d['name'],
               description=d.get('description', None),
               stack=d['stack_str'],
               important=d.get('important', False),
               ir_stable_id=d.get('ir_stable_id', None))

  def GetGTestPrintString(self):
    return self.stack


class SkipValue(Value):

  def __init__(self, canonical_url, skipped_result_name,
               description=None, important=False, ir_stable_id=None):
    super(SkipValue, self).__init__(canonical_url,
                                    name=skipped_result_name,
                                    units=None,
                                    description=description,
                                    important=important,
                                    ir_stable_id=ir_stable_id)

  def __repr__(self):
    return '%s("%s", "%s")' % (self.__class__.__name__,
                               self.name, self.description)

  def _AsDictInto(self, d):
    d['type'] = 'skip'

  @classmethod
  def FromDict(cls, d):
    assert d.get('units', None) == None
    return cls(d['canonical_url'],
               skipped_result_name=d['name'],
               description=d.get('description', None),
               important=d.get('important', False),
               ir_stable_id=d.get('ir_stable_id', None))