1#!/usr/bin/env python3
2
3"""Convert HLCH raw entries to HLCH events."""
4
5import logging
6
7from chart.alg import init_algorithm
8from chart.events.event import Event
9from chart.alg import JobStatus
10from chart.db import ts
11
12logger = logging.getLogger()
13
14# The parent APEX events
15EVENT_CLASS = 'SCHEDULE-EVENTS'
16
17# Acitvities event class and table
18ACITIVITY_EVENT_CLASS = 'ACTIVITY-EVENTS'
19ACTIVITIES_TABLE = 'SCHEDULE_ACTIVITIES'
20
21# Actions event class and table
22ACTION_EVENT_CLASS = 'TML-ACTION-EVENTS'
23ACTIONS_TABLE = 'SCHEDULE_TML_ACTIONS'
24
25
26def activity_events(sid, sensing_start, sensing_stop, eventraiser):
27 """Raise ACTIVITY-EVENT events."""
28 logger.info('Scanning for ACTIVITY events for {sid} from {start} to {stop}'.format(sid=sid, start=sensing_start, stop=sensing_stop))
29
30 for (sensing_time,
31 #stop_time,
32 id,
33 name,
34 proc_id,
35 ssid,
36 type,
37 _class,
38 exec_time,
39 trig_time,
40 abs_orbit,
41 pso,
42 status,
43 actions,
44 params,
45
46 ) in ts.select(
47 table=ACTIVITIES_TABLE,
48 sid=sid,
49 sensing_start=sensing_start,
50 sensing_stop=sensing_stop,
51 fields=('SENSING_TIME',
52 'INTERNAL_ID',
53 'NAME',
54 'PROC_ID',
55 'SSID',
56 'TYPE',
57 'CLASS',
58 'EXECUTION_TIME',
59 'TRIGGER_TIME',
60 'ABS_ORBIT',
61 'PSO',
62 'FINAL_STATUS',
63 'ACTIONS',
64 'PARAMETERS'
65 )):
66 eventraiser.add(Event(event_classname=ACITIVITY_EVENT_CLASS,
67 sid=sid,
68 start_time=sensing_time,
69 #stop_time=stop_time,
70 instance_properties={
71 'internal_id': id,
72 'name': name,
73 'procedure_id': proc_id,
74 'SSID': ssid,
75 'type': type,
76 'class': _class,
77 'execution_time': exec_time,
78 'trigger_time': trig_time,
79 'absolute_orbit': abs_orbit,
80 'PSO': pso,
81 'final_status': status,
82 'actions': actions,
83 'parameters': params
84 }))
85
86
87def tml_action_events(sid, sensing_start, sensing_stop, eventraiser):
88 """Raise TML ACTION events."""
89 logger.info('Scanning for TML ACTIONS events for {sid} from {start} to {stop}'.format(sid=sid, start=sensing_start, stop=sensing_stop))
90
91 for (sensing_time,
92 message,
93 status
94 ) in ts.select(
95 table=ACTIONS_TABLE,
96 sid=sid,
97 sensing_start=sensing_start,
98 sensing_stop=sensing_stop,
99 fields=('SENSING_TIME',
100 'MESSAGE',
101 'STATUS'
102 )):
103 eventraiser.add(Event(event_classname=ACTION_EVENT_CLASS,
104 sid=sid,
105 start_time=sensing_time,
106 instance_properties={
107 'message': message,
108 'status': status
109 }))
110
111
112def dispatch(wo, resultfile, eventraiser):
113 """We are being run by the dispatcher tool."""
114 logging.info('Starting {ev} Events'.format(ev=EVENT_CLASS))
115 for job in wo.read_jobs():
116 logging.info('Begin event from {start} to {stop}'.
117 format(start=job['sensing_start'],
118 stop=job['sensing_stop']))
119
120 activity_events(job.sid,
121 job.sensing_start,
122 job.sensing_stop,
123 eventraiser)
124
125 tml_action_events(job.sid,
126 job.sensing_start,
127 job.sensing_stop,
128 eventraiser)
129
130 resultfile.add_job(job, JobStatus.COMPLETED)
131
132 eventraiser.log_report()
133 logging.info('All done')
134
135
136def main():
137 """Command line entry point.\
138 This program can either be called via `dispatcher` or standalone."""
139 dispatch(*init_algorithm())
140
141
142if __name__ == '__main__':
143 main()