/
update_cruise_definition.py
executable file
·488 lines (433 loc) · 18.3 KB
/
update_cruise_definition.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/env python3
"""Read sensor and parameter tables from CORIOLIX database and create
a cruise definition file. If the --interval flag is specified, check
the database every that many seconds and, if there has been a change,
output a new cruise definition.
"""
import json
import logging
import sys
import time
import urllib.request
import urllib.error
import yaml
LOGGING_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
# Templates into which variables are to be folded to create a config
# for each logger in each mode.
MODE_CONFIG_TEMPLATES = {
'off': """ {config_name}:
name: {config_name}
""",
'raw': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
writers:
- class: TextFileWriter
""",
'parsed': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
transforms:
- class: ParseTransform
kwargs:
record_format: '{{timestamp:ti}},{{field_string}}'
field_patterns: {field_patterns}
metadata: {metadata}
metadata_interval: {metadata_interval}
writers:
- class: TextFileWriter
""",
'udp': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
transforms:
- class: PrefixTransform
kwargs:
prefix: {sensor_id}
- class: ParseTransform
kwargs:
record_format: '{{data_id:w}} {{timestamp:ti}},{{field_string}}'
field_patterns: {field_patterns}
metadata: {metadata}
metadata_interval: {metadata_interval}
writers:
- class: UDPWriter
kwargs:
port: 6221
""",
'cache': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
transforms:
- class: PrefixTransform
kwargs:
prefix: {sensor_id}
- class: ParseTransform
kwargs:
record_format: '{{data_id:w}} {{timestamp:ti}},{{field_string}}'
field_patterns: {field_patterns}
metadata: {metadata}
metadata_interval: {metadata_interval}
writers:
- class: CachedDataWriter
kwargs:
data_server: localhost:8766
""",
'file': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
writers:
- class: LogfileWriter
kwargs:
filebase: /var/tmp/log/openrvdas/{sensor_id}/raw/{sensor_id}
split_char: ','
""",
'database': """ {config_name}:
name: {config_name}
readers:
- class: UDPReader
kwargs:
port: {transmit_port}
transforms:
- class: PrefixTransform
kwargs:
prefix: {sensor_id}
- class: ParseTransform
kwargs:
record_format: '{{data_id:w}} {{timestamp:ti}},{{field_string}}'
field_patterns: {field_patterns}
metadata: {metadata}
metadata_interval: {metadata_interval}
writers: {writers}
""",
}
################################################################################
class CruiseDefinitionCreator:
############################
def __init__(self, host_path, destination=None, interval=None,
metadata_interval=None):
"""
```
host_path - e.g. http://coriolix:8000/api/
destination - where to write the cruise definition. If omitted,
write to stdout
interval - if specified, check coriolix database every interval seconds
and if it has been changed, output a new cruise definition.
metadata_interval - If specified, how often parsers should send
metadata about their variables
```
"""
self.host_path = host_path
self.destination = destination
self.interval = interval
self.metadata_interval = metadata_interval
self.sensors = None
self.parameters = None
############################
def run(self):
"""Read database and create output file. If interval is not None, loop
and keep checking.
"""
self.get_update()
self.write_cruise_definition()
while self.interval:
time.sleep(self.interval)
logging.debug('Checking whether database has changed.')
changed = self.get_update()
if changed:
logging.info('Database changed - creating new cruise definition.')
self.write_cruise_definition()
############################
def get_update(self):
"""Get the latest sensor and parameter definitions from the
database. Return True if they have changed.
"""
logging.info('Getting updated parameters')
changed = False
sensors = self.make_get_request('sensor/?format=json')
if not sensors == self.sensors:
logging.info('Sensors changed')
# logging.debug('Sensors changed from %s to %s', self.sensors, sensors)
self.sensors = sensors
changed = True
parameters = self.make_get_request('parameter/?format=json')
if not parameters == self.parameters:
logging.info('Parameters changed')
self.parameters = parameters
changed = True
if not changed:
logging.info('No change.')
return changed
############################
def write_cruise_definition(self):
"""Get the latest sensor and parameter definitions from the
database.
"""
definition = self.create_cruise_definition()
if not definition:
logging.info('No cruise definition - not writing anything.')
return
# Actually do something here
logging.info('Writing cruise definition')
if self.destination:
with open(self.destination, 'w') as dest_file:
dest_file.write(definition)
else:
print(definition)
############################
def create_cruise_definition(self):
"""Compile the acquired information into a Python dict.
"""
if not self.sensors:
logging.info('No sensor definitions?')
return None
if not self.parameters:
logging.info('No parameter definitions?')
return None
# Create a map from sensor_id->list of parameters
sensor_parameters = {}
for parameter in self.parameters:
sensor_id = parameter.get('sensor_id', None)
if not sensor_id:
logging.error('No Sensor ID found for parameter %s', parameter)
continue
# If it's a sensor_id we haven't seen yet, create an entry for
# it in the dict.
if sensor_id not in sensor_parameters:
sensor_parameters[sensor_id] = []
sensor_parameters[sensor_id].append(parameter)
# Create quick lookup map: sensor_id->sensor definition, but only
# for sensors that actually have all the information we need.
sensor_map = {}
for sensor in self.sensors:
sensor_id = sensor.get('sensor_id', None)
if not sensor.get('transmit_port', None):
logging.info('Sensor %s - no port specified - skipping', sensor_id)
continue
if not sensor.get('text_regex_format', None):
logging.info('Sensor %s missing text regex - skipping', sensor_id)
continue
if not sensor_parameters.get(sensor_id, None):
logging.info('Sensor %s has no parameters - skipping', sensor_id)
continue
sensor_map[sensor_id] = sensor
# Empty template of cruise definition we're going to fill in.
cruise_definition = ''
# Fill in the logger definitions
cruise_definition += '####################\nloggers:\n'
for sensor_id in sensor_map:
cruise_definition += ' {}:\n'.format(sensor_id)
cruise_definition += ' configs:\n'
# configs = [sensor_id + '->' + mode for mode in MODE_CONFIG_TEMPLATES]
# cruise_definition['loggers'][sensor_id] = {'configs': configs}
for mode in MODE_CONFIG_TEMPLATES:
cruise_definition += ' - {}->{}\n'.format(sensor_id, mode)
# Fill in the mode definitions
cruise_definition += '####################\nmodes:\n'
for mode in MODE_CONFIG_TEMPLATES:
cruise_definition += ' \'{}\':\n'.format(mode)
for sensor_id in sensor_map:
cruise_definition += ' {}: {}->{}\n'.format(sensor_id,
sensor_id, mode)
cruise_definition += '\n####################\n'
cruise_definition += 'default_mode: \'off\'\n'
# Now create a config entry for every sensor x mode combination
cruise_definition += '\n####################\nconfigs:\n'
for sensor_id, sensor in sensor_map.items():
cruise_definition += ' #############\n # {}\n'.format(sensor_id)
for mode, mode_template in MODE_CONFIG_TEMPLATES.items():
# What's below is for creating the YAML text for a single
# logger config, defined by sensor_id X mode.
# Get the pattern(s) we're supposed to match and munge them a
# little to get in the right format. First, decode it from
# JSON. Second, make sure it's a list (and make it one if it
# isn't). Third, we're going to pull the datetime/timestamp
# off separately, so if it's there at the start of the
# pattern, get rid of it.
field_pattern_json = sensor.get('text_regex_format').strip()
field_patterns = json.loads(field_pattern_json) # decode JSON
if not isinstance(field_patterns, list): # make it a list
field_patterns = [field_patterns]
ts_field = '{datetime:ti},' # get rid of ts field
field_patterns = [
pat[len(ts_field):]
for pat in field_patterns
if pat.find(ts_field) == 0
]
logging.debug('pattern: %s', field_patterns)
# Assemble names of variables in the pattern(s) to form a
# metadata dict.
device_type = '{}: {} {}'.format(
sensor.get('sensor_name', 'unknown type'),
sensor.get('vendor', 'unknown vendor'),
sensor.get('model', 'unknown model')
)
# If we're supposed to be outputting metadata regularly,
# assemble the descriptions of each of the variables.
metadata_dict = {}
if self.metadata_interval:
for parameter in sensor_parameters[sensor_id]:
parameter_name = parameter.get('data_fieldname')
metadata_dict[parameter_name] = {
'device': sensor_id,
'device_type': device_type,
'device_type_field': parameter.get('short_name', '-'),
'description': parameter.get('description', '-'),
'units': parameter.get('units', '-'),
}
# Assemble the list of tables to which this sensor's data
# should be written.
sensor_tables = {}
for parameter in sensor_parameters[sensor_id]:
data_fieldname = parameter.get('data_fieldname')
data_table = parameter.get('data_table')
data_model = parameter.get('data_model')
if data_table not in sensor_tables:
sensor_tables[data_table] = {}
if data_model not in sensor_tables[data_table]:
sensor_tables[data_table][data_model] = []
sensor_tables[data_table][data_model].append(data_fieldname)
# Format of the writer(s) will depend on whether its
# parameters need to be written to more than one table. Break
# it out into a separate method to keep things more readable.
sensor_writers = self.generate_writers(sensor_tables)
# Assemble all the information needed for the config here and
# append to our dict of configs.
value_dict = {
'config_name': sensor_id + '->' + mode,
'sensor_id': sensor_id,
'mode': mode,
'transmit_port': sensor.get('transmit_port'),
'field_patterns': field_patterns,
'metadata': metadata_dict,
'metadata_interval': self.metadata_interval,
'writers': sensor_writers,
}
config = self.fill_template(mode_template, value_dict)
cruise_definition += config
return cruise_definition
############################
def generate_writers(self, sensor_tables):
"""Given a dict of the tables and parameters that are to be written
to, create a simple or composed writer to do that. A simple writer
is one where all the parameters are getting written to the same
table/model. A composed writer is needed when some parameters are
going to one table and others to another one.
"""
SIMPLE_WRITER = """ - class: PostgresWriter
module: local.rcrv.modules.postgres_writer
kwargs:
data_table: {data_table}
data_model: {data_model}
data_fieldnames: {data_fieldnames}"""
COMPOSED_WRITER = """ - class: ComposedWriter
kwargs:
transforms:
- class: SelectFieldsTransform
module: logger.transforms.select_fields_transform
kwargs:
keep: {data_fieldnames}
writers:
- class: PostgresWriter
module: local.rcrv.modules.postgres_writer
kwargs:
data_table: {data_table}
data_model: {data_model}
data_fieldnames: {data_fieldnames}"""
if len(sensor_tables) == 1:
template = SIMPLE_WRITER
else:
template = COMPOSED_WRITER
writer_str = '\n'
for data_table, models in sensor_tables.items():
# 'models' should be a dict of data_model:[param, param,...]
if len(models) != 1:
logging.fatal('More than one model used by a single data table?!? '
'Table: %s, models: %s', data_table, models)
# Turn parameters into a comma-separated list of names. This
# 'for' should only have a single iteration, based on the prior
# test that len(models) == 1.
for data_model, parameters in models.items():
data_fieldnames = ','.join(parameters)
# Append our formatted writer to any existing ones for this
# sensor (if we have a simple writer, there will only be a
# single one of these).
writer_str += template.format(
data_table=data_table,
data_model=data_model,
data_fieldnames=data_fieldnames) + '\n'
return writer_str
############################
def fill_template(self, template, value_dict):
"""Fill all {name} strings in the template with respective values from
the value_dict.
"""
return template.format(**value_dict)
############################
def make_get_request(self, request):
"""Make a GET request and return the (hopefully JSON) result.
"""
# Add a little resiliency in how we're called, whether host_path
# has ending slash and/or request has leading slash. Make sure
# there's one and only one.
url = self.host_path
if not url[-1] == '/':
url += '/'
if request[0] == '/':
request = request[1:]
url += request
logging.info('Getting %s', url)
try:
with urllib.request.urlopen(url) as response:
result = response.read().decode('utf-8')
logging.debug('Result of GET %s: %s', url, result)
except urllib.error.HTTPError:
logging.fatal('Unable to open URL "%s"', url)
sys.exit(1)
try:
return yaml.load(result, Loader=yaml.FullLoader)
except AttributeError:
# If they've got an older yaml, it may not have FullLoader)
return yaml.load(result)
################################################################################
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--host_path', dest='host_path', required=True,
help='Path to query for tables, e.g. '
'http://coriolix:8000/api/')
parser.add_argument('--destination', dest='destination', help='Where to write'
'the cruise definition. If omitted, write to stdout.')
parser.add_argument('--interval', dest='interval', type=float, default=None,
help='If specified, check coriolix database every '
'interval seconds and if it has been changed, output '
'a new cruise definition.')
parser.add_argument('--metadata_interval', dest='metadata_interval',
type=float, default=None,
help='If specified, how often parsers should send '
'metadata about their variables')
parser.add_argument('-v', '--verbosity', dest='verbosity',
default=0, action='count',
help='Increase output verbosity')
args = parser.parse_args()
LOG_LEVELS = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}
log_level = LOG_LEVELS[min(args.verbosity, max(LOG_LEVELS))]
logging.basicConfig(format=LOGGING_FORMAT, level=log_level)
creator = CruiseDefinitionCreator(args.host_path, args.destination,
args.interval, args.metadata_interval)
creator.run()