|
| 1 | +# Copyright 2026 Kevin (AluminatiAI) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# AluminatiAI — https://github.com/AgentMulder404/AluminatAI |
| 16 | +""" |
| 17 | +TagClient: polls GET /api/v1/tag and caches active job registrations. |
| 18 | +
|
| 19 | +Usage: |
| 20 | + client = TagClient(api_endpoint, api_key, poll_interval=30) |
| 21 | + client.start() # starts background polling thread |
| 22 | +
|
| 23 | + match = client.match(gpu_index=0, pid=12345, ts=datetime.now(UTC)) |
| 24 | + if match: |
| 25 | + # match.job_id, match.team_id, match.model_tag |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import logging |
| 31 | +import threading |
| 32 | +import time |
| 33 | +from dataclasses import dataclass |
| 34 | +from datetime import datetime, timezone, timedelta |
| 35 | +from typing import Optional |
| 36 | + |
| 37 | +import requests |
| 38 | + |
| 39 | +logger = logging.getLogger(__name__) |
| 40 | + |
| 41 | +# ── Data model ───────────────────────────────────────────────────────────────── |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class TagRecord: |
| 46 | + id: str |
| 47 | + job_id: str |
| 48 | + team_id: Optional[str] |
| 49 | + model_tag: Optional[str] |
| 50 | + gpu_indices: Optional[list[int]] # None = all GPUs |
| 51 | + start_time: datetime |
| 52 | + end_time: Optional[datetime] # None = open-ended |
| 53 | + pid: Optional[int] |
| 54 | + |
| 55 | + |
| 56 | +# ── Client ───────────────────────────────────────────────────────────────────── |
| 57 | + |
| 58 | + |
| 59 | +class TagClient: |
| 60 | + """ |
| 61 | + Background-polling client for the /api/v1/tag REST endpoint. |
| 62 | +
|
| 63 | + Thread-safe: `match()` can be called from any thread. |
| 64 | + """ |
| 65 | + |
| 66 | + def __init__( |
| 67 | + self, |
| 68 | + api_endpoint: str, # e.g. "https://aluminatiai.com/v1/metrics/ingest" |
| 69 | + api_key: str, |
| 70 | + poll_interval: int = 30, # seconds between polls |
| 71 | + ): |
| 72 | + # Derive base URL from the ingest endpoint (strip trailing path) |
| 73 | + # Expected: https://host/v1/metrics/ingest → https://host |
| 74 | + parts = api_endpoint.split("/v1/") |
| 75 | + self._base_url = parts[0].rstrip("/") |
| 76 | + self._tag_url = f"{self._base_url}/api/v1/tag" |
| 77 | + |
| 78 | + self._api_key = api_key |
| 79 | + self._poll_interval = poll_interval |
| 80 | + |
| 81 | + self._lock = threading.Lock() |
| 82 | + self._tags: list[TagRecord] = [] |
| 83 | + self._last_poll: Optional[datetime] = None |
| 84 | + self._thread: Optional[threading.Thread] = None |
| 85 | + self._stop_event = threading.Event() |
| 86 | + |
| 87 | + # ── Lifecycle ────────────────────────────────────────────────────────────── |
| 88 | + |
| 89 | + def start(self) -> None: |
| 90 | + """Start the background polling thread (idempotent).""" |
| 91 | + if self._thread and self._thread.is_alive(): |
| 92 | + return |
| 93 | + self._stop_event.clear() |
| 94 | + self._thread = threading.Thread(target=self._poll_loop, daemon=True, name="tag-client") |
| 95 | + self._thread.start() |
| 96 | + logger.info("TagClient started — polling %s every %ds", self._tag_url, self._poll_interval) |
| 97 | + |
| 98 | + def stop(self) -> None: |
| 99 | + """Signal the polling thread to stop.""" |
| 100 | + self._stop_event.set() |
| 101 | + |
| 102 | + # ── Matching ─────────────────────────────────────────────────────────────── |
| 103 | + |
| 104 | + def match( |
| 105 | + self, |
| 106 | + gpu_index: int, |
| 107 | + pid: Optional[int], |
| 108 | + ts: datetime, |
| 109 | + ) -> Optional[TagRecord]: |
| 110 | + """ |
| 111 | + Return the highest-priority tag that covers this (gpu_index, pid, timestamp). |
| 112 | +
|
| 113 | + Priority: most-recently-started tag wins when multiple match. |
| 114 | + Returns None if no tag matches. |
| 115 | + """ |
| 116 | + if ts.tzinfo is None: |
| 117 | + ts = ts.replace(tzinfo=timezone.utc) |
| 118 | + |
| 119 | + with self._lock: |
| 120 | + candidates: list[TagRecord] = [] |
| 121 | + for tag in self._tags: |
| 122 | + # Time window: start_time ≤ ts ≤ end_time (or open-ended) |
| 123 | + if ts < tag.start_time: |
| 124 | + continue |
| 125 | + if tag.end_time is not None and ts > tag.end_time: |
| 126 | + continue |
| 127 | + # GPU index filter (None = all GPUs) |
| 128 | + if tag.gpu_indices is not None and gpu_index not in tag.gpu_indices: |
| 129 | + continue |
| 130 | + # PID filter (None = any PID) |
| 131 | + if tag.pid is not None and pid != tag.pid: |
| 132 | + continue |
| 133 | + candidates.append(tag) |
| 134 | + |
| 135 | + if not candidates: |
| 136 | + return None |
| 137 | + |
| 138 | + # Most recently started tag wins |
| 139 | + return max(candidates, key=lambda t: t.start_time) |
| 140 | + |
| 141 | + # ── Internal polling ─────────────────────────────────────────────────────── |
| 142 | + |
| 143 | + def _poll_loop(self) -> None: |
| 144 | + while not self._stop_event.is_set(): |
| 145 | + try: |
| 146 | + self._fetch() |
| 147 | + except Exception as exc: |
| 148 | + logger.warning("TagClient poll error: %s", exc) |
| 149 | + self._stop_event.wait(self._poll_interval) |
| 150 | + |
| 151 | + def _fetch(self) -> None: |
| 152 | + # Ask for tags from the last 25 hours (slightly more than a day to catch |
| 153 | + # long-running jobs that started before the last poll window). |
| 154 | + since = (datetime.now(timezone.utc) - timedelta(hours=25)).isoformat() |
| 155 | + headers = {"X-API-Key": self._api_key} |
| 156 | + |
| 157 | + resp = requests.get( |
| 158 | + self._tag_url, |
| 159 | + params={"since": since}, |
| 160 | + headers=headers, |
| 161 | + timeout=10, |
| 162 | + ) |
| 163 | + resp.raise_for_status() |
| 164 | + |
| 165 | + raw_tags = resp.json().get("tags", []) |
| 166 | + parsed: list[TagRecord] = [] |
| 167 | + for t in raw_tags: |
| 168 | + try: |
| 169 | + parsed.append(TagRecord( |
| 170 | + id=t["id"], |
| 171 | + job_id=t["job_id"], |
| 172 | + team_id=t.get("team_id"), |
| 173 | + model_tag=t.get("model_tag"), |
| 174 | + gpu_indices=t.get("gpu_indices"), |
| 175 | + start_time=_parse_dt(t["start_time"]), |
| 176 | + end_time=_parse_dt(t["end_time"]) if t.get("end_time") else None, |
| 177 | + pid=t.get("pid"), |
| 178 | + )) |
| 179 | + except Exception as exc: |
| 180 | + logger.debug("TagClient: skipping malformed tag %s: %s", t.get("id"), exc) |
| 181 | + |
| 182 | + with self._lock: |
| 183 | + self._tags = parsed |
| 184 | + self._last_poll = datetime.now(timezone.utc) |
| 185 | + |
| 186 | + logger.debug("TagClient: fetched %d tag(s)", len(parsed)) |
| 187 | + |
| 188 | + @property |
| 189 | + def tag_count(self) -> int: |
| 190 | + with self._lock: |
| 191 | + return len(self._tags) |
| 192 | + |
| 193 | + |
| 194 | +def _parse_dt(value: str) -> datetime: |
| 195 | + """Parse ISO 8601 timestamp to an aware datetime (UTC).""" |
| 196 | + dt = datetime.fromisoformat(value.replace("Z", "+00:00")) |
| 197 | + if dt.tzinfo is None: |
| 198 | + dt = dt.replace(tzinfo=timezone.utc) |
| 199 | + return dt |
0 commit comments