1#!/usr/bin/env python3
 2
 3# SK 23/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 = 'MANOEUVRE-PREDICTION-EVENTS'
15TABLE = 'MANOEUVRE_PREDICTION'
16EVENT_CLASSNAME = 'MANO-PRED-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    event_count = 0
42
43    # Reset ordering for sensing_time
44    order_clauses = process_ordering_sensing_time(ordering)
45
46    table_info = TableInfo(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', 'PRODUCT', 'FILE_GEN_TIME', 'OPTIM_TIME', 'MODE', 'SIZE', 'PSO')
61
62    # get each row using ts select
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,
68                         fields=fields,
69                         ordering=order_clauses,
70                         limit=limit):
71
72        sensing_time, product, file_gen_time, optim_time, mode, size, pso = row
73        inst_properties = {
74            'SID_NUM': sid.sid_num,
75            'PRODUCT': product,
76            'File_Gen_Time': file_gen_time,
77            'Optim_Time': optim_time,
78            'Mode': mode,
79            'Size': size,
80            'PSO': pso,
81        }
82
83        if not include_event_check(properties, inst_properties):
84            continue
85
86        event_count += 1
87
88        if not count and (from_to is None or (page_start <= event_count and event_count <= page_stop)):
89            events.append(Event(sid=sid, 
90                                event_classname=EVENT_CLASSNAME,
91                                start_time=sensing_time, 
92                                instance_properties=inst_properties))
93        if single:
94            break
95
96    return event_count, events