You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仪表盘中的KPI区域指标聚合:服务器数据库结构与Uptime统计需求

Got it, let's walk through how to build those server uptime KPI aggregations for your dashboard, based on the database schema you shared. I’ll break this down into actionable steps with SQL examples and dashboard tips:

1. Map Out Your Data Relationships First

First, let's clarify how all your tables connect — this is key for building accurate aggregations:

  • Country → Region (each region belongs to one country)
  • Region → State (each state is in one region)
  • State ↔ Servers (many-to-many via the Server_State join table)
  • Servers → ServerMetrics (you’ll need this table to track uptime data, more on that below)
2. Core SQL Queries for Uptime Aggregations

Quick Pre-Requisite

Your existing schema doesn’t store actual uptime metrics, so you’ll need a ServerMetrics table. Here’s a basic create statement if you don’t have it:

CREATE TABLE ServerMetrics (
    metric_id SERIAL PRIMARY KEY,
    server_id INT REFERENCES Servers(server_id),
    measurement_timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    uptime_pct NUMERIC(5,2) CHECK (uptime_pct BETWEEN 0 AND 100),
    is_up BOOLEAN NOT NULL
);

Now, let’s build the queries for your KPI dashboard:

2.1 Aggregate Uptime by State

This gives you state-level performance metrics, perfect for a regional breakdown:

SELECT
    s.state_name,
    COUNT(DISTINCT sv.server_id) AS total_servers_in_state,
    ROUND(AVG(sm.uptime_pct), 2) AS average_uptime_pct,
    SUM(CASE WHEN sm.is_up THEN 1 ELSE 0 END) AS active_servers,
    ROUND(
        (SUM(CASE WHEN sm.is_up THEN 1 ELSE 0 END)::FLOAT / COUNT(DISTINCT sv.server_id)) * 100,
        2
    ) AS state_availability_rate
FROM
    Servers sv
JOIN
    Server_State ss ON sv.server_id = ss.server_id
JOIN
    State s ON ss.state_id = s.state_id
JOIN
    ServerMetrics sm ON sv.server_id = sm.server_id
-- Filter to a relevant time window (e.g., last 24 hours)
WHERE
    sm.measurement_timestamp >= NOW() - INTERVAL '24 hours'
GROUP BY
    s.state_name
ORDER BY
    average_uptime_pct DESC;

2.2 Aggregate Uptime by Region & Country

Drill up to higher-level regional/country metrics:

SELECT
    c.country_name,
    r.region_name,
    COUNT(DISTINCT sv.server_id) AS total_servers,
    ROUND(AVG(sm.uptime_pct), 2) AS regional_avg_uptime,
    ROUND(
        (SUM(CASE WHEN sm.is_up THEN 1 ELSE 0 END)::FLOAT / COUNT(DISTINCT sv.server_id)) * 100,
        2
    ) AS regional_availability_rate
FROM
    Servers sv
JOIN
    Server_State ss ON sv.server_id = ss.server_id
JOIN
    State s ON ss.state_id = s.state_id
JOIN
    Region r ON s.region_id = r.region_id  -- Assumes State has a region_id foreign key
JOIN
    Country c ON r.country_id = c.country_id  -- Assumes Region has a country_id foreign key
JOIN
    ServerMetrics sm ON sv.server_id = sm.server_id
WHERE
    sm.measurement_timestamp >= NOW() - INTERVAL '7 days'
GROUP BY
    c.country_name, r.region_name
ORDER BY
    c.country_name, regional_avg_uptime DESC;

2.3 Single Server Uptime Trend

For deep dives into individual server performance:

SELECT
    sv.server_name,
    sm.measurement_timestamp,
    sm.uptime_pct,
    sm.is_up
FROM
    Servers sv
JOIN
    ServerMetrics sm ON sv.server_id = sm.server_id
WHERE
    sv.server_id = 123  -- Replace with your target server ID
    AND sm.measurement_timestamp >= NOW() - INTERVAL '7 days'
ORDER BY
    sm.measurement_timestamp DESC;
3. Dashboard KPI Implementation Tips
  • Core KPI Cards: Use the global version of the above queries (remove GROUP BY) to display:
    • Overall average uptime
    • Total number of servers
    • Current available servers
    • Global availability rate
  • Drill-Down Functionality: Set up your dashboard to let users click from a country → region → state → individual server, loading the corresponding aggregated data each time.
  • Trend Visualizations: Use the single-server or regional time-series data to build line charts, showing uptime fluctuations over time. Highlight downtime periods with red markers.
  • Threshold Alerts: Add color coding to your KPI cards (e.g., red if availability drops below 95%, green for 99%+ uptime) to quickly flag issues.
4. Performance Optimizations
  • Add indexes to all foreign key fields (e.g., Server_State.server_id, State.region_id) to speed up join queries, especially as your metrics data grows.
  • Use materialized views to precompute daily/hourly aggregations. This lets your dashboard load instantly instead of running heavy real-time queries:
    CREATE MATERIALIZED VIEW DailyStateUptime AS
    SELECT
        s.state_name,
        DATE(sm.measurement_timestamp) AS metric_date,
        ROUND(AVG(sm.uptime_pct), 2) AS daily_avg_uptime
    FROM
        Servers sv
        JOIN Server_State ss ON sv.server_id = ss.server_id
        JOIN State s ON ss.state_id = s.state_id
        JOIN ServerMetrics sm ON sv.server_id = sm.server_id
    GROUP BY
        s.state_name, DATE(sm.measurement_timestamp);
    
  • Refresh the materialized view periodically (e.g., hourly) to keep data up-to-date.

内容的提问来源于stack exchange,提问作者TrigerFish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:18:14