KPI Capacity Factor Values¶
KpiCapacityFactorValues(perfdb)
¶
Class used for handling capacity factor values. Can be accessed via perfdb.kpis.capacityfactor.values.
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(period, time_res='daily', aggregation_window=None, object_or_group_names=None, object_group_types=None, measurement_points=None, filter_type='and', output_type='DataFrame', values_only=False)
¶
Gets capacity factor values for the desired period and objects.
The most useful keys/columns returned are:
- capacity_factor
Parameters:
-
(period¶DateTimeRange) –Period of time to get the data for.
-
(time_res¶Literal['daily', 'monthly', 'quarterly', 'yearly'], default:'daily') –Time resolution of the data. Can be one of ["daily", "monthly", "quarterly", "yearly"], by default "daily"
-
(aggregation_window¶Literal['mtd', 'ytd', '12m'] | None, default:None) –Aggregation window to use. Can be one of ["mtd", "ytd", "12m"], by default None
-
(object_or_group_names¶list[str], default:None) –List of object or group names to get the data for. By default None
-
(object_group_types¶list[str], default:None) –List of object group types to get the data for. By default None
-
(measurement_points¶list[ALLOWED_MEASUREMENT_POINTS], default:None) –List of measurement points to get the data for, like Connection Point, Gravity Center, Asset, etc. By default None
-
(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', 'pl.DataFrame'], default:'DataFrame') –Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"] By default "dict"
-
(values_only¶bool, default:False) –If set to True, when returning a dict will only return the values, ignoring other attributes like modified_date. Is ignored when output_type is "DataFrame". By default False
Returns:
-
DataFrame–In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = MultiIndex["group_type_name", "object_or_group_name", "measurement_point_name", "date"], columns = [value, target, modified_date]
-
DataFrame–In case output_type is "pl.DataFrame", returns a Polars DataFrame
-
dict[str, dict[Timestamp, dict[str, dict[str, Any]]]]–In case output_type is "dict", returns a dictionary in the format {group_type_name: {object_or_group_name: {measurement_point: {date: {attribute: value, ...}, ...}, ...}, ...}
Source code in echo_postgres/kpi_capacityfactor_values.py
@validate_call
def get(
self,
period: DateTimeRange,
time_res: Literal["daily", "monthly", "quarterly", "yearly"] = "daily",
aggregation_window: Literal["mtd", "ytd", "12m"] | None = None,
object_or_group_names: list[str] | None = None,
object_group_types: list[str] | None = None,
measurement_points: list[ALLOWED_MEASUREMENT_POINTS] | None = None,
filter_type: Literal["and", "or"] = "and",
output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "DataFrame",
values_only: bool = False,
) -> pd.DataFrame | pl.DataFrame | dict[str, dict[Timestamp, dict[str, dict[str, Any]]]]:
"""Gets capacity factor values for the desired period and objects.
The most useful keys/columns returned are:
- capacity_factor
Parameters
----------
period : DateTimeRange
Period of time to get the data for.
time_res : Literal["daily", "monthly", "quarterly", "yearly"], optional
Time resolution of the data. Can be one of ["daily", "monthly", "quarterly", "yearly"], by default "daily"
aggregation_window : Literal["mtd", "ytd", "12m"] | None, optional
Aggregation window to use. Can be one of ["mtd", "ytd", "12m"], by default None
object_or_group_names : list[str], optional
List of object or group names to get the data for. By default None
object_group_types : list[str], optional
List of object group types to get the data for. By default None
measurement_points : list[ALLOWED_MEASUREMENT_POINTS], optional
List of measurement points to get the data for, like Connection Point, Gravity Center, Asset, etc. By default None
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", "pl.DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]
By default "dict"
values_only : bool, optional
If set to True, when returning a dict will only return the values, ignoring other attributes like modified_date. Is ignored when output_type is "DataFrame". By default False
Returns
-------
pd.DataFrame
In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = MultiIndex["group_type_name", "object_or_group_name", "measurement_point_name", "date"], columns = [value, target, modified_date]
pl.DataFrame
In case output_type is "pl.DataFrame", returns a Polars DataFrame
dict[str, dict[Timestamp, dict[str, dict[str, Any]]]]
In case output_type is "dict", returns a dictionary in the format {group_type_name: {object_or_group_name: {measurement_point: {date: {attribute: value, ...}, ...}, ...}, ...}
"""
# build the query
query = [
sql.SQL(
"SELECT * FROM performance.{table} WHERE (date >= {start} AND date <= {end})",
).format(
table=sql.Identifier(
f"mv_capacityfactor_{time_res}{f'_{aggregation_window}' if aggregation_window else ''}",
),
start=sql.Literal(f"{period.start:%Y-%m-%d %H:%M:%S}"),
end=sql.Literal(f"{period.end:%Y-%m-%d %H:%M:%S}"),
),
]
where = []
if object_or_group_names:
where.append(
sql.SQL("object_or_group_name IN ({names})").format(
names=sql.SQL(", ").join(map(sql.Literal, object_or_group_names)),
),
)
if object_group_types:
where.append(
sql.SQL("group_type_name IN ({names})").format(
names=sql.SQL(", ").join(map(sql.Literal, object_group_types)),
),
)
if measurement_points:
where.append(
sql.SQL("measurement_point_name IN ({points})").format(
points=sql.SQL(", ").join(map(sql.Literal, measurement_points)),
),
)
if where:
query.append(sql.SQL(" AND ("))
query.append(sql.SQL(f" {filter_type.upper()} ").join(where))
query.append(sql.SQL(")"))
query.append(sql.SQL(" ORDER BY object_or_group_name, group_type_name, measurement_point_name, date"))
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
return convert_output(
df,
output_type=output_type,
index_col=["group_type_name", "object_or_group_name", "measurement_point_name", "date"],
drop_id_cols=True,
nest_by_index=True,
values_only_key="value" if values_only else None,
)