1#!/usr/bin/env python3
  2
  3# SK 28/06/2023: Created
  4
  5import logging
  6import json
  7from chart.db import ts
  8from chart.events.event import Event
  9from chart.events.event import include_event_check
 10from chart.products.events.multitable_utils import process_ordering_sensing_time
 11from chart.products.pus.packetdef import PacketDef
 12from chart.db.model.table import TableInfo
 13from chart.common.traits import is_listlike
 14from chart.web.user import User
 15
 16DECODER_ID = 'MOC-ARCHIVE-EVENTS'
 17TABLE = 'MOC_ARCHIVE_EVENTS'
 18EVENT_CLASSNAME = 'MOC-ARCHIVE-EVENTS'
 19
 20logger = logging.getLogger()
 21
 22def find_events(sid,
 23                start_time,
 24                start_time_eq,
 25                stop_time,
 26                event_classes,
 27                ordering,
 28                from_to,
 29                properties,
 30                count,
 31                single,
 32                filtering,
 33                multitable_extra_params,
 34                user: User = None):
 35
 36    if from_to is not None:
 37        page_start, page_stop = from_to
 38
 39    limit = 1 if single and start_time_eq is None else None
 40
 41    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(TABLE, sid=sid)
 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', 'MSG_ID',
 62                         'CATEGORY',
 63                         'DOMAIN',
 64                         'EVENT_ID',
 65                         'EVENT_TYPE',
 66                         'GEN_TIME',
 67                         'SOURCE',
 68                         'APPLICATION',
 69                         'SEVERITY',
 70                         'MSG_TYPE',
 71                         'WORKSTATION',
 72                         'MESSAGE') 
 73
 74    # get each row using ts select
 75    for row in ts.select(sid=sid,
 76                         sensing_start=start_time,
 77                         sensing_stop=stop_time,
 78                         sensing_time=start_time_eq,
 79                         table=table_info.storage_table,
 80                         fields=fields,
 81                         ordering=order_clauses,
 82                         limit=limit):
 83
 84        sensing_time, msg_id, category, domain, event_id, event_type, \
 85             gen_time, source, application, severity, msg_type, \
 86             workstation, message = row
 87
 88        inst_properties={
 89                            'MSG_ID':   msg_id,
 90                            'CATEGORY':   category,
 91                            'DOMAIN':   domain,
 92                            'EVENT_ID':   event_id,
 93                            'EVENT_TYPE':  event_type,
 94                            'GEN_TIME':   gen_time,
 95                            'SOURCE':   source,
 96                            'APPLICATION':   application,
 97                            'SEVERITY':   severity,
 98                            'MSG_TYPE':   msg_type,
 99                            'WORKSTATION':   workstation,
100                            'MESSAGE':   message,
101                    }
102
103        if not include_event_check(properties, inst_properties):
104            continue
105
106        event_count += 1
107
108        if not count and (from_to is None or (page_start <= event_count and event_count <= page_stop)):
109            events.append(Event(sid=sid, 
110                                event_classname=EVENT_CLASSNAME,
111                                start_time=sensing_time, 
112                                instance_properties=inst_properties))
113
114        if single:
115            break
116
117    return event_count, events