-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm6schedulemon.py
executable file
·428 lines (315 loc) · 17.9 KB
/
m6schedulemon.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python
###########################################################################
# Copyright (C) 2018 Helge Rottmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
###########################################################################
from mark6control import Mark6, Mark6Exception, Mark6Scan
from tkinter import *
import tkinter.messagebox
import tkinter.ttk
import subprocess
import argparse
import os
import sys
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
from time import sleep
class GUI(Frame):
def __init__(self, parent, args, mark6):
'''
Constructor
'''
Frame.__init__(self, parent)
self.parent=parent
self.args = args
self.mark6 = mark6
self.scans = []
self.showFutureScans = BooleanVar()
self.showFutureScans.set(False)
self.nextScan = -1
self.initScanList()
self.mk6InputStreams = self.mark6.getInputStreams()
self.setupWidgets()
self.updateClock()
self.updateMark6State()
self.update()
def onChkShowFutureClick(self, event):
self.update()
def setupWidgets(self):
"""
setup the widgets
"""
self.mk6Slot = []
self.mk6Capacity = []
self.parent.title("Mark6 monitor: %s" % (self.args.recorder))
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=100)
self.parent.grid_columnconfigure(1,weight=1)
self.parent.grid_rowconfigure(10,weight=100)
self.parent.grid_columnconfigure(10,weight=1)
self.parent.grid_rowconfigure(20,weight=1)
self.parent.grid_columnconfigure(20,weight=1)
#self.parent.config(background="lavender")
# main frame
mainFrame = LabelFrame(self.parent, text='')
mainFrame.grid(row=0,column=0, columnspan=2, sticky='new')
mainFrame.grid_columnconfigure(1,weight=10)
schedFrame = LabelFrame(mainFrame, text='Schedule')
schedFrame.grid(row=0,column=0, sticky="news")
Label(schedFrame, text=os.path.basename(self.args.schedule), font=("Courier", 12)).grid(row=0,column=0, sticky=W)
utcFrame = LabelFrame(mainFrame, text='')
utcFrame.grid(row=1,column=0, sticky="news")
self.clock = Label(utcFrame, font=('Courier', 24, 'bold') )
self.clock.grid(row=0,column=0, sticky="news")
mk6Frame = LabelFrame(mainFrame, text='Mark6')
mk6Frame.grid(row=0,column=1, rowspan=2, sticky="news")
for i in range(4):
self.mk6Slot.append( Button(mk6Frame, text="Slot", wraplength=70, bg="lawn green", width=7, height=4,disabledforeground="black",state=DISABLED))
self.mk6Capacity.append ( Label(mk6Frame, font=('Courier', 8)))
self.mk6Slot[0].grid(row=0,column=0, rowspan=2)
self.mk6Slot[1].grid(row=0,column=1, rowspan=2)
self.mk6Slot[2].grid(row=2,column=0, rowspan=2)
self.mk6Slot[3].grid(row=2,column=1, rowspan=2)
self.mk6Capacity[0].grid(row=0,column=2, sticky=W)
self.mk6Capacity[1].grid(row=1,column=2, sticky=W)
self.mk6Capacity[2].grid(row=2,column=2, sticky=W)
self.mk6Capacity[3].grid(row=3,column=2, sticky=W)
# Define widgets in the options frame
optionsFrame = LabelFrame(self.parent, text='Options')
optionsFrame.grid(row=20,column=0, columnspan=2, sticky='new')
self.chkShowFuture = Checkbutton(optionsFrame, text = "show future scans", variable = self.showFutureScans)
self.chkShowFuture.grid(row=0,column=0, columnspan=2,sticky='ew')
self.chkShowFuture.bind("<ButtonRelease-1>", self.onChkShowFutureClick)
# treeview frame
self.tree = tkinter.ttk.Treeview( self.parent, columns=('tick','source', 'start', 'stop', 'duration','gaptime', 'status'))
self.tree.heading('#0', text='+',anchor=W)
self.tree.heading('#1', text='scan')
self.tree.heading('#2', text='source')
self.tree.heading('#3', text='start')
self.tree.heading('#4', text='stop')
self.tree.heading('#5', text='duration [s]')
self.tree.heading('#6', text='gap [s]')
self.tree.heading('#7', text='status', anchor=W)
self.tree.column('#0', width=0,stretch=NO)
self.tree.column('#1', width=160,stretch=NO)
self.tree.column('#2', width=130, stretch=NO)
self.tree.column('#3', width=130, stretch=NO)
self.tree.column('#4', width=130, stretch=NO)
self.tree.column('#5', width=70, stretch=NO)
self.tree.column('#6', width=70, stretch=NO)
self.tree.column('#7', stretch=YES)
self.tree.grid(row=10, column=0, sticky='nesw')
#scrollbar
vsb = tkinter.ttk.Scrollbar(orient="vertical",command=self.tree.yview)
self.tree.configure(yscrollcommand=vsb.set)
vsb.grid(row=10, column=1, sticky='ns')
# color code the lines based on status field
self.tree.tag_configure('missing', background='salmon')
self.tree.tag_configure('pending', background='yellow')
self.tree.tag_configure('OK', background='green yellow')
self.tree.tag_configure('Error', background='red')
self.tree.tag_configure('recording', background='green')
def updateMark6State(self):
self.mark6.readSlotInfo()
self.mk6InputStreams = self.mark6.getInputStreams()
msg = ""
streamSlots = []
groupSlots = []
activeSlots = []
# check the input_streams
if len(self.mk6InputStreams) == 0:
msg += "No input streams defined"
for stream in self.mk6InputStreams:
streamSlots += list(stream['slots'])
# check if all modules are activated and the number of discovered disks is 8
for i in range(4):
error = 0
capacityStr = "Slot {:d} capacity [TB] (total/free): {:.1f} / {:.1f}"
if self.mark6.slots[i].vsn == "unknown":
self.mk6Slot[i]["text"] = "inactive"
capacityStr = capacityStr.format(i+1, 0, 0)
error += 1
if str(i+1) in streamSlots:
msg += "Required (by stream) slot {:d} is currently inactive\n".format(i+1)
else:
activeSlots.append(i+1)
if self.mark6.slots[i].numDisksDiscovered != 8 or self.mark6.slots[i].numDisksRegistered != 8:
error += 1
msg += "Module {} (slot {}): less than 8 disks found\n".format(self.mark6.slots[i].vsn, i+1)
groupSlots += list(map(int, self.mark6.slots[i].group))
self.mk6Slot[i]["text"] = "{}\n{:.1f}%".format(self.mark6.slots[i].vsn, self.mark6.slots[i].freePercentage)
capacityStr = capacityStr.format(i+1, self.mark6.slots[i].groupCapacityGB/1000, self.mark6.slots[i].capacityRemainingGB/1000)
self.mk6Capacity[i]["text"] = capacityStr
# change color to red in case of error
if error > 0:
self.mk6Slot[i]["bg"] = "red"
# verify that all slots requested by the group are active
groupSlots = list(set(groupSlots))
for i in groupSlots:
if i not in activeSlots:
msg += "Required (by group) slot {:d} is currently inactive\n".format(int(i)+1)
if len(msg) > 0:
tkinter.messagebox.showerror("Error", msg)
self.mk6Slot[0].after(10000, self.updateMark6State)
def updateClock(self):
global time1
# get the current local time from the PC
timeStr = datetime.utcnow().strftime('%H:%M:%S')
self.clock.config(text=timeStr)
self.clock.after(300, self.updateClock)
def update(self):
"""
update the scan state and refresh the table
This method is refreshed based on the schedule
"""
self.nextScan, future = self.updateScanList()
sleepUntil = None
# disable chkbox if no future scans to come
if (future == 0):
self.chkShowFuture['state'] = DISABLED
else:
self.chkShowFuture['state'] = NORMAL
# clear the treeview
self.tree.delete(*self.tree.get_children())
for scan in self.scans:
idx = self.scans.index(scan)
if scan['tense'] == 1 and idx != self.nextScan and self.showFutureScans.get() == False:
continue
# script was started in the middle of a running scan
if scan["tense"] == 0:
sleepUntil = scan['stopDateTime'] + timedelta(seconds=5)
marker = "+"
elif idx == self.nextScan:
sleepUntil = scan['startDateTime'] + timedelta(seconds=5)
marker = "+"
else:
marker = " "
gapMin = scan["gapTime"]
self.tree.insert('', 0, text=marker, values=(scan["name"], scan["source"], scan["startTimeStr"], scan["stopTimeStr"], scan["duration"], gapMin, scan['status']), tags=(scan["status"]))
# sleep until next scans finishes or current one stops recording
if sleepUntil:
print("sleeping until %s" % sleepUntil.strftime("%H:%M:%S"))
self.parent.after((sleepUntil - datetime.utcnow()).seconds*1000, self.update)
def updateScanList(self):
'''
update the scan list (status, sizes) based on the recorder feedback
Returns:
int: the index of the scan upcoming next in the schedule
int: the number of future scans
'''
self.mark6.readScanList()
candidate = ""
next = -1
future = 0
for scan in self.scans:
idx = self.scans.index(scan)
dtNow = datetime.utcnow()
if scan['stopDateTime'] < dtNow: # past scan
scan['tense'] = -1 #past
recFilename = "%s_%s_%s" %(scan['exp'],scan['station'],scan['name'])
recScan = self.mark6.getScanByName(recFilename)
if recScan:
scan['recSize'] = recScan.size
scan['status'] = 'OK'
else:
scan['recSize'] = 0
scan['status'] = 'missing'
elif scan['startDateTime'] < dtNow and scan['stopDateTime'] > dtNow: # scan should be recording now
scan['tense'] = 0 #present
recState = self.mark6.getRecordingState()
if recState['state'] == "recording":
scan['status'] = recState['state']
else: # scan should be recording but is not
# only show error once
if scan['status'] != "Error":
tkinter.messagebox.showerror("Error", "scan %s is not recording on %s" % (scan['name'], self.args.recorder))
scan['status'] = "Error"
next = idx
elif scan['startDateTime'] > dtNow: # future scan
scan['tense'] = 1 #future
future =+ 1
# first future scan
if next == -1:
# check if scheduled
recState = self.mark6.getRecordingState()
if recState['state'] == 'pending' or recState['state'] == 'off':
scan['status'] = "pending"
next = idx
return next, future
def initScanList(self):
tree = ET.parse(self.args.schedule)
root = tree.getroot()
for scan in root.findall("scan"):
scanInfo = {}
duration = int(scan.get('duration'))
scanName = scan.get('scan_name').strip()
station = scan.get('station_code').strip()
exp = scan.get('experiment').strip()
startTime = scan.get('start_time')
dtStart = datetime.strptime(startTime, '%Y%j%H%M%S')
dtStop = dtStart + timedelta(seconds=duration)
dtNow = datetime.utcnow()
#recFilename = "%s_%s_%s" %(exp,station,scanName)
#recScan = mark6.getScanByName(recFilename)
scanInfo['name'] = scanName
scanInfo['station'] = station
scanInfo['exp'] = exp
scanInfo['duration'] = duration
scanInfo['startTimeStr'] = dtStart.strftime('%Y-%j %H:%M:%S')
scanInfo['stopTimeStr'] = dtStop.strftime('%Y-%j %H:%M:%S')
scanInfo['startDateTime'] = dtStart
scanInfo['stopDateTime'] = dtStop
scanInfo['recsize'] = 0
scanInfo['source'] = scan.get('source')
scanInfo['status'] = ""
scanInfo['tense'] = -1 # 1=future,0=present,-1=past
self.scans.append(scanInfo)
# calculate the gap times
for i in range(len(self.scans)-1):
self.scans[i]["gapTime"] = (self.scans[i+1]['startDateTime'] - self.scans[i]['stopDateTime']).total_seconds()
# last scan has empty gap time
self.scans[-1]['gapTime'] = ""
return
def getRecordedScans(mark6):
mark6.readScans()
return mark6.scans
def getRecordedScanSize(scanname, files):
for file in files:
size, name = file.split()
if os.path.splitext(name)[0] == scanname:
return int(size)
return -1
def main():
parser = argparse.ArgumentParser(description='Monitor the recordering progress of a Mark6 recorder')
parser.add_argument("recorder",type=str, help='the host name of the recorder to monitor')
parser.add_argument("schedule",type=str, help='the name of the xml recording schedule')
parser.add_argument("-p","--port", type=int, default=14242, help='Port on which cplane is listening (default 14242)"')
args = parser.parse_args()
# check that schedule file exists
if not os.path.isfile(args.schedule):
sys.exit ("schedule file does not exist: %s" % args.schedule)
# connect to cplane
mark6 = Mark6(args.recorder, args.port)
try:
mark6.connect()
except Exception as e:
sys.exit(e.message)
root = Tk()
root.title("mark6 schedule monitor")
root.geometry("820x500")
app = GUI(root, args, mark6)
root.mainloop()
if __name__ == "__main__":
main()