跳转到内容

Java 生成「错误率监控仪表盘(系统运维 / 接口 SLA)」PDF 示例(Guage)

JQuick-PdfBiz Demo

Guage1 页 A4 · 57 KB

用仪表盘呈现当前接口错误率与 SLA 阈值的关系,超阈值可一眼识别。

下载 PDF 文件查看模板原始文件返回全部示例

错误率监控仪表盘(系统运维 / 接口 SLA) — Guage
渲染效果

输出规格

PDF 页数
1
图表类型
Guage
模板行数
89
源码行数
83

Java 源码 · JQuickBiz44ErrorRateTest.java

以下代码即生成上方 PDF 的完整测试类,可直接复制运行;Maven 依赖见快速开始章节。

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) [2025-2099] Martin ([email protected])
 */
package com.github.paohaijiao.demo.biz;

import com.github.paohaijiao.JOption;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.demo.constant.JQuickConstant;
import com.github.paohaijiao.enums.JChartType;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import com.github.paohaijiao.factory.JChartRendererFactory;
import com.github.paohaijiao.guage.GuageConfig;
import com.github.paohaijiao.guage.JGuageOption;
import org.junit.Test;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 业务场景 Demo 44:错误率监控仪表盘(系统运维 / 接口 SLA)
 *
 * <p>业务场景:核心业务系统对接口可用性进行 SLA 监控,仪表盘分值表示近 30 天接口健康度
 * 评分(0~100,由可用率与错误率综合折算),用于服务等级考核与故障复盘,主色采用深紫 #4A148C。</p>
 * <p>实际图表类型:{@link JChartType#Guage} 仪表盘(用于展示 SLA 健康度评分)。</p>
 *
 * <p>模板:{@code report/biz/44_error_rate.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\44_error_rate.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt × 400pt(宽 × 高)的方形比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz44ErrorRateTest {

    public static final String path = JQuickConstant.path;

    @Test
    @SuppressWarnings("deprecation")
    public void biz44ErrorRate() throws IOException {
        GuageConfig scoreConfig = GuageConfig.builder()
                .score(93)
                .pointerColor(new Color(74, 20, 140))
                .backgroundColor(new Color(243, 240, 247))
                .title("SLA HEALTH")
                .build();
        JGuageOption guageOption = JGuageOption.builder().scoreMeter(scoreConfig).build();
        JOption option = new JOption();
        option.setGuageOption(guageOption);

        String svg = JChartRendererFactory.renderChart(JChartType.Guage, option);

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/44_error_rate.pdf");
        JPdfConfig config = new JPdfConfig();
        // 开启 flex 行布局,使模板中的 display:flex 生效(图表居中)
        config.getLayoutConfig().setFlexLayout(true);
        JQuickPdfFactory factory = new JQuickPdfFactory(config);
        factory.bind("svg", svg);
        byte[] bytes = factory.executeResource("report/biz/44_error_rate.txt");
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }
}

最小调用(3 行)

不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。

// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/44_error_rate.txt");

模板文件 · 44_error_rate.txt

类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。

<pdf>
<body>
  <div style="marginBottom:10px; borderBottom:1px solid #B9C2CC; paddingBottom:8px">
    <h1 style="color:#243B53; fontSize:17; bold:true; marginBottom:3px">'接口错误率监控报表'</h1>
    <p style="color:#6c757d; fontSize:9">'统计周期:2025年2月1日 - 2025年2月28日 | 数据口径:核心接口调用(SLA 阈值:可用率 ≥ 99.9%)'</p>
  </div>

  <div style="borderLeft:4px solid #4A148C; paddingLeft:8px; marginBottom:6px">
    <h2 style="color:#4A148C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'一、接口 SLA 健康度'</h2>
  </div>
  <div style="display:flex; justifyContent:space-between; marginBottom:10px">
    <div style="width:285px">
      <div style="display:flex; justifyContent:center">
        <svg style="width:285px">${svg}</svg>
      </div>
    </div>
    <div style="width:265px">
      <h3 style="color:#4A148C; fontSize:12; bold:true; marginBottom:6px">'评分说明'</h3>
      <list style="listStyleType:disc; paddingLeft:14px; fontSize:9">
        <li style="marginBottom:5px">'健康度评分 93 分(满分 100)'</li>
        <li style="marginBottom:5px">'由可用率与错误率综合折算'</li>
        <li style="marginBottom:5px">'24 小时自动刷新一次'</li>
        <li style="marginBottom:5px">'低于 90 分触发告警'</li>
      </list>
    </div>
  </div>

  <div style="borderLeft:4px solid #4A148C; paddingLeft:8px; marginBottom:6px">
    <h2 style="color:#4A148C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'二、SLA 关键指标完成情况'</h2>
  </div>
  <table style="width:560px; fontSize:10">
    <tr>
      <th style="backgroundColor:#4A148C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'监控指标'</th>
      <th style="backgroundColor:#4A148C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'本期值'</th>
      <th style="backgroundColor:#4A148C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'SLA 目标'</th>
      <th style="backgroundColor:#4A148C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'环比变化'</th>
      <th style="backgroundColor:#4A148C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'结论'</th>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'接口可用率'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'99.93%'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'≥99.90%'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+0.02pct'</td>
      <td style="padding:5px; textAlign:right; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'达标'</td>
    </tr>
    <tr>
      <td style="padding:5px; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'接口错误率'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'0.07%'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'≤0.10%'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'-0.02pct'</td>
      <td style="padding:5px; textAlign:right; color:#1B5E20; bold:true; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'达标'</td>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'P95 响应耗时'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'286 ms'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'≤300 ms'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+12 ms'</td>
      <td style="padding:5px; textAlign:right; color:#BF360C; bold:true; border:1px solid #DCE3EC">'预警'</td>
    </tr>
    <tr>
      <td style="padding:5px; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'5xx 错误次数'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'1,842 次'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'≤2,600 次'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'-316 次'</td>
      <td style="padding:5px; textAlign:right; color:#1B5E20; bold:true; backgroundColor:#F1EAF6; border:1px solid #DCE3EC">'达标'</td>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'健康度综合评分'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'93 分'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'≥90 分'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+2 分'</td>
      <td style="padding:5px; textAlign:right; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'达标'</td>
    </tr>
  </table>

  <div style="borderLeft:4px solid #4A148C; paddingLeft:8px; marginBottom:6px; marginTop:10px">
    <h2 style="color:#4A148C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'三、运维分析与建议'</h2>
  </div>
  <list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
    <li style="marginBottom:5px">'本月 SLA 综合健康度 93 分,可用率 99.93% 高于 99.90% 目标,错误率 0.07% 低于 0.10% 阈值,五项指标中四项达标。'</li>
    <li style="marginBottom:5px">'错误率环比下降 0.02pct、5xx 错误次数环比减少 316 次,主要受益于上月末上线的连接池扩容与慢 SQL 治理。'</li>
    <li style="marginBottom:5px">'唯一需要盯防的是 P95 响应耗时环比上升 12ms 至 286ms,已逼近 300ms 目标线,2月25日压测期间曾瞬时触及 302ms。'</li>
    <li style="marginBottom:5px">'建议对耗时上升最快的订单查询接口做缓存命中率排查,并在下一版本引入异步化改造,避免高峰时段击穿 SLA。'</li>
  </list>

  <p style="textAlignment:center; color:#8a939b; fontSize:8; italic:true; marginTop:10px">'数据来源:APM 应用性能监控平台 / 日志告警系统 · 生成时间:2025年3月3日 · 密级:内部'</p>
</body>
</pdf>

同分组示例

本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。