-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriverlevels.py
297 lines (279 loc) · 11.6 KB
/
riverlevels.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
#!/usr/bin/python
#=======================================================================
"""
Get river levels from Environment Agency
"""
#-----------------------------------------------------------------------
# Copyright (C) 2016-2024 Colin Hogben <colin@pythontech.co.uk>
#
# This library 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 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#=======================================================================
from __future__ import print_function
import os
import logging
import sys
if sys.version_info[0] < 3:
from urllib2 import urlopen
else:
from urllib.request import urlopen
from collections import namedtuple
import json
import subprocess
try:
from html import escape
except ImportError: # PY2
from cgi import escape
DEFAULT_CONFIG_FILE = '~/.riverlevels.conf'
DEFAULT_SAVE_FILE = '~/.riverlevels.save'
API_ROOT = 'http://environment.data.gov.uk/flood-monitoring'
WEB_BASE = 'https://check-for-flooding.service.gov.uk/station'
# Acknowledgement of data source, requested by EA
ACKNOWLEDGEMENT = 'This uses Environment Agency flood and river level data'\
' from the real-time data API (Beta)'
_log = logging.getLogger('riverlevels')
Alert = namedtuple('Alert', ['updown', # 'UP' or 'DOWN' for level change
'name',
'RLOIid',
'direction', # 'u' or 'd' for monitor
'text'])
class Monitor(object):
"""Handler for a single measurement at a station."""
def __init__(self, station, qualifier='Stage', name=None, RLOIid=None, threshold=0.1):
self.station = station
self.qualifier = qualifier
self.name = name or station
self.RLOIid = RLOIid
self.threshold = threshold
self.key = '%s.%s' % (station, qualifier)
self.alert_level = None
self.alert_date = None
def from_save(self, save):
"""Update internal state from savefile data."""
if self.key not in save:
return
data = save[self.key]
self.alert_level = data['alert_level']
self.alert_date = data['alert_date']
def to_save(self, save):
"""Update savefile data from internal state."""
data = dict()
data['alert_level'] = self.alert_level
data['alert_date'] = self.alert_date
save[self.key] = data
def get_measures(self):
url = API_ROOT+'/id/measures?stationReference='+self.station
f = urlopen(url)
body = f.read()
data = json.loads(body)
return data
def get_level(self):
data = self.get_measures()
for item in data['items']:
if item['qualifier'] == self.qualifier:
if item['parameter'] == 'level':
latest = item['latestReading']
return latest['value'], latest['dateTime']
raise KeyError('No level measure for %s' % self.qualifier)
def check_alert(self, value, date):
"""Return an alert for the monitor if the level has changed.
The criterion is that the current level differs from the
level at the last alert (or the first measurement) by more than
the given threshold. That way (1) small changes in level do
not raise spurious alerts; and (2) a fast-changing level will
result in a sequence of alerts - which is no bad thing.
"""
if self.alert_level is None:
# First reading
self.alert_level = value
self.alert_date = date
return None
delta = value - self.alert_level
_log.debug('value=%g delta=%g', value, delta)
if abs(delta) > self.threshold:
updown = 'UP' if delta > 0 else 'DOWN'
text = ('now %.2fm, %s by %.0fcm since %s' %
(value,
updown,
abs(delta) * 100,
self.alert_date.replace('T',' ').replace('Z','')))
self.alert_level = value
self.alert_date = date
dirn = 'd' if 'Downstream' in self.qualifier else 'u'
return Alert(updown, self.name, self.RLOIid, dirn, text)
return None
class Manager(object):
def __init__(self, monitors, config={}):
self.monitors = monitors
self.config = config
self.savefile = os.path.expanduser(config.get('savefile',
DEFAULT_SAVE_FILE))
self.save = {}
self.read_save()
@classmethod
def from_config(cls, config):
monitors = []
for mondef in config.pop('monitors', []):
monitors.append(Monitor(**mondef))
return cls(monitors, config)
@classmethod
def from_config_file(cls, filespec):
if hasattr(filespec, 'read'):
config = json.load(filespec)
else:
with open(filespec,'r') as f:
config = json.load(f)
return cls.from_config(config)
def read_save(self):
if not os.path.exists(self.savefile):
return
with open(self.savefile,'r') as f:
data = f.read()
self.save = json.loads(data)
for mon in self.monitors:
mon.from_save(self.save)
def write_save(self):
for mon in self.monitors:
mon.to_save(self.save)
newsave = self.savefile + '.new'
with open(newsave,'w') as f:
data = json.dumps(self.save)
json.dump(self.save, f, indent=2, sort_keys=True)
os.rename(newsave, self.savefile)
def evaluate_alerts(self):
"""Check level for each monitor and return alerts.
An alert is raised if the current level differs from the previously
alerted level by more than the configured threshold.
Each alert is a tuple ('UP' or 'DOWN', name, text)
"""
alerts = []
for mon in self.monitors:
mon.from_save(self.save)
try:
value, date = mon.get_level()
except Exception as e:
_log.warn('%s: %s', mon.name, e)
else:
_log.info('%s: old=%s new=%s',
mon.name, mon.alert_level, value)
alert = mon.check_alert(value, date)
if alert:
alerts.append(alert)
return alerts
def email_alerts(self, no_action=False):
"""Evaluate alerts and send via email.
"""
email = self.config.get('email')
if not email:
raise ValueError('No "email" group in configuration')
recipients = email.get('recipients')
if not recipients:
raise ValueError('No email recipients in configuration')
html = email.get('html')
alerts = self.evaluate_alerts()
if not alerts:
return
up = any([a.updown == 'UP' for a in alerts])
down = any([a.updown == 'DOWN' for a in alerts])
updown = 'UP/DOWN' if up and down else 'UP' if up else 'DOWN'
subject = email.get('subject', 'River level changes ' + updown)
headers = ['To: %s' % ','.join(recipients),
'Subject: %s' % subject]
from_ = email.get('from')
if from_:
headers.append('From: %s' % from_)
if html:
body = ['<!doctype html>',
'<html lang="en">',
'<head><title>%s</title></head>' % escape(subject),
'<body>',
'<div id="alerts">']
for a in alerts:
hname = escape(a.name)
if a.RLOIid is not None:
url = '%s/%s' % (WEB_BASE, a.RLOIid)
if a.direction == 'd':
url += '/downstream'
hname = '<a href="%s">%s</a>' % (escape(url), hname)
body.append('%s %s<br>' % (hname, escape(a.text)))
body += ['</div>',
'<div id="ack">%s</div>' % escape(ACKNOWLEDGEMENT),
'</body>',
'</html>']
headers.append('Content-type: text/html; charset=UTF-8')
else:
body = ['%s %s' % (alert.name, alert.text) for alert in alerts]
body += ['', ACKNOWLEDGEMENT]
headers.append('Content-type: text/plain; charset=UTF-8')
text = '\n'.join(headers +
[''] +
[b.encode('utf-8') for b in body]) + '\n'
if no_action:
print(text, end='')
else:
sendmail = email.get('sendmail', '/usr/sbin/sendmail')
p = subprocess.Popen([sendmail, '-t', '-oi'],
stdin=subprocess.PIPE)
p.communicate(text.encode('utf8'))
def cmdline():
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-v','--verbose', dest='loglevel',
action='store_const', const=logging.INFO,
default=logging.WARNING)
ap.add_argument('-d','--debug', dest='loglevel',
action='store_const', const=logging.DEBUG)
sub = ap.add_subparsers(dest='action', metavar='ACTION')
# level [-q qual] stationref
level = sub.add_parser('level',
help='Get the current level from a given station')
level.add_argument('-q','--qualifier', default='Stage',
help='Select which measure (default: Stage)')
level.add_argument('station')
# alerts [-c conf]
alerts = sub.add_parser('alerts',
help=Manager.evaluate_alerts.__doc__.split('\n')[0])
alerts.add_argument('-c','--config', type=argparse.FileType('r'),
help='Configuration file (default: %s)' %
DEFAULT_CONFIG_FILE)
# email-alerts [-c conf] [-n]
email = sub.add_parser('email-alerts',
help=Manager.email_alerts.__doc__.split('\n')[0])
email.add_argument('-c','--config', type=argparse.FileType('r'),
help='Configuration file (default: %s)' %
DEFAULT_CONFIG_FILE)
email.add_argument('-n','--no-action', action='store_true',
help='Output email but do not send it.')
# --
args = ap.parse_args()
logging.basicConfig(level=args.loglevel)
if args.action == 'level':
mon = Monitor(args.station, args.qualifier)
value, date = mon.get_level()
print(value, date)
elif args.action == 'alerts':
conffile = args.config or os.path.expanduser(DEFAULT_CONFIG_FILE)
manager = Manager.from_config_file(conffile)
alerts = manager.evaluate_alerts()
manager.write_save()
for alert in alerts:
print(alert)
elif args.action == 'email-alerts':
conffile = args.config or os.path.expanduser(DEFAULT_CONFIG_FILE)
manager = Manager.from_config_file(conffile)
manager.email_alerts(no_action=args.no_action)
manager.write_save()
if __name__=='__main__':
cmdline()