无法使用 grafanalib 创建仪表板

时间:2021-03-15 16:04:06

标签: python grafana grafana-templating grafana-variable grafana-api

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

    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 的 query-builder 复制粘贴的。 这是我导入生成的 json 后的屏幕截图: You can see that my custom query is not in the query builder

0 个答案:

没有答案
相关问题