90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
"""Runs a domain's indicator pipeline with Hamilton.
|
|
|
|
The steps are the kit's own transformations as Orquestacion declares them
|
|
(`orquestacion.aristas.aristas_de`): each one names the table groups it reads
|
|
and writes, and moves data only through the database. Here every step becomes
|
|
a Hamilton node whose inputs are the steps that write what it reads, so
|
|
Hamilton orders and runs the graph; nothing of Orquestacion's own executor is
|
|
used. Steps left out of `solo` are given as overrides and don't run.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
import time
|
|
from dataclasses import dataclass
|
|
from types import ModuleType
|
|
from typing import Any, Callable, Sequence
|
|
|
|
from hamilton import ad_hoc_utils, driver
|
|
from orquestacion.arista import Arista, Contexto
|
|
from orquestacion.aristas import aristas_de
|
|
from orquestacion.repositorios import repositorios_de
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Paso:
|
|
"""What a step left: its short summary and how long it took. `None` summary: not run."""
|
|
|
|
arista: str
|
|
resumen: Any
|
|
segundos: float
|
|
|
|
|
|
def _node(arista: Arista, ctx: Contexto, inputs: Sequence[str]) -> Callable[..., Paso]:
|
|
def run(**_upstream: Paso) -> Paso:
|
|
started = time.monotonic()
|
|
return Paso(arista.nombre, arista.correr(ctx), time.monotonic() - started)
|
|
|
|
run.__name__ = arista.nombre
|
|
run.__doc__ = arista.descripcion
|
|
run.__annotations__ = {name: Paso for name in inputs} | {"return": Paso}
|
|
run.__signature__ = inspect.Signature(
|
|
[inspect.Parameter(name, inspect.Parameter.KEYWORD_ONLY, annotation=Paso) for name in inputs],
|
|
return_annotation=Paso,
|
|
)
|
|
return run
|
|
|
|
|
|
def modulo(ctx: Contexto, aristas: Sequence[Arista]) -> ModuleType:
|
|
"""The Hamilton module of a domain: one function per step."""
|
|
writer = {vertex: arista.nombre for arista in aristas for vertex in arista.escribe}
|
|
nodes = []
|
|
for arista in aristas:
|
|
inputs = sorted({writer[v] for v in arista.lee if v in writer and writer[v] != arista.nombre})
|
|
nodes.append(_node(arista, ctx, inputs))
|
|
return ad_hoc_utils.create_temporary_module(*nodes)
|
|
|
|
|
|
def correr(ctx: Contexto, *, solo: Sequence[str] | None = None) -> list[Paso]:
|
|
"""Runs the domain's steps (all, or only `solo`) in dependency order."""
|
|
if ctx.repos is None:
|
|
ctx = Contexto(ctx.proyecto, ctx.anio_inicio, ctx.anio_fin, ctx.corte, ctx.permitir_sin_definir,
|
|
ctx.parametros, repos=repositorios_de(ctx.proyecto))
|
|
aristas = aristas_de(ctx.proyecto)
|
|
nombres = [a.nombre for a in aristas]
|
|
wanted = [n for n in nombres if solo is None or n in solo]
|
|
skipped = {n: Paso(n, None, 0.0) for n in nombres if n not in wanted}
|
|
dr = driver.Builder().with_modules(modulo(ctx, aristas)).build()
|
|
results = dr.execute(wanted, overrides=skipped)
|
|
return [results[n] for n in _order(dr, wanted)]
|
|
|
|
|
|
def _order(dr: driver.Driver, wanted: Sequence[str]) -> list[str]:
|
|
"""Topological order of the requested steps, for reporting."""
|
|
graph = dr.graph
|
|
order: list[str] = []
|
|
seen: set[str] = set()
|
|
|
|
def visit(name: str) -> None:
|
|
if name in seen:
|
|
return
|
|
seen.add(name)
|
|
for dependency in graph.nodes[name].dependencies:
|
|
visit(dependency.name)
|
|
if name in wanted:
|
|
order.append(name)
|
|
|
|
for name in wanted:
|
|
visit(name)
|
|
return order
|