From 4ca705f3c6fc3d781bd65c1313cba96fca40e2b5 Mon Sep 17 00:00:00 2001
From: "valentin.pocotilenco" <valentin.pocotilenco@renam.md>
Date: Wed, 18 Oct 2023 22:54:20 +0300
Subject: [PATCH] get data parsing

---
 api.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/api.py b/api.py
index 5730393..4177733 100755
--- a/api.py
+++ b/api.py
@@ -9,6 +9,7 @@ from flask import Flask, request, jsonify
 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, date
 
 app = Flask(__name__)
 api = Api(app)
@@ -58,6 +59,12 @@ def getSimpleDict(aux):
     }
     return simpleDict
 
+def isValidDate(date_text):
+      try:
+         date.fromisoformat(date_text)
+      except ValueError:
+         return False
+      return True
 
 ### Classes
 
@@ -278,6 +285,63 @@ class FedStats(Resource):
                results.append(resultDict)
            return jsonify(results)
 
+        
+# /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_OUTPUTDIR}/{e_p.ECCS_RESULTSLOG}"
+      date_from = date_to = e_p.DAY
+      reg_auth = None
+      eccsDataTable = False
+      in_data = request.args
+
+      if ('date_from' in in_data and isValidDate(in_data['date_from'])):
+         date_from = in_data['date_from']
+         if ('date_to' not in in_data):
+            date_to = date_from + timedelta(days=30)
+      
+      if ('date_to' in in_data and isValidDate(in_data['date_to'])):
+         date_to = in_data['date_to']
+         if ('date_from' not in in_data):
+            date_from = date_to - timedelta(days=30)
+      
+      if ('request_source' in in_data and in_data['request_source'] == 'divided'):
+         request_source = 'divided'
+         
+      if ('reg_auth' in in_data):
+         reg_auth = in_data['reg_auth']
+      
+      if ('status' in in_data and in_data['status'].upper() in ['OK','DISABLED','ERROR','UNKNOWN']):
+         status = in_data['status'].upper()
+      
+      if ('idp' in in_data):
+         idp = in_data['idp']
+
+      lines = []
+      results = []
+      cur_date = date_from
+      
+      while cur_date <= date_to:
+         file_path = f"{e_p.ECCS_OUTPUTDIR}/eccs_{cur_date}.log"
+         
+         try:
+            with open(file_path,"r",encoding="utf-8") as fo:
+               lines = fo.readlines()
+         
+         except FileNotFoundError as e:
+            if (eccsDataTable):
+               return ''
+            else:
+               return jsonify(error=f'FileNotFound: ECCS script has not been executed on {date} yet')
+            
+            
+            
+            
+         cur_date += timedelta(days=1)      
+
 # /api/
 class Help(Resource):
     def get(self):
@@ -306,6 +370,7 @@ api.add_resource(Help, '/') # Route_1
 api.add_resource(Test, '/test') # Route_2
 api.add_resource(EccsResults, '/eccsresults') # Route_3
 api.add_resource(FedStats, '/fedstats') # Route_4
+api.add_resource(WebData, '/webdata') # Route_4
 
 if __name__ == '__main__':
 
-- 
GitLab