Initial commit: core-api service extraction from portainer-core
Build and Push / build (release) Successful in 43s
Build and Push / build (release) Successful in 43s
This commit is contained in:
@@ -0,0 +1,491 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>AI Performance Stats</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: transparent;
|
||||
color: #e0e0e0;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(245, 101, 101, 0.1);
|
||||
border: 1px solid rgba(245, 101, 101, 0.4);
|
||||
color: #f56565;
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 15px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.stat-panel {
|
||||
background: rgba(40, 40, 40, 0.95);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.panel-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 12px;
|
||||
padding-bottom: 8px;
|
||||
border-bottom: 2px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.panel-icon {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.panel-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.panel-subtitle {
|
||||
font-size: 11px;
|
||||
color: #a0a0a0;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.metrics-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.metric-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 6px 0;
|
||||
}
|
||||
|
||||
.metric-label {
|
||||
font-size: 13px;
|
||||
color: #b0b0b0;
|
||||
}
|
||||
|
||||
.metric-value {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.metric-value.excellent {
|
||||
color: #48bb78;
|
||||
}
|
||||
|
||||
.metric-value.good {
|
||||
color: #68d391;
|
||||
}
|
||||
|
||||
.metric-value.warning {
|
||||
color: #ed8936;
|
||||
}
|
||||
|
||||
.metric-value.critical {
|
||||
color: #f56565;
|
||||
}
|
||||
|
||||
.metric-value.neutral {
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
.tools-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.tool-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
font-size: 12px;
|
||||
padding: 4px 8px;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.tool-name {
|
||||
color: #d0d0d0;
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
|
||||
.tool-stats {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.tool-calls {
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
.tool-success-rate {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.bottom-links {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
justify-content: center;
|
||||
padding-top: 10px;
|
||||
}
|
||||
|
||||
.link-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 8px 14px;
|
||||
background: rgba(66, 153, 225, 0.15);
|
||||
color: #4299e1;
|
||||
text-decoration: none;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.link-btn:hover {
|
||||
background: rgba(66, 153, 225, 0.25);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 30px;
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.spinner {
|
||||
display: inline-block;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
border: 2px solid rgba(255, 255, 255, 0.3);
|
||||
border-top-color: #fff;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.6s linear infinite;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
font-size: 10px;
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.badge.success {
|
||||
background: rgba(72, 187, 120, 0.2);
|
||||
color: #48bb78;
|
||||
}
|
||||
|
||||
.badge.error {
|
||||
background: rgba(245, 101, 101, 0.2);
|
||||
color: #f56565;
|
||||
}
|
||||
|
||||
/* Responsive design */
|
||||
@media (max-width: 768px) {
|
||||
.stats-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div id="error-container"></div>
|
||||
<div id="stats-container" class="loading">
|
||||
<div class="spinner"></div>Loading AI performance metrics...
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Use relative URL to work in any context
|
||||
const API_BASE = '';
|
||||
|
||||
let metricsData = null;
|
||||
|
||||
function formatDuration(ms) {
|
||||
if (ms < 1000) return `${Math.round(ms)}ms`;
|
||||
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
|
||||
return `${(ms / 60000).toFixed(1)}m`;
|
||||
}
|
||||
|
||||
function formatUptime(seconds) {
|
||||
if (seconds < 60) return `${seconds}s`;
|
||||
if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
return `${hours}h ${minutes}m`;
|
||||
}
|
||||
|
||||
function getResponseTimeClass(ms) {
|
||||
if (ms < 1000) return 'excellent';
|
||||
if (ms < 3000) return 'good';
|
||||
if (ms < 10000) return 'warning';
|
||||
return 'critical';
|
||||
}
|
||||
|
||||
function getSuccessRateClass(rate) {
|
||||
if (rate >= 0.99) return 'excellent';
|
||||
if (rate >= 0.95) return 'good';
|
||||
if (rate >= 0.90) return 'warning';
|
||||
return 'critical';
|
||||
}
|
||||
|
||||
function getHitRateClass(rate) {
|
||||
if (rate >= 0.85) return 'excellent';
|
||||
if (rate >= 0.70) return 'good';
|
||||
if (rate >= 0.50) return 'warning';
|
||||
return 'critical';
|
||||
}
|
||||
|
||||
async function fetchMetrics() {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/ai/metrics`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
metricsData = await response.json();
|
||||
renderMetrics();
|
||||
document.getElementById('error-container').innerHTML = '';
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching AI metrics:', error);
|
||||
document.getElementById('error-container').innerHTML =
|
||||
`<div class="error">❌ Core-AI service unavailable: ${error.message}</div>`;
|
||||
document.getElementById('stats-container').innerHTML =
|
||||
'<div class="loading">Waiting for Core-AI service...</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function renderMetrics() {
|
||||
if (!metricsData) return;
|
||||
|
||||
const agent = metricsData.agent || {};
|
||||
const tools = metricsData.tools || {};
|
||||
const memory = metricsData.memory || {};
|
||||
const users = metricsData.users || {};
|
||||
const concurrency = metricsData.concurrency || {};
|
||||
|
||||
const html = `
|
||||
<div class="stats-grid">
|
||||
<!-- Agent Performance Panel -->
|
||||
<div class="stat-panel">
|
||||
<div class="panel-header">
|
||||
<span class="panel-icon">🤖</span>
|
||||
<span class="panel-title">Agent Performance</span>
|
||||
<span class="panel-subtitle">${agent.total_requests || 0} requests</span>
|
||||
</div>
|
||||
<div class="metrics-list">
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Avg Response</span>
|
||||
<span class="metric-value ${getResponseTimeClass(agent.avg_response_time_ms)}">
|
||||
${formatDuration(agent.avg_response_time_ms || 0)}
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">P95 Latency</span>
|
||||
<span class="metric-value ${getResponseTimeClass(agent.p95_response_time_ms)}">
|
||||
${formatDuration(agent.p95_response_time_ms || 0)}
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Requests/min</span>
|
||||
<span class="metric-value neutral">${(agent.requests_per_minute || 0).toFixed(1)}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Errors</span>
|
||||
<span class="metric-value ${agent.errors_total > 0 ? 'warning' : 'excellent'}">
|
||||
${agent.errors_total || 0}
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Concurrent</span>
|
||||
<span class="metric-value neutral">
|
||||
${concurrency.current || 0} / ${concurrency.max || 0} max
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tool Execution Panel -->
|
||||
<div class="stat-panel">
|
||||
<div class="panel-header">
|
||||
<span class="panel-icon">🔧</span>
|
||||
<span class="panel-title">Tool Execution</span>
|
||||
<span class="panel-subtitle">${tools.total_calls || 0} calls</span>
|
||||
</div>
|
||||
<div class="metrics-list">
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Success Rate</span>
|
||||
<span class="metric-value ${getSuccessRateClass(tools.success_rate || 0)}">
|
||||
${((tools.success_rate || 0) * 100).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Unique Tools</span>
|
||||
<span class="metric-value neutral">${tools.total_unique_tools || 0}</span>
|
||||
</div>
|
||||
</div>
|
||||
${renderTopTools(tools.top_tools || {})}
|
||||
</div>
|
||||
|
||||
<!-- Memory System Panel -->
|
||||
<div class="stat-panel">
|
||||
<div class="panel-header">
|
||||
<span class="panel-icon">💾</span>
|
||||
<span class="panel-title">Memory System</span>
|
||||
<span class="panel-subtitle">Tier 1 Cache</span>
|
||||
</div>
|
||||
<div class="metrics-list">
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Hit Rate</span>
|
||||
<span class="metric-value ${getHitRateClass(memory.tier1_hit_rate || 0)}">
|
||||
${((memory.tier1_hit_rate || 0) * 100).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Cache Hits</span>
|
||||
<span class="metric-value neutral">${memory.tier1_hits || 0}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Tier 2 Queries</span>
|
||||
<span class="metric-value neutral">${memory.tier2_queries || 0}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Consolidations</span>
|
||||
<span class="metric-value neutral">${memory.total_consolidations || 0}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- System Health Panel -->
|
||||
<div class="stat-panel">
|
||||
<div class="panel-header">
|
||||
<span class="panel-icon">📊</span>
|
||||
<span class="panel-title">System Health</span>
|
||||
<span class="panel-subtitle">Live Status</span>
|
||||
</div>
|
||||
<div class="metrics-list">
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Service Uptime</span>
|
||||
<span class="metric-value excellent">${formatUptime(metricsData.uptime_seconds || 0)}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Active Users</span>
|
||||
<span class="metric-value neutral">${users.total_active || 0}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Streaming</span>
|
||||
<span class="metric-value neutral">${agent.streaming_requests || 0}</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Non-Streaming</span>
|
||||
<span class="metric-value neutral">${agent.non_streaming_requests || 0}</span>
|
||||
</div>
|
||||
${renderAgentStatus()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bottom-links">
|
||||
<a href="${API_BASE}/ai/metrics" target="_blank" class="link-btn">
|
||||
📄 Full Metrics JSON
|
||||
</a>
|
||||
<a href="${API_BASE}/ai/health" target="_blank" class="link-btn">
|
||||
🏥 Health Check
|
||||
</a>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.getElementById('stats-container').innerHTML = html;
|
||||
}
|
||||
|
||||
function renderTopTools(topTools) {
|
||||
if (!topTools || Object.keys(topTools).length === 0) {
|
||||
return '<div class="metric-row"><span class="metric-label">No tool calls yet</span></div>';
|
||||
}
|
||||
|
||||
const toolsArray = Object.entries(topTools).slice(0, 5);
|
||||
const toolsHtml = toolsArray.map(([name, stats]) => {
|
||||
const successRate = stats.success_rate || 0;
|
||||
const successClass = getSuccessRateClass(successRate);
|
||||
|
||||
return `
|
||||
<div class="tool-row">
|
||||
<span class="tool-name">${name}</span>
|
||||
<div class="tool-stats">
|
||||
<span class="tool-calls">${stats.calls} calls</span>
|
||||
<span class="tool-success-rate ${successClass}">
|
||||
${(successRate * 100).toFixed(0)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
return `
|
||||
<div style="margin-top: 8px;">
|
||||
<div class="metric-label" style="margin-bottom: 6px;">Top 5 Tools:</div>
|
||||
<div class="tools-list">${toolsHtml}</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderAgentStatus() {
|
||||
const agent = metricsData.agent || {};
|
||||
const hasActivity = (agent.total_requests || 0) > 0;
|
||||
|
||||
return `
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Agent Status</span>
|
||||
<span class="badge ${hasActivity ? 'success' : 'neutral'}">
|
||||
${hasActivity ? '✓ Active' : 'Idle'}
|
||||
</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Initial fetch
|
||||
fetchMetrics();
|
||||
|
||||
// Auto-refresh every 10 seconds
|
||||
setInterval(fetchMetrics, 10000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,487 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Service Control</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: transparent;
|
||||
color: #e0e0e0;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.section {
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.section-header {
|
||||
color: #fff;
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
padding-bottom: 8px;
|
||||
border-bottom: 2px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.service-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.service-row {
|
||||
background: rgba(40, 40, 40, 0.95);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 6px;
|
||||
padding: 12px 16px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
transition: all 0.2s ease;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.service-row:hover {
|
||||
border-color: rgba(66, 153, 225, 0.4);
|
||||
background: rgba(45, 45, 45, 0.95);
|
||||
}
|
||||
|
||||
.service-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 15px;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.service-name {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
text-transform: capitalize;
|
||||
min-width: 300px;
|
||||
}
|
||||
|
||||
.service-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.status-indicator {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.status-indicator.running {
|
||||
background: #48bb78;
|
||||
box-shadow: 0 0 8px rgba(72, 187, 120, 0.6);
|
||||
}
|
||||
|
||||
.status-indicator.stopped {
|
||||
background: #f56565;
|
||||
box-shadow: 0 0 8px rgba(245, 101, 101, 0.6);
|
||||
}
|
||||
|
||||
.status-text {
|
||||
font-size: 13px;
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
.uptime-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-width: 150px;
|
||||
padding: 4px 10px;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.uptime-status:hover {
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.uptime-percentage {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.uptime-percentage.excellent {
|
||||
color: #48bb78;
|
||||
}
|
||||
|
||||
.uptime-percentage.good {
|
||||
color: #68d391;
|
||||
}
|
||||
|
||||
.uptime-percentage.warning {
|
||||
color: #ed8936;
|
||||
}
|
||||
|
||||
.uptime-percentage.critical {
|
||||
color: #f56565;
|
||||
}
|
||||
|
||||
.uptime-percentage.unknown {
|
||||
color: #718096;
|
||||
}
|
||||
|
||||
.uptime-icon {
|
||||
font-size: 11px;
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
.service-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 6px 16px;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
min-width: 70px;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.3;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-start {
|
||||
background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-start:hover:not(:disabled) {
|
||||
background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 2px 8px rgba(72, 187, 120, 0.3);
|
||||
}
|
||||
|
||||
.btn-stop {
|
||||
background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-stop:hover:not(:disabled) {
|
||||
background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 2px 8px rgba(245, 101, 101, 0.3);
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 30px;
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(245, 101, 101, 0.1);
|
||||
border: 1px solid rgba(245, 101, 101, 0.4);
|
||||
color: #f56565;
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.always-on-badge {
|
||||
font-size: 10px;
|
||||
color: #4299e1;
|
||||
background: rgba(66, 153, 225, 0.15);
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
margin-left: 8px;
|
||||
text-transform: uppercase;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.spinner {
|
||||
display: inline-block;
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border: 2px solid rgba(255, 255, 255, 0.3);
|
||||
border-top-color: #fff;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.6s linear infinite;
|
||||
margin-right: 6px;
|
||||
}
|
||||
|
||||
/* Responsive design */
|
||||
@media (max-width: 768px) {
|
||||
.service-row {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.service-name {
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.uptime-status {
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.service-right {
|
||||
width: 100%;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div id="error-container"></div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">🎛️ Stoppable Services</div>
|
||||
<div id="stoppable-container" class="loading">Loading services...</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">🔒 Always-On Infrastructure</div>
|
||||
<div id="always-on-container" class="loading">Loading infrastructure...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Use relative URL to work in any context (iframe, direct access, etc.)
|
||||
const API_BASE = '';
|
||||
const KUMA_BASE = window.location.protocol + '//' + window.location.hostname + ':3001';
|
||||
|
||||
let services = [];
|
||||
let alwaysOnServices = [];
|
||||
let monitors = {};
|
||||
|
||||
async function fetchData() {
|
||||
try {
|
||||
// Single API call to get all data
|
||||
const response = await fetch(`${API_BASE}/infrastructure/widget-data`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error('API returned unsuccessful response');
|
||||
}
|
||||
|
||||
// Update services
|
||||
services = data.services || [];
|
||||
|
||||
// Update always-on services list
|
||||
if (data.service_groups && data.service_groups.always_on) {
|
||||
alwaysOnServices = data.service_groups.always_on;
|
||||
}
|
||||
|
||||
// Build monitors map
|
||||
const monitorsMap = {};
|
||||
if (data.monitors) {
|
||||
data.monitors.forEach(monitor => {
|
||||
const name = monitor.name.toLowerCase().replace(/[^a-z0-9]/g, '-');
|
||||
monitorsMap[name] = {
|
||||
id: monitor.id,
|
||||
uptime_24h: monitor.uptime_24h || 0,
|
||||
active: monitor.active !== false
|
||||
};
|
||||
});
|
||||
}
|
||||
monitors = monitorsMap;
|
||||
|
||||
renderServices();
|
||||
document.getElementById('error-container').innerHTML = '';
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
document.getElementById('error-container').innerHTML =
|
||||
`<div class="error">❌ Failed to connect to API: ${error.message}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function isAlwaysOn(serviceName) {
|
||||
return alwaysOnServices.includes(serviceName.toLowerCase());
|
||||
}
|
||||
|
||||
function getUptimeInfo(serviceName) {
|
||||
const monitorKey = serviceName.toLowerCase().replace(/[^a-z0-9]/g, '-');
|
||||
const monitor = monitors[monitorKey];
|
||||
|
||||
if (!monitor) {
|
||||
return {
|
||||
percentage: 0,
|
||||
class: 'unknown',
|
||||
text: 'No monitor',
|
||||
id: null
|
||||
};
|
||||
}
|
||||
|
||||
const uptime = monitor.uptime_24h;
|
||||
let className = 'unknown';
|
||||
|
||||
if (uptime >= 99.5) className = 'excellent';
|
||||
else if (uptime >= 95) className = 'good';
|
||||
else if (uptime >= 90) className = 'warning';
|
||||
else if (uptime > 0) className = 'critical';
|
||||
|
||||
return {
|
||||
percentage: uptime,
|
||||
class: className,
|
||||
text: uptime > 0 ? `${uptime.toFixed(1)}% ↑` : 'Down',
|
||||
id: monitor.id
|
||||
};
|
||||
}
|
||||
|
||||
function renderServiceRow(service) {
|
||||
const isRunning = service.containers_running > 0;
|
||||
const alwaysOn = isAlwaysOn(service.name);
|
||||
const uptime = getUptimeInfo(service.name);
|
||||
|
||||
const kumaLink = uptime.id ?
|
||||
`${KUMA_BASE}/dashboard/${uptime.id}` :
|
||||
KUMA_BASE;
|
||||
|
||||
return `
|
||||
<div class="service-row" data-service="${service.name}">
|
||||
<div class="service-left">
|
||||
<div class="service-name">
|
||||
${service.name}
|
||||
${alwaysOn ? '<span class="always-on-badge">Protected</span>' : ''}
|
||||
</div>
|
||||
<div class="service-status">
|
||||
<div class="status-indicator ${isRunning ? 'running' : 'stopped'}"></div>
|
||||
<span class="status-text">
|
||||
${isRunning ? `Running (${service.containers_running}/${service.containers_total})` : 'Stopped'}
|
||||
</span>
|
||||
</div>
|
||||
<a href="${kumaLink}" target="_blank" class="uptime-status" title="View in Uptime Kuma">
|
||||
<span class="uptime-icon">📊</span>
|
||||
<span class="uptime-percentage ${uptime.class}">${uptime.text}</span>
|
||||
</a>
|
||||
</div>
|
||||
<div class="service-right">
|
||||
<button class="btn btn-start"
|
||||
onclick="controlService('${service.name}', 'start')"
|
||||
${isRunning || alwaysOn ? 'disabled' : ''}>
|
||||
Start
|
||||
</button>
|
||||
<button class="btn btn-stop"
|
||||
onclick="controlService('${service.name}', 'stop')"
|
||||
${!isRunning || alwaysOn ? 'disabled' : ''}>
|
||||
Stop
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderServices() {
|
||||
const stoppableContainer = document.getElementById('stoppable-container');
|
||||
const alwaysOnContainer = document.getElementById('always-on-container');
|
||||
|
||||
// Stoppable services
|
||||
const stoppableServices = services.filter(s => !isAlwaysOn(s.name));
|
||||
|
||||
if (stoppableServices.length === 0) {
|
||||
stoppableContainer.innerHTML = '<div class="loading">No stoppable services found</div>';
|
||||
} else {
|
||||
stoppableContainer.className = 'service-list';
|
||||
stoppableContainer.innerHTML = stoppableServices
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
.map(service => renderServiceRow(service))
|
||||
.join('');
|
||||
}
|
||||
|
||||
// Always-on services
|
||||
const alwaysOnServicesList = services.filter(s => isAlwaysOn(s.name));
|
||||
|
||||
if (alwaysOnServicesList.length === 0) {
|
||||
alwaysOnContainer.innerHTML = '<div class="loading">No infrastructure services found</div>';
|
||||
} else {
|
||||
alwaysOnContainer.className = 'service-list';
|
||||
alwaysOnContainer.innerHTML = alwaysOnServicesList
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
.map(service => renderServiceRow(service))
|
||||
.join('');
|
||||
}
|
||||
}
|
||||
|
||||
async function controlService(serviceName, action) {
|
||||
const row = document.querySelector(`[data-service="${serviceName}"]`);
|
||||
const buttons = row.querySelectorAll('button');
|
||||
|
||||
// Disable all buttons and show loading
|
||||
buttons.forEach(btn => {
|
||||
btn.disabled = true;
|
||||
if (btn.textContent.toLowerCase().includes(action)) {
|
||||
btn.innerHTML = `<span class="spinner"></span>${action}`;
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/infrastructure/services/${serviceName}/${action}`, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok || !result.success) {
|
||||
throw new Error(result.message || result.detail || 'Operation failed');
|
||||
}
|
||||
|
||||
console.log(`${action} ${serviceName}:`, result);
|
||||
|
||||
// Wait for containers to start/stop
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// Refresh service list
|
||||
await fetchData();
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error ${action}ing ${serviceName}:`, error);
|
||||
alert(`Failed to ${action} ${serviceName}: ${error.message}`);
|
||||
|
||||
// Re-enable buttons on error
|
||||
await fetchData();
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-refresh every 10 seconds
|
||||
setInterval(fetchData, 10000);
|
||||
|
||||
// Initial load
|
||||
fetchData();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user