summaryrefslogtreecommitdiffstats
path: root/openshift-storage-libs/openshiftstoragelibs/utils.py
diff options
context:
space:
mode:
authorValerii Ponomarov <vponomar@redhat.com>2019-03-07 20:30:44 +0530
committervponomar <vponomar@redhat.com>2019-03-18 11:34:37 +0000
commit32b611b2a6498b1de307142e335e09d1e0ec082c (patch)
treeaaf600ab6e6adabab7c3facbf30ae6f056731969 /openshift-storage-libs/openshiftstoragelibs/utils.py
parent0fcdb081517c5904969b89b20326d21b361e448e (diff)
Reorder lib files removing redundant dir layer
Move all the files of 'cns-libs/cnslibs/common' dir to the 'openshift-storage-libs/openshiftstoragelibs', because 'common' is the only dir there, which doesn't really makes sense. And "cns" is old project name, so, replace it with "openshift-storage-libs". Also, fix all the imports of these libs. Change-Id: Ife00a73554e73b21b214b15016b0c8dbbf423446
Diffstat (limited to 'openshift-storage-libs/openshiftstoragelibs/utils.py')
-rw-r--r--openshift-storage-libs/openshiftstoragelibs/utils.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/openshift-storage-libs/openshiftstoragelibs/utils.py b/openshift-storage-libs/openshiftstoragelibs/utils.py
new file mode 100644
index 0000000..2d16c49
--- /dev/null
+++ b/openshift-storage-libs/openshiftstoragelibs/utils.py
@@ -0,0 +1,40 @@
+"""Generic host utility functions.
+
+Generic utility functions not specifc to a larger suite of tools.
+For example, not specific to OCP, Gluster, Heketi, etc.
+"""
+
+import random
+import string
+
+from prometheus_client.parser import text_string_to_metric_families
+
+
+def get_random_str(size=14):
+ chars = string.ascii_lowercase + string.digits
+ return ''.join(random.choice(chars) for _ in range(size))
+
+
+def parse_prometheus_data(text):
+ """Parse prometheus-formatted text to the python objects
+
+ Args:
+ text (str): prometheus-formatted data
+
+ Returns:
+ dict: parsed data as python dictionary
+ """
+ metrics = {}
+ for family in text_string_to_metric_families(text):
+ for sample in family.samples:
+ key, data, val = (sample.name, sample.labels, sample.value)
+ if data.keys():
+ data['value'] = val
+ if key in metrics.keys():
+ metrics[key].append(data)
+ else:
+ metrics[key] = [data]
+ else:
+ metrics[key] = val
+
+ return metrics