Adaptation to changes in ECS, PMS and RAPP Catalogue
[nonrtric.git] / test / cr / app / cr.py
index fe0fbe4..9e04d57 100644 (file)
 from flask import Flask, request, Response
 from time import sleep
 import time
-import datetime
+from datetime import datetime
 import json
 import traceback
+import logging
+import socket
+
+# Disable all logging of GET on reading counters and db
+class AjaxFilter(logging.Filter):
+    def filter(self, record):
+        return ("/counter/" not in record.getMessage()) and ("/db" not in record.getMessage())
+
+log = logging.getLogger('werkzeug')
+log.addFilter(AjaxFilter())
 
 app = Flask(__name__)
 
@@ -47,6 +57,24 @@ MIME_TEXT="text/plain"
 MIME_JSON="application/json"
 CAUGHT_EXCEPTION="Caught exception: "
 SERVER_ERROR="Server error :"
+TIME_STAMP="cr-timestamp"
+
+# Remote host lookup and print host name
+def remote_host_logging(request):
+
+    if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
+        host_ip=str(request.environ['REMOTE_ADDR'])
+    else:
+        host_ip=str(request.environ['HTTP_X_FORWARDED_FOR'])
+    prefix='::ffff:'
+    if (host_ip.startswith('::ffff:')):
+        host_ip=host_ip[len(prefix):]
+    try:
+        name, alias, addresslist = socket.gethostbyaddr(host_ip)
+        print("Calling host: "+str(name))
+    except Exception:
+        print("Calling host not possible to retrieve IP: "+str(host_ip))
+
 
 #I'm alive function
 @app.route('/',
@@ -71,6 +99,7 @@ def receiveresponse(id):
             cntr_callbacks[id][1]+=1
             msg=msg_callbacks[id][0]
             print("Fetching msg for id: "+id+", msg="+str(msg))
+            del msg[TIME_STAMP]
             del msg_callbacks[id][0]
             return json.dumps(msg),200
         print("No messages for id: "+id)
@@ -96,6 +125,8 @@ def receiveresponse_all(id):
             cntr_callbacks[id][1]+=len(msg_callbacks[id])
             msg=msg_callbacks[id]
             print("Fetching all msgs for id: "+id+", msg="+str(msg))
+            for sub_msg in msg:
+                del sub_msg[TIME_STAMP]
             del msg_callbacks[id]
             return json.dumps(msg),200
         print("No messages for id: "+id)
@@ -118,6 +149,7 @@ def events_write(id):
 
     try:
         print("Received callback for id: "+id +", content-type="+request.content_type)
+        remote_host_logging(request)
         try:
             if (request.content_type == MIME_JSON):
                 data = request.data
@@ -132,6 +164,7 @@ def events_write(id):
             traceback.print_exc()
 
         cntr_msg_callbacks += 1
+        msg[TIME_STAMP]=str(datetime.now())
         if (id in msg_callbacks.keys()):
             msg_callbacks[id].append(msg)
         else:
@@ -210,10 +243,12 @@ def reset():
     global msg_callbacks
     global cntr_msg_fetched
     global cntr_msg_callbacks
+    global cntr_callbacks
 
     msg_callbacks={}
     cntr_msg_fetched=0
     cntr_msg_callbacks=0
+    cntr_callbacks={}
 
     return Response('OK', status=200, mimetype=MIME_TEXT)