-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimelyMetricTest.py
181 lines (128 loc) · 4.72 KB
/
TimelyMetricTest.py
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
import Timely
import time
import datetime
import json
import pandas
import getopt
import sys
import matplotlib
import seaborn
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
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 = int(obj.get("timestamp")/1000);
dateStr = datetime.datetime.fromtimestamp(date).strftime('%Y-%m-%d %H:%M:%S')
# d = {'date' : date, obj.get("metric"): obj.get("value")}
data = []
datatype = []
names = []
data += [pandas.datetime.fromtimestamp(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(index=pandas.DatetimeIndex(self.data['date']))
self.df['date'] = self.data['date']
self.df['timely.metrics.received'] = self.data['timely.metrics.received']
self.df.index = pandas.DatetimeIndex(self.data['date'])
self.df = self.df.resample('1h').mean()
print("df begin --------------------")
print(self.df)
print("df end --------------------")
self.df.reset_index(inplace=True)
print(self.df)
# build the figure
fig, ax = plt.subplots()
seaborn.tsplot(self.df['timely.metrics.received'], time=matplotlib.dates.date2num(self.df.index), unit=None, interpolate=True, ax=ax, scalex=False, scaley=False)
# sns.tsplot(df, time='Date', value='Value', unit='Unit', ax=ax)
# assign locator and formatter for the xaxis ticks.
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y.%m.%d %H:%M:%S'))
# put the labels at 45deg since they tend to be too long
# fig.autofmt_xdate()
plt.show()
# plot = seaborn.tsplot(self.df['timely.metrics.received'], time=self.df.index, unit=None, interpolate=True, scalex=False, scaley=False)
# seaborn.axes_style(style='dark')
# seaborn.barplot(y=self.df['timely.metrics.received'], x=self.df.index, unit=None, units=0)
# seaborn.barplot(y='timely.metrics.received', data=self.df)
seaborn.plt.show()
# self.df.plot()
# locs, labels = pyplot.xticks()
# pyplot.setp(labels, rotation=90)
# 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()