0

我想使用 grafanalib 创建 grafana 仪表板来对我的仪表板进行版本控制,但输出 json 不会复制我的查询。我有 2 个面板,第一个是 BarGauge,第二个是 Stats 面板。这是我生成所需仪表板的代码。

    from grafanalib.core import (
    Alert, AlertCondition, Dashboard, Graph, BarGauge, Stat,
    GreaterThan, OP_AND, OPS_FORMAT, Row, RTYPE_SUM, SECONDS_FORMAT,
    SHORT_FORMAT, single_y_axis, Target, TimeRange, YAxes, YAxis
)


dashboard = Dashboard(
    title="Frontend Stats",
    rows=[
        Row(panels=[
          BarGauge(
              title="Frontend QPS",
            dataSource='MySQL',
            targets=[
                Target(
                    expr="""select
                            candidate_jd_company_statuses.updatedAt as "time",
                            "ABC" as metric,
                            (COUNT(CASE candidate_jd_company_statuses.candidateStatusId WHEN 70 THEN 1 ELSE NULL END) +COUNT(CASE candidate_jd_company_statuses.candidateStatusId WHEN 60 THEN 1 ELSE NULL END) + COUNT(CASE candidate_jd_company_statuses.candidateStatusId WHEN 100 THEN 1 ELSE NULL END))/COUNT(CASE candidate_jd_company_statuses.candidateStatusId WHEN 99 THEN 1 ELSE NULL END) as value
                            from candidate_jd_company_statuses
                            WHERE
                            $__timeFilter(candidate_jd_company_statuses.updatedAt);""",
                    refId='A',
                    datasource= 'MySQL'
                )          
            ],   
            ),
          Stat(
              title="Frontend latency",
              dataSource='MySQL',
              targets=[
                  Target(
                    expr="""select 
                            recruiter_invites.updatedAt as "time",
                            concat(companies.companyName, '-', jobdescriptions.jobTitle) as metric,
                            COUNT(recruiter_invites.jobdescriptionIds) as value
                            from recruiter_invites
                            left join jobdescriptions
                            on recruiter_invites.jobdescriptionIds = jobdescriptions.id
                            left join companies
                            on jobdescriptions.companyId = companies.id
                            WHERE
                              $__timeFilter(recruiter_invites.updatedAt)
                            GROUP BY concat(companies.companyName, '-', jobdescriptions.jobTitle)
                            ORDER BY value DESC;""",
                    refId='A',
                    datasource= 'MySQL'
                  )
                  
              ],
          ),
        ]),
    ],
).auto_panel_ids()

如您所见,我使用的是 SQL 数据源,并且查询正在工作,因为它们是直接从 Grafana的查询构建器复制粘贴的。这是导入生成的 json 后的屏幕截图: 您可以看到我的自定义查询不在查询生成器中

4

0 回答 0