Skip to content
Snippets Groups Projects
Commit 47759bb4 authored by Marco Malavolti's avatar Marco Malavolti
Browse files

Fixed API and added Calendar for the input date value

parent b2f7a796
Branches
Tags
No related merge requests found
...@@ -5,6 +5,7 @@ import logging ...@@ -5,6 +5,7 @@ import logging
import re import re
from eccs2properties import DAY,ECCS2LOGSDIR,ECCS2OUTPUTDIR from eccs2properties import DAY,ECCS2LOGSDIR,ECCS2OUTPUTDIR
from flask.logging import default_handler
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flask_restful import Resource, Api from flask_restful import Resource, Api
from utils import getLogger from utils import getLogger
...@@ -13,11 +14,7 @@ app = Flask(__name__) ...@@ -13,11 +14,7 @@ app = Flask(__name__)
api = Api(app) api = Api(app)
# /eccs2/test ### Functions
class Test(Resource):
def get(self):
app.logger.info("Test Passed!")
return {'test':'It Works!'}
# Build Email Addresses Link for ECCS2 Web Gui # Build Email Addresses Link for ECCS2 Web Gui
def buildEmailAddress(listContacts): def buildEmailAddress(listContacts):
...@@ -29,72 +26,115 @@ def buildEmailAddress(listContacts): ...@@ -29,72 +26,115 @@ def buildEmailAddress(listContacts):
return hrefList return hrefList
### Classes
# Test
class Test(Resource):
def get(self):
return {'test':'It Works!'}
# /eccs2/api/eccsresults
class EccsResults(Resource): class EccsResults(Resource):
def get(self): def get(self):
app.logger.info("Request 'EccsResults'")
file_path = "%s/eccs2_%s.log" % (ECCS2OUTPUTDIR,DAY) file_path = "%s/eccs2_%s.log" % (ECCS2OUTPUTDIR,DAY)
date = DAY date = DAY
pretty = 0 pretty = 0
status = None status = None
idp = None idp = None
reg_auth = None
eccsDataTable = False
if 'eccsdt' in request.args:
eccsDataTable = True
if 'date' in request.args: if 'date' in request.args:
app.logger.info("'date' parameter inserted")
date = request.args['date'] date = request.args['date']
file_path = "%s/eccs2_%s.log" % (ECCS2OUTPUTDIR,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: if 'status' in request.args:
app.logger.info("'status' parameter inserted") status = request.args['status'].upper()
status = request.args['status'] if (status not in ['OK','DISABLED','ERROR']):
return "Incorrect status format, should be 'ok','disabled','error'"
if 'idp' in request.args: if 'idp' in request.args:
app.logger.info("'idp' parameter inserted")
idp = request.args['idp'] idp = request.args['idp']
app.logger.info(idp) with open(file_path,"r",encoding="utf-8") as fo:
lines = fo.readlines()
fo = open(file_path,"r",encoding="utf-8") found = False
result = [] for line in lines:
lines = fo.readlines() 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: for line in lines:
# Strip the line feed and carriage return characters # Strip the line feed and carriage return characters
line = line.rstrip("\n\r") line = line.rstrip("\n\r")
print(line)
# Loads the json line into aux # Loads the json line into aux
aux = json.loads(line) aux = json.loads(line)
aux['date'] = date 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): 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']): 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): elif (idp):
app.logger.info("eccsresults: results for IdP:'%s'." % idp) if (idp == aux['entityID']):
if (re.search(".*."+idp+".*.", aux['entityID'], re.IGNORECASE)): results.append(aux)
result.append( aux ) elif (reg_auth):
if (reg_auth == aux['registrationAuthority']):
results.append(aux)
elif (status): elif (status):
if (status == aux['status']): if (status == aux['status']):
result.append( aux ) results.append(aux)
else: 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 # /eccs2/api/fedstats
api.add_resource(EccsResults, '/eccs/eccsresults') # Route_2 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__': if __name__ == '__main__':
app.config['JSON_AS_ASCII'] = False #app.config['JSON_AS_ASCII'] = True
app.logger = getLogger("eccs2api.log", ECCS2LOGSDIR, "w", "INFO") #app.logger.removeHandler(default_handler)
#app.logger = getLogger("eccs2api.log", ECCS2LOGSDIR, "w", "INFO")
app.run(port='5002') app.run(port='5002')
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
<body> <body>
<hr> <hr>
<div id="status"> <div id="status">
<input type="checkbox" name="status" value="ERROR">ERROR <input type="checkbox" name="status" value="ERROR">ERROR</input>
<input type="checkbox" name="status" value="OK">OK <input type="checkbox" name="status" value="OK">OK</input>
<input type="checkbox" name="status" value="DISABLE">DISABLE <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> </div>
<hr> <hr>
<button id="btn-show-all-children" type="button">Expand All</button> <button id="btn-show-all-children" type="button">Expand All</button>
......
// Global URL
var url = "/eccs2/api/eccsresults?eccdt=1";
var table;
// use URL constructor and return hostname // use URL constructor and return hostname
function getHostname(url) { function getHostname(url) {
const urlNew = new URL(url); const urlNew = new URL(url);
...@@ -51,11 +55,15 @@ function format ( d ) { ...@@ -51,11 +55,15 @@ function format ( d ) {
'</table>'; '</table>';
} }
function getPastResults() {
url = "/eccs2/api/eccsresults?eccsdt=1&date=" + document.getElementById("myDate").value;
table.ajax.url( url ).load();
}
$(document).ready(function() { $(document).ready(function() {
var table = $('#eccstable').DataTable( { table = $('#eccstable').DataTable( {
"ajax": { "ajax": {
"url": "data.json", "url": url,
"dataSrc": "" "dataSrc": ""
}, },
"lengthMenu": [[10, 20, 30, 40, 50, 100, -1], [10, 20, 30, 40, 50, 100, "All"]], "lengthMenu": [[10, 20, 30, 40, 50, 100, -1], [10, 20, 30, 40, 50, 100, "All"]],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment