Convert replication dashboard to grafonnet

Change-Id: Icffb8ffbec8541e5b956487e5ce9ec54b3c8b617
diff --git a/dashboards/gerrit-replication.json b/dashboards/gerrit-replication.json
deleted file mode 100644
index c4473ec..0000000
--- a/dashboards/gerrit-replication.json
+++ /dev/null
@@ -1,333 +0,0 @@
-{
-  "__inputs": [
-    {
-      "name": "DS_PROMETHEUS",
-      "label": "Prometheus",
-      "description": "",
-      "type": "datasource",
-      "pluginId": "prometheus",
-      "pluginName": "Prometheus"
-    }
-  ],
-  "__requires": [
-    {
-      "type": "grafana",
-      "id": "grafana",
-      "name": "Grafana",
-      "version": "7.1.5"
-    },
-    {
-      "type": "panel",
-      "id": "graph",
-      "name": "Graph",
-      "version": ""
-    },
-    {
-      "type": "datasource",
-      "id": "prometheus",
-      "name": "Prometheus",
-      "version": "1.0.0"
-    }
-  ],
-  "annotations": {
-    "list": [
-      {
-        "builtIn": 1,
-        "datasource": "-- Grafana --",
-        "enable": true,
-        "hide": true,
-        "iconColor": "rgba(0, 211, 255, 1)",
-        "name": "Annotations & Alerts",
-        "type": "dashboard"
-      }
-    ]
-  },
-  "editable": true,
-  "gnetId": null,
-  "graphTooltip": 0,
-  "links": [],
-  "panels": [
-    {
-      "aliasColors": {},
-      "bars": false,
-      "dashLength": 10,
-      "dashes": false,
-      "datasource": "${DS_PROMETHEUS}",
-      "description": "",
-      "fill": 1,
-      "fillGradient": 1,
-      "gridPos": {
-        "h": 10,
-        "w": 12,
-        "x": 0,
-        "y": 0
-      },
-      "hiddenSeries": false,
-      "id": 1,
-      "interval": "",
-      "legend": {
-        "avg": false,
-        "current": false,
-        "max": false,
-        "min": false,
-        "show": true,
-        "total": false,
-        "values": false
-      },
-      "lines": true,
-      "linewidth": 1,
-      "nullPointMode": "null",
-      "options": {
-        "dataLinks": []
-      },
-      "percentage": false,
-      "pointradius": 2,
-      "points": false,
-      "renderer": "flot",
-      "seriesOverrides": [],
-      "spaceLength": 10,
-      "stack": false,
-      "steppedLine": false,
-      "targets": [
-        {
-          "expr": "plugins_replication_replication_delay_$target{instance=\"$instance\",replica=\"$replica\"}",
-          "legendFormat": "quantile: {{quantile}}",
-          "refId": "B"
-        }
-      ],
-      "thresholds": [],
-      "timeFrom": null,
-      "timeRegions": [],
-      "timeShift": null,
-      "title": "replication delay $target",
-      "tooltip": {
-        "shared": true,
-        "sort": 0,
-        "value_type": "individual"
-      },
-      "type": "graph",
-      "xaxis": {
-        "buckets": null,
-        "mode": "time",
-        "name": null,
-        "show": true,
-        "values": []
-      },
-      "yaxes": [
-        {
-          "format": "ms",
-          "label": "delay",
-          "logBase": 10,
-          "max": null,
-          "min": "5000",
-          "show": true
-        },
-        {
-          "format": "short",
-          "label": null,
-          "logBase": 1,
-          "max": null,
-          "min": null,
-          "show": true
-        }
-      ],
-      "yaxis": {
-        "align": false,
-        "alignLevel": null
-      }
-    },
-    {
-      "aliasColors": {},
-      "bars": false,
-      "dashLength": 10,
-      "dashes": false,
-      "datasource": "${DS_PROMETHEUS}",
-      "description": "",
-      "fill": 1,
-      "fillGradient": 1,
-      "gridPos": {
-        "h": 10,
-        "w": 12,
-        "x": 12,
-        "y": 0
-      },
-      "hiddenSeries": false,
-      "id": 2,
-      "legend": {
-        "avg": false,
-        "current": false,
-        "max": false,
-        "min": false,
-        "show": true,
-        "total": false,
-        "values": false
-      },
-      "lines": true,
-      "linewidth": 1,
-      "nullPointMode": "null",
-      "options": {
-        "dataLinks": []
-      },
-      "percentage": false,
-      "pointradius": 2,
-      "points": false,
-      "renderer": "flot",
-      "seriesOverrides": [],
-      "spaceLength": 10,
-      "stack": false,
-      "steppedLine": false,
-      "targets": [
-        {
-          "expr": "plugins_replication_replication_latency_$target{instance=\"$instance\",replica=\"$replica\"}",
-          "legendFormat": "quantile: {{quantile}}",
-          "refId": "A"
-        }
-      ],
-      "thresholds": [],
-      "timeFrom": null,
-      "timeRegions": [],
-      "timeShift": null,
-      "title": "replication latency $target",
-      "tooltip": {
-        "shared": true,
-        "sort": 0,
-        "value_type": "individual"
-      },
-      "type": "graph",
-      "xaxis": {
-        "buckets": null,
-        "mode": "time",
-        "name": null,
-        "show": true,
-        "values": []
-      },
-      "yaxes": [
-        {
-          "format": "ms",
-          "label": "latency",
-          "logBase": 10,
-          "max": null,
-          "min": "0.1",
-          "show": true
-        },
-        {
-          "format": "short",
-          "label": null,
-          "logBase": 1,
-          "max": null,
-          "min": null,
-          "show": true
-        }
-      ],
-      "yaxis": {
-        "align": false,
-        "alignLevel": null
-      }
-    }
-  ],
-  "schemaVersion": 21,
-  "style": "dark",
-  "tags": [],
-  "templating": {
-    "list": [
-      {
-        "allValue": null,
-        "current": {
-          "text": "",
-          "value": ""
-        },
-        "datasource": "${DS_PROMETHEUS}",
-        "definition": "label_values(git_upload_pack_phase_writing_total, instance)",
-        "hide": 0,
-        "includeAll": false,
-        "label": "Gerrit Instance",
-        "multi": false,
-        "name": "instance",
-        "options": [],
-        "query": "label_values(git_upload_pack_phase_writing_total, instance)",
-        "refresh": 1,
-        "regex": "",
-        "skipUrlSync": false,
-        "sort": 0,
-        "tagValuesQuery": "",
-        "tags": [],
-        "tagsQuery": "",
-        "type": "query",
-        "useTags": false
-      },
-      {
-        "allValue": null,
-        "current": {
-          "text": "",
-          "value": ""
-        },
-        "datasource": "${DS_PROMETHEUS}",
-        "definition": "label_values(git_upload_pack_phase_writing_total{instance=\"$instance\"}, replica)",
-        "hide": 0,
-        "includeAll": false,
-        "label": "Replica",
-        "multi": false,
-        "name": "replica",
-        "options": [],
-        "query": "label_values(git_upload_pack_phase_writing_total{instance=\"$instance\"}, replica)",
-        "refresh": 2,
-        "regex": "",
-        "skipUrlSync": false,
-        "sort": 0,
-        "tagValuesQuery": "",
-        "tags": [],
-        "tagsQuery": "",
-        "type": "query",
-        "useTags": false
-      },
-      {
-        "allValue": null,
-        "current": {
-          "tags": [],
-          "text": "",
-          "value": ""
-        },
-        "datasource": "${DS_PROMETHEUS}",
-        "definition": "metrics(plugins_replication_replication_latency_.*_count)",
-        "hide": 0,
-        "includeAll": false,
-        "label": "Replication target",
-        "multi": false,
-        "name": "target",
-        "options": [],
-        "query": "metrics(plugins_replication_replication_latency_.*_count)",
-        "refresh": 1,
-        "regex": "plugins_replication_replication_latency_(.*)_count",
-        "skipUrlSync": false,
-        "sort": 1,
-        "tagValuesQuery": "",
-        "tags": [],
-        "tagsQuery": "",
-        "type": "query",
-        "useTags": false
-      }
-    ]
-  },
-  "time": {
-    "from": "now-6h",
-    "to": "now"
-  },
-  "timepicker": {
-    "refresh_intervals": [
-      "5s",
-      "10s",
-      "30s",
-      "1m",
-      "5m",
-      "15m",
-      "30m",
-      "1h",
-      "2h",
-      "1d"
-    ]
-  },
-  "timezone": "",
-  "title": "Replication",
-  "uid": "RFLS1GsWk",
-  "version": 4
-}
diff --git a/dashboards/gerrit/replication/gerrit-replication.jsonnet b/dashboards/gerrit/replication/gerrit-replication.jsonnet
new file mode 100644
index 0000000..3e69cc8
--- /dev/null
+++ b/dashboards/gerrit/replication/gerrit-replication.jsonnet
@@ -0,0 +1,63 @@
+local grafana = import '../../../vendor/grafonnet/grafana.libsonnet';
+local dashboard = grafana.dashboard;
+local template = grafana.template;
+local row = grafana.row;
+local graphPanel = grafana.graphPanel;
+local prometheus = grafana.prometheus;
+
+local defaults = import '../../globals/defaults.libsonnet';
+local gridPos = import '../../globals/grid_pos.libsonnet';
+local publishVariables = import '../../globals/publish.libsonnet';
+local variables = import '../globals/variables.libsonnet';
+
+local replication_panel = import './panels/replication.libsonnet';
+
+dashboard.new(
+  'Gerrit - Replication',
+  tags=['gerrit'],
+  schemaVersion=defaults.dashboards.schemaVersion,
+  editable=defaults.dashboards.editable,
+  time_from=defaults.dashboards.timeFrom,
+  time_to=defaults.dashboards.timeTo,
+  refresh=defaults.dashboards.refresh,
+  graphTooltip='shared_tooltip',
+)
+.addTemplate(variables.instance)
+.addTemplate(variables.replica)
+.addTemplate(
+  template.new(
+    name='target',
+    datasource='Prometheus',
+    query='metrics(plugins_replication_replication_latency_.*_count)',
+    regex='^plugins_replication_replication_latency_(.*)_count$',
+    label='Replication Target',
+    multi=true,
+    includeAll=true,
+    refresh='time',
+  )
+)
+.addPanel(
+  replication_panel.new(
+    metric='plugins_replication_replication_delay_$target',
+    title='replication DELAY $target',
+    label='delay',
+  ) + {
+    repeat: 'target',
+    repeatDirection: 'v',
+    maxPerRow: 2,
+  },
+  gridPos=gridPos.new(0, 0)
+)
+.addPanel(
+  replication_panel.new(
+    metric='plugins_replication_replication_latency_$target',
+    title='replication LATENCY $target',
+    label='latency',
+  ) + {
+    repeat: 'target',
+    repeatDirection: 'v',
+    maxPerRow: 2,
+  },
+  gridPos=gridPos.new(0, 1)
+)
++ if std.extVar('publish') then publishVariables else {}
diff --git a/dashboards/gerrit/replication/panels/replication.libsonnet b/dashboards/gerrit/replication/panels/replication.libsonnet
new file mode 100644
index 0000000..3776bec
--- /dev/null
+++ b/dashboards/gerrit/replication/panels/replication.libsonnet
@@ -0,0 +1,46 @@
+local grafana = import '../../../../vendor/grafonnet/grafana.libsonnet';
+local graphPanel = grafana.graphPanel;
+local prometheus = grafana.prometheus;
+
+local lineGraph = import '../../../globals/line-graph.libsonnet';
+local yAxis = import '../../../globals/yaxis.libsonnet';
+
+{
+  new(
+      metric,
+      title,
+      label,
+  ):: lineGraph.new(
+    title=title,
+    yAxis1=yAxis.new(
+      label=label,
+      format='ms',
+      logBase=10,
+    ),
+  )
+  .addTarget(
+    prometheus.target(
+      std.format(
+        '%s{instance="$instance",replica="$replica"}', metric),
+      legendFormat='quantile: {{quantile}}',
+    )
+  )
+  .addSeriesOverride(
+    {
+      alias: 'quantile: 0.75',
+      hiddenSeries: true,
+    }
+  )
+  .addSeriesOverride(
+    {
+      alias: 'quantile: 0.98',
+      hiddenSeries: true,
+    }
+  )
+  .addSeriesOverride(
+    {
+      alias: 'quantile: 0.99',
+      hiddenSeries: true,
+    }
+  )
+}