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
# * subclasses
self.comments = EventInstanceComments(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
conn.execute(query, skip_retry=True)
logger.debug(f"Deleted {conn.rowcount} rows from events table")
edit(event_id, companies_involved=None, company_responsible=None, start_date=None, end_date=None, event_description=None, component_location=None, event_detection_type=None, under_warranty=None, expected_cost=None, real_cost=None, lost_energy=None, crane_used=None, new_component_model=None, new_component_serial_number=None, root_cause=None, event_date_status=None, event_review_status=None)
¶
Edits an event instance in the database.
Parameters:
-
(event_id¶int) –The ID of the event to edit.
-
(companies_involved¶list[str] | None, default:None) –The names of the companies 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.
-
(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.
-
(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.
-
(root_cause¶str | None, default:None) –The root cause of the event, if known.
-
(event_date_status¶str | None, default:None) –The event date status of the event, if applicable. Example: "Evento finalizado", "Evento em andamento"
-
(event_review_status¶str | None, default:None) –The review status of the event, if applicable. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
Returns:
-
None–
Source code in echo_postgres/event_instances.py
@validate_call
def edit(
self,
event_id: int,
companies_involved: list[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,
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,
new_component_model: str | None = None,
new_component_serial_number: str | None = None,
root_cause: str | None = None,
event_date_status: str | None = None,
event_review_status: 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.
companies_involved : list[str] | None, optional
The names of the companies 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.
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.
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.
root_cause : str | None, optional
The root cause of the event, if known.
event_date_status : str | None, optional
The event date status of the event, if applicable. Example: "Evento finalizado", "Evento em andamento"
event_review_status : str | None, optional
The review status of the event, if applicable. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
Returns
-------
None
"""
# Checking arguments
# arguments "name": (value, required, type)
args = {
"event_id": (event_id, True, int),
"companies_involved": (companies_involved, False, list),
"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),
"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),
"new_component_model": (new_component_model, False, str),
"new_component_serial_number": (new_component_serial_number, False, str),
"root_cause": (root_cause, False, str),
"event_date_status": (event_date_status, False, str),
"event_review_status": (event_review_status, False, str),
}
# creating query
query = sql.SQL(
"""
SELECT * FROM performance.fn_edit_event(
_event_id=>{event_id},
_companies_involved_names=>{companies_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},
_under_warranty=>{under_warranty},
_expected_cost=>{expected_cost},
_real_cost=>{real_cost},
_lost_energy=>{lost_energy},
_crane_used=>{crane_used},
_new_component_model=>{new_component_model},
_new_component_serial_number=>{new_component_serial_number},
_root_cause_name=>{root_cause},
_event_date_status=>{event_date_status},
_event_review_status=>{event_review_status})
""",
).format(
event_id=sql.Literal(event_id),
companies_involved=sql.Literal(companies_involved) if companies_involved else sql.SQL("NULL"),
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),
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),
new_component_model=sql.Literal(new_component_model),
new_component_serial_number=sql.Literal(new_component_serial_number),
root_cause=sql.Literal(root_cause),
event_date_status=sql.Literal(event_date_status),
event_review_status=sql.Literal(event_review_status),
)
# 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, 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, root_causes=None, under_warranty=None, crane_used=None, parent_event=None, child_event=None, event_date_status=None, event_review_status=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,
- 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_model_id,
- old_component_model_name,
- new_component_id,
- new_component_serial_number,
- new_component_model_id,
- new_component_model_name,
- component_location_id,
- component_location_name,
- subcomponents_replaced,
- old_subcomponents,
- new_subcomponents,
- companies_involved_ids,
- companies_involved_names,
- company_responsible_id,
- company_responsible_name,
- event_detection_type_id,
- event_detection_type_name,
- root_cause_id,
- root_cause_name,
- under_warranty,
- expected_cost,
- real_cost,
- lost_energy,
- crane_used,
- comments,
- parent_event,
- child_event,
- event_date_status,
- event_review_status,
- 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.
-
(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.
-
(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.
-
(parent_event¶bool | None, default:None) –Whether the event is a parent event (i.e., has child events). By default None.
-
(child_event¶bool | None, default:None) –Whether the event is a child event (i.e., has a parent event). By default None.
-
(event_date_status¶str | None, default:None) –The event date status of the event. By default None. Example: "Evento finalizado", "Evento em andamento"
-
(event_review_status¶str | None, default:None) –The review status of the event. By default None. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
-
(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,
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,
root_causes: list[str] | None = None,
under_warranty: bool | None = None,
crane_used: bool | None = None,
parent_event: bool | None = None,
child_event: bool | None = None,
event_date_status: str | None = None,
event_review_status: 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,
- 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_model_id,
- old_component_model_name,
- new_component_id,
- new_component_serial_number,
- new_component_model_id,
- new_component_model_name,
- component_location_id,
- component_location_name,
- subcomponents_replaced,
- old_subcomponents,
- new_subcomponents,
- companies_involved_ids,
- companies_involved_names,
- company_responsible_id,
- company_responsible_name,
- event_detection_type_id,
- event_detection_type_name,
- root_cause_id,
- root_cause_name,
- under_warranty,
- expected_cost,
- real_cost,
- lost_energy,
- crane_used,
- comments,
- parent_event,
- child_event,
- event_date_status,
- event_review_status,
- 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.
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.
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.
parent_event : bool | None, optional
Whether the event is a parent event (i.e., has child events). By default None.
child_event : bool | None, optional
Whether the event is a child event (i.e., has a parent event). By default None.
event_date_status : str | None, optional
The event date status of the event. By default None. Example: "Evento finalizado", "Evento em andamento"
event_review_status : str | None, optional
The review status of the event. By default None. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
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,
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,
root_causes=root_causes,
under_warranty=under_warranty,
crane_used=crane_used,
parent_event=parent_event,
child_event=child_event,
event_date_status=event_date_status,
event_review_status=event_review_status,
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_child_events_ids(parent_event_id)
¶
Gets the child event ids for a given parent event id.
Parameters:
-
(parent_event_id¶int) –The ID of the parent event.
Returns:
-
list[int]–A list of IDs of the child events.
Source code in echo_postgres/event_instances.py
def get_child_events_ids(self, parent_event_id: int) -> list[int]:
"""
Gets the child event ids for a given parent event id.
Parameters
----------
parent_event_id : int
The ID of the parent event.
Returns
-------
list[int]
A list of IDs of the child events.
"""
query = sql.SQL(
"SELECT child_event_id FROM performance.parent_child_event_mapping WHERE parent_event_id = {parent_event_id}",
).format(
parent_event_id=sql.Literal(parent_event_id),
)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
if len(df) == 0:
logger.warning(f"No child events found for parent event {parent_event_id}")
return None
logger.debug(f"Got child events for parent event {parent_event_id}: {df['child_event_id'].tolist()}")
return df["child_event_id"].tolist()
get_ids(event_ids=None, object_names=None, event_types=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, root_causes=None, under_warranty=None, crane_used=None, parent_event=None, child_event=None, event_date_status=None, event_review_status=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.
-
(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.
-
(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.
-
(parent_event¶bool | None, default:None) –Whether the event is a parent event (i.e., has child events). By default None.
-
(child_event¶bool | None, default:None) –Whether the event is a child event (i.e., has a parent event). By default None.
-
(event_date_status¶str | None, default:None) –The event date status of the event. By default None. Example: "Evento finalizado", "Evento em andamento"
-
(event_review_status¶str | None, default:None) –The review status of the event. By default None. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
-
(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,
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,
root_causes: list[str] | None = None,
under_warranty: bool | None = None,
crane_used: bool | None = None,
parent_event: bool | None = None,
child_event: bool | None = None,
event_date_status: str | None = None,
event_review_status: 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.
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.
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.
parent_event : bool | None, optional
Whether the event is a parent event (i.e., has child events). By default None.
child_event : bool | None, optional
Whether the event is a child event (i.e., has a parent event). By default None.
event_date_status : str | None, optional
The event date status of the event. By default None. Example: "Evento finalizado", "Evento em andamento"
event_review_status : str | None, optional
The review status of the event. By default None. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
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,
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,
root_causes=root_causes,
under_warranty=under_warranty,
crane_used=crane_used,
parent_event=parent_event,
child_event=child_event,
event_date_status=event_date_status,
event_review_status=event_review_status,
filter_type=filter_type,
)
query = [sql.SQL("SELECT event_id FROM performance.v_events"), where, sql.SQL(" ORDER BY event_type_name")]
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
return df["event_id"].tolist()
get_parent_event_id(child_event_id)
¶
Gets the parent event id for a given child event id.
Parameters:
-
(child_event_id¶list[int]) –A list of IDs of the child events.
Returns:
-
int | None–The ID of the parent event, or None if the child event has no parent.
Source code in echo_postgres/event_instances.py
def get_parent_event_id(self, child_event_id: list[int]) -> int | None:
"""
Gets the parent event id for a given child event id.
Parameters
----------
child_event_id : list[int]
A list of IDs of the child events.
Returns
-------
int | None
The ID of the parent event, or None if the child event has no parent.
"""
query = sql.SQL(
"SELECT parent_event_id FROM performance.parent_child_event_mapping WHERE child_event_id = ANY(ARRAY[{child_event_ids}])",
).format(
child_event_ids=sql.SQL(", ").join(sql.Literal(cid) for cid in child_event_id),
)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
if len(df) == 0:
logger.warning(f"No parent event found for child events {child_event_id}")
return None
return df["parent_event_id"].iloc[0]
insert(object_name, event_type, companies_involved, company_responsible, start_date, end_date=None, event_description=None, component_type=None, component_location=None, subcomponent_type=None, event_detection_type=None, under_warranty=None, expected_cost=None, real_cost=None, lost_energy=None, crane_used=None, new_component_model=None, new_component_serial_number=None, new_subcomponent_model=None, new_subcomponent_serial_number=None, root_cause=None, parent_event=None, child_event=None, event_date_status=None, event_review_status=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.
-
(companies_involved¶list[str]) –The names of the companies 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.
-
(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.
-
(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_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.
-
(root_cause¶str | None, default:None) –The root cause of the event, if known.
-
(parent_event¶bool | None, default:None) –Indicates whether the event is a parent event, if applicable.
-
(child_event¶bool | None, default:None) –Indicates whether the event is a child event, if applicable.
-
(event_date_status¶str | None, default:None) –The event date status of the event, if applicable. Example: "Evento finalizado", "Evento em andamento"
-
(event_review_status¶str | None, default:None) –The review status of the event, if applicable. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
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,
companies_involved: list[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,
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,
new_component_model: str | None = None,
new_component_serial_number: str | None = None,
new_subcomponent_model: str | None = None,
new_subcomponent_serial_number: str | None = None,
root_cause: str | None = None,
parent_event: bool | None = None,
child_event: bool | None = None,
event_date_status: str | None = None,
event_review_status: 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.
companies_involved : list[str]
The names of the companies 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.
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.
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_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.
root_cause : str | None, optional
The root cause of the event, if known.
parent_event : bool | None, optional
Indicates whether the event is a parent event, if applicable.
child_event : bool | None, optional
Indicates whether the event is a child event, if applicable.
event_date_status : str | None, optional
The event date status of the event, if applicable. Example: "Evento finalizado", "Evento em andamento"
event_review_status : str | None, optional
The review status of the event, if applicable. Example: "Revisão pendente", "Revisão em andamento", "Aguardando retorno externo", "Revisão finalizada"
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},
_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},
_companies_involved=>{companies_involved},
_company_responsible=>{company_responsible},
_event_detection_type=>{event_detection_type},
_under_warranty=>{under_warranty},
_expected_cost=>{expected_cost},
_real_cost=>{real_cost},
_lost_energy=>{lost_energy},
_crane_used=>{crane_used},
_new_component_model=>{new_component_model},
_new_component_serial_number=>{new_component_serial_number},
_new_subcomponent_model=>{new_subcomponent_model},
_new_subcomponent_serial_number=>{new_subcomponent_serial_number},
_root_cause=>{root_cause},
_parent_event=>{parent_event},
_child_event=>{child_event},
_event_date_status=>{event_date_status},
_event_review_status=>{event_review_status})
""",
).format(
event_type=sql.Literal(event_type),
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),
companies_involved=sql.Literal(companies_involved) if companies_involved else sql.SQL("NULL"),
company_responsible=sql.Literal(company_responsible),
event_detection_type=sql.Literal(event_detection_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),
new_component_model=sql.Literal(new_component_model),
new_component_serial_number=sql.Literal(new_component_serial_number),
new_subcomponent_model=sql.Literal(new_subcomponent_model),
new_subcomponent_serial_number=sql.Literal(new_subcomponent_serial_number),
root_cause=sql.Literal(root_cause),
parent_event=sql.Literal(parent_event),
child_event=sql.Literal(child_event),
event_date_status=sql.Literal(event_date_status),
event_review_status=sql.Literal(event_review_status),
)
# 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
map_parent_child_events(parent_event_id, child_event_ids)
¶
Maps parent and child events in the database.
Parameters:
-
(parent_event_id¶int) –The ID of the parent event.
-
(child_event_ids¶list[int]) –A list of IDs of the child events.
Returns:
-
None–
Source code in echo_postgres/event_instances.py
@validate_call
def map_parent_child_events(
self,
parent_event_id: int,
child_event_ids: list[int],
) -> None:
"""
Maps parent and child events in the database.
Parameters
----------
parent_event_id : int
The ID of the parent event.
child_event_ids : list[int]
A list of IDs of the child events.
Returns
-------
None
"""
# creating query (child_event_ids as an array)
query = sql.SQL("SELECT performance.fn_map_parent_child_event({parent_event_id}, ARRAY[{child_event_ids}])").format(
parent_event_id=sql.Literal(parent_event_id),
child_event_ids=sql.SQL(", ").join(sql.Literal(child_event_id) for child_event_id in child_event_ids),
)
# executing query
with self._perfdb.conn.reconnect() as conn:
conn.execute(query, skip_retry=True)
logger.debug(f"Mapped parent event {parent_event_id} with child events {child_event_ids}")
unmap_parent_child_events(parent_event_id, child_event_ids)
¶
Unmaps parent and child events in the database.
Parameters:
-
(parent_event_id¶int) –The ID of the parent event.
-
(child_event_ids¶list[int]) –A list of IDs of the child events to unmap.
Returns:
-
None–
Source code in echo_postgres/event_instances.py
def unmap_parent_child_events(self, parent_event_id: int, child_event_ids: list[int]) -> None:
"""
Unmaps parent and child events in the database.
Parameters
----------
parent_event_id : int
The ID of the parent event.
child_event_ids : list[int]
A list of IDs of the child events to unmap.
Returns
-------
None
"""
# creating query (child_event_ids as an array)
query = sql.SQL("SELECT performance.fn_unmap_parent_child_event({parent_event_id}, ARRAY[{child_event_ids}])").format(
parent_event_id=sql.Literal(parent_event_id),
child_event_ids=sql.SQL(", ").join(sql.Literal(child_event_id) for child_event_id in child_event_ids),
)
# executing query
with self._perfdb.conn.reconnect() as conn:
conn.execute(query, skip_retry=True)
logger.debug(f"Unmapped parent event {parent_event_id} from child events {child_event_ids}")