Component Instances - Latest¶
ComponentInstancesLatest(perfdb)
¶
Class used for handling the latest installed component instances. Can be accessed via perfdb.components.instances.latest.
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, component_ids=None, component_serial_numbers=None, component_models=None, component_manufacturers=None, component_types=None, locations=None, filter_type='and', get_attributes=False, output_type='DataFrame')
¶
Gets components instances latest (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
- manufacturer_id
- manufacturer_name
- component_model_id
- component_model_name
- location_id
- location_name
- component_instance_id
- serial_number
- installation_date
- duration
Parameters:
-
(object_names¶list[str] | None, default:None) –List of object names to filter the results. By default None.
-
(component_ids¶list[int] | None, default:None) –List of component ids to filter the results. By default None.
-
(component_serial_numbers¶list[str] | None, default:None) –List of component serial numbers to filter the results. By default None.
-
(component_models¶list[str] | None, default:None) –List of component model names to filter the results. By default None.
-
(component_manufacturers¶list[str] | None, default:None) –List of component manufacturer 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.
-
(locations¶list[str] | None, default:None) –List of locations to filter the results. 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 component 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:
-
dict[str, dict[str, dict[str, Any]]]–Dictionary on the format {"object_name": {"component_type_name": {"location_name": {"attribute": value, ...}, ...}, ...}, ...}
-
DataFrame–In case output_type is "DataFrame", returns a pandas DataFrame with the latest component instances. Index is a MultiIndex with levels "object_name", "component_type_name", "location_name"
-
DataFrame–In case output_type is "pl.DataFrame", returns a Polars DataFrame
Source code in echo_postgres/component_instances_latest.py
@validate_call
def get(
self,
object_names: list[str] | None = None,
component_ids: list[int] | None = None,
component_serial_numbers: list[str] | None = None,
component_models: list[str] | None = None,
component_manufacturers: list[str] | None = None,
component_types: list[str] | None = None,
locations: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
get_attributes: bool = False,
output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "DataFrame",
) -> dict[str, dict[str, dict[str, Any]]] | pd.DataFrame | pl.DataFrame:
"""Gets components instances latest (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
- manufacturer_id
- manufacturer_name
- component_model_id
- component_model_name
- location_id
- location_name
- component_instance_id
- serial_number
- installation_date
- duration
Parameters
----------
object_names : list[str] | None, optional
List of object names to filter the results. By default None.
component_ids : list[int] | None, optional
List of component ids to filter the results. By default None.
component_serial_numbers : list[str] | None, optional
List of component serial numbers to filter the results. By default None.
component_models : list[str] | None, optional
List of component model names to filter the results. By default None.
component_manufacturers : list[str] | None, optional
List of component manufacturer 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.
locations : list[str] | None, optional
List of locations to filter the results. 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 component 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
-------
dict[str, dict[str, dict[str, Any]]]
Dictionary on the format {"object_name": {"component_type_name": {"location_name": {"attribute": value, ...}, ...}, ...}, ...}
pd.DataFrame
In case output_type is "DataFrame", returns a pandas DataFrame with the latest component instances. Index is a MultiIndex with levels "object_name", "component_type_name", "location_name"
pl.DataFrame
In case output_type is "pl.DataFrame", returns a Polars DataFrame
"""
where = self._check_get_args(
object_names=object_names,
component_ids=component_ids,
component_serial_numbers=component_serial_numbers,
component_models=component_models,
component_manufacturers=component_manufacturers,
component_types=component_types,
locations=locations,
filter_type=filter_type,
)
query = [
sql.SQL(
"SELECT object_id, object_name, component_type_id, component_type_name, manufacturer_id, manufacturer_name, component_model_id, component_model_name, location_id, location_name, component_instance_id, serial_number, installation_date, EXTRACT(EPOCH FROM duration)::FLOAT AS duration FROM performance.v_component_instance_latest ",
),
where,
sql.SQL(" ORDER BY object_name, component_type_name, location_name"),
]
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(
query,
schema_overrides={
"object_id": pl.Int64,
"object_name": pl.Utf8,
"component_type_id": pl.Int64,
"component_type_name": pl.Utf8,
"manufacturer_id": pl.Int64,
"manufacturer_name": pl.Utf8,
"component_model_id": pl.Int64,
"component_model_name": pl.Utf8,
"location_id": pl.Int64,
"location_name": pl.Utf8,
"component_instance_id": pl.Int64,
"serial_number": pl.Utf8,
"installation_date": pl.Datetime("ms"),
"duration": pl.Float64,
},
)
# 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 component models
got_component_instances = df["component_instance_id"].to_list()
attrs: pl.DataFrame = self._perfdb.components.instances.attributes.get(
component_ids=got_component_instances,
output_type="pl.DataFrame",
values_only=True,
)
# pivot the attributes
attrs = attrs.pivot(index="component_instance_id", on="attribute_name", values="attribute_value")
# merging the attributes with the component models
df = df.join(attrs, on="component_instance_id", how="left")
return convert_output(df, output_type, index_col=["object_name", "component_type_name", "location_name"], nest_by_index=True)