diff --git a/api.py b/api.py index 30a80002a7f58b1bb41654c83e80a19dd3a74690..ef28b91ca2e2f139140c19df673924e0f6964603 100755 --- a/api.py +++ b/api.py @@ -5,6 +5,7 @@ import logging import re from eccs2properties import DAY,ECCS2LOGSDIR,ECCS2OUTPUTDIR +from flask.logging import default_handler from flask import Flask, request, jsonify from flask_restful import Resource, Api from utils import getLogger @@ -13,11 +14,7 @@ app = Flask(__name__) api = Api(app) -# /eccs2/test -class Test(Resource): - def get(self): - app.logger.info("Test Passed!") - return {'test':'It Works!'} +### Functions # Build Email Addresses Link for ECCS2 Web Gui def buildEmailAddress(listContacts): @@ -29,72 +26,115 @@ def buildEmailAddress(listContacts): return hrefList + +### Classes + +# Test +class Test(Resource): + def get(self): + return {'test':'It Works!'} + + +# /eccs2/api/eccsresults class EccsResults(Resource): def get(self): - app.logger.info("Request 'EccsResults'") file_path = "%s/eccs2_%s.log" % (ECCS2OUTPUTDIR,DAY) date = DAY pretty = 0 status = None idp = None + reg_auth = None + eccsDataTable = False + if 'eccsdt' in request.args: + eccsDataTable = True if 'date' in request.args: - app.logger.info("'date' parameter inserted") date = request.args['date'] file_path = "%s/eccs2_%s.log" % (ECCS2OUTPUTDIR,date) - if 'pretty' in request.args: - app.logger.info("'pretty' parameter inserted") - pretty = request.args['pretty'] if 'status' in request.args: - app.logger.info("'status' parameter inserted") - status = request.args['status'] + status = request.args['status'].upper() + if (status not in ['OK','DISABLED','ERROR']): + return "Incorrect status format, should be 'ok','disabled','error'" if 'idp' in request.args: - app.logger.info("'idp' parameter inserted") idp = request.args['idp'] - app.logger.info(idp) - - fo = open(file_path,"r",encoding="utf-8") - result = [] - lines = fo.readlines() + 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 '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 + + lines = [] + results = [] + with open(file_path,"r",encoding="utf-8") as fo: + lines = fo.readlines() for line in lines: # Strip the line feed and carriage return characters line = line.rstrip("\n\r") - print(line) # Loads the json line into aux aux = json.loads(line) - + aux['date'] = date - aux['contacts']['technical'] = buildEmailAddress(aux['contacts']['technical']) - aux['contacts']['support'] = buildEmailAddress(aux['contacts']['support']) + + # If the results are for ECCS2 DataTable, otherwise... remove only "mailto:" prefix + if (eccsDataTable): + aux['contacts']['technical'] = buildEmailAddress(aux['contacts']['technical']) + aux['contacts']['support'] = buildEmailAddress(aux['contacts']['support']) + else: + aux['contacts']['technical'] = aux['contacts']['technical'].replace("mailto:","") + aux['contacts']['support'] = aux['contacts']['support'].replace("mailto:","") if (idp and status): - app.logger.info("eccsresults: check for 'idp':'%s' with 'status':'%s'" % (idp, status)) if (idp == aux['entityID'] and status == aux['status']): - result.append( aux ) + results.append(aux) + elif (reg_auth and status): + if (reg_auth == aux['registrationAuthority'] and status == aux['status']): + results.append(aux) elif (idp): - app.logger.info("eccsresults: results for IdP:'%s'." % idp) - if (re.search(".*."+idp+".*.", aux['entityID'], re.IGNORECASE)): - result.append( aux ) + if (idp == aux['entityID']): + results.append(aux) + elif (reg_auth): + if (reg_auth == aux['registrationAuthority']): + results.append(aux) elif (status): if (status == aux['status']): - result.append( aux ) + results.append(aux) else: - result.append(aux) + results.append(aux) + + return jsonify(results) - if (pretty): - pp_json = json.dumps(result, indent=4, sort_keys=True) - return jsonify(pp_json) - else: - return jsonify(result) -api.add_resource(Test, '/eccs/test') # Route_1 -api.add_resource(EccsResults, '/eccs/eccsresults') # Route_2 +# /eccs2/api/fedstats +class FedStats(Resource): + def get(self): + return {'fedstats':'It Works!'} + +# Routes +api.add_resource(Test, '/test') # Route_1 +api.add_resource(EccsResults, '/eccsresults') # Route_2 +api.add_resource(FedStats, '/fedstats') # Route_3 if __name__ == '__main__': - - app.config['JSON_AS_ASCII'] = False - app.logger = getLogger("eccs2api.log", ECCS2LOGSDIR, "w", "INFO") + + #app.config['JSON_AS_ASCII'] = True + #app.logger.removeHandler(default_handler) + #app.logger = getLogger("eccs2api.log", ECCS2LOGSDIR, "w", "INFO") app.run(port='5002') diff --git a/web/index.php b/web/index.php index c5d243a18d60ea07f1c8841adfdada09592a5202..46767066bbaba19ea42ac6c957b2b0086428249e 100644 --- a/web/index.php +++ b/web/index.php @@ -15,9 +15,11 @@ <body> <hr> <div id="status"> - <input type="checkbox" name="status" value="ERROR">ERROR - <input type="checkbox" name="status" value="OK">OK - <input type="checkbox" name="status" value="DISABLE">DISABLE + <input type="checkbox" name="status" value="ERROR">ERROR</input> + <input type="checkbox" name="status" value="OK">OK</input> + <input type="checkbox" name="status" value="DISABLE">DISABLE</input> + <button style="float:right;" onclick="getPastResults()">Go</button> + <input style="float:right;" type="date" id="myDate" min="2020-07-03" max="<?php echo date("Y-m-d")?>" value="<?php echo date("Y-m-d")?>"/> </div> <hr> <button id="btn-show-all-children" type="button">Expand All</button> diff --git a/web/script.js b/web/script.js index 817f2b9767a24e37df8d6fe573b9bce6e4ee036e..a71266ec0d0fd5e9369cd8ff59ee9af39b706b4a 100644 --- a/web/script.js +++ b/web/script.js @@ -1,3 +1,7 @@ +// Global URL +var url = "/eccs2/api/eccsresults?eccdt=1"; +var table; + // use URL constructor and return hostname function getHostname(url) { const urlNew = new URL(url); @@ -51,11 +55,15 @@ function format ( d ) { '</table>'; } +function getPastResults() { + url = "/eccs2/api/eccsresults?eccsdt=1&date=" + document.getElementById("myDate").value; + table.ajax.url( url ).load(); +} $(document).ready(function() { - var table = $('#eccstable').DataTable( { + table = $('#eccstable').DataTable( { "ajax": { - "url": "data.json", + "url": url, "dataSrc": "" }, "lengthMenu": [[10, 20, 30, 40, 50, 100, -1], [10, 20, 30, 40, 50, 100, "All"]],