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 = 'BAD-FRAME-EVENTS'
 15TM_TABLE = 'TM'
 16EVENT_CLASSNAME = 'BAD-FRAME-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
 58    fields = ('SENSING_TIME', 'SPID', 'SEQUENCE_COUNT', 'PAYLOAD', 'HEX')
 59
 60    for row in ts.select(sid=sid,
 61                         sensing_start=start_time,
 62                         sensing_stop=stop_time,
 63                         sensing_time=start_time_eq,
 64                         table=table_info.storage_table,
 65                         fields=fields,
 66                         ordering=order_clauses,
 67                         limit=limit):
 68
 69        sensing_time, spid, sequence_count, payload, hexdata = row
 70        packet_reader = PacketDef.find_one(spid=spid, sid=sid)
 71        severity = 0
 72
 73        if packet_reader is not None and (packet_reader.spid == 20):
 74            inst_properties = {'Name': packet_reader.name,
 75                               'Severity': severity,
 76                               'Description': packet_reader.description,
 77                               'SPID': packet_reader.spid,
 78                               'Type': packet_reader.service,
 79                               'SType': packet_reader.subservice,
 80                               'APID': packet_reader.apid,
 81                               'SSC': sequence_count,
 82                               'Param1': packet_reader.param1,
 83                               'Param2': packet_reader.param2,
 84                               'Param_Data': payload,
 85                               'Hex_Data': hexdata}
 86
 87            if not include_event_check(properties, inst_properties):
 88                continue
 89
 90            event_count += 1
 91
 92            if not count and (not from_to or page_start <= event_count <= page_stop):
 93                events.append(Event(sid=sid,
 94                              event_classname=EVENT_CLASSNAME,
 95                              start_time=sensing_time,
 96                              instance_properties=inst_properties))
 97
 98                if single: 
 99                    break
100
101    return event_count, events