AETHER-369 Export Prometheus metrics

Change-Id: I1e06310b17360f99b6aef969e8ecb19bdc96925c
diff --git a/edge-monitoring/edge_monitoring_server.py b/edge-monitoring/edge_monitoring_server.py
index ed2088c..41ed675 100755
--- a/edge-monitoring/edge_monitoring_server.py
+++ b/edge-monitoring/edge_monitoring_server.py
@@ -15,7 +15,10 @@
 # limitations under the License.
 
 import time
-from flask import Flask, jsonify, abort, request
+from flask import Flask, jsonify, abort, request, Response
+import prometheus_client as prom
+
+NO_RESULT_THRESHOLD = 720
 
 app = Flask(__name__)
 edges = [
@@ -29,6 +32,43 @@
     }
 ]
 
+status_codes = {
+    "no result": -2,
+    "error": -1,
+    "disconnected": 0,
+    "connecting": 1,
+    "connected": 2
+}
+
+cp_status = prom.Gauge("aetheredge_status_control_plane", "Control plane status code", ["name"])
+up_status = prom.Gauge("aetheredge_status_user_plane", "User plane status code", ["name"])
+last_update = prom.Gauge("aetheredge_last_update", "Last reported test result", ["name"])
+
+def time_out_stale_results():
+    for edge in edges:
+        time_elapsed = time.time() - edge["last_update"]
+        if time_elapsed > NO_RESULT_THRESHOLD:
+            edge['status']['control_plane'] = "no result"
+            edge['status']['user_plane'] = "no result"
+
+
+@app.route('/edges/metrics', methods=['GET'])
+def get_prometheus_metrics():
+    res = []
+    time_out_stale_results()
+    for edge in edges:
+        if edge['name'] == "production-edge-example":
+            continue
+
+        cp_status.labels(edge['name']).set(status_codes[edge['status']['control_plane']])
+        up_status.labels(edge['name']).set(status_codes[edge['status']['user_plane']])
+        last_update.labels(edge['name']).set(edge['last_update'])
+
+    res.append(prom.generate_latest(cp_status))
+    res.append(prom.generate_latest(up_status))
+    res.append(prom.generate_latest(last_update))
+    return Response(res, mimetype="text/plain")
+
 
 @app.route('/edges/healthz', methods=['GET'])
 def get_health():
@@ -37,11 +77,13 @@
 
 @app.route('/edges', methods=['GET'])
 def get_edges():
+    time_out_stale_results()
     return jsonify({'edges': edges})
 
 
 @app.route('/edges/<string:name>', methods=['GET'])
 def get_edge(name):
+    time_out_stale_results()
     edge = [edge for edge in edges if edge['name'] == name]
     if len(edge) == 0:
         abort(404)