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 = 'APEX-EVENTS'
 16
 17# INSTANCES event class and table
 18INSTANCES_EVENT_CLASS = 'APEX-INSTANCES'
 19TABLE_INSTANCES = 'APEX_INSTANCES'
 20
 21# LOG event class and tablese
 22LOG_EVENT_CLASS = 'APEX-LOG'
 23TABLE_LOG = 'APEX_LOG'
 24
 25
 26def instances_events(sid, sensing_start, sensing_stop, eventraiser):
 27    """Raise APEX_INSTANCES events."""
 28    logger.info('Scanning for APEX Instances entries for {sid} from {strt} to {stop}'.format(
 29            sid=sid, strt=sensing_start, stop=sensing_stop))
 30
 31    for (sensing_time,
 32         stop_time,
 33         domain,
 34         instance,
 35         procedure,
 36         source,
 37         parent,
 38         identifier,
 39         level,
 40         attributes
 41         ) in ts.select(
 42            table=TABLE_INSTANCES,
 43            sid=sid,
 44            sensing_start=sensing_start,
 45            sensing_stop=sensing_stop,
 46            fields=('SENSING_TIME',
 47                    'END_TIME',
 48                    'DOMAIN',
 49                    'INSTANCE',
 50                    'PROCEDURE',
 51                    'SOURCE',
 52                    'PARENT',
 53                    'IDENTIFIER',
 54                    'LEVEL',
 55                    'ATTRIBUTES'
 56                    )):
 57        eventraiser.add(Event(event_classname=INSTANCES_EVENT_CLASS,
 58                              sid=sid,
 59                              start_time=sensing_time,
 60                              stop_time=stop_time,
 61                              instance_properties={
 62                                  'Domain': domain,
 63                                  'Instance': instance,
 64                                  'Procedure': procedure,
 65                                  'Source': source,
 66                                  'Parent': parent,
 67                                  'Identifier': identifier,
 68                                  'Level': level,
 69                                  'Attributes': attributes
 70                              }))
 71
 72
 73def log_events(sid, sensing_start, sensing_stop, eventraiser):
 74    """Raise APEX_INSTANCES events."""
 75    logger.info('Scanning for APEX Log entries for {sid} from {strt} to {stop}'.format(
 76            sid=sid, strt=sensing_start, stop=sensing_stop))
 77
 78    for (sensing_time,
 79         domain,
 80         instance,
 81         identifier,
 82         procedure,
 83         source,
 84         parent,
 85         level,
 86         attribute,
 87         value,
 88         counter
 89         ) in ts.select(
 90            table=TABLE_LOG,
 91            sid=sid,
 92            sensing_start=sensing_start,
 93            sensing_stop=sensing_stop,
 94            fields=('SENSING_TIME',
 95                    'DOMAIN',
 96                    'INSTANCE',
 97                    'IDENTIFIER',
 98                    'PROCEDURE',
 99                    'SOURCE',
100                    'PARENT',
101                    'LEVEL',
102                    'ATTRIBUTE',
103                    'VALUE',
104                    'COUNTER'
105                    )):
106        eventraiser.add(Event(event_classname=LOG_EVENT_CLASS,
107                              sid=sid,
108                              start_time=sensing_time,
109                              instance_properties={
110                                  'Domain': domain,
111                                  'Instance': instance,
112                                  'Identifier': identifier,
113                                  'Procedure': procedure,
114                                  'Source': source,
115                                  'Parent': parent,
116                                  'Level': level,
117                                  'Attribute': attribute,
118                                  'Value': value,
119                                  'Counter': counter,
120                              }))
121
122
123def dispatch(wo, resultfile, eventraiser):
124    """We are being run by the dispatcher tool."""
125    logging.info('Starting {ec} Events'.format(ec=EVENT_CLASS))
126    for job in wo.read_jobs():
127        logging.info('Begin event from {start} to {stop}'.
128                     format(start=job['sensing_start'],
129                            stop=job['sensing_stop']))
130
131        instances_events(job.sid,
132                         job.sensing_start,
133                         job.sensing_stop,
134                         eventraiser)
135
136        log_events(job.sid,
137                   job.sensing_start,
138                   job.sensing_stop,
139                   eventraiser)
140
141        resultfile.add_job(job, JobStatus.COMPLETED)
142
143    eventraiser.log_report()
144    logging.info('All done')
145
146
147def main():
148    """Command line entry point.\
149    This program can either be called via `dispatcher` or standalone."""
150    dispatch(*init_algorithm())
151
152
153if __name__ == '__main__':
154    main()