-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimelyMetric.py
More file actions
144 lines (103 loc) · 3.3 KB
/
TimelyMetric.py
File metadata and controls
144 lines (103 loc) · 3.3 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
import Timely
import time
import datetime
import json
import pandas
import getopt
import sys
import matplotlib
import numpy as np
from matplotlib import pyplot
from tornado import ioloop
class TimelyMetric(Timely.TimelyWebSocketClient):
client = None
endtime = None
def __init__(self, metric, startTime, endTime):
Timely.TimelyWebSocketClient.__init__(self, metric, startTime, endTime)
self.df = pandas.DataFrame()
self.series = pandas.Series()
self.data = None
def _on_message(self, msg):
global df
global endtime
obj = json.loads(msg)
date = datetime.datetime.fromtimestamp(int(obj.get("timestamp")/1000)).strftime('%Y-%m-%d %H:%M:%S')
# d = {'date' : date, obj.get("metric"): obj.get("value")}
data = []
datatype = []
names = []
data += [(date)]
datatype += [("date", "S20")]
names += [("date")]
data += [(obj.get("value"))]
datatype += [(str(obj.get("metric")), "i8")]
names += [(str(obj.get("metric")))]
d = {obj.get("metric"): obj.get("value")}
tags = obj.get("tags")[0]
for t in tags:
data += [(tags[t])]
datatype += [(str(t), "S50")]
names += [(str(t))]
currdata = np.rec.array(data, dtype=datatype)
currdata.dtype.names = names
if self.data is None:
self.data = currdata
else:
self.data = np.append(self.data, currdata)
print("--------------------")
print(datatype)
print(self.data)
print(self.data.dtype.names)
timestamp = int(obj.get("timestamp"));
if (timestamp >= self.endTime or (self.endTime - timestamp) < 60000):
print("exiting")
self._on_connection_close()
def _on_connection_close(self):
global client
print("--------------------")
print(self.data)
print(self.data.dtype.names)
print(self.data.dtype.names[1:])
self.df = pandas.DataFrame(self.data, columns=['timely.metrics.received'], index=self.data['date'])
print(self.df)
plt = self.df.plot();
locs, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.tight_layout(pad=2)
pyplot.show(block=True)
client.close()
exit()
def main():
global client
global endtime
# try:
# argv = sys.argv
# opts, args = getopt.getopt(argv, "hm", ["metric="])
# except getopt.GetoptError:
# print 'TimelyMetric.py -m <metric>'
# sys.exit(2)
# for opt, arg in args:
# if opt == '-h':
# print 'TimelyMetric.py -m <metric>'
# sys.exit()
# elif opt in ("-m", "--metric"):
# metric = arg
# print 'metric is "', metric
#
# pyplot.plot(range(10))
# pyplot.show(block=True)
#
# exit()
now = int(time.time() * 1000)
rangeInSec = 3600
metric = "timely.metrics.received"
startTime = int(now - (rangeInSec * 1000))
endTime = now
client = TimelyMetric(metric, startTime, endTime)
client.connect('wss://localhost:54323/websocket')
try:
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
client.close()
if __name__ == '__main__':
main()