Database context

class luna_events.db.db_context.DBContext(logger, adaptor, streams, storageTime)

Class to work with database.

logger

the current request logger

streams

streams with data

adaptor

A DBAdaptor instance

storageTime

A str, storage time setting

async checkEvent(eventId, accountId=None)

Check event existence :type eventId: str :param eventId: event id :type accountId: Optional[str, None] :param accountId: account id

Return type:

bool

Returns:

True if event exists otherwise False

async collectBodyDetectResult(conn, numericIds, targets)

Prepare map: event numeric id vs body detection result

Parameters:
  • conn (AbstractDBConnection) – connection

  • numericIds (list[int]) – event numeric ids to collect data for

  • targets (list[Literal[‘body_samples’, ‘image_origins’]]) – targets to collect

Returns:

event numeric id vs body samples & image origins

Return type:

map

async collectFaceDetectResult(conn, numericIds, targets)

Prepare map: event numeric id vs face detection result

Parameters:
  • conn (AbstractDBConnection) – connection

  • numericIds (list[int]) – event numeric ids to collect data for

  • targets (list[Literal[‘face_samples’, ‘image_origins’]]) – targets to collect

Returns:

event numeric id vs face samples & image origins

Return type:

map

property dbType: str

Returns a string, database type (‘postgres’ only).

Return type:

str

async deleteDescriptors(filters)

Delete events descriptors

Parameters:

filters (DescriptorsFilters) – DescriptorsFilters instance

Return type:

list[str]

Returns:

event ids which descriptors have been deleted

async deleteEvents(targets, createTimeGte=None, createTimeLt=None, insertTimeGte=None, insertTimeLt=None, accountId=None, handlerId=None)

Delete events from database :type targets: list[str] :param targets: deletion info targets :type createTimeGte: Optional[datetime, None] :param createTimeGte: lower create time boundary :type createTimeLt: Optional[datetime, None] :param createTimeLt: upper create time boundary :type insertTimeGte: Optional[datetime, None] :param insertTimeGte: lower insert time boundary :type insertTimeLt: Optional[datetime, None] :param insertTimeLt: upper insert time boundary :type accountId: Optional[str, None] :param accountId: account id :type handlerId: Optional[str, None] :param handlerId: handler id

Returns:

event_id with deletion info attributes

Return type:

list with dicts for each deleted event

async getDescriptorById(eventId, accountId=None, descriptorVersion=None, descriptorModel=<class 'db.events_db_tools.models.events_models.Descriptor'>)

Search event descriptor by event id.

Parameters:
  • eventId (str) – event id

  • accountId (Optional[str, None]) – account id

  • descriptorVersion (Optional[int, None]) – descriptor version

  • descriptorModel (UnionType[Descriptor, BodyDescriptor]) – descriptor model. Either Descriptor or BodyDescriptor

Raises:
  • VLException(Error.EventNotFound) if no event found

  • VLException(Error.DescriptorNotFound) if no descriptor of specified version found

Return type:

bytes

Returns:

descriptor as bytes

async getSequenceValues(eventCount)

Get several sequence.next_val values from db for new events :type eventCount: int :param eventCount: count of incoming event

Return type:

list[int]

Returns:

list of ids for incoming events

getdictsFromResult(result, fields)

Prepare list of dicts from query result :type result: list :param result: query result records :type fields: list[str] :param fields: query column names

Return type:

list[dict]

Returns:

query result as list of dicts

makeResultMap(rows, targets)

Prepare db reply as a map. First item from each row is key (e.g. numeric id), other items correspond to the collection targets :type rows: list[RowProxy] :param rows: raw db reply :type targets: list[str] :param targets: detection targets

Return type:

dict[int, dict[str, set[str]]]

Returns:

db result map

static prepareSearchQueryFilters(filters, meta, descriptor_version=None, descriptorModel=<class 'db.events_db_tools.models.events_models.Descriptor'>)

Prepare search query filters on “Event” and “Location” models.

Parameters:
  • filters (SearchEventsFilters) – query filters

  • meta (list[UDFilter]) – user-defined event meta’s filters

  • descriptor_version (Optional[int, None]) – descriptor version

Return type:

tuple[BooleanClauseList, list[FromClause]]

Returns:

filters for Event select query

static prepareTarget(target)

If target is not empty, add Event.id column to it. !!!WARNING!!! function changes input target

Parameters:

target (list[str]) – target to change

Return type:

UnionType[list[str], None]

Returns:

if target exists - changed target if target does not exists - None

async searchEvents(filters, *, meta=None, descriptorVersion, target=None, page=1, page_size=100, order='desc')

Search events by given filters.

Parameters:
  • target (Optional[list[str], None]) – target Event columns’ names to get info on

  • filters (SearchEventsFilters) – raw search request filters

  • meta (Optional[list[UDFilter], None]) – user-defined event meta’s filters

  • descriptor_version – descriptor version

  • page (UnionType[int, None]) – number of result page

  • page_size (UnionType[int, None]) – count of results in page

  • order (UnionType[str, None]) – result sort order (ask or desc)

Return type:

list[dict[str, object]]

Returns:

events

async searchEventsMissing(filters, page=1, pageSize=100)

Get events with samples without specified attributes (filters.kind).

Parameters:
  • filters (SearchEventsMissingFilters) – A SearchEventsMissingFilters instance

  • page (UnionType[int, None]) – number of result page

  • pageSize (UnionType[int, None]) – count of results in page

Return type:

list[dict[str, object]]

Returns:

List of events

async searchEventsMissingCount(filters)

Get number of events with samples without specified attributes (filters.kind).

Parameters:

filters (SearchEventsMissingFilters) – A SearchEventsMissingFilters instance

Return type:

int

Returns:

An integer, count of events.

async updateEvent(eventId, accountId, eventForUpdate)

Update event.

Parameters:
  • eventId (str) – event id

  • accountId (UnionType[str, None]) – account id

  • eventForUpdate (dict[str, object]) – event containing data to update

Raises:

VLException(Error.EventNotFound) if no event found

Return type:

None

class luna_events.db.db_context.ExternalDemands(target)

Class to store flags of need to use data from ‘external’ tables (all tables, except for ‘Event’ table)

isAnyDemand()

Check that data from any of tables is required.

Returns: True if data from one or more ‘external’ tables is need

Return type:

bool

isTableNeed(tableName)

Check that data from one of tables is required :type tableName: str :param tableName: name of table, similar as in db

Return type:

bool

Returns:

True if data is required, else False