dp3.database.database ¶
EntityDatabase ¶
EntityDatabase(config: HierarchicalDict, model_spec: ModelSpec, num_processes: int, process_index: int = 0, elog: Optional[EventGroupType] = None)
MongoDB database wrapper responsible for whole communication with database server. Initializes database schema based on database configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
HierarchicalDict
|
configuration of database connection (content of database.yml) |
required |
model_spec
|
ModelSpec
|
ModelSpec object, configuration of data model (entities and attributes) |
required |
process_index
|
int
|
index of worker process - used for sharding metadata |
0
|
num_processes
|
int
|
number of worker processes |
required |
Source code in dp3/database/database.py
start ¶
stop ¶
register_on_entity_delete ¶
register_on_entity_delete(f_one: Callable[[str, AnyEidT], None], f_many: Callable[[str, list[AnyEidT]], None])
Registers function to be called when entity is forcibly deleted.
Source code in dp3/database/database.py
update_schema ¶
Checks whether schema saved in database is up-to-date and updates it if necessary.
Will NOT perform any changes in master collections on conflicting model changes. Any such changes must be performed via the CLI.
As this method modifies the schema collection, it should be called only by the main worker.
Source code in dp3/database/database.py
await_updated_schema ¶
Checks whether schema saved in database is up-to-date and awaits its update by the main worker on mismatch.
insert_datapoints ¶
Inserts datapoint to raw data collection and updates master record.
Raises DatabaseError when insert or update fails.
Source code in dp3/database/database.py
update_master_records ¶
Replace master records of etype
:eid
with the provided records
.
Raises DatabaseError when update fails.
Source code in dp3/database/database.py
extend_ttl ¶
Extends TTL of given etype
:eid
by ttl_tokens
.
Source code in dp3/database/database.py
remove_expired_ttls ¶
Removes expired TTL of given etype
:eid
.
Source code in dp3/database/database.py
delete_eids ¶
Delete master record and all snapshots of etype
:eids
.
Source code in dp3/database/database.py
delete_eid ¶
Delete master record and all snapshots of etype
:eid
.
Source code in dp3/database/database.py
mark_all_entity_dps_t2 ¶
Updates the min_t2s
of the master records of etype
for all records.
Periodically called for all etype
s from HistoryManager.
Source code in dp3/database/database.py
delete_old_dps ¶
Delete old datapoints from master collection.
Periodically called for all etype
s from HistoryManager.
Source code in dp3/database/database.py
delete_link_dps ¶
Delete link datapoints from master collection.
Called from LinkManager for deleted entities.
Source code in dp3/database/database.py
delete_many_link_dps ¶
delete_many_link_dps(etypes: list[str], affected_eids: list[list[AnyEidT]], attr_names: list[str], eids_to: list[list[AnyEidT]]) -> None
Delete link datapoints from master collection.
Called from LinkManager for deleted entities, when deleting multiple entities.
Source code in dp3/database/database.py
get_master_record ¶
Get current master record for etype/eid.
If doesn't exist, returns {}.
Source code in dp3/database/database.py
ekey_exists ¶
Checks whether master record for etype/eid exists
Source code in dp3/database/database.py
get_master_records ¶
Get cursor to current master records of etype.
get_worker_master_records ¶
get_worker_master_records(worker_index: int, worker_cnt: int, etype: str, query_filter: dict = None, **kwargs) -> Cursor
Get cursor to current master records of etype.
Source code in dp3/database/database.py
get_value_or_history ¶
get_value_or_history(etype: str, attr_name: str, eid: AnyEidT, t1: Optional[datetime] = None, t2: Optional[datetime] = None) -> dict
Gets current value and/or history of attribute for given eid
.
Depends on attribute type: - plain: just (current) value - observations: (current) value and history stored in master record (optionally filtered) - timeseries: just history stored in master record (optionally filtered)
Returns dict with two keys: current_value
and history
(list of values).
Source code in dp3/database/database.py
estimate_count_eids ¶
Estimates count of eid
s in given etype
save_metadata ¶
Saves metadata dict under the caller module and passed timestamp.
Source code in dp3/database/database.py
update_metadata ¶
update_metadata(time: datetime, metadata: dict, increase: dict = None, worker_id: Optional[int] = None)
Updates existing metadata of caller module and passed timestamp.
Source code in dp3/database/database.py
get_observation_history ¶
get_observation_history(etype: str, attr_name: str, eid: AnyEidT, t1: datetime = None, t2: datetime = None, sort: int = None) -> list[dict]
Get full (or filtered) history of observation attribute.
This method is useful for displaying eid
's history on web.
Also used to feed data into get_timeseries_history()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
etype
|
str
|
entity type |
required |
attr_name
|
str
|
name of attribute |
required |
eid
|
AnyEidT
|
id of entity, to which data-points correspond |
required |
t1
|
datetime
|
left value of time interval (inclusive) |
None
|
t2
|
datetime
|
right value of time interval (inclusive) |
None
|
sort
|
int
|
sort by timestamps - 0: ascending order by t1, 1: descending order by t2, None: don't sort |
None
|
Returns: list of dicts (reduced datapoints)
Source code in dp3/database/database.py
get_timeseries_history ¶
get_timeseries_history(etype: str, attr_name: str, eid: AnyEidT, t1: datetime = None, t2: datetime = None, sort: int = None) -> list[dict]
Get full (or filtered) history of timeseries attribute. Outputs them in format:
This method is useful for displayingeid
's history on web.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
etype
|
str
|
entity type |
required |
attr_name
|
str
|
name of attribute |
required |
eid
|
AnyEidT
|
id of entity, to which data-points correspond |
required |
t1
|
datetime
|
left value of time interval (inclusive) |
None
|
t2
|
datetime
|
right value of time interval (inclusive) |
None
|
sort
|
int
|
sort by timestamps - |
None
|
Returns: list of dicts (reduced datapoints) - each represents just one point at time
Source code in dp3/database/database.py
move_raw_to_archive ¶
Rename the current raw collection to archive collection.
Multiple archive collections can exist for one entity type, though they are exported and dropped over time.
Source code in dp3/database/database.py
get_archive ¶
Get archived raw datapoints where t1
is in <after
, before
).
All plain datapoints will be returned (default).
Source code in dp3/database/database.py
delete_old_archived_dps ¶
Delete archived raw datapoints older than before
.
Deletes all plain datapoints if plain
is True
(default).
Source code in dp3/database/database.py
drop_empty_archives ¶
Drop empty archive collections.
Source code in dp3/database/database.py
get_module_cache ¶
Return a persistent cache collection for given module name.
Module name is determined automatically, but you can override it.
Source code in dp3/database/database.py
get_estimated_entity_count ¶
get_caller_id ¶
Returns the name of the caller method's class, or function name if caller is not a method.