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