Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/admin-storage-panel-window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

fix(admin): show storage count window and 24h default label (#1322)
106 changes: 106 additions & 0 deletions crates/nostream-admin/tests/console_parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,109 @@ fn a_missing_console_file_is_a_failure() {
assert!(!assets_dir().join(missing).exists());
let _ = ADMIN_BASE;
}

/// The storage-count panel renders the reply's `since`/`until` window beside the count and labels
/// defaulted requests (`last 24 h`), rather than presenting a 24 h slice as the store total (#1322).
#[test]
fn the_storage_panel_renders_the_window_it_reports() {
let html = std::fs::read_to_string(dashboard_html()).expect("dashboard.html");
let start = html
.find(">Storage counts</h2>")
.expect("Storage counts section");
let end = html[start..]
.find("</section>")
.expect("closing </section>")
+ start;
let section = &html[start..end];
assert!(
!section.contains("optional since/until unix seconds"),
"storage copy must state the 24 h default: {section}"
);
assert!(section.contains("<th scope=\"col\">Window</th>"));
assert!(section.contains("colspan=\"3\""));

let script = r#"
const fs = require('fs');
class El {
constructor(tag) {
this.tagName = tag.toUpperCase(); this.children = []; this.innerHTML = ''; this.textContent = '';
this.value = ''; this.disabled = false;
this.classList = { set: new Set(), add(c){this.set.add(c)}, remove(c){this.set.delete(c)}, contains(c){return this.set.has(c)} };
this.listeners = {};
}
addEventListener(ev, fn) { (this.listeners[ev] ||= []).push(fn); }
replaceChildren(...nodes) { this.children = nodes; }
append() {}
}
const kinds = new El('input'); kinds.value = '1';
const since = new El('input');
const until = new El('input');
const runBtn = new El('button');
const errEl = new El('div');
const body = new El('tbody');
const byId = {
'ops-workers-body': new El('tbody'), 'ops-storage-kinds': kinds, 'ops-storage-since': since,
'ops-storage-until': until, 'ops-storage-run': runBtn, 'ops-storage-error': errEl, 'ops-storage-body': body,
};
global.document = { getElementById: (id) => byId[id] || null, createElement: (t) => new El(t) };
global.window = { __ADMIN_DASHBOARD__: { pathPrefix: '' } };
global.HTMLElement = El;
let nextPayload;
global.fetch = async () => ({ status: 200, ok: true, json: async () => nextPayload });
eval(fs.readFileSync(process.argv[1], 'utf8'));
const flush = () => new Promise((r) => setTimeout(r, 15));
const assertHas = (hay, needle) => { if (!hay.includes(needle)) throw new Error(`expected ${JSON.stringify(needle)} in ${hay}`); };
const assertLacks = (hay, needle) => { if (hay.includes(needle)) throw new Error(`did not expect ${JSON.stringify(needle)} in ${hay}`); };
(async () => {
nextPayload = { since: 1700000000, until: 1700086400, counts: [{ kinds: [1], count: 42 }] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, '42');
assertHas(body.innerHTML, 'last 24 h (2023-11-14 22:13:20 UTC – 2023-11-15 22:13:20 UTC)');

since.value = '1700000000'; until.value = '1700172800';
nextPayload = { since: 1700000000, until: 1700172800, counts: [{ kinds: [1], count: 99 }] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, '99');
assertHas(body.innerHTML, '2023-11-14 22:13:20 UTC – 2023-11-16 22:13:20 UTC');
assertLacks(body.innerHTML, 'last 24 h');

since.value = ''; until.value = '1700086400';
nextPayload = { since: 1700000000, until: 1700086400, counts: [{ kinds: [1], count: 7 }] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, 'default 24 h (2023-11-14 22:13:20 UTC – 2023-11-15 22:13:20 UTC)');

since.value = '0'; until.value = '3600';
nextPayload = { since: 0, until: 3600, counts: [{ kinds: [1], count: 3 }] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, '1970-01-01 00:00:00 UTC – 1970-01-01 01:00:00 UTC');

since.value = '10000000000000'; until.value = '10000000000001';
nextPayload = { since: 10000000000000, until: 10000000000001, counts: [{ kinds: [1], count: 5 }] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, '5');
assertHas(body.innerHTML, '10000000000000 – 10000000000001');

nextPayload = { since: 0, until: 3600, counts: [] };
runBtn.listeners.click[0]();
await flush();
assertHas(body.innerHTML, 'colspan="3"');
})().catch((err) => { console.error(err); process.exit(1); });
"#;
let output = std::process::Command::new("node")
.arg("-e")
.arg(script)
.arg(assets_dir().join("admin-ops.js"))
.output()
.expect("run node storage panel harness");
assert!(
output.status.success(),
"node storage panel harness failed:\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
42 changes: 37 additions & 5 deletions resources/admin/assets/admin-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,47 @@
}
}

const renderStorage = (counts) => {
const formatBound = (seconds) => {
if (typeof seconds !== 'number' || !Number.isFinite(seconds)) {
return '—'
}
const date = new Date(seconds * 1000)
return Number.isNaN(date.getTime()) ? String(seconds) : formatTime(date.toISOString())
}

const formatStorageWindow = (windowRange, { defaultedSince = false, defaultedUntil = false } = {}) => {
const hasSince = typeof windowRange?.since === 'number' && Number.isFinite(windowRange.since)
const hasUntil = typeof windowRange?.until === 'number' && Number.isFinite(windowRange.until)
if (!hasSince || !hasUntil) {
return '—'
}
const range = `${formatBound(windowRange.since)} – ${formatBound(windowRange.until)}`
if (defaultedSince && defaultedUntil) {
return `last 24 h (${range})`
}
if (defaultedSince) {
return `default 24 h (${range})`
}
if (defaultedUntil) {
return `${range} (until defaulted to now)`
}
return range
}

const renderStorage = (counts, windowRange, options = {}) => {
if (!Array.isArray(counts) || !counts.length) {
elements.storageBody.innerHTML =
'<tr><td colspan="2" class="admin-muted small">No counts returned.</td></tr>'
'<tr><td colspan="3" class="admin-muted small">No counts returned.</td></tr>'
return
}

const windowLabel = formatStorageWindow(windowRange, options)
elements.storageBody.innerHTML = counts
.map(
(row) => `<tr>
<td><code class="small">${escapeHtml((row.kinds ?? []).join(', '))}</code></td>
<td>${escapeHtml(row.count)}</td>
<td class="small">${escapeHtml(windowLabel)}</td>
</tr>`,
)
.join('')
Expand Down Expand Up @@ -252,8 +281,8 @@
}

const params = new URLSearchParams({ kinds })
const since = elements.storageSince?.value
const until = elements.storageUntil?.value
const since = elements.storageSince?.value?.trim() ?? ''
const until = elements.storageUntil?.value?.trim() ?? ''
if (since) {
params.set('since', since)
}
Expand All @@ -267,7 +296,10 @@
showStorageError(payload.error ?? 'Storage count query failed')
return
}
renderStorage(payload.counts)
renderStorage(payload.counts, payload, {
defaultedSince: !since,
defaultedUntil: !until,
})
}

