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