Event Instances¶
EventInstances(perfdb)
¶
Class used for handling event instances. Can be accessed via perfdb.events.instances.
Parameters:
Source code in echo_postgres/event_instances.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
"""Class used for handling object instances. Can be accessed via `perfdb.objects.instances`.
Parameters
----------
perfdb : PerfDB
Top level object carrying all functionality and the connection handler.
"""
super().__init__(perfdb)
from .event_instance_comments import EventInstanceComments
from .event_instance_labels import EventInstanceLabels
# * subclasses
self.comments = EventInstanceComments(perfdb)
self.labels = EventInstanceLabels(perfdb)
delete(event_ids)
¶
Deletes event instances
Parameters:
-
(event_ids¶list[int]) –IDs of the events to delete
Source code in echo_postgres/event_instances.py
@validate_call
def delete(
self,
event_ids: list[int],
) -> None:
"""Deletes event instances
Parameters
----------
event_ids : list[int]
IDs of the events to delete
"""
# creating query (event_ids as an array)
query = sql.SQL("SELECT performance.fn_delete_events(ARRAY[{event_ids}])").format(
event_ids=sql.SQL(", ").join(sql.Literal(event_id) for event_id in event_ids),
)
# executing query
with self._perfdb.conn.reconnect() as conn:
# deleting
result = conn.execute(query, skip_retry=True)
logger.debug(f"Deleted {result.rowcount} rows from events table")
edit(event_id, event_name=None, company_involved=None, company_responsible=None, start_date=None, end_date=None, event_description=None, component_location=None, event_detection_type=None, event_confirmation_type=None, under_warranty=None, expected_cost=None, real_cost=None, lost_energy=None, crane_used=None, severity_level=None, new_component_model=None, new_component_serial_number=None, new_component_batch=None, labels=None, root_cause=None)
¶
Edits an event instance in the database.
Parameters:
-
(event_id¶int) –The ID of the event to edit.
-
(event_name¶str | None, default:None) –The name of the event, if applicable.
-
(company_involved¶str | None, default:None) –The name of the company involved in the event, if applicable.
-
(company_responsible¶str | None, default:None) –The name of the company responsible for the event, if applicable.
-
(start_date¶datetime | None, default:None) –The start date of the event, if applicable.
-
(end_date¶datetime | None, default:None) –The end date of the event, if applicable.
-
(event_description¶str | None, default:None) –The description of the event, if available.
-
(component_location¶str | None, default:None) –The location of the component associated with the event, if applicable.
-
(event_detection_type¶str | None, default:None) –The type of event detection used for the event, if applicable.
-
(event_confirmation_type¶str | None, default:None) –The type of event confirmation used for the event, if applicable.
-
(under_warranty¶bool | None, default:None) –Indicates whether the component is under warranty, if applicable.
-
(expected_cost¶float | None, default:None) –The expected cost of the event, if applicable.
-
(real_cost¶float | None, default:None) –The real cost of the event, if applicable.
-
(lost_energy¶float | None, default:None) –The amount of lost energy due to the event, if applicable.
-
(crane_used¶bool | None, default:None) –Indicates whether a crane was used for the event, if applicable.
-
(severity_level¶int | None, default:None) –The severity level of the event, if applicable.
-
(new_component_model¶str | None, default:None) –The model of the new component associated with the event, if applicable.
-
(new_component_serial_number¶str | None, default:None) –The serial number of the new component associated with the event, if applicable.
-
(new_component_batch¶str | None, default:None) –The batch number of the new component associated with the event, if applicable.
-
(labels¶list[str] | None, default:None) –The labels associated with the event, if applicable.
-
(root_cause¶str | None, default:None) –The root cause of the event, if known.
Returns:
-
None–
Source code in echo_postgres/event_instances.py
@validate_call
def edit(
self,
event_id: int,
event_name: str | None = None,
company_involved: str | None = None,
company_responsible: str | None = None,
start_date: datetime | None = None,
end_date: datetime | None = None,
event_description: str | None = None,
component_location: str | None = None,
event_detection_type: str | None = None,
event_confirmation_type: str | None = None,
under_warranty: bool | None = None,
expected_cost: float | None = None,
real_cost: float | None = None,
lost_energy: float | None = None,
crane_used: bool | None = None,
severity_level: int | None = None,
new_component_model: str | None = None,
new_component_serial_number: str | None = None,
new_component_batch: str | None = None,
labels: list[str] | None = None,
root_cause: str | None = None,
) -> None:
# Complete the docstring
"""
Edits an event instance in the database.
Parameters
----------
event_id : int
The ID of the event to edit.
event_name : str | None, optional
The name of the event, if applicable.
company_involved : str | None, optional
The name of the company involved in the event, if applicable.
company_responsible : str | None, optional
The name of the company responsible for the event, if applicable.
start_date : datetime | None, optional
The start date of the event, if applicable.
end_date : datetime | None, optional
The end date of the event, if applicable.
event_description : str | None, optional
The description of the event, if available.
component_location : str | None, optional
The location of the component associated with the event, if applicable.
event_detection_type : str | None, optional
The type of event detection used for the event, if applicable.
event_confirmation_type : str | None, optional
The type of event confirmation used for the event, if applicable.
under_warranty : bool | None, optional
Indicates whether the component is under warranty, if applicable.
expected_cost : float | None, optional
The expected cost of the event, if applicable.
real_cost : float | None, optional
The real cost of the event, if applicable.
lost_energy : float | None, optional
The amount of lost energy due to the event, if applicable.
crane_used : bool | None, optional
Indicates whether a crane was used for the event, if applicable.
severity_level : int | None, optional
The severity level of the event, if applicable.
new_component_model : str | None, optional
The model of the new component associated with the event, if applicable.
new_component_serial_number : str | None, optional
The serial number of the new component associated with the event, if applicable.
new_component_batch : str | None, optional
The batch number of the new component associated with the event, if applicable.
labels : list[str] | None, optional
The labels associated with the event, if applicable.
root_cause : str | None, optional
The root cause of the event, if known.
Returns
-------
None
"""
# Checking arguments
# arguments "name": (value, required, type)
args = {
"event_id": (event_id, True, int),
"event_name": (event_name, False, str),
"company_involved": (company_involved, False, str),
"company_responsible": (company_responsible, False, str),
"start_date": (start_date, False, datetime),
"end_date": (end_date, False, datetime),
"event_description": (event_description, False, str),
"component_location": (component_location, False, str),
"event_detection_type": (event_detection_type, False, str),
"event_confirmation_type": (event_confirmation_type, False, str),
"under_warranty": (under_warranty, False, bool),
"expected_cost": (expected_cost, False, float),
"real_cost": (real_cost, False, float),
"lost_energy": (lost_energy, False, float),
"crane_used": (crane_used, False, bool),
"severity_level": (severity_level, False, int),
"new_component_model": (new_component_model, False, str),
"new_component_serial_number": (new_component_serial_number, False, str),
"new_component_batch": (new_component_batch, False, str),
"labels": (labels, False, list),
"root_cause": (root_cause, False, str),
}
# creating query
query = sql.SQL(
"""
SELECT * FROM performance.fn_edit_event(
_event_id=>{event_id},
_event_name=>{event_name},
_company_involved_name=>{company_involved},
_company_responsible_name=>{company_responsible},
_start_date=>{start_date},
_end_date=>{end_date},
_description=>{event_description},
_location_name=>{component_location},
_event_detection_type_name=>{event_detection_type},
_event_confirmation_type_name=>{event_confirmation_type},
_under_warranty=>{under_warranty},
_expected_cost=>{expected_cost},
_real_cost=>{real_cost},
_lost_energy=>{lost_energy},
_crane_used=>{crane_used},
_severity_level=>{severity_level},
_new_component_model=>{new_component_model},
_new_component_serial_number=>{new_component_serial_number},
_new_component_batch=>{new_component_batch},
_labels=>{labels},
_root_cause_name=>{root_cause})
""",
).format(
event_id=sql.Literal(event_id),
event_name=sql.Literal(event_name),
company_involved=sql.Literal(company_involved),
company_responsible=sql.Literal(company_responsible),
start_date=sql.Literal(f"{start_date:%Y-%m-%d %H:%M:%S}") if start_date else sql.SQL("NULL"),
end_date=sql.Literal(f"{end_date:%Y-%m-%d %H:%M:%S}") if end_date else sql.SQL("NULL"),
event_description=sql.Literal(event_description),
component_location=sql.Literal(component_location),
event_detection_type=sql.Literal(event_detection_type),
event_confirmation_type=sql.Literal(event_confirmation_type),
under_warranty=sql.Literal(under_warranty),
expected_cost=sql.Literal(expected_cost),
real_cost=sql.Literal(real_cost),
lost_energy=sql.Literal(lost_energy),
crane_used=sql.Literal(crane_used),
severity_level=sql.Literal(severity_level),
new_component_model=sql.Literal(new_component_model),
new_component_serial_number=sql.Literal(new_component_serial_number),
new_component_batch=sql.Literal(new_component_batch),
labels=sql.Literal(labels),
root_cause=sql.Literal(root_cause),
)
# Quero que no log apareça os parâmetros que foram alterados, ou seja que não são nulos ao chamar a função
# executing query
# the return type is a boolean indicating if the event was edited
with self._perfdb.conn.reconnect() as conn:
conn.execute(query, skip_retry=True)
changed_params = {k: v for k, v in args.items() if v[0] is not None}
logger.debug(f"Edited event with id {event_id}. Changed parameters: {changed_params}")
get(event_ids=None, object_names=None, event_types=None, event_names=None, period=None, component_replaced=None, component_type_names=None, subcomponent_type_names=None, old_component_model_names=None, old_component_serial_numbers=None, new_component_model_names=None, new_component_serial_numbers=None, component_locations=None, subcomponents_replaced=None, involved_companies=None, responsible_companies=None, detection_types=None, confirmation_types=None, root_causes=None, under_warranty=None, crane_used=None, severity_levels=None, labels=None, filter_type='and', output_type='DataFrame')
¶
Gets events based on the given filters.
The most useful keys/columns returned are:
- object_id,
- object_name,
- event_type_id,
- event_type_name,
- event_name,
- description,
- start_date,
- end_date,
- duration,
- component_replaced,
- component_type_id,
- component_type_name,
- subcomponent_type_id,
- subcomponent_type_name,
- old_component_id,
- old_component_serial_number,
- old_component_batch,
- old_component_model_id,
- old_component_model_name,
- new_component_id,
- new_component_serial_number,
- new_component_batch,
- new_component_model_id,
- new_component_model_name,
- component_location_id,
- component_location_name,
- subcomponents_replaced,
- old_subcomponents,
- new_subcomponents,
- company_involved_id,
- company_involved_name,
- company_responsible_id,
- company_responsible_name,
- event_detection_type_id,
- event_detection_type_name,
- event_confirmation_type_id,
- event_confirmation_type_name,
- root_cause_id,
- root_cause_name,
- under_warranty,
- expected_cost,
- real_cost,
- lost_energy,
- crane_used,
- severity_level,
- label_ids,
- label_names,
- comments,
- modified_date
Parameters:
-
(event_ids¶list[int] | None, default:None) –Ids of the events. By default None.
-
(object_names¶list[str] | None, default:None) –Names of the objects. By default None.
-
(event_types¶list[str] | None, default:None) –Types of the events. By default None.
-
(event_names¶list[str] | None, default:None) –Names of the events. By default None.
-
(period¶DateTimeRange | None, default:None) –Period of the events. Will filter both start_date and end_date. By default None.
-
(component_replaced¶bool | None, default:None) –Whether the component was replaced. By default None.
-
(component_type_names¶list[str] | None, default:None) –Names of the component types. By default None.
-
(subcomponent_type_names¶list[str] | None, default:None) –Names of the subcomponent types. By default None.
-
(old_component_model_names¶list[str] | None, default:None) –Names of the old component models. By default None.
-
(old_component_serial_numbers¶list[str] | None, default:None) –Serial numbers of the old components. By default None.
-
(new_component_model_names¶list[str] | None, default:None) –Names of the new component models. By default None.
-
(new_component_serial_numbers¶list[str] | None, default:None) –Serial numbers of the new components. By default None.
-
(component_locations¶list[str] | None, default:None) –Location of the components. By default None.
-
(subcomponents_replaced¶bool | None, default:None) –Whether the subcomponents were replaced. By default None.
-
(involved_companies¶list[str] | None, default:None) –Names of the involved companies. By default None.
-
(responsible_companies¶list[str] | None, default:None) –Names of the responsible companies. By default None.
-
(detection_types¶list[str] | None, default:None) –Detection types of the events. By default None.
-
(confirmation_types¶list[str] | None, default:None) –Confirmation types of the events. By default None.
-
(root_causes¶list[str] | None, default:None) –Root causes of the events. By default None.
-
(under_warranty¶bool | None, default:None) –Whether the event is under warranty. By default None.
-
(crane_used¶bool | None, default:None) –Whether a crane was used. By default None.
-
(severity_levels¶list[int] | None, default:None) –Severity levels of the events. By default None.
-
(labels¶list[str] | None, default:None) –Labels of the events. By default None.
-
(filter_type¶Literal['and', 'or'], default:'and') –How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
-
(output_type¶Literal['dict', 'DataFrame'], default:'DataFrame') –Output type of the data. Can be one of ["dict", "DataFrame"] By default "dict"
Returns:
-
dict[int, dict[str, Any]]–In case output_value is "dict", returns a dictionary in the format {event_id: {attribute: value, ...
-
DataFrame–In case output_value is "DataFrame", returns a DataFrame with the following format: index = event_id, columns = [attribute, ...]
Source code in echo_postgres/event_instances.py
@validate_call
def get(
self,
event_ids: list[int] | None = None,
object_names: list[str] | None = None,
event_types: list[str] | None = None,
event_names: list[str] | None = None,
period: DateTimeRange | None = None,
component_replaced: bool | None = None,
component_type_names: list[str] | None = None,
subcomponent_type_names: list[str] | None = None,
old_component_model_names: list[str] | None = None,
old_component_serial_numbers: list[str] | None = None,
new_component_model_names: list[str] | None = None,
new_component_serial_numbers: list[str] | None = None,
component_locations: list[str] | None = None,
subcomponents_replaced: bool | None = None,
involved_companies: list[str] | None = None,
responsible_companies: list[str] | None = None,
detection_types: list[str] | None = None,
confirmation_types: list[str] | None = None,
root_causes: list[str] | None = None,
under_warranty: bool | None = None,
crane_used: bool | None = None,
severity_levels: list[int] | None = None,
labels: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
output_type: Literal["dict", "DataFrame"] = "DataFrame",
) -> dict[int, dict[str, Any]] | DataFrame:
"""Gets events based on the given filters.
The most useful keys/columns returned are:
- object_id,
- object_name,
- event_type_id,
- event_type_name,
- event_name,
- description,
- start_date,
- end_date,
- duration,
- component_replaced,
- component_type_id,
- component_type_name,
- subcomponent_type_id,
- subcomponent_type_name,
- old_component_id,
- old_component_serial_number,
- old_component_batch,
- old_component_model_id,
- old_component_model_name,
- new_component_id,
- new_component_serial_number,
- new_component_batch,
- new_component_model_id,
- new_component_model_name,
- component_location_id,
- component_location_name,
- subcomponents_replaced,
- old_subcomponents,
- new_subcomponents,
- company_involved_id,
- company_involved_name,
- company_responsible_id,
- company_responsible_name,
- event_detection_type_id,
- event_detection_type_name,
- event_confirmation_type_id,
- event_confirmation_type_name,
- root_cause_id,
- root_cause_name,
- under_warranty,
- expected_cost,
- real_cost,
- lost_energy,
- crane_used,
- severity_level,
- label_ids,
- label_names,
- comments,
- modified_date
Parameters
----------
event_ids : list[int] | None, optional
Ids of the events. By default None.
object_names : list[str] | None, optional
Names of the objects. By default None.
event_types : list[str] | None, optional
Types of the events. By default None.
event_names : list[str] | None, optional
Names of the events. By default None.
period : DateTimeRange | None, optional
Period of the events. Will filter both start_date and end_date. By default None.
component_replaced : bool | None, optional
Whether the component was replaced. By default None.
component_type_names : list[str] | None, optional
Names of the component types. By default None.
subcomponent_type_names : list[str] | None, optional
Names of the subcomponent types. By default None.
old_component_model_names : list[str] | None, optional
Names of the old component models. By default None.
old_component_serial_numbers : list[str] | None, optional
Serial numbers of the old components. By default None.
new_component_model_names : list[str] | None, optional
Names of the new component models. By default None.
new_component_serial_numbers : list[str] | None, optional
Serial numbers of the new components. By default None.
component_locations : list[str] | None, optional
Location of the components. By default None.
subcomponents_replaced : bool | None, optional
Whether the subcomponents were replaced. By default None.
involved_companies : list[str] | None, optional
Names of the involved companies. By default None.
responsible_companies : list[str] | None, optional
Names of the responsible companies. By default None.
detection_types : list[str] | None, optional
Detection types of the events. By default None.
confirmation_types : list[str] | None, optional
Confirmation types of the events. By default None.
root_causes : list[str] | None, optional
Root causes of the events. By default None.
under_warranty : bool | None, optional
Whether the event is under warranty. By default None.
crane_used : bool | None, optional
Whether a crane was used. By default None.
severity_levels : list[int] | None, optional
Severity levels of the events. By default None.
labels : list[str] | None, optional
Labels of the events. By default None.
filter_type : Literal["and", "or"], optional
How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
output_type : Literal["dict", "DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame"]
By default "dict"
Returns
-------
dict[int, dict[str, Any]]
In case output_value is "dict", returns a dictionary in the format {event_id: {attribute: value, ...
DataFrame
In case output_value is "DataFrame", returns a DataFrame with the following format: index = event_id, columns = [attribute, ...]
"""
# checks and where clause
where = self._check_get_args(
event_ids=event_ids,
object_names=object_names,
event_types=event_types,
event_names=event_names,
period=period,
component_replaced=component_replaced,
component_type_names=component_type_names,
subcomponent_type_names=subcomponent_type_names,
old_component_model_names=old_component_model_names,
old_component_serial_numbers=old_component_serial_numbers,
new_component_model_names=new_component_model_names,
new_component_serial_numbers=new_component_serial_numbers,
component_locations=component_locations,
subcomponents_replaced=subcomponents_replaced,
involved_companies=involved_companies,
responsible_companies=responsible_companies,
detection_types=detection_types,
confirmation_types=confirmation_types,
root_causes=root_causes,
under_warranty=under_warranty,
crane_used=crane_used,
severity_levels=severity_levels,
labels=labels,
filter_type=filter_type,
)
if output_type not in ["dict", "DataFrame"]:
raise ValueError(f"output_type must be one of ['dict', 'DataFrame'], not {output_type}")
query = [sql.SQL("SELECT * FROM performance.v_events"), where, sql.SQL(" ORDER BY object_name, start_date")]
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query, post_convert="pyarrow")
if output_type == "dict":
return df.set_index("event_id").to_dict(orient="index")
return df
get_ids(event_ids=None, object_names=None, event_types=None, event_names=None, period=None, component_replaced=None, component_type_names=None, subcomponent_type_names=None, old_component_model_names=None, old_component_serial_numbers=None, new_component_model_names=None, new_component_serial_numbers=None, component_locations=None, subcomponents_replaced=None, involved_companies=None, responsible_companies=None, detection_types=None, confirmation_types=None, root_causes=None, under_warranty=None, crane_used=None, severity_levels=None, labels=None, filter_type='and')
¶
Gets all events and their respective ids.
Parameters:
-
(event_ids¶list[int] | None, default:None) –Ids of the events. By default None.
-
(object_names¶list[str] | None, default:None) –Names of the objects. By default None.
-
(event_types¶list[str] | None, default:None) –Types of the events. By default None.
-
(event_names¶list[str] | None, default:None) –Names of the events. By default None.
-
(period¶DateTimeRange | None, default:None) –Period of the events. Will filter both start_date and end_date. By default None.
-
(component_replaced¶bool | None, default:None) –Whether the component was replaced. By default None.
-
(component_type_names¶list[str] | None, default:None) –Names of the component types. By default None.
-
(subcomponent_type_names¶list[str] | None, default:None) –Names of the subcomponent types. By default None.
-
(old_component_model_names¶list[str] | None, default:None) –Names of the old component models. By default None.
-
(old_component_serial_numbers¶list[str] | None, default:None) –Serial numbers of the old components. By default None.
-
(new_component_model_names¶list[str] | None, default:None) –Names of the new component models. By default None.
-
(new_component_serial_numbers¶list[str] | None, default:None) –Serial numbers of the new components. By default None.
-
(component_locations¶list[str] | None, default:None) –Location of the components. By default None.
-
(subcomponents_replaced¶bool | None, default:None) –Whether the subcomponents were replaced. By default None.
-
(involved_companies¶list[str] | None, default:None) –Names of the involved companies. By default None.
-
(responsible_companies¶list[str] | None, default:None) –Names of the responsible companies. By default None.
-
(detection_types¶list[str] | None, default:None) –Detection types of the events. By default None.
-
(confirmation_types¶list[str] | None, default:None) –Confirmation types of the events. By default None.
-
(root_causes¶list[str] | None, default:None) –Root causes of the events. By default None.
-
(under_warranty¶bool | None, default:None) –Whether the event is under warranty. By default None.
-
(crane_used¶bool | None, default:None) –Whether a crane was used. By default None.
-
(severity_levels¶list[int] | None, default:None) –Severity levels of the events. By default None.
-
(labels¶list[str] | None, default:None) –Labels of the events. By default None.
-
(filter_type¶Literal['and', 'or'], default:'and') –How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
Returns:
-
list[int]–List of event ids.
Source code in echo_postgres/event_instances.py
@validate_call
def get_ids(
self,
event_ids: list[int] | None = None,
object_names: list[str] | None = None,
event_types: list[str] | None = None,
event_names: list[str] | None = None,
period: DateTimeRange | None = None,
component_replaced: bool | None = None,
component_type_names: list[str] | None = None,
subcomponent_type_names: list[str] | None = None,
old_component_model_names: list[str] | None = None,
old_component_serial_numbers: list[str] | None = None,
new_component_model_names: list[str] | None = None,
new_component_serial_numbers: list[str] | None = None,
component_locations: list[str] | None = None,
subcomponents_replaced: bool | None = None,
involved_companies: list[str] | None = None,
responsible_companies: list[str] | None = None,
detection_types: list[str] | None = None,
confirmation_types: list[str] | None = None,
root_causes: list[str] | None = None,
under_warranty: bool | None = None,
crane_used: bool | None = None,
severity_levels: list[int] | None = None,
labels: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
) -> list[int]:
"""Gets all events and their respective ids.
Parameters
----------
event_ids : list[int] | None, optional
Ids of the events. By default None.
object_names : list[str] | None, optional
Names of the objects. By default None.
event_types : list[str] | None, optional
Types of the events. By default None.
event_names : list[str] | None, optional
Names of the events. By default None.
period : DateTimeRange | None, optional
Period of the events. Will filter both start_date and end_date. By default None.
component_replaced : bool | None, optional
Whether the component was replaced. By default None.
component_type_names : list[str] | None, optional
Names of the component types. By default None.
subcomponent_type_names : list[str] | None, optional
Names of the subcomponent types. By default None.
old_component_model_names : list[str] | None, optional
Names of the old component models. By default None.
old_component_serial_numbers : list[str] | None, optional
Serial numbers of the old components. By default None.
new_component_model_names : list[str] | None, optional
Names of the new component models. By default None.
new_component_serial_numbers : list[str] | None, optional
Serial numbers of the new components. By default None.
component_locations : list[str] | None, optional
Location of the components. By default None.
subcomponents_replaced : bool | None, optional
Whether the subcomponents were replaced. By default None.
involved_companies : list[str] | None, optional
Names of the involved companies. By default None.
responsible_companies : list[str] | None, optional
Names of the responsible companies. By default None.
detection_types : list[str] | None, optional
Detection types of the events. By default None.
confirmation_types : list[str] | None, optional
Confirmation types of the events. By default None.
root_causes : list[str] | None, optional
Root causes of the events. By default None.
under_warranty : bool | None, optional
Whether the event is under warranty. By default None.
crane_used : bool | None, optional
Whether a crane was used. By default None.
severity_levels : list[int] | None, optional
Severity levels of the events. By default None.
labels : list[str] | None, optional
Labels of the events. By default None.
filter_type : Literal["and", "or"], optional
How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
Returns
-------
list[int]
List of event ids.
"""
# checks and where clause
where = self._check_get_args(
event_ids=event_ids,
object_names=object_names,
event_types=event_types,
event_names=event_names,
period=period,
component_replaced=component_replaced,
component_type_names=component_type_names,
subcomponent_type_names=subcomponent_type_names,
old_component_model_names=old_component_model_names,
old_component_serial_numbers=old_component_serial_numbers,
new_component_model_names=new_component_model_names,
new_component_serial_numbers=new_component_serial_numbers,
component_locations=component_locations,
subcomponents_replaced=subcomponents_replaced,
involved_companies=involved_companies,
responsible_companies=responsible_companies,
detection_types=detection_types,
confirmation_types=confirmation_types,
root_causes=root_causes,
under_warranty=under_warranty,
crane_used=crane_used,
severity_levels=severity_levels,
labels=labels,
filter_type=filter_type,
)
query = [sql.SQL("SELECT event_id FROM performance.v_events"), where, sql.SQL(" ORDER BY event_name")]
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
return df["event_id"].tolist()
insert(object_name, event_type, event_name, company_involved, company_responsible, start_date, end_date=None, event_description=None, component_type=None, component_location=None, subcomponent_type=None, event_detection_type=None, event_confirmation_type=None, under_warranty=None, expected_cost=None, real_cost=None, lost_energy=None, crane_used=None, severity_level=None, new_component_model=None, new_component_serial_number=None, new_component_batch=None, new_subcomponent_model=None, new_subcomponent_serial_number=None, new_subcomponent_batch=None, labels=None, root_cause=None)
¶
Inserts a new event instance into the database.
Parameters:
-
(object_name¶str) –The name of the object associated with the event.
-
(event_type¶str) –The type of the event.
-
(event_name¶str) –The name of the event.
-
(company_involved¶str) –The name of the company involved in the event.
-
(company_responsible¶str) –The name of the company responsible for the event.
-
(start_date¶datetime) –The start date of the event.
-
(end_date¶datetime | None, default:None) –The end date of the event, if applicable.
-
(event_description¶str | None, default:None) –The description of the event, if available.
-
(component_type¶str | None, default:None) –The type of the component associated with the event, if applicable.
-
(component_location¶str | None, default:None) –The location of the component associated with the event, if applicable.
-
(subcomponent_type¶str | None, default:None) –The type of the subcomponent associated with the event, if applicable.
-
(event_detection_type¶str | None, default:None) –The type of event detection used for the event, if applicable.
-
(event_confirmation_type¶str | None, default:None) –The type of event confirmation used for the event, if applicable.
-
(under_warranty¶bool | None, default:None) –Indicates whether the event is under warranty, if applicable.
-
(expected_cost¶float | None, default:None) –The expected cost of the event, if applicable.
-
(real_cost¶float | None, default:None) –The real cost of the event, if applicable.
-
(lost_energy¶float | None, default:None) –The amount of lost energy due to the event, if applicable.
-
(crane_used¶bool | None, default:None) –Indicates whether a crane was used for the event, if applicable.
-
(severity_level¶int | None, default:None) –The severity level of the event, if applicable.
-
(new_component_model¶str | None, default:None) –The model of the new component associated with the event, if applicable.
-
(new_component_serial_number¶str | None, default:None) –The serial number of the new component associated with the event, if applicable.
-
(new_component_batch¶str | None, default:None) –The batch number of the new component associated with the event, if applicable.
-
(new_subcomponent_model¶str | None, default:None) –The model of the new subcomponent associated with the event, if applicable.
-
(new_subcomponent_serial_number¶str | None, default:None) –The serial number of the new subcomponent associated with the event, if applicable.
-
(new_subcomponent_batch¶str | None, default:None) –The batch number of the new subcomponent associated with the event, if applicable.
-
(labels¶list[str] | None, default:None) –The labels associated with the event, if applicable.
-
(root_cause¶str | None, default:None) –The root cause of the event, if known.
Returns:
-
tuple[int, int | None, list[int] | None]–A tuple containing the event ID, new component instance ID (if applicable), and a list of new subcomponent instance IDs (if applicable).
Source code in echo_postgres/event_instances.py
@validate_call
def insert(
self,
object_name: str,
event_type: str,
event_name: str,
company_involved: str,
company_responsible: str,
start_date: datetime,
end_date: datetime | None = None,
event_description: str | None = None,
component_type: str | None = None,
component_location: str | None = None,
subcomponent_type: str | None = None,
event_detection_type: str | None = None,
event_confirmation_type: str | None = None,
under_warranty: bool | None = None,
expected_cost: float | None = None,
real_cost: float | None = None,
lost_energy: float | None = None,
crane_used: bool | None = None,
severity_level: int | None = None,
new_component_model: str | None = None,
new_component_serial_number: str | None = None,
new_component_batch: str | None = None,
new_subcomponent_model: str | None = None,
new_subcomponent_serial_number: str | None = None,
new_subcomponent_batch: str | None = None,
labels: list[str] | None = None,
root_cause: str | None = None,
) -> tuple[int, int | None, list[int] | None]:
"""
Inserts a new event instance into the database.
Parameters
----------
object_name : str
The name of the object associated with the event.
event_type : str
The type of the event.
event_name : str
The name of the event.
company_involved : str
The name of the company involved in the event.
company_responsible : str
The name of the company responsible for the event.
start_date : datetime
The start date of the event.
end_date : datetime | None, optional
The end date of the event, if applicable.
event_description : str | None, optional
The description of the event, if available.
component_type : str | None, optional
The type of the component associated with the event, if applicable.
component_location : str | None, optional
The location of the component associated with the event, if applicable.
subcomponent_type : str | None, optional
The type of the subcomponent associated with the event, if applicable.
event_detection_type : str | None, optional
The type of event detection used for the event, if applicable.
event_confirmation_type : str | None, optional
The type of event confirmation used for the event, if applicable.
under_warranty : bool | None, optional
Indicates whether the event is under warranty, if applicable.
expected_cost : float | None, optional
The expected cost of the event, if applicable.
real_cost : float | None, optional
The real cost of the event, if applicable.
lost_energy : float | None, optional
The amount of lost energy due to the event, if applicable.
crane_used : bool | None, optional
Indicates whether a crane was used for the event, if applicable.
severity_level : int | None, optional
The severity level of the event, if applicable.
new_component_model : str | None, optional
The model of the new component associated with the event, if applicable.
new_component_serial_number : str | None, optional
The serial number of the new component associated with the event, if applicable.
new_component_batch : str | None, optional
The batch number of the new component associated with the event, if applicable.
new_subcomponent_model : str | None, optional
The model of the new subcomponent associated with the event, if applicable.
new_subcomponent_serial_number : str | None, optional
The serial number of the new subcomponent associated with the event, if applicable.
new_subcomponent_batch : str | None, optional
The batch number of the new subcomponent associated with the event, if applicable.
labels : list[str] | None, optional
The labels associated with the event, if applicable.
root_cause : str | None, optional
The root cause of the event, if known.
Returns
-------
tuple[int, int | None, list[int] | None]
A tuple containing the event ID, new component instance ID (if applicable), and a list of new subcomponent instance IDs (if applicable).
"""
# creating query
query = sql.SQL(
"""
SELECT * FROM performance.fn_create_event(
_event_type=>{event_type},
_name=>{event_name},
_description=>{event_description},
_start_date=>{start_date},
_end_date=>{end_date},
_object_name=>{object_name},
_component_type=>{component_type},
_component_location=>{component_location},
_subcomponent_type=>{subcomponent_type},
_company_involved=>{company_involved},
_company_responsible=>{company_responsible},
_event_detection_type=>{event_detection_type},
_event_confirmation_type=>{event_confirmation_type},
_under_warranty=>{under_warranty},
_expected_cost=>{expected_cost},
_real_cost=>{real_cost},
_lost_energy=>{lost_energy},
_crane_used=>{crane_used},
_severity_level=>{severity_level},
_new_component_model=>{new_component_model},
_new_component_serial_number=>{new_component_serial_number},
_new_component_batch=>{new_component_batch},
_new_subcomponent_model=>{new_subcomponent_model},
_new_subcomponent_serial_number=>{new_subcomponent_serial_number},
_new_subcomponent_batch=>{new_subcomponent_batch},
_labels=>{labels},
_root_cause=>{root_cause})
""",
).format(
event_type=sql.Literal(event_type),
event_name=sql.Literal(event_name),
event_description=sql.Literal(event_description),
start_date=sql.Literal(f"{start_date:%Y-%m-%d %H:%M:%S}"),
end_date=sql.Literal(f"{end_date:%Y-%m-%d %H:%M:%S}") if end_date else sql.SQL("NULL"),
object_name=sql.Literal(object_name),
component_type=sql.Literal(component_type),
component_location=sql.Literal(component_location),
subcomponent_type=sql.Literal(subcomponent_type),
company_involved=sql.Literal(company_involved),
company_responsible=sql.Literal(company_responsible),
event_detection_type=sql.Literal(event_detection_type),
event_confirmation_type=sql.Literal(event_confirmation_type),
under_warranty=sql.Literal(under_warranty),
expected_cost=sql.Literal(expected_cost),
real_cost=sql.Literal(real_cost),
lost_energy=sql.Literal(lost_energy),
crane_used=sql.Literal(crane_used),
severity_level=sql.Literal(severity_level),
new_component_model=sql.Literal(new_component_model),
new_component_serial_number=sql.Literal(new_component_serial_number),
new_component_batch=sql.Literal(new_component_batch),
new_subcomponent_model=sql.Literal(new_subcomponent_model),
new_subcomponent_serial_number=sql.Literal(new_subcomponent_serial_number),
new_subcomponent_batch=sql.Literal(new_subcomponent_batch),
labels=sql.Literal(f"{', '.join(labels)}") if labels else sql.SQL("NULL"),
root_cause=sql.Literal(root_cause),
)
# executing query
# the return type is a text array with event_id, new_component_instance_id and new_subcomponent_instance_ids
with self._perfdb.conn.reconnect() as conn:
result = conn.execute(query, skip_retry=True).fetchone()
event_id = int(result[0][0])
new_component_instance_id = int(result[0][1]) if new_component_model is not None else None
new_subcomponent_instance_ids = [int(x) for x in result[0][2][1:-1].split(",")] if new_subcomponent_model is not None else None
return event_id, new_component_instance_id, new_subcomponent_instance_ids