diff --git a/api.py b/api.py index 43318f649ee9be1e29c1c8e8cada362a144571cc..72d37379bbdeeb3d8052bf61849d83be318ec055 100755 --- a/api.py +++ b/api.py @@ -10,7 +10,7 @@ from flask_restful import Resource, Api from utils import get_logger, get_list_from_url, get_reg_auth_dict, generate_login_url from markupsafe import escape from datetime import timedelta, datetime, date -from urllib.parse import urlparse, parse_qs +from urllib.parse import urlparse, parse_qs, unquote app = Flask(__name__) api = Api(app) @@ -125,7 +125,7 @@ def parseLog(lines,criteria): rowDate = datetime.strptime(clearDateString(row[1]), '%a %b %d %H:%M:%S %Y').strftime('%Y-%m-%d') # check for entries to be in range of date of rotated log - if 'prev_date' in criteria and criteria['date_from'] != rowDate: + if 'cur_date' in criteria and criteria['cur_date'] != rowDate: continue if ('date_from' in criteria and 'date_to' in criteria and criteria['date_from'] <= rowDate <= criteria['date_to']) or ('date_from' not in criteria and 'date_to' not in criteria and rowDate): @@ -138,25 +138,25 @@ def parseLog(lines,criteria): rowDateCheck = rowDate rowGET = parseReqURL(row[3]) - rowRequestSource = ('web' if len(row[5]) > 5 else 'api') - request_count['web'] += (1 if rowRequestSource == 'web' else 0) - request_count['api'] += (1 if rowRequestSource == 'api' else 0) + + if ('idp' not in criteria and 'reg_auth' not in criteria) or ('idp' in criteria and rowGET['idp'] and rowGET['idp'].find(criteria['idp']) >= 0) or ('reg_auth' in criteria and rowGET['reg_auth'] and rowGET['reg_auth'].find(criteria['reg_auth']) >= 0): + rowRequestSource = ('web' if len(row[5]) > 5 else 'api') + request_count['web'] += (1 if rowRequestSource == 'web' else 0) + request_count['api'] += (1 if rowRequestSource == 'api' else 0) if rowGET['idp']: - request_param['idp'] += 1 - - if ('idp' in criteria and criteria['idp'] in rowGET['idp']) or 'idp' not in criteria: + if ('idp' in criteria and rowGET['idp'].find(criteria['idp']) >= 0) or 'idp' not in criteria: if rowGET['idp'] not in idp.keys(): idp[rowGET['idp']] = 0 idp[rowGET['idp']] += 1 + request_param['idp'] += 1 if rowGET['reg_auth']: - request_param['reg_auth'] += 1 - - if ('reg_auth' in criteria and criteria['reg_auth'] in rowGET['reg_auth']) or 'reg_auth' not in criteria: + if ('reg_auth' in criteria and rowGET['reg_auth'].find(criteria['reg_auth']) >= 0) or 'reg_auth' not in criteria: if rowGET['reg_auth'] not in reg_auth.keys(): reg_auth[rowGET['reg_auth']] = 0 reg_auth[rowGET['reg_auth']] += 1 + request_param['reg_auth'] += 1 result[rowDate] = { 'request_count' : request_count, @@ -183,26 +183,34 @@ def parseReqURL(url): return result def filterParsedData(json_data,criteria): - # idp = {} - # reg_auth = {} + idp = {} + reg_auth = {} + out_data = {} - # if 'idp' in criteria and criteria['idp'] in json_data['idp'].keys(): - # for entry in json_data[criteria['prev_date']]['idp']: - # # if criteria['idp'] == entry.key(): - # if entry.key().find(criteria['idp']) >= 0: - # idp[entry.key()] = entry - # # search substring - # if 'reg_auth' in criteria and criteria['reg_auth'] in json_data['reg_auth'].keys(): - # for entry in json_data[criteria['reg_auth']]: - # if criteria['reg_auth'] == entry.key(): - # reg_auth[criteria['reg_auth']] = entry + if criteria['cur_date'] in json_data: + if 'idp' in criteria and len(criteria['idp']): + for idpEntry in json_data[criteria['cur_date']]['idp'].keys(): + if idpEntry.find(criteria['idp']) >= 0: + idp[criteria['idp']] = json_data[criteria['cur_date']]['idp'][criteria['idp']] + else: + idp = json_data[criteria['cur_date']]['idp'] + + if 'reg_auth' in criteria and len(criteria['reg_auth']): + for regAuthEntry in json_data[criteria['cur_date']]['reg_auth'].keys(): + if regAuthEntry.find(criteria['reg_auth']) >= 0: + reg_auth[criteria['reg_auth']] = json_data[criteria['cur_date']]['reg_auth'][criteria['reg_auth']] + else: + reg_auth = json_data[criteria['cur_date']]['reg_auth'] - # json_data[criteria['prev_date']] = { - # 'request_uniq' : {'idp':len(idp),'reg_auth':len(reg_auth)}, - # 'idp' : idp, - # 'reg_auth' : reg_auth - # } - return json_data + out_data[criteria['cur_date']] = { + 'request_count' : json_data[criteria['cur_date']]['request_count'], + 'request_param' : json_data[criteria['cur_date']]['request_param'], + 'request_uniq' : {'idp':len(idp),'reg_auth':len(reg_auth)}, + 'idp' : idp, + 'reg_auth' : reg_auth, + } + + return out_data ### Classes @@ -427,14 +435,12 @@ class FedStats(Resource): # /api/webdata class WebData(Resource): def get(self): - list_feds = get_list_from_url(e_p.ECCS_LISTFEDSURL, e_p.ECCS_LISTFEDSFILE) - regAuthDict = get_reg_auth_dict(list_feds) - file_path = f"{e_p.ECCS_LOGSDIR}/eccs-uwsgi-req.log" # will this name be moved to properties definer file ? criteria = {} criteria['date_from'] = criteria['date_to'] = e_p.DAY + # TBM to config eccsLogRotated = True - useParsedFile = True + useParsedFile = False in_data = request.args @@ -455,12 +461,12 @@ class WebData(Resource): if ('requestSource' in in_data and in_data['requestSource'] == 'divided'): criteria['request_source'] = 'divided' - - if ('regAuth' in in_data and in_data['regAuth'] in regAuthDict): - criteria['reg_auth'] = in_data['reg_auth'] - - if ('idp' in in_data): - criteria['idp'] = in_data['idp'] + + if 'regAuth' in in_data: + criteria['reg_auth'] = unquote(in_data['regAuth']) + + if 'idp' in in_data: + criteria['idp'] = unquote(in_data['idp'].strip()) lines = [] results = {} @@ -469,12 +475,12 @@ class WebData(Resource): if eccsLogRotated == True: while cur_date <= criteria['date_to']: json_data = {} - criteria['prev_date'] = (datetime.strptime(cur_date, '%Y-%m-%d') - timedelta(days=1)).strftime('%Y-%m-%d') - tmpDate = datetime.strptime(cur_date, '%Y-%m-%d').strftime('%Y%m%d') + criteria['logfile_date'] = (datetime.strptime(cur_date, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d') + criteria['cur_date'] = cur_date + tmpDate = datetime.strptime(criteria['logfile_date'], '%Y-%m-%d').strftime('%Y%m%d') file_path = f"{e_p.ECCS_LOGSDIR}/eccs-uwsgi-req.log-{tmpDate}" if useParsedFile == True: - tmpDate = (datetime.strptime(cur_date, '%Y-%m-%d') - timedelta(days=1)).strftime('%Y%m%d') json_file_path = f"{e_p.ECCS_DIR}/parsed/eccs-uwsgi-req-json-{tmpDate}" try: f = open(json_file_path) @@ -498,11 +504,10 @@ class WebData(Resource): storeParsedDay(json_file_path, json_data) except FileNotFoundError as e: - #print(e) + # print(e) pass - + results.update(json_data) - cur_date = (datetime.strptime(cur_date, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d') else: try: diff --git a/web/index.php b/web/index.php index bda9fa6b3dbd4fcb2478a26b641299ebf687d493..2e902d95f8aee4ec242e0b7a80113629fed72d12 100644 --- a/web/index.php +++ b/web/index.php @@ -111,7 +111,6 @@ $data['check_result'] = htmlspecialchars($_GET["check_result"]); var check_result = "<?php echo $data['check_result'] ?>"; </script> <script type="text/javascript" src="eccs.js" /></script> - <script type="text/javascript" src="dataX.js" /></script> <script type="text/javascript" src="statistics.js" /></script> </div> <!-- END eccs-central --> <div id="statisticsModal" class="modal"> diff --git a/web/statistics.js b/web/statistics.js index 6ad8f925839dbc05948246c026ab7f1d6b7aedcd..14c307d3401b86bead28bbbba7047f6dcf319549 100644 --- a/web/statistics.js +++ b/web/statistics.js @@ -1,27 +1,59 @@ var statChart = {}; -var baseURL = window.location.protocol + "//" + window.location.host; +var baseURL = window.location.protocol + "//" + window.location.host + "/eccs"; function getStatistics() { - - let checkDate = $.datepicker.formatDate("yy-mm-dd", $('#datepicker').datepicker().datepicker('getDate')); + showLoading(); + let reqData = {} + reqData['dateFrom'] = $.datepicker.formatDate("yy-mm-dd", $('#datepicker').datepicker().datepicker('getDate')); + if ($("#eccstable thead th:last-child input").val() != '') { + reqData['regAuth'] = $("#eccstable thead th:last-child input").val(); + } + if ($("#eccstable thead th:nth-last-child(2) input").val() != '') { + reqData['idp'] = $("#eccstable thead th:nth-last-child(2) input").val(); + } $.ajax({ - url: baseURL + "/dataX.php", - type : 'POST', - data : { - dateFrom : checkDate - }, + url: baseURL + "/api/webdata", + type : 'GET', + contentType: "application/json", + data : reqData, success: function(result) { drawChart(result) }, error: function (jqXHR,error,thrownError) { - showErr(); + showMessage("Something happened. Can't retrieve data"); } }); } -function showErr() { +function showMessage(text) { + hideChart(); + hideLoading(); + $("#statisticsModal .chart-error span").text(text) $("#statisticsModal .chart-error").css('display', 'block') +} + +function showLoading() { + hideChart(); + hideMessage(); + $("#statisticsModal .chart-loading").css('display', 'block') +} + +function showChart() { + hideMessage(); + hideLoading(); + $("#statisticsModal .chart-container").css('display', 'block') +} + +function hideMessage() { + $("#statisticsModal .chart-error").css('display', 'none') +} + +function hideLoading() { + $("#statisticsModal .chart-loading").css('display', 'none') +} + +function hideChart() { $("#statisticsModal .chart-container").css('display', 'none') } @@ -35,10 +67,13 @@ function drawChart(data) { try { chartData = $.parseJSON(data) } catch (err) { - showErr() + showMessage("Invalid data format received. Can't use to draw chart"); } - chartData = $.parseJSON(data) + if ($.isEmptyObject(chartData)) { + showMessage("No data for parameters specified. Please refine query string."); + return false; + } $.each( chartData, function( index, value ) { key.push(index) @@ -47,15 +82,21 @@ function drawChart(data) { data3.push(value.request_uniq.idp) }); + let dataSet = [ + { label: 'API', data: data1 }, + { label: 'WEB', data: data2 }, + { label: 'Unique IdP', data: data3 } + ]; + let options = { type: 'line', data: { labels: key, - datasets: [ - { label: 'API', data: data1 }, - { label: 'WEB', data: data2 }, - { label: 'Unique IdP', data: data3 } - ] + datasets: dataSet + // { label: 'API', data: data1 }, + // { label: 'WEB', data: data2 }, + // { label: 'Unique IdP', data: data3 } + // ] }, options: { responsive: true, @@ -67,13 +108,16 @@ function drawChart(data) { } } var ctx = document.getElementById('statistics').getContext('2d'); + + showChart(); - $("#statisticsModal .chart-error").css('display', 'none') - $("#statisticsModal .chart-container").css('display', 'block') - + + if ($.isEmptyObject(statChart)) { statChart = new Chart(ctx, options); } else { + statChart.data.datasets = dataSet; + statChart.data.labels = key; statChart.update(); } }