dashboard_for_sage/wwwroot/dashboard_refresh.js
yumoqing c7180bda77 fix: rewrite dashboard_refresh.js with correct bricks API
- Replace non-existent bricks.app.find_widget_by_id() with bricks.getWidgetById(id, bricks.app)
- Replace widget.el.textContent with widget.set_text() method
- Fix getBaseUrl() to handle /modulename paths (no .ui suffix) returning /undefined
- Remove chart refresh from JS — use ChartBar's built-in refresh_period: 10
- Update index.ui ChartBar widget with refresh_period: 10
- Also fixed all find_widget_by_id references across skills:
  * bricks-framework SKILL.md (2 occurrences)
  * bricks-framework references: auto-refresh-dashboard.md, auto-refresh-pattern.md, bar.md, line.md, websocket.md
  * module-development-spec references: read-only-module-pattern.md
  * harnessed-module-development SKILL.md (2 occurrences)
  * harnessed-module-development references: reasoning-visualization.md, bricks-ui-pitfalls.md
  * rbac-permission-initialization-pattern references: websocket-debugging.md
2026-05-24 16:05:56 +08:00

80 lines
2.4 KiB
JavaScript

/**
* Dashboard auto-refresh script
* Polls API every 10 seconds and updates stat cards
* ChartBar handles its own refresh via refresh_period
* Auto-loaded by Sage's header.tmpl from wwwroot/
*/
(function() {
'use strict';
function getBaseUrl() {
var path = window.location.pathname;
// path is like /dashboard_for_sage/index.ui or /dashboard_for_sage/
var parts = path.split('/').filter(function(p) { return p.length > 0; });
if (parts.length === 0) return '';
return '/' + parts[0];
}
function buildUrl(dspyFile) {
var base = getBaseUrl();
return base + '/' + dspyFile + '?_webbricks_=1';
}
function setCardText(cardId, value) {
try {
var widget = bricks.getWidgetById(cardId, bricks.app);
if (widget && typeof widget.set_text === 'function') {
widget.set_text(String(value));
}
} catch(e) {}
}
function formatNumber(n) {
if (typeof n === 'number') return n.toLocaleString();
return String(n);
}
function formatAmount(n) {
if (typeof n === 'number') return '\u00a5' + n.toFixed(2);
return '\u00a50.00';
}
async function refreshStats() {
try {
var url = buildUrl('api/get_today_usage.dspy');
var resp = await fetch(url, { credentials: 'include' });
if (!resp.ok) return;
var data = await resp.json();
setCardText('today_cnt_value', formatNumber(data.cnt));
setCardText('today_amount_value', formatAmount(data.total_amount));
} catch(e) {
console.error('refreshStats error:', e);
}
}
async function refreshUsers() {
try {
var url = buildUrl('api/get_user_stats.dspy');
var resp = await fetch(url, { credentials: 'include' });
if (!resp.ok) return;
var data = await resp.json();
setCardText('total_users_value', formatNumber(data.total_users));
setCardText('concurrent_users_value', formatNumber(data.concurrent_users));
} catch(e) {
console.error('refreshUsers error:', e);
}
}
async function refreshAll() {
await refreshStats();
await refreshUsers();
}
// Wait for bricks framework to initialize, then start auto-refresh
setTimeout(function() {
refreshAll();
setInterval(refreshAll, 10000);
}, 2000);
})();