This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCMS6History.py
327 lines (318 loc) · 16.6 KB
/
CMS6History.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-
#
# Copyright 2015 Institut für Experimentelle Kernphysik - Karlsruher Institut für Technologie
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hf
from sqlalchemy import TEXT, Column
import json
import datetime
import ast
class CMS6History(hf.module.ModuleBase):
config_keys = {'sourceurl': ('Source Url', ''),
'plotrange': ('number of hours in plot (maximum is 24)', '24'),
'plotsize_x': ('size of the plot in x', '10'),
'sites': ('differnet sites - input a python list with strings', '["gridka", "ekpcms6", "ekp-cloud", "ekpsg", "ekpsm","bwforcluster"]'),
'plotsize_y': ('size of plot in y', '5.8'),
'plot_width': ('width of bars in plot', '1'),
}
table_columns = [
Column('filename_plot', TEXT),
Column('error_msg', TEXT)
], ['filename_plot']
def prepareAcquisition(self):
link = self.config['sourceurl']
self.plotrange = int(self.config['plotrange'])
self.plotsize_x = float(self.config['plotsize_x'])
self.plotsize_y = float(self.config['plotsize_y'])
self.plot_width = float(self.config['plot_width'])
temp = self.config['sites']
self.sites = ast.literal_eval(temp)
# Download the file
self.source = hf.downloadService.addDownload(link)
# Get URL
self.source_url = self.source.getSourceUrl()
# Set up Container for subtable data
self.statistics_db_value_list = []
def extractData(self):
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
data = {}
data['filename_plot'] = ""
data['error_msg'] = "0"
path = self.source.getTmpPath()
# open file
with open(path, 'r') as f:
# fix the JSON-File, so the file is valid
content = f.read()
content_fixed = content.replace("}, }", "} }")
content_fixed = content_fixed.replace("},\n }", "} }")
services = json.loads(content_fixed)
job_id_list = list(services.keys())
if len(job_id_list) == 0: # stop script if no jobs in condor
data['status'] = 0.5
data['error_msg'] = "No jobs in the last " + str(self.plotrange) + " hours."
return data
# get time from file
current_time = int(services[job_id_list[0]]['time'])
# create lists with all neeeded values
completion_date_list = list(int(services[ID]['CompletionDate'])for ID in job_id_list)
start_date_list = list(services[ID]['JobStartDate']for ID in job_id_list)
final_status_list = list(int(services[ID]['JobStatus'])for ID in job_id_list)
last_status_list = []
for ID in job_id_list:
try:
last_status_list.append(int(services[ID]['LastJobStatus']))
except KeyError:
last_status_list.append(0)
except ValueError:
last_status_list.append(0)
final_status_date_list = list(
int(services[ID]['EnteredCurrentStatus'])for ID in job_id_list)
qdate_list = list(int(services[ID]['QueueDate'])for ID in job_id_list)
total_jobs = len(job_id_list)
starttime = current_time - (self.plotrange * 60 * 60)
qdate_list = list(map(lambda x: x - starttime, qdate_list))
qdate_list = list(map(lambda x: round(float(x) / (60 * 60), 0), qdate_list))
final_status_date_list = list(map(lambda x: x - starttime, final_status_date_list))
final_status_date_list = list(
map(lambda x: round(float(x) / (60 * 60), 0), final_status_date_list))
# define lists for plot
plot_data_running = np.zeros(self.plotrange + 1)
plot_data_idle = np.zeros(self.plotrange + 1)
plot_data_queued = np.zeros(self.plotrange + 1)
plot_data_finished = np.zeros(self.plotrange + 1)
plot_data_removed = np.zeros(self.plotrange + 1)
plot_data_hosts = np.zeros(len(self.sites))
for i in xrange(total_jobs):
try:
host = services[job_id_list[i]]['HostName']
except KeyError:
host = ""
# some jobs have no start_date_list value
if start_date_list[i] != "undefined":
start_date_list[i] = round(
float(int(start_date_list[i]) - starttime) / (60 * 60), 0)
# running jobs have a completion_date_list value of zero
if completion_date_list[i] > 0:
completion_date_list[i] = round(
float(completion_date_list[i] - starttime) / (60 * 60), 0)
for k in xrange(len(self.sites)):
if self.sites[k] in host:
plot_data_hosts[k] += 1
# remove ekpsm machines and add them to ekpsg for clearer monitoring
for k in xrange(len(self.sites)):
if self.sites[k] == "ekpsm":
temp = k
if self.sites[k] == "ekpsg":
temp_2 = k
plot_data_hosts[temp_2] += plot_data_hosts[temp]
self.sites.remove("ekpsm")
plot_data_hosts = np.delete(plot_data_hosts, temp)
########################################
# create Lists for barPlot, sorted by time
########################################
# Function for handling of the time in queue, same for every job #
def qtime_handling(qdate, startdate, plot_data_queued):
k = int(round(qdate, 0))
# if time when job was queued and starting time are older than plotrange,
# set k to zero and detect job as running
if k <= 0 and startdate <= 0:
k = 0
# if queue is older than plotrange but start is not - count job as queued
# if k is zero or greater
elif k < 0 and startdate > 0:
while k < startdate:
if k >= 0:
plot_data_queued[k] += 1
k += 1
# queued in plotrange - just check time queued
else:
while k <= startdate:
plot_data_queued[k] += 1
k += 1
return k
# Function to set the lists for the last two states correctly
def final_handling(completion, k, final_state_list, last_state_list):
# while k is smaller than the time entering the final state, put the job
# in the last state before that and iterate
while k <= completion:
last_state_list[k] += 1
k += 1
# if the arravial in the final state is less than an hour ago - put job in
# final_state and last_state at the same time
if k == self.plotrange + 1:
final_state_list[k - 1] += 1
# otherwise - put job in final state in the next hour
else:
final_state_list[k] += 1
for i in xrange(total_jobs):
# exclude jobs that are still running for the moment
if completion_date_list[i] != 0:
# normal jobs, finished with completed and before they ran
if final_status_list[i] == 4 and last_status_list[i] == 2:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(completion_date_list[i], k,
plot_data_finished, plot_data_running)
# jobs that completed but were removed after that
elif final_status_list[i] == 3 and last_status_list[i] == 4:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(completion_date_list[i], k, plot_data_removed, plot_data_running)
# jobs that were running and then went on hold
elif final_status_list[i] == 5 and last_status_list[i] == 2:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(completion_date_list[i], k,
plot_data_finished, plot_data_running)
# jobs that were idle and then finished
elif final_status_list[i] == 4 and last_status_list[i] == 1:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(completion_date_list[i], k, plot_data_finished, plot_data_queued)
# jobs that dont have an CompletionDate
else:
# jobs that started, queued and finished plotrange hours ago
if final_status_list[i] == 4 and last_status_list[i] == 2:
if qdate_list[i] == 0 and start_date_list[i] == 0 and completion_date_list[i] == 0:
plot_data_finished[0] += 1
# jobs that were removed and were on hold before - never ran
elif final_status_list[i] == 3 and last_status_list[i] == 5:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
if final_status_date_list[i] >= 0 and k > 25:
plot_data_removed[k] += 1
elif k == 25:
plot_data_removed[24] += 1
# jobs that were removed and ran before
elif final_status_list[i] == 3 and last_status_list[i] == 2:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(final_status_date_list[i],
k, plot_data_removed, plot_data_running)
# jobs that were removed and before removed
elif final_status_list[i] == 3 and last_status_list[i] == 3:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(final_status_date_list[i],
k, plot_data_removed, plot_data_queued)
# jobs from condor_q that are still running
elif final_status_list[i] == 2 and last_status_list[i] == 1 and start_date_list[i] != "undefined":
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
if k == self.plotrange + 1:
plot_data_running[k - 1] += 1
else:
k += 1
while k < self.plotrange + 1:
plot_data_running[k] += 1
k += 1
# jobs that are idle at the moment in condor_q and havent started yet
elif final_status_list[i] == 1 and last_status_list[i] == 0:
k = round(qdate_list[i])
if k < 0:
k = 0
while k < self.plotrange + 1:
plot_data_queued[k] += 1
k += 1
# queued Jobs that got removed before they started to run
elif final_status_list[i] == 3 and last_status_list[i] == 1:
k = qtime_handling(qdate_list[i], final_status_date_list[i], plot_data_queued)
k = round(final_status_date_list[i])
if k >= 0:
plot_data_removed[k] += 1
# jobs that completed - so ran before, but then got removed
elif final_status_list[i] == 3 and last_status_list[i] == 4:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(final_status_date_list[i],
k, plot_data_removed, plot_data_running)
# job that went from running to idle
elif final_status_list[i] == 1 and last_status_list[i] == 2:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(final_status_date_list[i], k, plot_data_idle, plot_data_running)
k += 1
while k < self.plotrange + 1:
plot_data_idle[k] += 1
k += 1
# job that went from running to removed
elif final_status_list[i] == 5 and last_status_list[i] == 2:
k = qtime_handling(qdate_list[i], start_date_list[i], plot_data_queued)
final_handling(final_status_date_list[i], k, plot_data_removed, plot_data_running)
###############
# Make plot #
###############
plot_color = {
'removed': '#e69f00',
'running': '#d55e00',
'finished': '#009e73',
'queued': '#0072b2',
'idle': '#56b4e9',
}
# define size according to config
fig = plt.figure(figsize=(self.plotsize_x, self.plotsize_y*2))
gs = gridspec.GridSpec(2, 1)
axis = plt.subplot(gs[0, 0])
axis_2 = plt.subplot(gs[1, 0])
ind = np.arange(self.plotrange + 1)
ind_2 = np.arange(len(self.sites))
width = self.plot_width
bar_1 = axis.bar(ind, plot_data_running, width, color=plot_color['running'], align='center')
bar_2 = axis.bar(ind, plot_data_queued, width, bottom=plot_data_running,
color=plot_color['queued'], align='center')
bar_3 = axis.bar(ind, plot_data_idle, width, bottom=plot_data_running +
plot_data_queued, color=plot_color['idle'], align='center')
bar_4 = axis.bar(ind, plot_data_removed, width, bottom=plot_data_queued +
plot_data_running + plot_data_idle, color=plot_color['removed'], align='center')
bar_5 = axis.bar(ind, plot_data_finished, width, bottom=plot_data_queued + plot_data_running +
plot_data_removed + plot_data_idle, color=plot_color['finished'], align='center')
axis.set_xlabel("Time")
axis.set_ylabel("Jobs")
xlabels = []
for i in xrange(self.plotrange + 1):
if i % 2 == 0:
time_tick = starttime + i * 60 * 60
xlabels.append(datetime.datetime.fromtimestamp(
float(time_tick)).strftime('%d.%m \n %H:%M'))
else:
xlabels.append("")
today_readable = datetime.datetime.fromtimestamp(
float(current_time)).strftime('%Y-%m-%d %H:%M:%S')
starttime_readable = datetime.datetime.fromtimestamp(
float(starttime)).strftime('%Y-%m-%d %H:%M:%S')
axis.set_title("Job Distribution from " + str(starttime_readable) +
" CET to " + str(today_readable) + " CET")
axis.set_xticks(ind)
axis.set_xticklabels(xlabels, rotation='vertical')
axis.set_xlim(-1, self.plotrange + 1)
fontLeg = FontProperties()
fontLeg.set_size('small')
axis.legend((bar_1[0], bar_2[0], bar_3[0], bar_4[0], bar_5[0]),
("runnings jobs", "queued jobs", "idle jobs", "removed jobs", "finished jobs"),
loc=6, bbox_to_anchor=(0.8, 0.88), borderaxespad=0., prop=fontLeg)
# plot that shows site usage
bar_21 = axis_2.bar(ind_2, plot_data_hosts, width*0.5, align='center', color=plot_color['finished'])
for rect in bar_21:
height = rect.get_height()
axis_2.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom')
axis_2.set_xticks(ind_2)
max_height = axis_2.get_ylim()[1]
axis_2.set_ylim(0, max_height*1.2)
axis_2.set_xlim(-0.5, len(self.sites)-0.5)
axis_2.set_xticklabels(self.sites)
axis_2.set_title("Site Distribution from " + str(starttime_readable) +
" CET to " + str(today_readable) + " CET")
axis_2.set_ylabel("finished Jobs")
axis_2.set_xlabel("Sites")
plt.tight_layout()
fig.savefig(hf.downloadService.getArchivePath(
self.run, self.instance_name + "_history.png"), dpi=91)
data["filename_plot"] = self.instance_name + "_history.png"
print data
return data