Skip to content

Subcomponent Instances

SubcomponentInstances(perfdb)

Class used for handling subcomponent instances. Can be accessed via perfdb.components.subcomponents.instances.

Parameters:

  • perfdb

    (PerfDB) –

    Top level object carrying all functionality and the connection handler.

Source code in echo_postgres/subcomponent_instances.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
    """Class used for handling subcomponent instances. Can be accessed via `perfdb.components.subcomponents.instances`.

    Parameters
    ----------
    perfdb : PerfDB
        Top level object carrying all functionality and the connection handler.
    """
    super().__init__(perfdb)

    from .subcomponent_instance_attributes import SubcomponentInstanceAttributes
    from .subcomponent_instances_history import SubcomponentInstancesHistory
    from .subcomponent_instances_latest import SubcomponentInstancesLatest

    # * subclasses

    self.attributes = SubcomponentInstanceAttributes(perfdb)
    self.history = SubcomponentInstancesHistory(perfdb)
    self.latest = SubcomponentInstancesLatest(perfdb)

get(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='dict')

Gets all subcomponent instances.

The most useful keys/columns returned are:

  • id
  • serial_number
  • batch
  • subcomponent_model_id
  • subcomponent_model_name
  • subcomponent_type_id
  • subcomponent_type_name
  • manufacturer_id
  • manufacturer_name

Parameters:

  • 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"].

  • get_attributes

    (bool, default: False ) –

    If True, will also get the attributes of the subcomponent models.

  • output_type

    (Literal['dict', 'DataFrame'], default: 'dict' ) –

    Output type of the data. Can be one of ["dict", "DataFrame"] By default "dict"

Returns:

  • list[dict[str, Any]]

    List with the subcomponent instances. Each element is a dictionary with the keys being the column names.

  • DataFrame

    DataFrame with the subcomponent instances. Index can be ignored.

Source code in echo_postgres/subcomponent_instances.py
@validate_call
def get(
    self,
    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"] = "dict",
) -> list[dict[str, Any]] | DataFrame:
    """Gets all subcomponent instances.

    The most useful keys/columns returned are:

    - id
    - serial_number
    - batch
    - subcomponent_model_id
    - subcomponent_model_name
    - subcomponent_type_id
    - subcomponent_type_name
    - manufacturer_id
    - manufacturer_name

    Parameters
    ----------
    subcomponent_ids : list[int] | None
        List of subcomponent ids to filter the results. By default None.
    subcomponent_serial_numbers : list[str] | None
        List of subcomponent serial numbers to filter the results. By default None.
    subcomponent_batches : list[str] | None
        List of subcomponent batches to filter the results. By default None.
    subcomponent_models : list[str] | None
        List of subcomponent model names to filter the results. By default None.
    subcomponent_manufacturers : list[str] | None
        List of subcomponent manufacturer names to filter the results. By default None.
    subcomponent_types : list[str] | None
        List of subcomponent type names to filter the results. By default None.
    component_types : list[str] | None
        List of component type names to filter the results. By default None.
    filter_type : Literal["and", "or"]
        How to treat multiple filters. Can be one of ["and", "or"].
    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 "dict"

    Returns
    -------
    list[dict[str, Any]]
        List with the subcomponent instances. Each element is a dictionary with the keys being the column names.
    DataFrame
        DataFrame with the subcomponent instances. Index can be ignored.
    """
    where = self._check_get_args(
        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_instances "),
        where,
        sql.SQL(" ORDER BY id"),
    ]
    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("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,
        )
        if attrs.empty:
            logger.debug("No attributes found")
        else:
            # 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)

    return df.to_dict(orient="records") if output_type == "dict" else df

get_ids(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')

Gets all subcomponent instances ids.

Parameters:

  • 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"].

Returns:

  • list[int]

    List with the subcomponent ids.

Source code in echo_postgres/subcomponent_instances.py
@validate_call
def get_ids(
    self,
    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",
) -> list[int]:
    """Gets all subcomponent instances ids.

    Parameters
    ----------
    subcomponent_ids : list[int] | None
        List of subcomponent ids to filter the results. By default None.
    subcomponent_serial_numbers : list[str] | None
        List of subcomponent serial numbers to filter the results. By default None.
    subcomponent_batches : list[str] | None
        List of subcomponent batches to filter the results. By default None.
    subcomponent_models : list[str] | None
        List of subcomponent model names to filter the results. By default None.
    subcomponent_manufacturers : list[str] | None
        List of subcomponent manufacturer names to filter the results. By default None.
    subcomponent_types : list[str] | None
        List of subcomponent type names to filter the results. By default None.
    component_types : list[str] | None
        List of component type names to filter the results. By default None.
    filter_type : Literal["and", "or"]
        How to treat multiple filters. Can be one of ["and", "or"].

    Returns
    -------
    list[int]
        List with the subcomponent ids.
    """
    # checking inputs
    where = self._check_get_args(
        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 id FROM performance.v_subcomponent_instances "),
        where,
        sql.SQL(" ORDER BY id"),
    ]
    query = sql.Composed(query)

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_pandas(query)

    return df["id"].tolist()