|
| 1 | +"""QualiCharge prefect indicators: usage. |
| 2 | +
|
| 3 | +U6: session duration by power category. |
| 4 | +""" |
| 5 | + |
| 6 | +from string import Template |
| 7 | +from typing import List |
| 8 | +from uuid import UUID |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pandas as pd # type: ignore |
| 12 | +from prefect import flow, runtime, task |
| 13 | +from prefect.futures import wait |
| 14 | +from prefect.task_runners import ThreadPoolTaskRunner |
| 15 | +from sqlalchemy.engine import Connection |
| 16 | + |
| 17 | +from ..conf import settings |
| 18 | +from ..models import Indicator, IndicatorTimeSpan, Level |
| 19 | +from ..utils import ( |
| 20 | + POWER_RANGE_CTE, |
| 21 | + export_indic, |
| 22 | + get_database_engine, |
| 23 | + get_num_for_level_query_params, |
| 24 | + get_targets_for_level, |
| 25 | + get_timespan_filter_query_params, |
| 26 | +) |
| 27 | + |
| 28 | +DURATION_FOR_LEVEL_QUERY_TEMPLATE = """ |
| 29 | + WITH |
| 30 | + $power_range, |
| 31 | + sessionf AS ( |
| 32 | + SELECT |
| 33 | + point_de_charge_id, |
| 34 | + sum(session.end -session.start) as duree_pdc |
| 35 | + FROM |
| 36 | + session |
| 37 | + WHERE |
| 38 | + $timespan |
| 39 | + GROUP BY |
| 40 | + point_de_charge_id |
| 41 | + ) |
| 42 | + SELECT |
| 43 | + extract ('epoch' from sum(duree_pdc)) / 3600.0 AS value, |
| 44 | + category, |
| 45 | + $level_id AS level_id |
| 46 | + FROM |
| 47 | + sessionf |
| 48 | + INNER JOIN PointDeCharge ON sessionf.point_de_charge_id = PointDeCharge.id |
| 49 | + LEFT JOIN Station ON station_id = Station.id |
| 50 | + LEFT JOIN Localisation ON localisation_id = Localisation.id |
| 51 | + LEFT JOIN City ON City.code = code_insee_commune |
| 52 | + LEFT JOIN puissance ON puissance_nominale::numeric <@ category |
| 53 | + $join_extras |
| 54 | + WHERE |
| 55 | + $level_id IN ($indexes) |
| 56 | + GROUP BY |
| 57 | + $level_id, |
| 58 | + category |
| 59 | + """ |
| 60 | + |
| 61 | +QUERY_NATIONAL_TEMPLATE = """ |
| 62 | + WITH |
| 63 | + $power_range, |
| 64 | + sessionf AS ( |
| 65 | + SELECT |
| 66 | + point_de_charge_id, |
| 67 | + sum(session.end -session.start) as duree_pdc |
| 68 | + FROM |
| 69 | + session |
| 70 | + WHERE |
| 71 | + $timespan |
| 72 | + GROUP BY |
| 73 | + point_de_charge_id |
| 74 | + ) |
| 75 | + SELECT |
| 76 | + extract ('epoch' from sum(duree_pdc)) / 3600.0 AS value, |
| 77 | + category |
| 78 | + FROM |
| 79 | + sessionf |
| 80 | + INNER JOIN PointDeCharge ON sessionf.point_de_charge_id = PointDeCharge.id |
| 81 | + LEFT JOIN puissance ON puissance_nominale::numeric <@ category |
| 82 | + GROUP BY |
| 83 | + category |
| 84 | + """ |
| 85 | + |
| 86 | + |
| 87 | +@task(task_run_name="values-for-target-{level:02d}") |
| 88 | +def get_values_for_targets( |
| 89 | + connection: Connection, |
| 90 | + level: Level, |
| 91 | + timespan: IndicatorTimeSpan, |
| 92 | + indexes: List[UUID], |
| 93 | +) -> pd.DataFrame: |
| 94 | + """Fetch sessions given input level, timestamp and target index.""" |
| 95 | + query_template = Template(DURATION_FOR_LEVEL_QUERY_TEMPLATE) |
| 96 | + query_params = {"indexes": ",".join(f"'{i}'" for i in map(str, indexes))} |
| 97 | + query_params |= POWER_RANGE_CTE |
| 98 | + query_params |= get_num_for_level_query_params(level) |
| 99 | + query_params |= get_timespan_filter_query_params(timespan, session=True) |
| 100 | + return pd.read_sql_query(query_template.substitute(query_params), con=connection) |
| 101 | + |
| 102 | + |
| 103 | +@flow( |
| 104 | + task_runner=ThreadPoolTaskRunner(max_workers=settings.THREAD_POOL_MAX_WORKERS), |
| 105 | + flow_run_name="u6-{timespan.period.value}-{level:02d}-{timespan.start:%y-%m-%d}", |
| 106 | +) |
| 107 | +def u6_for_level( |
| 108 | + level: Level, |
| 109 | + timespan: IndicatorTimeSpan, |
| 110 | + chunk_size=settings.DEFAULT_CHUNK_SIZE, |
| 111 | +) -> pd.DataFrame: |
| 112 | + """Calculate u6 for a level and a timestamp.""" |
| 113 | + if level == Level.NATIONAL: |
| 114 | + return u6_national(timespan) |
| 115 | + engine = get_database_engine() |
| 116 | + with engine.connect() as connection: |
| 117 | + targets = get_targets_for_level(connection, level) |
| 118 | + ids = targets["id"] |
| 119 | + chunks = ( |
| 120 | + np.array_split(ids, int(len(ids) / chunk_size)) |
| 121 | + if len(ids) > chunk_size |
| 122 | + else [ids.to_numpy()] |
| 123 | + ) |
| 124 | + futures = [ |
| 125 | + get_values_for_targets.submit(connection, level, timespan, chunk) # type: ignore[call-overload] |
| 126 | + for chunk in chunks |
| 127 | + ] |
| 128 | + wait(futures) |
| 129 | + |
| 130 | + # Concatenate results and serialize indicators |
| 131 | + results = pd.concat([future.result() for future in futures], ignore_index=True) |
| 132 | + merged = targets.merge(results, how="left", left_on="id", right_on="level_id") |
| 133 | + |
| 134 | + # Build result DataFrame |
| 135 | + indicators = { |
| 136 | + "target": merged["code"], |
| 137 | + "value": merged["value"].fillna(0), |
| 138 | + "code": "u6", |
| 139 | + "level": level, |
| 140 | + "period": timespan.period, |
| 141 | + "timestamp": timespan.start.isoformat(), |
| 142 | + "category": merged["category"].astype("str"), |
| 143 | + "extras": None, |
| 144 | + } |
| 145 | + return pd.DataFrame(indicators) |
| 146 | + |
| 147 | + |
| 148 | +@flow( |
| 149 | + task_runner=ThreadPoolTaskRunner(max_workers=settings.THREAD_POOL_MAX_WORKERS), |
| 150 | + flow_run_name="u6-{timespan.period.value}-00-{timespan.start:%y-%m-%d}", |
| 151 | +) |
| 152 | +def u6_national(timespan: IndicatorTimeSpan) -> pd.DataFrame: |
| 153 | + """Calculate u6 at the national level.""" |
| 154 | + engine = get_database_engine() |
| 155 | + query_template = Template(QUERY_NATIONAL_TEMPLATE) |
| 156 | + query_params = get_timespan_filter_query_params(timespan, session=True) |
| 157 | + query_params |= POWER_RANGE_CTE |
| 158 | + with engine.connect() as connection: |
| 159 | + res = pd.read_sql_query(query_template.substitute(query_params), con=connection) |
| 160 | + indicators = { |
| 161 | + "target": None, |
| 162 | + "value": res["value"].fillna(0), |
| 163 | + "code": "u6", |
| 164 | + "level": Level.NATIONAL, |
| 165 | + "period": timespan.period, |
| 166 | + "timestamp": timespan.start.isoformat(), |
| 167 | + "category": res["category"].astype("str"), |
| 168 | + "extras": None, |
| 169 | + } |
| 170 | + return pd.DataFrame(indicators) |
| 171 | + |
| 172 | + |
| 173 | +@flow( |
| 174 | + task_runner=ThreadPoolTaskRunner(max_workers=settings.THREAD_POOL_MAX_WORKERS), |
| 175 | + flow_run_name="meta-u6-{timespan.period.value}", |
| 176 | +) |
| 177 | +def calculate( |
| 178 | + timespan: IndicatorTimeSpan, |
| 179 | + levels: List[Level], |
| 180 | + create_artifact: bool = False, |
| 181 | + chunk_size: int = 1000, |
| 182 | + format_pd: bool = False, |
| 183 | +) -> List[Indicator]: |
| 184 | + """Run all u6 subflows.""" |
| 185 | + subflows_results = [ |
| 186 | + u6_for_level(level, timespan, chunk_size=chunk_size) for level in levels |
| 187 | + ] |
| 188 | + indicators = pd.concat(subflows_results, ignore_index=True) |
| 189 | + description = f"u6 report at {timespan.start} (period: {timespan.period.value})" |
| 190 | + flow_name = runtime.flow_run.name |
| 191 | + return export_indic(indicators, create_artifact, flow_name, description, format_pd) |
0 commit comments