Commit b3b11c19 by arun.uday

commit2

parent 99ff5a5c
# using redis database
# using mongo aggregation operations
import uvicorn
......
......@@ -3,7 +3,10 @@ base_path = scripts/
sub_path = temp/
[file]
file_name_json = data.json
file_name_xlsx = data.xlsx
[connection]
mongodb = mongodb://localhost:27017
\ No newline at end of file
mongodb = mongodb://localhost:27017
[mongodb]
project = $project
\ No newline at end of file
File deleted
......@@ -12,8 +12,11 @@ sub_path = config.get("path", "sub_path")
# file name
file_name_json = config.get("file", "file_name_json")
full_path_json = base_path + sub_path + file_name_json
file_name_xlsx = config.get("file", "file_name_xlsx")
full_path_xlsx = base_path + sub_path + file_name_xlsx
# db connection
client_connect = config.get("connection", "mongodb")
# mongo
project = config.get("mongodb", "project")
import pandas as pd
def generate_excel(cursor_data):
def generate_excel(path_xlsx, cursor_data):
data_list = []
if cursor_data.alive > 0:
for business_names in cursor_data:
data_list.append(business_names)
data_frame = pd.DataFrame(data_list)
data_frame.to_excel("data.xlsx", index=False)
data_frame = pd.DataFrame(data_list, columns=['result', 'business_name', 'date'])
data_frame.to_excel(path_xlsx, index=False)
else:
print("Error")
from scripts.utils.mongo_operations import MongoDb
def db_select_no_violate(db):
cursor_data = db.aggregate([
{
'$group': {
'_id': '$business_name',
'violations': {
'$sum': {
'$cond': [
{
'$eq': [
'$result', 'No Violation Issued'
]
}, 1, 0
]
}
}
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
])
mongo_obj = MongoDb()
cursor_data = mongo_obj.no_violations_db(db)
if cursor_data.alive > 0:
print("Data collected....")
for business_names in cursor_data:
......
from scripts.utils.mongo_operations import MongoDb
def results_data(db):
cursor_data = db.aggregate([
{
'$project': {
'_id': 0,
'result': 1,
'business_name': 1,
'date': 1
}
}
])
mongo_obj = MongoDb()
cursor_data = mongo_obj.view_db_data(db)
if cursor_data.alive > 0:
print("Data collected....")
return cursor_data
......
from scripts.utils.mongo_operations import MongoDb
def db_select_violate(db):
cursor_data = db.aggregate([
{
'$group': {
'_id': '$business_name',
'violations': {
'$sum': {
'$cond': [
{
'$eq': [
'$result', 'Violation Issued'
]
}, 1, 0
]
}
}
}
}, {
'$sort': {
'violations': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
])
mongo_obj = MongoDb()
cursor_data = mongo_obj.violations_db(db)
if cursor_data.alive > 0:
print("Data collected....")
print(cursor_data.next())
......
from fastapi import FastAPI, Request, UploadFile, File
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from scripts.config.applications_config import full_path_json
from scripts.config.applications_config import full_path_xlsx
from scripts.core.handlers.generate_excel_file import generate_excel
from scripts.core.handlers.uploaded_file_ import extract_uploaded_data
from scripts.services.mongo_db_operations import MongoDbStart
......@@ -16,12 +17,12 @@ def template_render(request: Request):
return templates.TemplateResponse("upload_file.html", {"request": request})
# handling the form submit using api
# handling the form submit using api and aggregations
@app.post("/submit_file/")
async def submit_file_data(file_data: UploadFile = File(...)):
try:
data_read = await file_data.read()
decoded_data = extract_uploaded_data(full_path_json, data_read)
decoded_data = extract_uploaded_data(data_read)
db_class = MongoDbStart()
db_created = db_class.mongo_create()
db_class.mongo_insert(db_created, decoded_data)
......@@ -31,7 +32,7 @@ async def submit_file_data(file_data: UploadFile = File(...)):
db_class.mongo_no_violation(db_created)
print("\nGenerating Excel file")
cursor_data = db_class.mongo_view_results(db_created)
generate_excel(cursor_data)
generate_excel(full_path_xlsx, cursor_data)
except Exception as e:
print(e)
else:
......
# class for mongo operations
from scripts.config.applications_config import project
class MongoDb:
# find business name with most violations
@staticmethod
def violations_db(db):
cursor_data = db.aggregate([
{
'$group': {
'_id': '$business_name',
'violations': {
'$sum': {
'$cond': [
{
'$eq': [
'$result', 'Violation Issued'
]
}, 1, 0
]
}
}
}
}, {
'$sort': {
'violations': -1
}
}, {
'$limit': 1
}, {
project: {
'business_name': '$_id',
'_id': 0
}
}
])
return cursor_data
# business names with no violations
@staticmethod
def no_violations_db(db):
cursor_data = db.aggregate([
{
'$group': {
'_id': '$business_name',
'violations': {
'$sum': {
'$cond': [
{
'$eq': [
'$result', 'No Violation Issued'
]
}, 1, 0
]
}
}
}
}, {
project: {
'business_name': '$_id',
'_id': 0
}
}
])
return cursor_data
# business name and dates based on results
@staticmethod
def view_db_data(db):
cursor_data = db.aggregate([
{
project: {
'_id': 0,
'result': 1,
'business_name': 1,
'date': 1
}
}
])
return cursor_data
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