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] :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] :param createTimeGte: lower create time boundary :type createTimeLt: Optional[datetime] :param createTimeLt: upper create time boundary :type insertTimeGte: Optional[datetime] :param insertTimeGte: lower insert time boundary :type insertTimeLt: Optional[datetime] :param insertTimeLt: upper insert time boundary :type accountId: Optional[str] :param accountId: account id :type handlerId: Optional[str] :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]) – account id

  • descriptorVersion (Optional[int]) – descriptor version

  • descriptorModel (Union[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

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

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

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

async matchEvents(descriptor, descriptor_version, filters, target=None, similarityOrder='asc', limit=5, threshold=0.0, isNeededToReturnSimilarity=True, descriptorModel=<class 'db.events_db_tools.models.events_models.Descriptor'>)

Search events by given filters, then match them with a provided descriptor.

Parameters:
  • descriptor (bytes) – a raw descriptor bytes to match with

  • descriptor_version (int) – descriptor version

  • target (Optional[List[str]]) – target Event columns’ names to get info on

  • filters (SearchEventsFilters) – raw request filters

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

  • similarityOrder (Optional[str]) – Result sort order by similarity (“asc” for ascending or “desc” descending)

  • limit (int) – maximum count of events in result

  • threshold (Optional[float]) – Similarity threshold. Only events with higher similarity will be returned.

  • isNeededToReturnSimilarity (bool) – whether similarity is expected in result

Return type:

List[Dict]

Returns:

events, ordered by similarity

static prepareSearchQueryFilters(filters, 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

  • descriptor_version (Optional[int]) – descriptor version

Return type:

BooleanClauseList

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 (Optional[List[str]]) – target to change

Return type:

Optional[List[str]]

Returns:

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

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

Search events by given filters.

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

  • filters (SearchEventsFilters) – raw search request filters

  • descriptor_version (int) – descriptor version

  • page (Optional[int]) – number of result page

  • page_size (Optional[int]) – count of results in page

  • order (Optional[str]) – result sort order (ask or desc)

Return type:

List[Dict[str, Any]]

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 (Optional[int]) – number of result page

  • pageSize (Optional[int]) – count of results in page

Return type:

List[Dict[str, Any]]

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 (Optional[str]) – account id

  • eventForUpdate (Dict[str, Any]) – 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