const reverify = async (pubkey) => {
Expand Down
5 changes: 3 additions & 2 deletions resources/admin/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ <h2 class="section-title">NIP-05 verifications</h2>

<section class="dashboard-section">
<h2 class="section-title">Storage counts</h2>
<p class="admin-muted small mb-2">Per-kind counts use NIP-45-style filters (comma-separated kinds, optional since/until unix seconds).</p>
<p class="admin-muted small mb-2">Per-kind counts use NIP-45-style filters (comma-separated kinds; since/until unix seconds default to the last 24 h when omitted).</p>
<div class="d-flex flex-wrap gap-2 mb-3 align-items-end">
<div class="flex-grow-1">
<label class="form-label field-label small mb-1" for="ops-storage-kinds">Kinds</label>
Expand All @@ -338,10 +338,11 @@ <h2 class="section-title">Storage counts</h2>
<tr>
<th scope="col">Kinds</th>
<th scope="col">Count</th>
<th scope="col">Window</th>
</tr>
</thead>
<tbody id="ops-storage-body">
<tr><td colspan="2" class="admin-muted small">Run a query to load counts.</td></tr>
<tr><td colspan="3" class="admin-muted small">Run a query to load counts.</td></tr>
</tbody>
</table>
</div>
Expand Down
Loading