Commit 416b5612 by mohammed.shibili

endpoints

parent a8c202de
[mongo]
server=mongodb://localhost:27017
database =aggregation
collection =my_table
\ No newline at end of file
collection =my_table
[end_points]
post_insert=/insert_data
get_violation=/get_violation
get_noViolation=/no_violation
get_to_exel=/to_exel
exel_file=/data.xlsx
[folder_path]
folder=temp
\ No newline at end of file
import configparser
config = configparser.RawConfigParser()
config.read("conf/datas.conf")
post_insert = config.get("end_points", "post_insert")
get_violation = config.get("end_points", "get_violation")
get_noViolation = config.get("end_points", "get_noViolation")
get_to_exel = config.get("end_points", "get_to_exel")
exel_path = config.get("end_points", "exel_file")
# folder
folder_name = config.get("folder_path", "folder")
def aggregation():
# aggregation for violation issued
data_violation = [
{
'$match': {
'result': 'Violation Issued'
}
}, {
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'count': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
# aggregation for no violation issued
data_no_violation = [
{
'$match': {
'result': 'No Violation Issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
# aggregation for convert to exel
data_to_exel = [
{
'$project': {
'business_name': '$business_name',
'date': '$date',
'result': '$result',
'_id': 0
}
}
]
return data_violation, data_no_violation, data_to_exel
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("/insert_data", tags=['FIRST'])
@app.post(post_insert, tags=['Aggregation'])
async def post_all():
try:
collection_name.insert_many(dict_from_file)
......@@ -15,86 +21,37 @@ async def post_all():
print("data insertion error", e)
@app.get("/get_violation", tags=['FIRST'])
@app.get(get_violation, tags=['Aggregation'])
async def get_violation():
try:
data = [
{
'$match': {
'result': 'Violation Issued'
}
}, {
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'count': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
violation = next(collection_name.aggregate(data))
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("/no_violation")
@app.get(get_noViolation, tags=['Aggregation'])
async def get_no_violation():
try:
data = [
{
'$match': {
'result': 'No Violation Issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
no_violation = list(collection_name.aggregate(data))
print(no_violation)
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("/to_exel", tags=['FIRST'])
@app.get(get_to_exel, tags=['Aggregation'])
async def convert_exel():
data = [
{
'$project': {
'business_name': '$business_name',
'date': '$date',
'result': '$result',
'_id': 0
}
}
]
values = collection_name.aggregate(data)
list_data = []
for data in values:
list_data.append(data)
dataframe = pd.DataFrame(list_data)
dataframe.to_excel("temp/data.xlsx")
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()
......
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