-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathehtobs-bot.py
106 lines (83 loc) · 3.25 KB
/
ehtobs-bot.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
import sys
import datetime
from collections import defaultdict
import time
import pyvexfile
import slack_utils
vex_date_format = '%Yy%jd%Hh%Mm%Ss'
def vex_duration(value):
return int(value.replace(' sec', '').strip())
def get_events(vexfile):
v = pyvexfile.Vex('foo', vexfile=vexfile)
events = []
all_stations = set()
sfirst = {}
slast = {}
for scan in v['SCHED'].values():
if 'Scan' not in str(type(scan)):
continue
entry = scan['start']
# start = 2023y109d17h25m00s; mode=band6; source=3C84;
# in theory the vex parser should split these entries, but it doesn't
start = entry.value.split(';')[0]
start = datetime.datetime.strptime(start, vex_date_format).timestamp()
s_stations = []
s_starts = []
s_ends = []
entries = scan['station']
if not isinstance(entries, list):
# single-station scans come back as scalar, not list
entries = [entries]
for entry in entries:
s_station = entry.value[0]
all_stations.add(s_station)
s_start = vex_duration(entry.value[1])
if s_station not in sfirst:
sfirst[s_station] = start + s_start
s_end = vex_duration(entry.value[2])
slast[s_station] = start + s_end
s_stations.append(s_station)
s_starts.append(s_start)
s_ends.append(s_end)
if any(x != s_starts[0] for x in s_starts):
for x in s_starts:
print(x)
raise ValueError('different s_start in', s_starts)
if any(x != s_ends[0] for x in s_ends):
raise ValueError('different s_start in', s_ends)
station_string = ':'.join(sorted(s_stations))
events.append((start, 'start of scan '+scan.name, station_string))
events.append((start+s_ends[0], 'end of scan '+scan.name, station_string))
assert len(s_starts) == len(s_ends)
start_to_stations = defaultdict(list)
for station, start in sfirst.items():
start_to_stations[start].append(station)
end_to_stations = defaultdict(list)
for station, end in slast.items():
end_to_stations[end].append(station)
for start, stations in start_to_stations.items():
station_string = ':'.join(sorted(stations))
events.append((start - 3600, 'first scan in 1 hour', station_string))
events.append((start - 300, 'first scan in 5 minutes', station_string))
for end, stations in end_to_stations.items():
events.append((end, 'last scan done', ':'.join(sorted(stations))))
# sort events by time
events = sorted(events) # defaults to first element
final = max([x for x in end_to_stations.keys()])
events.append((final, 'end of schedule', 'for all stations'))
return events
events = get_events(sys.argv[1])
beginning = True
webhook = slack_utils.get_slack_webhook('eht', 'ehtobs_bots_schedule')
for e in events:
now = time.time()
t, message, stations = e
if t < now-2 and beginning:
print('skipping', message, stations)
continue
beginning = False
delta = t - now
if delta > 0:
time.sleep(delta)
print(message, stations)
slack_utils.slack_message(message+' '+stations, webhook)