Commit 5f14357e by mohammed.shibili

corrections

parent 416b5612
import uvicorn
if __name__ == "__main__":
uvicorn.run("scripts.service.main:app", port=8000, reload=True)
\ No newline at end of file
uvicorn.run("scripts.service.ApiCalls:app", port=8000, reload=True)
\ No newline at end of file
class endpoints:
@staticmethod
def api_end():
post_insert = "/insert_data"
get_violation = "/get_violation"
get_noViolation = "/no_violation"
get_to_exel = "/to_exel"
exel_file = "/data.xlsx"
import json
import pandas as pd
from scripts.config.end_points import folder_name, exel_path
from scripts.database.mongo_db.aggregation import aggregation
from util.mongo_util import mongo_connection
class operations_on_api:
def __init__(self):
self.collection_name = mongo_connection()
self.data_violation = aggregation().data_violation()
self.data_no_violation = aggregation().resul_no_violation()
self.data_to_exel = aggregation().exel_conversion()
def data_insert(self, file):
try:
dict_from_file = json.loads(file)
self.collection_name.insert_many(dict_from_file)
except Exception as e:
return "data insertion error", e
def violation_issued(self):
try:
violation = next(self.collection_name.aggregate(self.data_violation))
return violation
except Exception as e:
return "error while getting no:of violations", e
def no_violation(self):
try:
no_violation = list(self.collection_name.aggregate(self.data_no_violation))
return no_violation
except Exception as e:
return "error in fetching no violation", e
def create_exel_file(self):
try:
values = list(self.collection_name.aggregate(self.data_to_exel))
dataframe = pd.DataFrame(values)
dataframe.to_excel(folder_name + exel_path)
return f'data inserted to path {folder_name}{exel_path}'
except Exception as e:
return "exel conversion failed", e
import pandas as pd
import tkinter as tk
from tkinter import filedialog
# reading csv
def data_upload():
# section for file upload
try:
print("upload json file")
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
dataset = pd.read_json(file_path)
dict_dataset = dataset.to_dict(orient='records')
return dict_dataset
except Exception as e:
print("reading error", e)
def aggregation():
class aggregation:
# aggregation for violation issued
data_violation = [
{
'$match': {
'result': 'Violation Issued'
}
}, {
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
@staticmethod
def data_violation():
resul_violation = [
{
'$match': {
'result': 'Violation Issued'
}
}, {
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'count': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
}, {
'$sort': {
'count': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
]
return resul_violation
# aggregation for no violation issued
data_no_violation = [
{
'$match': {
'result': 'No Violation Issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
@staticmethod
def resul_no_violation():
data_no_violation = [
{
'$match': {
'result': 'No Violation Issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
}
]
]
return data_no_violation
# aggregation for convert to exel
data_to_exel = [
{
'$project': {
'business_name': '$business_name',
'date': '$date',
'result': '$result',
'_id': 0
@staticmethod
def exel_conversion():
data_to_exel = [
{
'$project': {
'business_name': '$business_name',
'date': '$date',
'result': '$result',
'_id': 0
}
}
}
]
return data_violation, data_no_violation, data_to_exel
]
return data_to_exel
from fastapi import FastAPI, UploadFile
from scripts.config.end_points import post_insert, get_violation, get_noViolation, get_to_exel
from scripts.core.handlers.api_function import operations_on_api
app = FastAPI()
# for insert datas in file
@app.post(post_insert, tags=['Aggregation'])
async def post_all(file: UploadFile):
operations_on_api().data_insert(await file.read())
return "data inserted"
# to get business name with maximum violation issued
@app.get(get_violation, tags=['Aggregation'])
async def get_violation():
print(operations_on_api().violation_issued())
return "data extracted"
# to get business names with no violations
@app.get(get_noViolation, tags=['Aggregation'])
async def get_no_violation():
print(operations_on_api().no_violation())
return "values extracted"
# convert to exel file
@app.get(get_to_exel, tags=['Aggregation'])
async def convert_exel():
print(operations_on_api().create_exel_file())
return "exel file created"
from fastapi import FastAPI
import pandas as pd
from scripts.config.end_points import post_insert, get_violation, get_noViolation, get_to_exel, exel_path, folder_name
from scripts.core.handlers.aggregation import aggregation
from scripts.core.handlers.file_upload import data_upload
from util.mongo_util import mongo_connection
app = FastAPI()
# calling aggregation values
data_violation, data_no_violation, data_to_exel = aggregation()
@app.post(post_insert, tags=['Aggregation'])
async def post_all():
try:
collection_name.insert_many(dict_from_file)
return "data inserted"
except Exception as e:
print("data insertion error", e)
@app.get(get_violation, tags=['Aggregation'])
async def get_violation():
try:
violation = next(collection_name.aggregate(data_violation))
print(violation)
return violation
except Exception as e:
print("error while getting no:of violations", e)
@app.get(get_noViolation, tags=['Aggregation'])
async def get_no_violation():
try:
no_violation = list(collection_name.aggregate(data_no_violation))
return no_violation
except Exception as e:
print("error in fetching no violation", e)
@app.get(get_to_exel, tags=['Aggregation'])
async def convert_exel():
try:
values = collection_name.aggregate(data_to_exel)
list_data = []
for data in values:
list_data.append(data)
dataframe = pd.DataFrame(list_data)
dataframe.to_excel(folder_name + exel_path)
return f'data inserted to path {folder_name}{exel_path}'
except Exception as e:
print("exel conversion failed", e)
collection_name = mongo_connection()
dict_from_file = data_upload()
No preview for this file type
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