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, include_event_check
  9from chart.products.events.multitable_utils import process_ordering_sensing_time
 10from chart.products.pus.packetdef import PacketDef
 11from chart.db.model.table import TableInfo
 12from chart.common.traits import is_listlike
 13from chart.web.user import User
 14
 15DECODER_ID = 'GEO-EVENTS'
 16TABLE = 'GEO_EVENTS'
 17EVENT_CLASSNAME = 'GEO-EVENTS'
 18
 19logger = logging.getLogger()
 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)
 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', 'NAME',
 61              'DESCRIPTION',  'ABSOLUTE_ORBIT_NUMBER', 'TIME_FROM_ANX',
 62              'DEG_FROM_ANX', 'PARAMETERS')
 63
 64    # get each row using ts select
 65    for row in ts.select(sid=sid,
 66                         sensing_start=start_time,
 67                         sensing_stop=stop_time,
 68                         sensing_time=start_time_eq,
 69                         table=table_info.storage_table,
 70                         fields=fields,
 71                         ordering=order_clauses,
 72                         limit=limit):
 73
 74        sensing_time, product, name, description, absolute_orbit_number, time_from_anx, deg_from_anx, parameters = row
 75        # print('ROW: ', row)
 76
 77        inst_properties = {
 78                'SID': sid.sid_num,
 79                'PRODUCT': product,
 80                'Name': name,
 81                'Description': description,
 82                'Absolute_Orbit': absolute_orbit_number,
 83                'Time_From_ANX': time_from_anx,
 84                'Deg_From_ANX': deg_from_anx,
 85                'Parameters': parameters,
 86                }
 87                # 'EV_PARAMETER_NAME': parameters[0]["EV_Parameter_Name"] if parameters else '-',
 88                # 'EV_PARAMETER_TYPE': parameters[0]["EV_Parameter_Type"] if parameters else '-',
 89                # 'EV_PARAMETER_UNIT': parameters[0]["EV_Parameter_Unit"] if parameters else '-',
 90                # 'EV_PARAMETER_VALUE': parameters[0]["EV_Parameter_Value"] if parameters else '-',
 91                # 'EV_PARAMETER_DESCRIPTION': parameters[0]["EV_Parameter_Description"] if parameters else '-',
 92                # }
 93
 94        if not include_event_check(properties, inst_properties):
 95            continue
 96
 97        event_count += 1
 98
 99        if not count and (from_to is None or (page_start <= event_count and event_count <= page_stop)):
100            events.append(Event(sid=sid, 
101                                event_classname=EVENT_CLASSNAME,
102                                start_time=sensing_time, 
103                                instance_properties=inst_properties))
104
105        if single:
106            break
107
108    return event_count, events