zoukankan      html  css  js  c++  java
  • Grafana配置mysql展示自定义分组柱状图(Mac)

    安装Grafana

    安装使用环境为MAC,使用工具安装:

    brew update
    brew install grafana
    

    配置Grafana连接本地安装的mysql,mysql安装不做说明,配置文件列表如下:

    配置文件应该位于/usr/local/etc/grafana/grafana.ini
    日志文件应该位于/usr/local/var/log/grafana/grafana.log
    如果你想手动安装一个插件放在这里:/usr/local/var/lib/grafana/plugins
    默认的sqlite数据库位于 /usr/local/var/lib/grafana

    其中,编辑grafana.ini文件连接数据库,重点配置数据库的连接类型,账号,密码,数据库名(切记数据库建立对应的数据库)

    clipboard.png

    重启Grafana服务

    brew tap homebrew/services
    brew services start grafana
    

    此时网页访问localhost:3000即为Grafana配置数据源页面(默认账号密码:admin / admin)

    clipboard.png

    配置mysql数据源

    连接测试后保存,这样在图表展示数据源勾选默认即为mysql指定数据库,或者勾选指定连接名
    clipboard.png

    数据库构建表结构,录入测试数据

    clipboard.png

    新建一个用于展示的Graph

    编辑,鉴于展示目的为不按照时序排列的柱状图,但是Grafana的展示要求有时间字段在列,命名与time相关,故查询时添加time字段为当前时间,返回结果可以为两种可用形式,故展示两种SQL查询:

    select
                    now() as time,
    
                    case when (score >=80) then '[80, ~)'
    
                    when (score >=60 and score <80) then '[60, 80)'
    
                    when (score >=40 and score <60) then '[40, 60)'
    
                    when (score >=20 and score <40) then '[20, 40)'
    
                    else '(~, 20)'
    
                    end grade, count(*) num
    
    from grade group by
    
                    case when (score >=80) then '[80, ~)'
    
                    when (score >=60 and score <80) then '[60, 80)'
    
                    when (score >=40 and score <60) then '[40, 60)'
    
                    when (score >=20 and score <40) then '[20, 40)'
    
                    else '(~, 20)' end
    
                    order by 1;

    展示结果为:

    clipboard.png

    select *, now() as time
    
    from
    
                    (select count(*) as '[80, ~)' from grade g where g.score >=80) a,
    
                    (select count(*) as '[60, 80)' from grade g where g.score >=60 and g.score <80) b,
    
                    (select count(*) as '[40, 60)' from grade g where g.score >=40 and g.score <60) c,
    
                    (select count(*) as '[20, 40)' from grade g where g.score >=20 and g.score <40) d,
    
                    (select count(*) as '(~, 20)' from grade g where g.score <20) e;

    展示结果为:

    clipboard.png

    重要设置,选择series,选个代表数据是按series分组不是按时间,当前所选时间段进行计算。Y轴仍然表示 值。计算series的avg、min、max、total等。:

    clipboard.png

    效果图为:

    clipboard.png

    两种结果都能正常显示,根据显示规律总结为:

      1. 返回记录中包含字符串格式的情况下,取字符串值的一列为分组名,对应的柱状图值为avg、min、max、total选择的方法取聚合对应一行记录上的其他数字值:
        如果多出来一列字符串值,则图表报错
        # 如果没有字符串值列,则取数字列的列名为分组名,对应的柱状图值为每一列分组名下所有值的聚合
      2. 返回记录中不包含字符串格式的情况下,取数字列的列名为分组名,对应的柱状图值为每一列分组名下所有值的聚合avg、min、max、total
      3. 适用上述所有的,时间字段必须存在,即使目前在分组柱状图中无用,否则会报错,选择format as中的table表现形式为表格,不考虑。
  • 相关阅读:
    常见的arp欺骗
    ARP欺骗分析
    周总结
    周总结
    周总结
    win10下 修改host文件 新添域名不生效
    周总结
    周总结
    周总结
    周总结
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/12760930.html
Copyright © 2011-2022 走看看