Commit c26c5270 by rakesh.pv

complete modifies

parent 2913f1a8
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (mongodb_aggregations_functions)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (task8_mongodb_aggregations_functions)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (mongodb_aggregations_functions)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.11 (task8_mongodb_aggregations_functions)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -9,6 +9,7 @@ def create_excel_file():
try:
with open(path_file) as data:
file_data = json.load(data)
df = pd.DataFrame(file_data, columns=["business_name", "result", "date"])
df.to_excel("temp/my_report_in_excel_data.xlsx", index=False)
except Exception as e:
......
import json
from fastapi import APIRouter
from scripts.config.configurations import path_file
from scripts.core.handlers.json_to_excel import create_excel_file
from scripts.services.main import coll
router=APIRouter()
# insert json data into database
@router.post("/upload_my_json", tags=["UPLOAD YOUR JSON"])
async def insert_data():
try:
with open(path_file) as data:
file_data = json.load(data)
coll.insert_many(file_data)
return {"JSON data inserted"}
except Exception as e:
print(str(e))
@router.get("/max_violation_issued")
async def max_violation():
try:
pipeline = [
{
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
},
{
'$sort':
{
'count': -1
}
},
{
'$limit': 1
},
{
'$project':
{
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
@router.get("/no_violations")
async def no_violation():
try:
pipeline = [
{
'$match': {
'result': 'No violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
@router.get("/with_violations")
async def with_violation():
try:
pipeline = [
{
'$match': {
'result': 'Violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
# create Excel file
@router.get("/convert_JSON_to_excel")
async def convert_excel():
create_excel_file()
return {"Excel file generated "}
......@@ -2,9 +2,9 @@ import json
from fastapi import FastAPI
from scripts.config.configurations import path_file
from scripts.config.db_connection import db_connect
from scripts.core.handlers.json_to_excel import create_excel_file
from scripts.routes import routes
from scripts.utils.mongo_utils import create_collection
app = FastAPI()
......@@ -18,109 +18,5 @@ coll = create_collection(db)
async def root():
return {"data": "Mongo aggregation task"}
app.include_router(routes.router)
# insert json data into database
@app.post("/upload_my_json", tags=["UPLOAD YOUR JSON"])
async def insert_data():
try:
with open(path_file) as data:
file_data = json.load(data)
coll.insert_many(file_data)
return {"JSON data inserted"}
except Exception as e:
print(str(e))
@app.get("/max_violation_issued")
async def max_violation():
try:
pipeline = [
{
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
},
{
'$sort':
{
'count': -1
}
},
{
'$limit': 1
},
{
'$project':
{
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
@app.get("/no_violations")
async def no_violation():
try:
pipeline = [
{
'$match': {
'result': 'No violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
@app.get("/with_violations")
async def with_violation():
try:
pipeline = [
{
'$match': {
'result': 'Violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(coll.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
# create Excel file
@app.get("/convert_JSON_to_excel")
async def convert_excel():
create_excel_file()
return {"Excel file generated "}
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