From 87d20b1d7b56b8a87545ca27e27240dae542fdb7 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 19 Apr 2022 14:54:24 +0200
Subject: [PATCH] L2SS-758: Add index column to precompute absolute element
 position. This helps in overviews that simply display a flattened array.

---
 .../code/tango-prometheus-client.py                  | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py b/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
index f7dc1e5e7..18eca65bd 100644
--- a/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
+++ b/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
@@ -69,7 +69,7 @@ class CustomCollector(object):
         self.proxy_timeout = proxy_timeout
 
     @staticmethod
-    def _to_metric(dev, attr_info, x, y, value):
+    def _to_metric(dev, attr_info, x, y, idx, value):
         """ Convert the given values to a (labels, value) pair, used to construct a Metric. """
 
         if attr_info.data_type in [ArgType.DevShort, ArgType.DevLong, ArgType.DevUShort, ArgType.DevULong, ArgType.DevLong64, ArgType.DevULong64, ArgType.DevInt, ArgType.DevFloat, ArgType.DevDouble]:
@@ -97,12 +97,12 @@ class CustomCollector(object):
             return None
 
         # (labels, value)
-        return ([dev.dev_name(), attr_info.name, str_value, data_type, f"{x:02}", f"{y:02}"], float_value)
+        return ([dev.dev_name(), attr_info.name, str_value, data_type, f"{x:02}", f"{y:02}", f"{idx:03}"]], float_value)
 
     def metrics_scalar(self, dev, attr_info, attr_value):
         """ Return all metrics for a given SCALAR attribute. """
 
-        new_metric = self._to_metric(dev, attr_info, 0, 0, attr_value.value)
+        new_metric = self._to_metric(dev, attr_info, 0, 0, 0, attr_value.value)
         return [new_metric] if new_metric else []
 
     def metrics_spectrum(self, dev, attr_info, attr_value):
@@ -110,7 +110,7 @@ class CustomCollector(object):
 
         metrics = []
         for x in range(int(attr_value.dim_x)):
-            new_metric = self._to_metric(dev, attr_info, x, 0, attr_value.value[x])
+            new_metric = self._to_metric(dev, attr_info, x, 0, x, attr_value.value[x])
             metrics.append(new_metric) if new_metric else None
 
         return metrics
@@ -124,7 +124,7 @@ class CustomCollector(object):
                 """ NOTE: We switch x and y in the annotation, to allow queries to combine 1D and 2D arrays in their first dimension using the same label (x). We effectively expose
                           the array as [x][y] instead of [y][x]. """
 
-                new_metric = self._to_metric(dev, attr_info, y, x, attr_value.value[y][x])
+                new_metric = self._to_metric(dev, attr_info, y, x, y * attr_value.dim_x + x, attr_value.value[y][x])
                 metrics.append(new_metric) if new_metric else None
 
         return metrics
@@ -182,7 +182,7 @@ class CustomCollector(object):
         logger.info("Start scraping")
         scrape_begin = time.time()
 
-        attribute_metrics = GaugeMetricFamily("device_attribute", 'Device attribute value', labels=['device', 'name', 'str_value', 'type', 'x', 'y'])
+        attribute_metrics = GaugeMetricFamily("device_attribute", 'Device attribute value', labels=['device', 'name', 'str_value', 'type', 'x', 'y', 'idx'])
         scraping_metrics = GaugeMetricFamily("device_scraping", 'Device scraping duration', labels=['device'])
 
         for device_name in self.policy.devices():
-- 
GitLab