File size: 6,639 Bytes
52db224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
r"""Supabase storage mirror for cdpr experiment recordings.

The adapter is deliberately thin: local disk (via
:mod:`cdpr.recording`) remains the source of truth, and Supabase only
holds a queryable copy of the experiment metadata + summary metrics.
Heavy time-series CSVs stay on disk; the cloud row carries the
``local_path`` so the operator can fetch the bundle by ID later.

Three failure modes are explicitly *non-fatal*:

* the ``supabase`` Python package is not installed,
* ``SUPABASE_URL`` / ``SUPABASE_SERVICE_ROLE_KEY`` env vars are missing,
* the HTTP call to Supabase fails (network blip, RLS policy issue).

In all three cases the mirror is skipped silently and a diagnostic is
attached to the :class:`SupabaseMirror.last_error` field. The recorder
never re-raises a Supabase exception --- it must not interrupt the
scientific run.
"""

from __future__ import annotations

import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:                                           # pragma: no cover
    from cdpr.recording.recorder import ExperimentLog


# ---------------------------------------------------------------------------
# Capability detection
# ---------------------------------------------------------------------------

def supabase_available() -> bool:
    """True iff the supabase package is importable AND env vars are set."""
    if not (os.getenv("SUPABASE_URL") and os.getenv("SUPABASE_SERVICE_ROLE_KEY")):
        return False
    try:
        import supabase  # noqa: F401
    except ImportError:
        return False
    return True


# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

@dataclass(slots=True, frozen=True)
class SupabaseConfig:
    """Connection settings, normally pulled from env."""

    url: str
    service_role_key: str
    schema: str = "public"

    @classmethod
    def from_env(cls) -> "SupabaseConfig | None":
        url = os.getenv("SUPABASE_URL")
        key = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
        if not url or not key:
            return None
        return cls(url=url, service_role_key=key)


# ---------------------------------------------------------------------------
# Mirror
# ---------------------------------------------------------------------------

@dataclass(slots=True)
class SupabaseMirror:
    """Write-side adapter: takes a local experiment and copies its metadata
    into the Supabase experiments table.

    Stateless across writes; safe to construct once per process and reuse.
    The actual ``supabase-py`` client is lazy-instantiated on first use so
    importing this module costs nothing when the env vars are absent.
    """

    config: SupabaseConfig
    _client: Any = None
    last_error: str | None = None

    def _connect(self) -> Any:
        if self._client is None:
            from supabase import create_client                # type: ignore[import-not-found]
            self._client = create_client(self.config.url, self.config.service_role_key)
        return self._client

    def push_experiment(
        self,
        log: "ExperimentLog",
        *,
        metrics: dict[str, float] | None = None,
        extra_tags: dict[str, Any] | None = None,
    ) -> str | None:
        """Push one experiment row + optional metric rows. Returns the new
        row's UUID on success, ``None`` on any failure (with the error
        captured in :attr:`last_error`)."""
        try:
            import json
            metadata_path = log.metadata_path
            manifest_path = log.manifest_path
            metadata = json.loads(metadata_path.read_text())
            manifest = json.loads(manifest_path.read_text())

            payload = {
                "name": metadata.get("title", "unknown"),
                "scenario_hash": str(metadata.get("experiment_id", "")),
                "config_hash": manifest.get("experiment_id_short", manifest.get("git_revision", "")),
                "cable_mode": (metadata.get("tags") or {}).get("cable_mode", "none"),
                "backend": (metadata.get("tags") or {}).get("backend", "cdpr"),
                "seed": manifest.get("seed"),
                "cdpr_version": manifest.get("cdpr_version"),
                "git_revision": manifest.get("git_revision"),
                "notes": (metadata.get("simulation") or {}).get("notes"),
                "tags": {**(metadata.get("tags") or {}), **(extra_tags or {})},
                "local_path": str(Path(log.root).resolve()),
            }

            client = self._connect()
            res = (
                client.schema(self.config.schema)
                .table("experiments")
                .insert(payload)
                .execute()
            )
            if not res.data:
                self.last_error = "Supabase returned no row on insert."
                return None
            experiment_id = res.data[0]["id"]

            if metrics:
                metric_rows = [
                    {"experiment_id": experiment_id, "metric": k, "value": float(v)}
                    for k, v in metrics.items()
                ]
                client.schema(self.config.schema).table(
                    "experiment_metrics"
                ).insert(metric_rows).execute()

            self.last_error = None
            return experiment_id

        except Exception as exc:                              # pragma: no cover - network path
            self.last_error = f"{type(exc).__name__}: {exc}"
            return None


# ---------------------------------------------------------------------------
# Convenience entry point
# ---------------------------------------------------------------------------

def mirror_experiment(
    log: "ExperimentLog",
    *,
    metrics: dict[str, float] | None = None,
    extra_tags: dict[str, Any] | None = None,
) -> str | None:
    """One-shot helper: read env, push, return new UUID (or None on failure).

    The recorder calls this after a successful local write when the user
    asks for cloud mirroring. A return of ``None`` is *not* an error
    condition for the scientific run --- the local artefact bundle is
    still intact.
    """
    if not supabase_available():
        return None
    cfg = SupabaseConfig.from_env()
    if cfg is None:                                           # pragma: no cover - guarded above
        return None
    mirror = SupabaseMirror(config=cfg)
    return mirror.push_experiment(log, metrics=metrics, extra_tags=extra_tags)