{% autoescape off %}"""
The run_info component fetches metadata about the current pipeline execution
from kubeflow and passes it on to the user code step components.
"""
from typing import NamedTuple


def run_info_fn(
    run_id: str,
) -> NamedTuple("RunInfoOutput", [("run_info", str),]):
    from base64 import urlsafe_b64encode
    from collections import namedtuple
    import datetime
    import base64
    import dill
    import kfp

    client = kfp.Client(host="http://ml-pipeline:8888")
    run_info = client.get_run(run_id=run_id)

    run_info_dict = {
        "run_id": run_info.run.id,
        "name": run_info.run.name,
        "created_at": run_info.run.created_at.isoformat(),
        "pipeline_id": run_info.run.pipeline_spec.pipeline_id,
    }

    # Track kubernetes resources associated wth the run.
    for r in run_info.run.resource_references:
        run_info_dict[f"{r.key.type.lower()}_id"] = r.key.id

    # Base64-encoded as value is visible in kubeflow ui.
    output = urlsafe_b64encode(dill.dumps(run_info_dict))

    return namedtuple("RunInfoOutput", ["run_info"])(
        str(output, encoding="ascii")
    )
{% endautoescape %}
