1#!/usr/bin/env python3
  2
  3# SK 22/06/2023: Created
  4
  5import logging
  6from chart.db import ts
  7from chart.events.event import Event, include_event_check
  8from chart.products.events.multitable_utils import process_ordering_sensing_time
  9from chart.products.pus.packetdef import PacketDef
 10from chart.db.model.table import TableInfo
 11from chart.common.traits import is_listlike
 12from chart.web.user import User
 13
 14DECODER_ID = 'ALL-TM-EVENTS'
 15TM_TABLE = 'TM'
 16EVENT_CLASSNAME = 'ALL-TM-EVENTS'
 17logger = logging.getLogger()
 18
 19def find_events(sid,
 20                start_time,
 21                start_time_eq,
 22                stop_time,
 23                event_classes,
 24                ordering,
 25                from_to,
 26                properties,
 27                count,
 28                single,
 29                filtering,
 30                multitable_extra_params,
 31                user: User = None):
 32
 33    page_start, page_stop = from_to if from_to else (None, None)
 34
 35    limit = 1 if single and not start_time_eq else None
 36
 37    events = []
 38    event_count = 0
 39
 40    # Reset ordering for sensing_time
 41    order_clauses = process_ordering_sensing_time(ordering)
 42
 43    table_info = TableInfo(TM_TABLE, sid=sid, fast_load=True)
 44
 45    if not single and count:
 46        # just return count of events
 47        event_count = ts.count(
 48            table=table_info.storage_table,
 49            field='SENSING_TIME',
 50            method='ap',
 51            sid=sid,
 52            sensing_start=start_time,
 53            sensing_stop=stop_time)
 54
 55        return event_count, events
 56
 57    fields = ('SENSING_TIME', 'SPID', 'SEQUENCE_COUNT', 'PAYLOAD', 'HEX')
 58
 59    for sensing_time, spid, sequence_count, payload, hexdata in ts.select(sid=sid, 
 60                                                                          sensing_start=start_time,
 61                                                                          sensing_stop=stop_time,
 62                                                                          sensing_time=start_time_eq,
 63                                                                          table=table_info.storage_table,
 64                                                                          fields=fields,
 65                                                                          ordering=order_clauses,
 66                                                                          limit=limit):
 67
 68        packet_reader = PacketDef.find_one(spid=spid, sid=sid)
 69
 70        if packet_reader:
 71            if packet_reader.service == 5 and packet_reader.subservice in [1, 2, 3, 4]:
 72                severity = packet_reader.subservice
 73            elif packet_reader.service == 1 and packet_reader.subservice in [1, 2, 7, 8]:
 74                severity = 4 + packet_reader.subservice
 75            else:
 76                severity = 0
 77        else:
 78            severity = 0
 79
 80        inst_properties = {'Name': packet_reader.name,
 81                           'Severity': severity,
 82                           'Description': packet_reader.description,
 83                           'SPID': packet_reader.spid,
 84                           'Type': packet_reader.service,
 85                           'SType': packet_reader.subservice,
 86                           'APID': packet_reader.apid,
 87                           'SSC': sequence_count,
 88                           'Param1': packet_reader.param1,
 89                           'Param2': packet_reader.param2,
 90                           'Param_Data': payload,
 91                           'Hex_Data': hexdata}
 92
 93        if not include_event_check(properties, inst_properties): 
 94            continue
 95
 96        event_count += 1
 97
 98        if not count and (not from_to or page_start <= event_count <= page_stop):
 99            events.append(Event(sid=sid,
100                                event_classname=EVENT_CLASSNAME,
101                                start_time=sensing_time,
102                                instance_properties=inst_properties))
103
104            if single: 
105                break
106
107    return event_count, events