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