-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharts.py
executable file
·176 lines (158 loc) · 4.73 KB
/
charts.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
#!/usr/bin/env python3
from sqlalchemy import desc
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
from models import (
Measurement,
CPUUtilization,
Process,
)
from engine import engine
import pygal
from collections import Counter
def round_to_interval(number, interval):
return (
(interval * int(number / interval))
+ (interval / 2)
)
def get_mode(list_like, bin_size):
binned_list = [
round_to_interval(item, bin_size)
for item in list_like
]
return Counter(binned_list).most_common(1)[0][0]
def chop_list(list_like, step_sizes):
return [
list_like[sum(step_sizes[:i]):sum(step_sizes[:i+1])]
for i in range(len(step_sizes))
]
def get_step_sizes(total_length, number_of_steps):
step_sizes = [int(total_length / number_of_steps)] * number_of_steps
remainder = total_length - sum(step_sizes)
for i in range(remainder):
step_sizes[i] += 1
return step_sizes
def get_measurement_times(session):
times_query = (
session
.query(Measurement.created_at)
.order_by(Measurement.created_at)
)
return [obj.created_at for obj in times_query]
def create_cpu_chart(session, measurement_times, steps):
query = (
session
.query(
Measurement.created_at,
CPUUtilization.index,
CPUUtilization.total,
)
.filter(
Measurement.id == CPUUtilization.measurement_id,
)
)
measurements = {}
for measurement_time, cpu_index, total in query:
cpu_measurements = measurements.setdefault(cpu_index, {})
cpu_measurements[measurement_time] = total
chart = pygal.Line(
x_label_rotation=20,
x_labels_major_count=10,
show_minor_x_labels=False,
show_dots=False,
)
step_sizes = get_step_sizes(len(measurement_times), steps)
mid_points = [
sum(step_sizes[:i]) + int(step_sizes[i]/2)
for i in range(len(step_sizes))
]
chart.x_labels = [
measurement_times[i].isoformat()
for i in mid_points
]
for cpu_index, cpu_measurements in measurements.items():
data = [
cpu_measurements.get(measurement_time, 0)
for measurement_time
in measurement_times
]
chopped_data = chop_list(data, step_sizes)
modes = [
get_mode(part, 2)
for part in chopped_data
]
chart.add(
'{}'.format(cpu_index),
modes,
allow_interruptions=True,
)
chart.render_to_file('./cpu_chart.svg')
chart.render_to_png('./cpu_chart.png')
def create_process_chart(session, measurement_times, steps):
interesting_cmdlines = (
session
.query(Process.cmdline)
.group_by(Process.cmdline)
.order_by(desc(func.sum(Process.cpu_percent)))
.slice(0, 5)
)
process_query = (
session
.query(
Measurement.created_at,
Process.cmdline,
Process.cpu_percent,
)
.filter(
Process.cmdline.in_(interesting_cmdlines),
Measurement.id == Process.measurement_id,
)
)
measurements = {}
for measurement_time, cmdline, cpu_percent in process_query:
cpu_measurements = measurements.setdefault(cmdline, {})
cpu_measurements[measurement_time] = cpu_percent
chart = pygal.Line(
x_label_rotation=20,
x_labels_major_count=10,
show_minor_x_labels=False,
show_dots=False,
legend_at_bottom=True,
legend_at_bottom_columns=1,
)
step_sizes = get_step_sizes(len(measurement_times), steps)
mid_points = [
sum(step_sizes[:i]) + int(step_sizes[i]/2)
for i in range(len(step_sizes))
]
chart.x_labels = [
measurement_times[i].isoformat()
for i in mid_points
]
for cmdline, cpu_measurements in measurements.items():
data = [
cpu_measurements.get(measurement_time, 0)
for measurement_time
in measurement_times
]
chopped_data = chop_list(data, step_sizes)
modes = [
get_mode(part, 2)
for part in chopped_data
]
chart.add(
'{}'.format(cmdline),
modes,
allow_interruptions=True,
)
chart.render_to_file('./process_chart.svg')
chart.render_to_png('./process_chart.png')
def run():
Session = sessionmaker(bind=engine)
session = Session()
measurement_times = get_measurement_times(session)
steps = 400
create_cpu_chart(session, measurement_times, steps)
create_process_chart(session, measurement_times, steps)
if __name__ == '__main__':
run()