From 3eb2ce61027f4d154c629a77a6e2f24a28662f0b Mon Sep 17 00:00:00 2001
From: Negash <i@negash.ru>
Date: Wed, 3 Jan 2024 14:50:24 +0000
Subject: [PATCH] add metrics script

---
 chart/templates/generator-application.yaml |  3 +++
 chart/templates/metrics-config-map.yaml    | 19 ++++++++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/chart/templates/generator-application.yaml b/chart/templates/generator-application.yaml
index 68188ca..17b425d 100644
--- a/chart/templates/generator-application.yaml
+++ b/chart/templates/generator-application.yaml
@@ -168,6 +168,9 @@ spec:
                   - imagePullPolicy: Always
                     name: uptime
                     image: python:alpine
+                    env:
+                      - name: WORKFLOW_NAME
+                        value: '{{`{{workflow.name}}`}}'
                     command:
                     - sh
                     - -c
diff --git a/chart/templates/metrics-config-map.yaml b/chart/templates/metrics-config-map.yaml
index d4f1060..9877f07 100644
--- a/chart/templates/metrics-config-map.yaml
+++ b/chart/templates/metrics-config-map.yaml
@@ -8,9 +8,22 @@ data:
     from prometheus_client.parser import text_string_to_metric_families
     import requests
     import time
+    import os
+    ALDAAS_TTL = int(os.getenv('ALDAAS_TTL','300'))
+    WORKFLOW_NAME = os.getenv("WORKFLOW_NAME")
+    ttl_time = ALDAAS_TTL
     while True:
       metrics = requests.get("http://0.0.0.0:{{ .Values.tunnel.port }}/metrics").content
-      for family in text_string_to_metric_families(metrics):
+      for family in text_string_to_metric_families(metrics.decode("utf-8")):
         for sample in family.samples:
-          print("Name: {0} Labels: {1} Value: {2}".format(*sample))
-      time.sleep(5)
\ No newline at end of file
+          if sample[0] == 'http_to_tcp_active_connections':
+            # if not connections
+            if sample[2] == 0.0:
+              ttl_time = ttl_time - 1
+            else:
+              ttl_time = ALDAAS_TTL
+      time.sleep(1)
+      print('TTL time = ', ttl_time)
+      if ttl_time < 0:
+        print('kill ', WORKFLOW_NAME)
+        requests.post("http://remove-{{ template "aldaas.fullname" . }}-eventsource-svc:12000/remove", json={"wf": WORKFLOW_NAME})
\ No newline at end of file