-
Notifications
You must be signed in to change notification settings - Fork 22
/
affected.py
executable file
·114 lines (95 loc) · 3.73 KB
/
affected.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
#!/usr/bin/env python
import yaml
from ooi_data.postgres.model import *
from tools.m2m import MachineToMachine
from database import create_engine_from_url, create_scoped_session
engine = create_engine_from_url(None)
session = create_scoped_session(engine)
MetadataBase.query = session.query_property()
def build_dpi_map():
"""
Build a map from a specific data product identifier to a set of parameters which fulfill it
:return:
"""
dpi_map = {}
for p in Parameter.query:
if p.data_product_identifier:
dpi_map.setdefault(p.data_product_identifier, set()).add(p)
return dpi_map
def build_affects_map():
"""
Build a map from parameter to the set of parameters *directly* affected by it
:return:
"""
dpi_map = build_dpi_map()
affects_map = {}
for p in Parameter.query:
if p.is_function:
pmap = p.parameter_function_map
for key in pmap:
values = pmap[key]
if not isinstance(values, list):
values = [values]
for value in values:
if isinstance(value, Number): continue
if value.startswith('CC'): continue
if value.startswith('dpi_'):
value = value.split('dpi_')[-1]
for param in dpi_map.get(value, []):
affects_map.setdefault(param, set()).add(p)
if 'PD' in value:
pdid = int(value.split('PD')[-1])
param = Parameter.query.get(pdid)
affects_map.setdefault(param, set()).add(p)
return affects_map
def parameter_affects(pdid, affects_map):
"""
Given a specific parameter and a map of parameter to the set of its directly affected parameters,
traverse the given graph to determine all possible affected parameters for the given parameter.
Return the map of stream_name to affected parameters.
:param pdid:
:param affects_map:
:return:
"""
p = Parameter.query.get(pdid)
affected = {p}
to_visit = affects_map[p]
while to_visit:
p = to_visit.pop()
affected.add(p)
for param in affects_map.get(p, []):
if param in affected:
continue
affected.add(param)
to_visit.add(param)
streams = {}
for p in affected:
for stream in p.streams:
streams.setdefault(stream.name, set()).add(p)
return streams
def find_affected(affected_streams, subsite, node, toc):
"""
Given a map of affected streams for a parameter, traverse the TOC and identify all instrument streams
with the same subsite and node which are affected. For each affected stream, print the affected parameters.
:param affected_streams:
:param subsite:
:param node:
:param toc:
:return:
"""
# TODO handle instruments at the same depth but with a different "node"
for each in toc['instruments']:
if each['platform_code'] == subsite and each['mooring_code'] == node:
for stream in each['streams']:
name = stream['stream']
for parameter in sorted(affected_streams.get(name, [])):
print '{refdes} {stream:<30} {parameter.id:<4} {parameter.name}'.format(
refdes=each['reference_designator'],
stream=stream['stream'],
parameter=parameter)
config = yaml.load(open('m2m_config.yml'))
m2m = MachineToMachine(config['url'], config['apiname'], config['apikey'])
toc = m2m.toc()
affects_map = build_affects_map()
affected_streams = parameter_affects(194, affects_map)
find_affected(affected_streams, 'RS03AXPS', 'SF03A', toc)