Commit f0fedd5e by mohammed.shibili

loggers added

parent 9d66ddc5
...@@ -26,6 +26,7 @@ class OperationsOnApi: ...@@ -26,6 +26,7 @@ class OperationsOnApi:
violation = next(self.collection_name.aggregate(self.data_violation)) violation = next(self.collection_name.aggregate(self.data_violation))
return violation return violation
except Exception as e: except Exception as e:
logger.exception(e)
return "error while getting no:of violations", e return "error while getting no:of violations", e
def no_violation(self): def no_violation(self):
......
class Aggregation: class Aggregation:
# aggregation for violation issued # aggregation for violation issued
@staticmethod def __init__(self):
def data_violation(): self.project = '$project'
self.business_name = '$business_name'
def data_violation(self):
resul_violation = [ resul_violation = [
{ {
'$match': { '$match': {
...@@ -9,7 +12,7 @@ class Aggregation: ...@@ -9,7 +12,7 @@ class Aggregation:
} }
}, { }, {
'$group': { '$group': {
'_id': '$business_name', '_id': self.business_name,
'count': { 'count': {
'$sum': 1 '$sum': 1
} }
...@@ -21,7 +24,7 @@ class Aggregation: ...@@ -21,7 +24,7 @@ class Aggregation:
}, { }, {
'$limit': 1 '$limit': 1
}, { }, {
'$project': { self.project: {
'business_name': '$_id', 'business_name': '$_id',
'_id': 0 '_id': 0
} }
...@@ -30,8 +33,8 @@ class Aggregation: ...@@ -30,8 +33,8 @@ class Aggregation:
return resul_violation return resul_violation
# aggregation for no violation issued # aggregation for no violation issued
@staticmethod
def resul_no_violation(): def resul_no_violation(self):
data_no_violation = [ data_no_violation = [
{ {
'$match': { '$match': {
...@@ -39,10 +42,10 @@ class Aggregation: ...@@ -39,10 +42,10 @@ class Aggregation:
} }
}, { }, {
'$group': { '$group': {
'_id': '$business_name' '_id': self.business_name
} }
}, { }, {
'$project': { self.project: {
'business_name': '$_id', 'business_name': '$_id',
'_id': 0 '_id': 0
} }
...@@ -51,12 +54,12 @@ class Aggregation: ...@@ -51,12 +54,12 @@ class Aggregation:
return data_no_violation return data_no_violation
# aggregation for convert to exel # aggregation for convert to exel
@staticmethod
def exel_conversion(): def exel_conversion(self):
data_to_exel = [ data_to_exel = [
{ {
'$project': { self.project: {
'business_name': '$business_name', 'business_name': self.business_name,
'date': '$date', 'date': '$date',
'result': '$result', 'result': '$result',
'_id': 0 '_id': 0
......
...@@ -10,7 +10,7 @@ def get_logger(): ...@@ -10,7 +10,7 @@ def get_logger():
file_path = "scripts/logging/" file_path = "scripts/logging/"
formatter = logging.Formatter('%(asctime)s - %(levelname)-6s - %(message)s', "%Y-%m-%d %H:%M:%S") formatter = logging.Formatter('%(asctime)s - %(levelname)-6s - %(message)s', "%Y-%m-%d %H:%M:%S")
log_file = os.path.join(f"{file_path}exceptions.log") log_file = os.path.join(f"{file_path}exceptions.log")
temp_handler = RotatingFileHandler(log_file, maxBytes=1, backupCount=10) temp_handler = RotatingFileHandler(log_file, maxBytes=1000, backupCount=10)
temp_handler.setFormatter(formatter) temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler) __logger__.addHandler(temp_handler)
......
from fastapi import APIRouter, UploadFile, Response from fastapi import APIRouter, UploadFile
from scripts.core.handlers.api_function import OperationsOnApi
from scripts.constants.const import Endpoints from scripts.constants.const import Endpoints
from scripts.core.handlers.api_function import OperationsOnApi
files = APIRouter() files = APIRouter()
extract = APIRouter() extract = APIRouter()
...@@ -9,15 +9,13 @@ extract = APIRouter() ...@@ -9,15 +9,13 @@ extract = APIRouter()
# for insert datas in file # for insert datas in file
@files.post(Endpoints().post_insert, tags=['Aggregation']) @files.post(Endpoints().post_insert, tags=['Aggregation'])
async def post_all(file: UploadFile): async def post_all(file: UploadFile):
OperationsOnApi().data_insert(await file.read()) return OperationsOnApi().data_insert(await file.read())
return "data inserted"
# to get business name with maximum violation issued # to get business name with maximum violation issued
@extract.get(Endpoints().get_violation, tags=['Aggregation']) @extract.get(Endpoints().get_violation, tags=['Aggregation'])
async def get_violation(): async def get_violation():
print(OperationsOnApi().violation_issued()) return {"Maximum violation": OperationsOnApi().violation_issued()}
return "data extracted"
# to get business names with no violations # to get business names with no violations
...@@ -31,5 +29,4 @@ async def get_no_violation(): ...@@ -31,5 +29,4 @@ async def get_no_violation():
@files.get(Endpoints().get_to_exel, tags=['Aggregation']) @files.get(Endpoints().get_to_exel, tags=['Aggregation'])
async def convert_exel(): async def convert_exel():
print(OperationsOnApi().create_exel_file()) print(OperationsOnApi().create_exel_file())
return Response(content=open(Endpoints().folder + Endpoints().exel_file, "rb").read(), return "converted to exel"
media_type="application/msexcel")
No preview for this file type
...@@ -10,7 +10,6 @@ def mongo_connection(): ...@@ -10,7 +10,6 @@ def mongo_connection():
client = MongoClient(server_name) client = MongoClient(server_name)
database = client[DbDetails().db_name] database = client[DbDetails().db_name]
collection = database[DbDetails().collection_name] collection = database[DbDetails().collection_name]
print("connection success")
return collection return collection
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment