-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpoint.py
More file actions
238 lines (192 loc) · 6.23 KB
/
point.py
File metadata and controls
238 lines (192 loc) · 6.23 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# btrdb.point
# Module for Point classes
#
# Author: PingThings
# Created: Fri Dec 21 14:57:30 2018 -0500
#
# For license information, see LICENSE.txt
# ID: point.py [] [email protected] $
"""
Module for Point classes
"""
##########################################################################
## Imports
##########################################################################
from btrdb.grpcinterface import btrdb_pb2
##########################################################################
## Point Classes
##########################################################################
class RawPoint(object):
"""
A point of data representing a single position within a time series. Each
point contains a read-only time and value attribute.
Parameters
----------
time : int
The time portion of a single value in the time series in nanoseconds
since the Unix epoch.
value : float
The value of a time series at a single point in time.
"""
__slots__ = ["_time", "_value"]
def __init__(self, time, value):
self._time = time
self._value = value
@property
def time(self):
"""
The time portion of a data point in nanoseconds since the Unix epoch.
"""
return self._time
@property
def value(self):
"""
The value portion of a data point as a float object.
"""
return self._value
@classmethod
def from_proto(cls, proto):
return cls(proto.time, proto.value)
@classmethod
def from_proto_list(cls, proto_list):
return [cls.from_proto(proto) for proto in proto_list]
def __getitem__(self, index):
if index == 0:
return self.time
elif index == 1:
return self.value
else:
raise IndexError("RawPoint index out of range")
@staticmethod
def to_proto(point):
return btrdb_pb2.RawPoint(time = point[0], value = point[1])
@staticmethod
def to_proto_list(points):
return [RawPoint.to_proto(p) for p in points]
def __repr__(self):
return "RawPoint({0}, {1})".format(repr(self.time), repr(self.value))
def __str__(self):
return self.__repr__()
def __eq__(self, other):
if not hasattr(other, "time") or not hasattr(other, "value"):
return False
return self.time == other.time and self.value == other.value
class StatPoint(object):
"""
An aggregated data point representing a summary or rollup of one or more
points of data within a single time series.
This aggregation point provides for the min, mean, max, count, and standard
deviation of all data values it spans. It is returned by windowing queries
such as `windows` or `aligned_windows`.
Parameters
----------
time : int
The time in which the aggregated values represent in nanoseconds since
the Unix epoch.
min : float
The minimum value in a time series within a specified range of time.
mean : float
The mean value in a time series within a specified range of time.
max : float
The maximum value in a time series within a specified range of time.
count : float
The number of values in a time series within a specified range of time.
stddev : float
The standard deviation of values in a time series within a specified
range of time.
Notes
-----
This object may also be treated as a tuple by referencing the values
according to position.
.. code-block:: python
// returns time
val = point[0]
// returns standard deviation
val = point[5]
"""
__slots__ = ["_time", "_min", "_mean", "_max", "_count", "_stddev"]
def __init__(self, time, minv, meanv, maxv, count, stddev):
self._time = time
self._min = minv
self._mean = meanv
self._max = maxv
self._count = count
self._stddev = stddev
@classmethod
def from_proto(cls, proto):
return cls(proto.time, proto.min, proto.mean, proto.max, proto.count, proto.stddev)
@classmethod
def from_proto_list(cls, proto_list):
return [cls.from_proto(proto) for proto in proto_list]
@property
def time(self):
"""
The mean value of the time series point within a range of time
"""
return self._time
@property
def min(self):
"""
The minimum value of the time series within a range of time
"""
return self._min
@property
def mean(self):
"""
The mean value of the time series within a range of time
"""
return self._mean
@property
def max(self):
"""
The maximum value of the time series within a range of time
"""
return self._max
@property
def count(self):
"""
The number of values within the time series for a range of time
"""
return self._count
@property
def stddev(self):
"""
The standard deviation of the values of a time series within a range of time
"""
return self._stddev
def __getitem__(self, index):
if index == 0:
return self.time
elif index == 1:
return self.min
elif index == 2:
return self.mean
elif index == 3:
return self.max
elif index == 4:
return self.count
elif index == 5:
return self.stddev
else:
raise IndexError("RawPoint index out of range")
def __repr__(self):
return "StatPoint({}, {}, {}, {}, {}, {})".format(
self.time,
self.min,
self.mean,
self.max,
self.count,
self.stddev
)
def __str__(self):
return self.__repr__()
def __eq__(self, other):
for attr in "time", "min", "mean", "max", "count", "stddev":
if not hasattr(other, attr):
return False
return self.time == other.time and \
self.min == other.min and \
self.mean == other.mean and \
self.max == other.max and \
self.count == other.count and \
self.stddev == other.stddev