Subcomponent Instances - History¶
SubcomponentInstancesHistory(perfdb)
¶
Class used for handling subcomponent instances history. Can be accessed via perfdb.components.subcomponents.instances.history.
Parameters:
Source code in echo_postgres/perfdb_root.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
"""Base class that all subclasses should inherit from.
Parameters
----------
perfdb : PerfDB
Top level object carrying all functionality and the connection handler.
"""
self._perfdb: e_pg.PerfDB = perfdb
get(object_names=None, subcomponent_ids=None, subcomponent_serial_numbers=None, subcomponent_models=None, subcomponent_manufacturers=None, subcomponent_types=None, component_types=None, period=None, filter_type='and', get_attributes=False, output_type='DataFrame')
¶
Gets subcomponents instances history (including dates when they were installed and removed).
The most useful keys/columns returned are:
- object_id
- object_name
- component_type_id
- component_type_name
- subcomponent_type_id
- subcomponent_type_name
- manufacturer_id
- manufacturer_name
- subcomponent_model_id
- subcomponent_model_name
- subcomponent_instance_id
- serial_number
- start_date
- end_date
- duration
Parameters:
-
(object_names¶list[str] | None, default:None) –List of object names to filter the results. By default None.
-
(subcomponent_ids¶list[int] | None, default:None) –List of subcomponent ids to filter the results. By default None.
-
(subcomponent_serial_numbers¶list[str] | None, default:None) –List of subcomponent serial numbers to filter the results. By default None.
-
(subcomponent_models¶list[str] | None, default:None) –List of subcomponent model names to filter the results. By default None.
-
(subcomponent_manufacturers¶list[str] | None, default:None) –List of subcomponent manufacturer names to filter the results. By default None.
-
(subcomponent_types¶list[str] | None, default:None) –List of subcomponent type names to filter the results. By default None.
-
(component_types¶list[str] | None, default:None) –List of component type names to filter the results. By default None.
-
(period¶DateTimeRange | None, default:None) –Period to filter the results. If the period when the subcomponent is installed on the turbine overlaps this it will be retrieved. By default None.
-
(filter_type¶Literal['and', 'or'], default:'and') –How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
-
(get_attributes¶bool, default:False) –If True, will also get the attributes of the subcomponent models.
-
(output_type¶Literal['dict', 'DataFrame', 'pl.DataFrame'], default:'DataFrame') –Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"] By default "DataFrame"
Returns:
-
list[dict[str, Any]]–List with the subcomponent instances. Each element is a dictionary with the keys being the column names.
-
DataFrame–In case output_type is "DataFrame", returns a pandas DataFrame with the subcomponent instances.
-
DataFrame–In case output_type is "pl.DataFrame", returns a Polars DataFrame
Source code in echo_postgres/subcomponent_instances_history.py
@validate_call
def get(
self,
object_names: list[str] | None = None,
subcomponent_ids: list[int] | None = None,
subcomponent_serial_numbers: list[str] | None = None,
subcomponent_models: list[str] | None = None,
subcomponent_manufacturers: list[str] | None = None,
subcomponent_types: list[str] | None = None,
component_types: list[str] | None = None,
period: DateTimeRange | None = None,
filter_type: Literal["and", "or"] = "and",
get_attributes: bool = False,
output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "DataFrame",
) -> list[dict[str, Any]] | pd.DataFrame | pl.DataFrame:
"""Gets subcomponents instances history (including dates when they were installed and removed).
The most useful keys/columns returned are:
- object_id
- object_name
- component_type_id
- component_type_name
- subcomponent_type_id
- subcomponent_type_name
- manufacturer_id
- manufacturer_name
- subcomponent_model_id
- subcomponent_model_name
- subcomponent_instance_id
- serial_number
- start_date
- end_date
- duration
Parameters
----------
object_names : list[str] | None, optional
List of object names to filter the results. By default None.
subcomponent_ids : list[int] | None, optional
List of subcomponent ids to filter the results. By default None.
subcomponent_serial_numbers : list[str] | None, optional
List of subcomponent serial numbers to filter the results. By default None.
subcomponent_models : list[str] | None, optional
List of subcomponent model names to filter the results. By default None.
subcomponent_manufacturers : list[str] | None, optional
List of subcomponent manufacturer names to filter the results. By default None.
subcomponent_types : list[str] | None, optional
List of subcomponent type names to filter the results. By default None.
component_types : list[str] | None, optional
List of component type names to filter the results. By default None.
period : DateTimeRange | None, optional
Period to filter the results. If the period when the subcomponent is installed on the turbine overlaps this it will be retrieved. By default None.
filter_type : Literal["and", "or"], optional
How to treat multiple filters. Can be one of ["and", "or"]. By default "and".
get_attributes : bool, optional
If True, will also get the attributes of the subcomponent models.
output_type : Literal["dict", "DataFrame", "pl.DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]
By default "DataFrame"
Returns
-------
list[dict[str, Any]]
List with the subcomponent instances. Each element is a dictionary with the keys being the column names.
pd.DataFrame
In case output_type is "DataFrame", returns a pandas DataFrame with the subcomponent instances.
pl.DataFrame
In case output_type is "pl.DataFrame", returns a Polars DataFrame
"""
where = self._check_get_args(
object_names=object_names,
component_types=component_types,
subcomponent_ids=subcomponent_ids,
subcomponent_serial_numbers=subcomponent_serial_numbers,
subcomponent_models=subcomponent_models,
subcomponent_manufacturers=subcomponent_manufacturers,
subcomponent_types=subcomponent_types,
period=period,
filter_type=filter_type,
)
query = [
sql.SQL(
"SELECT object_id, object_name, component_type_id, component_type_name, subcomponent_type_id, subcomponent_type_name, manufacturer_id, manufacturer_name, subcomponent_model_id, subcomponent_model_name, subcomponent_instance_id, serial_number, start_date, end_date, EXTRACT(EPOCH FROM duration)::FLOAT AS duration FROM performance.v_subcomponent_instance_history ",
),
where,
sql.SQL(" ORDER BY object_name, start_date"),
]
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
# casting duration to pl.Duration("ms")
df = df.with_columns(
(pl.col("duration") * 1_000).cast(pl.Int64).cast(pl.Duration("ms")).alias("duration"),
)
# getting attributes
if get_attributes:
# names of the subcomponent models
got_subcomponent_instances = df["subcomponent_instance_id"].to_list()
attrs: pl.DataFrame = self._perfdb.components.subcomponents.instances.attributes.get(
subcomponent_ids=got_subcomponent_instances,
output_type="pl.DataFrame",
values_only=True,
)
# pivot the attributes
attrs = attrs.pivot(index="subcomponent_instance_id", on="attribute_name", values="attribute_value")
# merging the attributes with the subcomponent models
df = df.join(attrs, on="subcomponent_instance_id", how="left")
return convert_output(df, output_type, orient="records")