File size: 2,130 Bytes
32a04d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const statusEl = document.getElementById("status");
const errorEl = document.getElementById("error");
const emptyEl = document.getElementById("empty");
const tableWrapper = document.getElementById("tableWrapper");
const bodyEl = document.getElementById("logBody");
const reloadBtn = document.getElementById("reloadBtn");

function setStatus(msg) {
  statusEl.textContent = msg || "";
}

function showError(msg) {
  errorEl.textContent = msg;
  errorEl.hidden = !msg;
}

function renderRows(items) {
  bodyEl.innerHTML = "";
  for (const item of items) {
    const tr = document.createElement("tr");

    const tdTime = document.createElement("td");
    tdTime.textContent = item.created_at || "";
    tr.appendChild(tdTime);

    const tdText = document.createElement("td");
    tdText.textContent = item.anonymized_text || "";
    tr.appendChild(tdText);

    const tdLabel = document.createElement("td");
    tdLabel.textContent = item.label || "";
    tr.appendChild(tdLabel);

    const tdScore = document.createElement("td");
    tdScore.textContent =
      typeof item.score === "number" ? item.score.toFixed(4) : String(item.score ?? "");
    tr.appendChild(tdScore);

    bodyEl.appendChild(tr);
  }
}

async function loadLogs() {
  showError("");
  setStatus("Loading logs…");

  try {
    const res = await fetch("/logs?limit=100");
    const data = await res.json().catch(() => null);

    if (!res.ok) {
      throw new Error(
        `API error (${res.status} ${res.statusText})` +
          (data?.detail ? `: ${JSON.stringify(data.detail)}` : "")
      );
    }

    if (!Array.isArray(data) || data.length === 0) {
      emptyEl.hidden = false;
      tableWrapper.hidden = true;
      setStatus("No logs yet.");
      return;
    }

    renderRows(data);
    emptyEl.hidden = true;
    tableWrapper.hidden = false;
    setStatus(`Loaded ${data.length} records.`);
  } catch (e) {
    showError(e?.message || String(e));
    setStatus("");
  }
}

reloadBtn.addEventListener("click", loadLogs);

// auto-load on page open
loadLogs();