From 13e660a8972ef0b1d0a6afbf53112e0e259f899f Mon Sep 17 00:00:00 2001
From: Marco Malavolti <marco.malavolti@gmail.com>
Date: Sat, 4 Jul 2020 13:38:47 +0200
Subject: [PATCH] Fixed no data available for a day

---
 api.py        | 49 ++++++++++++++++++++++++++++---------------------
 web/index.php |  2 +-
 web/script.js |  6 +++++-
 3 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/api.py b/api.py
index ef28b91..c894b63 100755
--- a/api.py
+++ b/api.py
@@ -27,6 +27,20 @@ def buildEmailAddress(listContacts):
     return hrefList
 
 
+def existsInFile(file_path, value, research_item):
+    with open(file_path,"r",encoding="utf-8") as fo:
+         lines = fo.readlines()
+         for line in lines:
+             aux = json.loads(line)
+             if (research_item == "entityID"):
+                if (value == aux['entityID']):
+                   return True
+             if (research_item == "registrationAuthority"):
+                if (value == aux['registrationAuthority']):
+                   return True
+         return False
+
+
 ### Classes
 
 # Test
@@ -55,34 +69,27 @@ class EccsResults(Resource):
        if 'status' in request.args:
           status = request.args['status'].upper()
           if (status not in ['OK','DISABLED','ERROR']):
-             return "Incorrect status format, should be 'ok','disabled','error'"
+              return '{ "error": "Incorrect status provided. It can be \'ok\',\'disabled\',\'error\'" }'
        if 'idp' in request.args:
           idp = request.args['idp']
-          with open(file_path,"r",encoding="utf-8") as fo:
-               lines = fo.readlines()
-               found = False
-               for line in lines:
-                   aux = json.loads(line)
-                   if (idp == aux['entityID']):
-                      found = True
-               if (found == False):
-                   return "Identity Provider not found with the entityID: %s" % idp
+          if (not existsInFile(file_path, idp, "entityID")):
+              return "Identity Provider not found with the entityID: %s" % idp
        if 'reg_auth' in request.args:
           reg_auth = request.args['reg_auth']
-          with open(file_path,"r",encoding="utf-8") as fo:
-               lines = fo.readlines()
-               found = False
-               for line in lines:
-                   aux = json.loads(line)
-                   if (reg_auth == aux['registrationAuthority']):
-                      found = True
-               if (found == False):
-                   return "Identity Providers not found with the Registration Authority: %s" % reg_auth
+          if (not existsInFile(file_path, reg_auth, "registrationAuthority")):
+              return "Identity Providers not found with the Registration Authority: %s" % reg_auth
 
        lines = []
        results = []
-       with open(file_path,"r",encoding="utf-8") as fo:
-            lines = fo.readlines()
+       try:
+          with open(file_path,"r",encoding="utf-8") as fo:
+               lines = fo.readlines()
+
+       except FileNotFoundError as e:
+           if (eccsDataTable):
+              return ''
+           else:
+              return "FileNotFound: ECCS2 script has not been executed for this day"
 
        for line in lines:
           # Strip the line feed and carriage return characters
diff --git a/web/index.php b/web/index.php
index 4676706..0f7ef5d 100644
--- a/web/index.php
+++ b/web/index.php
@@ -2,7 +2,7 @@
 <html>
    <head>
  
-      <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
+      <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js" crossorigin="anonymous"></script>
       <script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
       <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"/>
 
diff --git a/web/script.js b/web/script.js
index a71266e..878a3d2 100644
--- a/web/script.js
+++ b/web/script.js
@@ -1,9 +1,12 @@
 // Global URL
-var url = "/eccs2/api/eccsresults?eccdt=1";
+var url = "/eccs2/api/eccsresults?eccsdt=1";
 var table;
 
 // use URL constructor and return hostname
 function getHostname(url) {
+   if (url == ""){
+      return null
+   }
    const urlNew = new URL(url);
    if (urlNew.hostname){
       return urlNew.hostname;
@@ -53,6 +56,7 @@ function format ( d ) {
             '<td><a href="'+d.sp2.wayflessUrl+'" target="_blank">Click to retry</a></td>'+
         '</tr>'+
     '</table>';
+    }
 }
 
 function getPastResults() {
-- 
GitLab