Subcomponent Instances - Latest¶
SubcomponentInstancesLatest(perfdb)
¶
Class used for handling the latest installed subcomponent instances. Can be accessed via perfdb.components.subcomponents.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, subcomponent_ids=None, subcomponent_serial_numbers=None, subcomponent_batches=None, subcomponent_models=None, subcomponent_manufacturers=None, subcomponent_types=None, component_types=None, filter_type='and', get_attributes=False, output_type='DataFrame')
¶
Gets subcomponents 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
- subcomponent_type_id
- subcomponent_type_name
- manufacturer_id
- manufacturer_name
- subcomponent_model_id
- subcomponent_model_name
- subcomponent_instance_id
- serial_number
- batch
- installation_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_batches¶list[str] | None, default:None) –List of subcomponent batches 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.
-
(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'], default:'DataFrame') –Output type of the data. Can be one of ["dict", "DataFrame"] By default "DataFrame"
Returns:
-
dict[str, dict[str, dict[str, Any]]]–Dictionary on the format {"object_name": {"component_type_name": {"subcomponent_type_name": {"attribute": value, ...}, ...}, ...}, ...}
-
DataFrame–DataFrame with the latest subcomponent instances. Index is a MultiIndex with levels "object_name", "component_type_name", "subcomponent_type_name"
Source code in echo_postgres/subcomponent_instances_latest.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_batches: 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,
filter_type: Literal["and", "or"] = "and",
get_attributes: bool = False,
output_type: Literal["dict", "DataFrame"] = "DataFrame",
) -> dict[str, dict[str, dict[str, Any]]] | DataFrame:
"""Gets subcomponents 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
- subcomponent_type_id
- subcomponent_type_name
- manufacturer_id
- manufacturer_name
- subcomponent_model_id
- subcomponent_model_name
- subcomponent_instance_id
- serial_number
- batch
- installation_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_batches : list[str] | None, optional
List of subcomponent batches 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.
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"], optional
Output type of the data. Can be one of ["dict", "DataFrame"]
By default "DataFrame"
Returns
-------
dict[str, dict[str, dict[str, Any]]]
Dictionary on the format {"object_name": {"component_type_name": {"subcomponent_type_name": {"attribute": value, ...}, ...}, ...}, ...}
DataFrame
DataFrame with the latest subcomponent instances. Index is a MultiIndex with levels "object_name", "component_type_name", "subcomponent_type_name"
"""
where = self._check_get_args(
object_names=object_names,
subcomponent_ids=subcomponent_ids,
subcomponent_serial_numbers=subcomponent_serial_numbers,
subcomponent_batches=subcomponent_batches,
subcomponent_models=subcomponent_models,
subcomponent_manufacturers=subcomponent_manufacturers,
subcomponent_types=subcomponent_types,
component_types=component_types,
filter_type=filter_type,
)
query = [
sql.SQL("SELECT * FROM performance.v_subcomponent_instance_latest "),
where,
sql.SQL(" ORDER BY object_name, component_type_name, subcomponent_type_name"),
]
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
# getting attributes
if get_attributes:
df = df.set_index("subcomponent_instance_id")
# names of the subcomponent models
got_subcomponent_instances = df.index.tolist()
attrs: DataFrame = self._perfdb.components.subcomponents.instances.attributes.get(
subcomponent_ids=got_subcomponent_instances,
output_type="DataFrame",
values_only=True,
)
# pivot the attributes
attrs = attrs.reset_index(drop=False).pivot(
index="subcomponent_instance_id",
columns="attribute_name",
values="attribute_value",
)
# merging the attributes with the subcomponent models
df = df.merge(attrs, left_index=True, right_index=True, how="left")
# resetting the index
df = df.reset_index(drop=False)
# defining "object_name", "component_type_name", "subcomponent_type_name" as the index
df = df.set_index(["object_name", "component_type_name", "subcomponent_type_name"])
if output_type == "dict":
result = df.to_dict(orient="index")
final_result = {}
for (object_name, component_type_name, subcomponent_type_name), values in result.items():
if object_name not in final_result:
final_result[object_name] = {}
if component_type_name not in final_result[object_name]:
final_result[object_name][component_type_name] = {}
final_result[object_name][component_type_name][subcomponent_type_name] = values
return final_result
return df