Skip to content

Commit 757e351

Browse files
committed
changes
1 parent fa3c6b8 commit 757e351

5 files changed

Lines changed: 96 additions & 43 deletions

File tree

trackio/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ def init(
260260
space_url = deploy.SPACE_HOST_URL.format(
261261
user_name=user_name, space_name=space_name
262262
)
263-
print(f"* View dashboard by going to: {space_url}")
263+
print(
264+
f"* View dashboard by going to: {deploy._BOLD_ORANGE}{space_url}{deploy._RESET}"
265+
)
264266
if utils.is_in_notebook() and embed:
265267
utils.embed_url_in_notebook(space_url)
266268
context_vars.current_project.set(project)

trackio/bucket_storage.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from trackio.sqlite_storage import SQLiteStorage
77
from trackio.utils import MEDIA_DIR, TRACKIO_DIR
88

9-
DB_EXT = ".db"
10-
119

1210
def create_bucket_if_not_exists(bucket_id: str, private: bool | None = None) -> None:
1311
huggingface_hub.create_bucket(bucket_id, private=private or False, exist_ok=True)
@@ -40,14 +38,3 @@ def upload_project_to_bucket(project: str, bucket_id: str) -> None:
4038
files_to_add.append((str(media_file), str(rel)))
4139

4240
huggingface_hub.batch_bucket_files(bucket_id, add=files_to_add)
43-
44-
45-
def upload_all_projects_to_bucket(bucket_id: str) -> None:
46-
if not TRACKIO_DIR.exists():
47-
return
48-
for db_file in TRACKIO_DIR.glob(f"*{DB_EXT}"):
49-
project = db_file.stem
50-
try:
51-
upload_project_to_bucket(project, bucket_id)
52-
except FileNotFoundError:
53-
continue

trackio/deploy.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
SPACE_HOST_URL = "https://{user_name}-{space_name}.hf.space/"
3939
SPACE_URL = "https://huggingface.co/spaces/{space_id}"
40+
_BOLD_ORANGE = "\033[1m\033[38;5;208m"
41+
_RESET = "\033[0m"
4042

4143

4244
def _readme_linked_hub_yaml(dataset_id: str | None) -> str:
@@ -317,7 +319,9 @@ def create_space_if_not_exists(
317319
)
318320
try:
319321
huggingface_hub.repo_info(space_id, repo_type="space")
320-
print(f"* Found existing space: {SPACE_URL.format(space_id=space_id)}")
322+
print(
323+
f"* Found existing space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
324+
)
321325
return
322326
except RepositoryNotFoundError:
323327
pass
@@ -328,7 +332,9 @@ def create_space_if_not_exists(
328332
else:
329333
raise ValueError(f"Failed to create Space: {e}")
330334

331-
print(f"* Creating new space: {SPACE_URL.format(space_id=space_id)}")
335+
print(
336+
f"* Creating new space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
337+
)
332338
deploy_as_space(
333339
space_id,
334340
space_storage,
@@ -540,7 +546,9 @@ def sync_incremental(
540546
)
541547

542548
SQLiteStorage.set_project_metadata(project, "space_id", space_id)
543-
print(f"* Synced successfully to space: {SPACE_URL.format(space_id=space_id)}")
549+
print(
550+
f"* Synced successfully to space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
551+
)
544552

545553

546554
def upload_dataset_for_static(
@@ -693,7 +701,9 @@ def deploy_as_static_space(
693701
),
694702
)
695703

696-
print(f"* Static Space deployed: {SPACE_URL.format(space_id=space_id)}")
704+
print(
705+
f"* Static Space deployed: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
706+
)
697707

698708

699709
def sync(

trackio/server.py

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""The main API layer for the Trackio UI."""
22

33
import base64
4+
import logging
45
import os
56
import re
67
import secrets
78
import shutil
9+
import sqlite3
10+
import threading
811
import time
12+
from collections import deque
913
from functools import lru_cache
1014
from typing import Any
1115
from urllib.parse import urlencode
@@ -23,6 +27,66 @@
2327

2428
HfApi = hf.HfApi()
2529

30+
logger = logging.getLogger("trackio")
31+
32+
_write_queue: deque[tuple[str, Any]] = deque()
33+
_flush_thread: threading.Thread | None = None
34+
_flush_lock = threading.Lock()
35+
_FLUSH_INTERVAL = 2.0
36+
_MAX_RETRIES = 30
37+
38+
39+
def _enqueue_write(kind: str, payload: Any) -> None:
40+
_write_queue.append((kind, payload))
41+
_ensure_flush_thread()
42+
43+
44+
def _ensure_flush_thread() -> None:
45+
global _flush_thread
46+
with _flush_lock:
47+
if _flush_thread is not None and _flush_thread.is_alive():
48+
return
49+
_flush_thread = threading.Thread(target=_flush_loop, daemon=True)
50+
_flush_thread.start()
51+
52+
53+
def _flush_loop() -> None:
54+
retries = 0
55+
while _write_queue and retries < _MAX_RETRIES:
56+
kind, payload = _write_queue[0]
57+
try:
58+
if kind == "bulk_log":
59+
SQLiteStorage.bulk_log(**payload)
60+
elif kind == "bulk_log_system":
61+
SQLiteStorage.bulk_log_system(**payload)
62+
elif kind == "bulk_alert":
63+
SQLiteStorage.bulk_alert(**payload)
64+
_write_queue.popleft()
65+
retries = 0
66+
except sqlite3.OperationalError as e:
67+
msg = str(e).lower()
68+
if "disk i/o error" in msg or "readonly" in msg:
69+
retries += 1
70+
logger.warning(
71+
"write queue: flush failed (%s), retry %d/%d",
72+
e,
73+
retries,
74+
_MAX_RETRIES,
75+
)
76+
time.sleep(min(_FLUSH_INTERVAL * retries, 15.0))
77+
else:
78+
logger.error("write queue: non-retryable error (%s), dropping entry", e)
79+
_write_queue.popleft()
80+
retries = 0
81+
if _write_queue:
82+
logger.error(
83+
"write queue: giving up after %d retries, %d entries dropped",
84+
_MAX_RETRIES,
85+
len(_write_queue),
86+
)
87+
_write_queue.clear()
88+
89+
2690
write_token = secrets.token_urlsafe(32)
2791

2892
OAUTH_CALLBACK_PATH = "/login/callback"
@@ -345,14 +409,18 @@ def bulk_log(
345409

346410
for (project, run), data in logs_by_run.items():
347411
has_log_ids = any(lid is not None for lid in data["log_ids"])
348-
SQLiteStorage.bulk_log(
412+
payload = dict(
349413
project=project,
350414
run=run,
351415
metrics_list=data["metrics"],
352416
steps=data["steps"],
353417
config=data["config"],
354418
log_ids=data["log_ids"] if has_log_ids else None,
355419
)
420+
try:
421+
SQLiteStorage.bulk_log(**payload)
422+
except sqlite3.OperationalError:
423+
_enqueue_write("bulk_log", payload)
356424

357425

358426
def bulk_log_system(
@@ -372,13 +440,17 @@ def bulk_log_system(
372440

373441
for (project, run), data in logs_by_run.items():
374442
has_log_ids = any(lid is not None for lid in data["log_ids"])
375-
SQLiteStorage.bulk_log_system(
443+
payload = dict(
376444
project=project,
377445
run=run,
378446
metrics_list=data["metrics"],
379447
timestamps=data["timestamps"],
380448
log_ids=data["log_ids"] if has_log_ids else None,
381449
)
450+
try:
451+
SQLiteStorage.bulk_log_system(**payload)
452+
except sqlite3.OperationalError:
453+
_enqueue_write("bulk_log_system", payload)
382454

383455

384456
def bulk_alert(
@@ -408,7 +480,7 @@ def bulk_alert(
408480

409481
for (project, run), data in alerts_by_run.items():
410482
has_alert_ids = any(aid is not None for aid in data["alert_ids"])
411-
SQLiteStorage.bulk_alert(
483+
payload = dict(
412484
project=project,
413485
run=run,
414486
titles=data["titles"],
@@ -418,6 +490,10 @@ def bulk_alert(
418490
timestamps=data["timestamps"],
419491
alert_ids=data["alert_ids"] if has_alert_ids else None,
420492
)
493+
try:
494+
SQLiteStorage.bulk_alert(**payload)
495+
except sqlite3.OperationalError:
496+
_enqueue_write("bulk_alert", payload)
421497

422498

423499
def get_alerts(

trackio/space_volumes.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,6 @@ def get_space_volumes(
4141
return list(vols) if vols else []
4242

4343

44-
def create_bucket(
45-
namespace: str,
46-
short_name: str,
47-
*,
48-
private: bool = True,
49-
token: str | None = None,
50-
) -> None:
51-
t = _token(token)
52-
base = ENDPOINT.rstrip("/")
53-
url = f"{base}/api/buckets/{namespace}/{short_name}"
54-
headers = {"Authorization": f"Bearer {t}"}
55-
r = get_session().post(
56-
url,
57-
headers=headers,
58-
json={"private": private},
59-
timeout=60.0,
60-
)
61-
if r.status_code == 409:
62-
return
63-
hf_raise_for_status(r)
64-
65-
6644
def set_space_volumes(
6745
space_id: str,
6846
volumes: list[dict[str, Any]],

0 commit comments

Comments
 (0)