Commit 99ff5a5c by arun.uday

commit1

parents
# Default ignored files
/shelf/
/workspace.xml
task8aggregations
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (task8redis)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/task8aggregations.iml" filepath="$PROJECT_DIR$/.idea/task8aggregations.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# using redis database
import uvicorn
# starting the application
if __name__ == "__main__":
print("Fast API task")
uvicorn.run("scripts.services.app_services_run:app", port=8000)
# pip install python-multipart
[path]
base_path = scripts/
sub_path = temp/
[file]
file_name_json = data.json
[connection]
mongodb = mongodb://localhost:27017
\ No newline at end of file
File added
# reading conf file
import configparser
config = configparser.RawConfigParser()
config.read("conf/applications.conf")
# path
base_path = config.get("path", 'base_path')
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
# db connection
client_connect = config.get("connection", "mongodb")
# connecting to mongodb server
from pymongo import MongoClient
from scripts.config.applications_config import client_connect
# connect to server
client = MongoClient(client_connect)
# inserting data to mongo
def insert_data_db(db, decoded):
try:
insert_res = db.insert_many(decoded)
except Exception as e:
print("Exception occurred :", e)
else:
return insert_res
import pandas as pd
def generate_excel(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)
else:
print("Error")
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
}
}
])
if cursor_data.alive > 0:
print("Data collected....")
for business_names in cursor_data:
print(business_names)
else:
print("Data fetching failed")
def results_data(db):
cursor_data = db.aggregate([
{
'$project': {
'_id': 0,
'result': 1,
'business_name': 1,
'date': 1
}
}
])
if cursor_data.alive > 0:
print("Data collected....")
return cursor_data
else:
print("Data fetching failed")
return None
<html>
<body>
<!-- form upload -->
<form enctype = "multipart/form-data" action = "/submit_file/" method = "post">
<p>Upload File: <input type = "file" name = "file_data" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>
</body>
</html>
# file upload to csv
import json
def extract_uploaded_data(file_data):
decode = file_data.decode()
decoded_data_json = json.loads(decode)
return decoded_data_json
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
}
}
])
if cursor_data.alive > 0:
print("Data collected....")
print(cursor_data.next())
else:
print("Data fetching failed")
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.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
app = FastAPI()
templates = Jinja2Templates("scripts/core/handlers/")
# html form access using api
@app.get("/upload/", response_class=HTMLResponse)
def template_render(request: Request):
return templates.TemplateResponse("upload_file.html", {"request": request})
# handling the form submit using api
@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)
db_class = MongoDbStart()
db_created = db_class.mongo_create()
db_class.mongo_insert(db_created, decoded_data)
print("\nBusiness Name with most Violations")
db_class.mongo_violation(db_created)
print("\nBusiness Name with No Violations")
db_class.mongo_no_violation(db_created)
print("\nGenerating Excel file")
cursor_data = db_class.mongo_view_results(db_created)
generate_excel(cursor_data)
except Exception as e:
print(e)
else:
return {"msg": "file uploading successful..."}
# class for mongodb operations
from scripts.core.engine.mongodb_connect import client
from scripts.core.handlers.data_insert import insert_data_db
from scripts.core.handlers.no_violations_data import db_select_no_violate
from scripts.core.handlers.result_generate import results_data
from scripts.core.handlers.violation_find import db_select_violate
class MongoDbStart:
# create mongo table
@staticmethod
def mongo_create():
try:
client_con = client
except Exception as e:
print("Connection failed: ", e)
else:
db = client_con['violationslogs']
log_name = db['log']
return log_name
# mongo data insert
@staticmethod
# getting input and insert into db
def mongo_insert(db, decoded):
try:
insert_check = insert_data_db(db, decoded)
if insert_check.acknowledged:
print("Data inserted....")
else:
print("Something went wrong data cannot be inserted....")
except Exception as e:
print("Insertion failed", e)
# mongo aggregate find businessman with most violations
@staticmethod
def mongo_violation(db):
try:
db_select_violate(db)
except Exception as e:
print("fetching failed", e)
# mongo aggregate find businessman with no violations
@staticmethod
def mongo_no_violation(db):
try:
db_select_no_violate(db)
except Exception as e:
print("fetching failed", e)
# mongo aggregate find
@staticmethod
def mongo_view_results(db):
try:
data = results_data(db)
return data
except Exception as e:
print("fetching failed", e)
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