Commit f2e4c2ee by arjun.b

Project structure changed

parent 7774619a
[server]
server_path=scripts.services.main:app
server_path=scripts.services.mongo_services:app
port_no=8000
[mongo]
uri=mongodb://localhost:27017
......
class APIEndpoints:
# mongo end points
file_upload = "/upload"
max_violation = "/business_name with max violation issued"
no_violation = "/business name with no violation"
convert_excel = "/convert_to_excel"
import json
import pandas as pd
from scripts.config.application_config import file_path
def create_excel_file():
try:
with open(file_path) as data:
file_data = json.load(data)
df = pd.DataFrame(file_data, columns=["business_name", "result", "date"])
df.to_excel("temp/excel_data.xlsx", index=False)
except Exception as e:
print(str(e))
import json
import pandas as pd
from scripts.database.mongo.mongo_aggregation import inspection, project_fields
class MongoHandler:
@staticmethod
async def file_upload(file_content):
data = json.loads(file_content.decode("utf-8"))
inspection.insert_many(data)
@staticmethod
def create_excel_file():
file_data = project_fields()
df = pd.DataFrame(file_data)
df.to_excel("temp/excel_data.xlsx", index=False)
import csv
import json
from fastapi import FastAPI, UploadFile
from scripts.config.application_config import file_path
# create database
from scripts.constants.db_connection import db_connect
from scripts.core.handlers.create_excel_file import create_excel_file
from scripts.utils.mongo_utils import create_collection
app = FastAPI()
# create database
# database connection
db = db_connect()
# create collection
inspection = create_collection(db)
@app.get("/", tags=["root"])
async def root():
return {"data": "Mongo aggregation task"}
@app.post("/upload", tags=["upload file"])
async def upload_file(file: UploadFile):
file_content = file.file.read()
data = json.loads(file_content.decode("utf-8"))
inspection.insert_many(data)
return {"data": "CSV data uploaded successfully"}
def no_violation():
try:
pipeline = [
{
'$match': {
'result': 'No violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(inspection.aggregate(pipeline))
print(result)
return result
except Exception as e:
print(str(e))
@app.get("/business_name with max violation issued", tags=["tasks"])
async def max_violation():
def max_violation():
try:
pipeline = [
{
......@@ -58,39 +62,24 @@ async def max_violation():
]
result = list(inspection.aggregate(pipeline))
print(result)
return {"data": result}
return result
except Exception as e:
print(str(e))
@app.get("/business name with no violation", tags=["tasks"])
async def no_violation():
def project_fields():
try:
pipeline = [
{
'$match': {
'result': 'No violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'business_name': 1,
'result': 1,
'date': 1,
'_id': 0
}
}
]
result = list(inspection.aggregate(pipeline))
print(result)
return {"data": result}
return result
except Exception as e:
print(str(e))
# create Excel file
@app.get("/convert_to_excel")
async def convert_excel():
create_excel_file()
return {"data", "Excel file generated based on business name,result and date"}
print(e)
from fastapi import FastAPI, UploadFile
from scripts.constants.end_points import APIEndpoints
from scripts.database.mongo.mongo_aggregation import max_violation, no_violation
from scripts.core.handlers.mongo_handlers import MongoHandler
from fastapi import FastAPI, UploadFile
from scripts.constants.end_points import APIEndpoints
from scripts.core.handlers.mongo_handlers import MongoHandler
from scripts.database.mongo.mongo_aggregation import max_violation, no_violation
app = FastAPI(
title="Mongo Aggregations")
# root API
@app.get("/", tags=["root"])
async def root():
return {"data": "Mongo aggregation task"}
# upload file and store into database
@app.post(APIEndpoints.file_upload, tags=["upload file"])
async def upload_file(file: UploadFile):
file_content = file.file.read()
await MongoHandler.file_upload(file_content)
return {"data": "CSV data uploaded successfully"}
# business name with maximum violation
@app.get(APIEndpoints.max_violation, tags=["tasks"])
async def business_max_violation():
re = max_violation()
return {"data": re}
# business name with no violation
@app.get(APIEndpoints.no_violation, tags=["tasks"])
async def business_no_violation():
re = no_violation()
return {"data": re}
# create Excel file
@app.get(APIEndpoints.convert_excel, tags=["convert to excel"])
async def convert_excel():
MongoHandler.create_excel_file()
return {"data", "Excel file generated based on business name,result and date"}
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