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