diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index beca4d1b2..3dde36245 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -2141,6 +2141,29 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .proto-menu .pm-words { display: grid; gap: 1px; min-width: 0; } .proto-menu .pm-name { font-size: 12.5px; font-weight: 500; line-height: 16px; } .proto-menu .pm-note { font-size: 11px; color: var(--muted); line-height: 15px; } +/* Usage's days of the reader's own (#1492): the periods' last opt, and the + menu it opens, a few spans and a month of days */ +.range-opt { display: inline-flex; align-items: center; gap: 5px; } +.range-opt svg { flex: none; } +.pop.proto-menu.range-menu { width: 252px; max-height: none; display: block; padding: 4px 6px 8px; } +.range-menu .rm-quick { display: flex; flex-wrap: wrap; gap: 4px; padding: 0 2px 6px; border-bottom: 1px solid var(--line-2); } +.range-menu .rm-span { height: 22px; padding: 0 8px; border-radius: 6px; background: var(--pill); color: var(--fg-2); font-size: 11.5px; } +.range-menu .rm-span:hover, .range-menu .rm-span:focus-visible { color: var(--fg); } +.range-menu .rm-span.on { color: var(--fg); background: color-mix(in srgb, var(--accent) 18%, var(--pill)); } +.range-menu .rm-nav { display: flex; align-items: center; justify-content: space-between; padding: 6px 2px 4px; } +.range-menu .rm-month { font-size: 12.5px; font-weight: 600; } +.range-menu .rm-step { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; color: var(--muted); } +.range-menu .rm-step:hover:not(:disabled) { background: var(--pill); color: var(--fg); } +.range-menu .rm-step:disabled { opacity: .35; } +.range-menu .rm-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px 0; } +.range-menu .rm-wd { height: 20px; display: grid; place-items: center; font-size: 10.5px; color: var(--faint); } +.range-menu .rm-day { height: 28px; font-size: 12px; font-variant-numeric: tabular-nums; color: var(--fg); border-radius: 7px; outline: none; } +.range-menu .rm-day:hover:not(:disabled), .range-menu .rm-day:focus-visible { background: var(--pill); } +.range-menu .rm-day.today { font-weight: 650; color: var(--accent); } +.range-menu .rm-day.in { background: color-mix(in srgb, var(--accent) 14%, transparent); border-radius: 0; } +.range-menu .rm-day.end { background: var(--accent); color: var(--accent-fg, #fff); } +.range-menu .rm-day:disabled { color: var(--faint); opacity: .5; } +@media (max-width: 640px) { .range-opt:not(.on) > span { display: none; } } /* a subscription's allowance, under its name */ .accts .acc.with-aq { flex-wrap: wrap; row-gap: 3px; padding-top: 7px; padding-bottom: 8px; } .acc .aq { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; padding-left: 25px; font-size: 10.5px; color: var(--muted); min-height: 14px; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 958b20d39..90fab511e 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -13044,7 +13044,7 @@ async function loadUsage(asked) { renderUsageLoading(); if (!asked) loadQuotas(); const p = period, read = ++usageRead; - const next = await api("usage?period=" + p); + const next = await api("usage?period=" + encodeURIComponent(p)); if (read !== usageRead || view !== "usage" || usageTab !== "usage" || period !== p) return; usage = next; renderUsage(); @@ -13086,12 +13086,169 @@ function renderPeriod(loading) { for (const [id, name] of PERIODS) { const b = el("button", "opt" + (id === period ? " on" : ""), t(name)); b.disabled = !!loading; - b.onclick = () => { for (const x of seg.querySelectorAll(".opt")) x.classList.toggle("on", x === b); slide(seg, "period"); period = id; ledDay = ""; ledOffset = 0; loadUsage().catch((e) => status(e.message, "err")); }; + b.onclick = () => { for (const x of seg.querySelectorAll(".opt")) x.classList.toggle("on", x === b); slide(seg, "period"); pickPeriod(id); }; seg.append(b); } + // days of the reader's own (#1492): the last opt names them once picked, + // and opens the range menu to pick them, or others + const range = periodDays(period); + const b = el("button", "opt range-opt" + (range ? " on" : "")); + b.type = "button"; + b.disabled = !!loading; + b.setAttribute("aria-haspopup", "dialog"); + b.setAttribute("aria-expanded", "false"); + b.setAttribute("aria-label", range ? t("Days picked: {days}", { days: rangeName(range) }) : t("Pick days")); + b.append(svg("M3 4.5h10v8.5H3zM3 7h10M5.5 3v3M10.5 3v3", 12, 1.4), el("span", "", range ? rangeName(range) : t("Custom"))); + if (range) b.title = t("Days picked: {days}", { days: rangeName(range) }); + b.onclick = () => { + if (b.classList.contains("open")) return closeProtoMenu(); + openRangeMenu(b, range, (from, to) => { + for (const x of seg.querySelectorAll(".opt")) x.classList.toggle("on", x === b); + b.lastChild.textContent = rangeName({ from, to }); + b.title = t("Days picked: {days}", { days: rangeName({ from, to }) }); + b.setAttribute("aria-label", b.title); + slide(seg, "period"); + pickPeriod(from + ".." + to); + }); + }; + seg.append(b); slide(seg, "period"); } +function pickPeriod(id) { + period = id; ledDay = ""; ledOffset = 0; + loadUsage().catch((e) => status(e.message, "err")); +} + +// A period of picked days is "2026-10-01..2026-10-07", both included, in +// this computer's time zone, as the backend reads it (usage.RangeOf). +// periodDays is its two days, or null for a preset. +function periodDays(p) { + const m = /^(\d{4}-\d{2}-\d{2})\.\.(\d{4}-\d{2}-\d{2})$/.exec(p || ""); + return m ? { from: m[1], to: m[2] } : null; +} +const dayKeyOf = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; +const dayOfKey = (k) => new Date(k + "T12:00:00"); +// rangeName says picked days short: "Oct 1 – Oct 7", the year only when it +// isn't this one, one day alone as itself +function rangeName({ from, to }) { + const year = new Date().getFullYear(); + const name = (k) => dayOfKey(k).toLocaleDateString(intlLang() || "en", { month: "short", day: "numeric", ...(dayOfKey(k).getFullYear() !== year ? { year: "numeric" } : {}) }); + return from === to ? name(from) : name(from) + " – " + name(to); +} +// noCallsIn is what an empty page says of the period it is on +function noCallsIn(p) { + if (periodDays(p)) return "No calls on the days picked."; + return { today: "No calls today.", "7d": "No calls in the last 7 days.", "30d": "No calls in the last 30 days.", all: "No calls yet." }[p] || "No calls yet."; +} + +// openRangeMenu is the app's own menu for picking days (#1492): a few +// spans a click away, and a month to pick the first day on, then the last. +// Days after today can't be picked. choose gets the two days, in order. +function openRangeMenu(anchor, current, choose) { + closeProtoMenu(); + const today = dayKeyOf(new Date()); + let start = null; // the first day clicked, waiting for the last + let shown = dayOfKey(current?.to || today); + shown = new Date(shown.getFullYear(), shown.getMonth(), 1, 12); + const box = el("div", "pop proto-menu range-menu"); + box.setAttribute("role", "dialog"); + box.setAttribute("aria-label", t("Pick days")); + const head = el("div", "pm-head"); + const quick = el("div", "rm-quick"); + const span = (from, to) => ({ from: dayKeyOf(from), to: dayKeyOf(to) }); + const now = new Date(); + now.setHours(12, 0, 0, 0); + const back = (n) => { const d = new Date(now); d.setDate(d.getDate() - n); return d; }; + const quicks = [ + ["Yesterday", span(back(1), back(1))], + ["This month", span(new Date(now.getFullYear(), now.getMonth(), 1, 12), now)], + ["Last month", span(new Date(now.getFullYear(), now.getMonth() - 1, 1, 12), new Date(now.getFullYear(), now.getMonth(), 0, 12))], + ["90 days", span(back(89), now)], + ]; + for (const [name, r] of quicks) { + const b = el("button", "rm-span" + (current && current.from === r.from && current.to === r.to ? " on" : ""), t(name)); + b.type = "button"; + b.title = rangeName(r); + b.onclick = (e) => { e.stopPropagation(); closeProtoMenu(); choose(r.from, r.to); }; + quick.append(b); + } + const nav = el("div", "rm-nav"); + const prev = el("button", "rm-step"), next = el("button", "rm-step"), month = el("span", "rm-month"); + prev.type = next.type = "button"; + prev.setAttribute("aria-label", t("Previous month")); + next.setAttribute("aria-label", t("Next month")); + prev.append(svg("M10 4 6 8l4 4", 12, 1.6)); + next.append(svg("m6 4 4 4-4 4", 12, 1.6)); + nav.append(prev, month, next); + const grid = el("div", "rm-grid"); + grid.setAttribute("role", "grid"); + box.append(head, quick, nav, grid); + const draw = (focusDay) => { + head.textContent = start ? t("Now the last day") : t("Pick the first day, then the last"); + month.textContent = shown.toLocaleDateString(intlLang() || "en", { year: "numeric", month: "long" }); + next.disabled = dayKeyOf(new Date(shown.getFullYear(), shown.getMonth() + 1, 1, 12)) > today; + const cells = []; + // the weekdays, Monday first, as the app's weeks are + for (let i = 0; i < 7; i++) cells.push(el("span", "rm-wd", new Date(2024, 0, 1 + i, 12).toLocaleDateString(intlLang() || "en", { weekday: "narrow" }))); + const first = new Date(shown); + const lead = (first.getDay() + 6) % 7; + for (let i = 0; i < lead; i++) cells.push(el("span", "rm-pad")); + const lo = start || current?.from, hi = start ? null : current?.to; + for (let d = new Date(first); d.getMonth() === shown.getMonth(); d.setDate(d.getDate() + 1)) { + const k = dayKeyOf(d); + const b = el("button", "rm-day", String(d.getDate())); + b.type = "button"; + b.dataset.day = k; + b.setAttribute("aria-label", d.toLocaleDateString(intlLang() || "en", { year: "numeric", month: "long", day: "numeric" })); + if (k === today) b.classList.add("today"); + if (lo && (k === lo || k === hi)) b.classList.add("end"); + if (lo && hi && k > lo && k < hi) b.classList.add("in"); + b.disabled = k > today; + b.onclick = (e) => { + e.stopPropagation(); + if (!start) { start = k; draw(k); return; } + const [from, to] = start <= k ? [start, k] : [k, start]; + closeProtoMenu(); + choose(from, to); + }; + cells.push(b); + } + grid.replaceChildren(...cells); + (focusDay && grid.querySelector(`[data-day="${focusDay}"]`))?.focus({ preventScroll: true }); + }; + const turn = (by) => (e) => { e.stopPropagation(); shown = new Date(shown.getFullYear(), shown.getMonth() + by, 1, 12); draw(); }; + prev.onclick = turn(-1); + next.onclick = turn(1); + draw(); + document.body.append(box); + placeMenu(box, anchor); + anchor.classList.add("open"); + anchor.setAttribute("aria-expanded", "true"); + const outside = (e) => { if (!box.contains(e.target) && !anchor.contains(e.target)) closeProtoMenu(); }; + const scroll = (e) => { if (!box.contains(e.target)) closeProtoMenu(); }; + // Escape closes; arrows walk the days, a week up or down + const keys = (e) => { + if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); closeProtoMenu(); anchor.focus({ preventScroll: true }); return; } + const at = document.activeElement?.dataset?.day; + const by = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -7, ArrowDown: 7 }[e.key]; + if (!at || !by || !box.contains(document.activeElement)) return; + e.preventDefault(); e.stopPropagation(); + const d = dayOfKey(at); + d.setDate(d.getDate() + by); + const k = dayKeyOf(d); + if (k > today) return; + if (d.getMonth() !== shown.getMonth() || d.getFullYear() !== shown.getFullYear()) shown = new Date(d.getFullYear(), d.getMonth(), 1, 12); + draw(k); + }; + document.addEventListener("mousedown", outside, true); + document.addEventListener("keydown", keys, true); + document.addEventListener("scroll", scroll, true); + addEventListener("resize", closeProtoMenu); + protoMenu = { box, anchor, outside, keys, scroll, done: null }; + (grid.querySelector(".rm-day.end") || grid.querySelector(".rm-day.today") || quick.firstChild).focus({ preventScroll: true }); +} + function renderUsageLoading() { const view = $("#view-usage"); view.classList.add("loading"); @@ -13514,12 +13671,16 @@ function creditDays(sub) { const key = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; const today = new Date(); today.setHours(12, 0, 0, 0); - // the period's days, the first from when counting began for All - let n = { today: 1, "7d": 7, "30d": 30 }[period]; - if (!n) n = Math.min(120, Math.max(1, Math.round((today - new Date(since + "T12:00:00")) / 864e5) + 1)); + // the period's days, the first from when counting began for All, a + // picked range's own (#1492), its last 120 at most + const range = periodDays(period); + const last = range ? dayOfKey(range.to) : today; + let n = range ? Math.round((last - dayOfKey(range.from)) / 864e5) + 1 : { today: 1, "7d": 7, "30d": 30 }[period]; + if (!n) n = Math.round((today - new Date(since + "T12:00:00")) / 864e5) + 1; + n = Math.min(120, Math.max(1, n)); const list = []; for (let i = n - 1; i >= 0; i--) { - const d = new Date(today); + const d = new Date(last); d.setDate(d.getDate() - i); list.push({ day: key(d), date: d, used: by.get(key(d)) || 0, known: key(d) >= since }); } @@ -13529,7 +13690,7 @@ function creditDays(sub) { const head = el("div", "cd-head"); const name = el("span", "", t("Credits used per day")); name.title = t("Counted from magpie's readings of the vendor's meter, since {date}: what is used while magpie isn't reading it is counted on the day it next does", { date: dayName(new Date(since + "T12:00:00")) }); - head.append(name, el("b", "", n === 1 ? t("{n} today", { n: num(sum) }) : t("{n} in {days} days", { n: num(sum), days: n }))); + head.append(name, el("b", "", n > 1 ? t("{n} in {days} days", { n: num(sum), days: n }) : range && range.to !== key(today) ? t("{n} on {date}", { n: num(sum), date: dayName(last) }) : t("{n} today", { n: num(sum) }))); box.append(head); if (n > 1) { const bars = el("div", "cd-bars"); @@ -15214,7 +15375,7 @@ function renderUsage() { $("#usageAccountsHead").hidden = $("#usageAccounts").hidden = empty || !u.accounts?.length; if (empty) { stats.classList.add("empty"); - const none = { today: "No calls today.", "7d": "No calls in the last 7 days.", "30d": "No calls in the last 30 days.", all: "No calls yet." }[period]; + const none = noCallsIn(period); stats.append(el("div", "none", t(none) + " " + t("Point an agent at a catalog model and use it; every call through the gateway is counted here."))); $("#usageNote").textContent = ""; return; @@ -16468,7 +16629,7 @@ function renderLedger() { if (!l.total) { wrap.classList.add("none"); const filtered = ledPurpose || ledDay || ledRoute || ledAgent || ledProvider || ledAccount || ledComputer || ledCallerKey || ledModel || ledVia || ledFailed || ledQuery.trim(); - const none = { today: "No calls today.", "7d": "No calls in the last 7 days.", "30d": "No calls in the last 30 days.", all: "No calls yet." }[period]; + const none = noCallsIn(period); wrap.replaceChildren(el("div", "led-none", filtered ? t("No requests match these filters.") : t(none))); ledFit(); ledHScroll(); @@ -21292,7 +21453,7 @@ function refreshUsage(now = false) { else if (usage && performance.now() - quotasAsked > 60e3) { quotasAsked = performance.now(); loadQuotas(); } if (usage) { const p = period, read = ++usageRead; - const u = await api("usage?period=" + p); + const u = await api("usage?period=" + encodeURIComponent(p)); if (read === usageRead && view === "usage" && usageTab === "usage" && p === period) { if (JSON.stringify(u) !== JSON.stringify(usage)) { usage = u; renderUsage(); } // this read claimed the newest one: a pick it superseded had put the diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 46b942977..34c2af8a6 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -4,6 +4,17 @@ // works. {name} slots are filled by t(str, vars). const I18N = { zh: { + "Yesterday": "昨天", + "This month": "本月", + "Last month": "上个月", + "Pick days": "选择日期", + "Previous month": "上个月", + "Next month": "下个月", + "Days picked: {days}": "所选日期:{days}", + "Now the last day": "再选最后一天", + "Pick the first day, then the last": "先选第一天,再选最后一天", + "No calls on the days picked.": "所选日期内没有调用。", + "{n} on {date}": "{date} {n}", "You set {value}; the model's {window} comes first": "你设置的是 {value},但模型的 {window} 优先", "Auto-compact": "自动压缩", "Prompt cache · main conversation": "提示缓存 · 主对话", @@ -4005,6 +4016,17 @@ const I18N = { "Video · 7 days": "视频 · 7 天", }, "zh-TW": { + "Yesterday": "昨天", + "This month": "本月", + "Last month": "上個月", + "Pick days": "選擇日期", + "Previous month": "上個月", + "Next month": "下個月", + "Days picked: {days}": "所選日期:{days}", + "Now the last day": "再選最後一天", + "Pick the first day, then the last": "先選第一天,再選最後一天", + "No calls on the days picked.": "所選日期內沒有呼叫。", + "{n} on {date}": "{date} {n}", "You set {value}; the model's {window} comes first": "你設定的是 {value},但模型的 {window} 優先", "Auto-compact": "自動壓縮", "Prompt cache · main conversation": "提示快取 · 主對話", @@ -7989,6 +8011,17 @@ const I18N = { "Video · 7 days": "影片 · 7 天", }, ja: { + "Yesterday": "昨日", + "This month": "今月", + "Last month": "先月", + "Pick days": "日付を選ぶ", + "Previous month": "前の月", + "Next month": "次の月", + "Days picked: {days}": "選んだ日付:{days}", + "Now the last day": "次に最終日を選択", + "Pick the first day, then the last": "最初の日、次に最終日を選択", + "No calls on the days picked.": "選んだ日付の呼び出しはありません。", + "{n} on {date}": "{date} {n}", "You set {value}; the model's {window} comes first": "設定は {value} ですが、モデルの {window} が優先されます", "Auto-compact": "自動コンパクト", "Prompt cache · main conversation": "プロンプトキャッシュ · メイン会話", @@ -11975,6 +12008,17 @@ const I18N = { "Main navigation": "メインナビゲーション", }, de: { + "Yesterday": "Gestern", + "This month": "Dieser Monat", + "Last month": "Letzter Monat", + "Pick days": "Tage wählen", + "Previous month": "Vorheriger Monat", + "Next month": "Nächster Monat", + "Days picked: {days}": "Gewählte Tage: {days}", + "Now the last day": "Jetzt den letzten Tag", + "Pick the first day, then the last": "Erst den ersten Tag, dann den letzten wählen", + "No calls on the days picked.": "An den gewählten Tagen keine Aufrufe.", + "{n} on {date}": "{n} am {date}", "You set {value}; the model's {window} comes first": "Du hast {value} gesetzt; das Fenster des Modells ({window}) hat Vorrang", "Auto-compact": "Auto-Kompaktierung", "Prompt cache · main conversation": "Prompt-Cache · Hauptunterhaltung", diff --git a/internal/gui/tests/usage-range.test.cjs b/internal/gui/tests/usage-range.test.cjs new file mode 100644 index 000000000..26741a37d --- /dev/null +++ b/internal/gui/tests/usage-range.test.cjs @@ -0,0 +1,143 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Usage's days of the reader's own (#1492, nianlee-official): after Today, +// 7 days, 30 days and All, a Custom opt opens the app's own menu (never a +// native