Commit f05403dc by ajil.k

added

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="dict.mobile" />
<option value="dict.dob" />
<option value="dict.gender" />
<option value="dict.name" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ 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 (mongo_advanced)" 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/mongo_advanced.iml" filepath="$PROJECT_DIR$/.idea/mongo_advanced.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
# importing libraries
import uvicorn
from scripts.constants.application_config import port_no, link_to_fastapi
if __name__ == "__main__":
try:
uvicorn.run(link_to_fastapi, port=int(port_no), reload=True)
except Exception as e:
print("Error-", e)
[MongoDB]
mongo_uri = mongodb://localhost:27017/
port = 5000
[FastAPI]
link_to_fastapi = scripts.services.main:app
\ No newline at end of file
from configparser import ConfigParser
config = ConfigParser()
config.read(f"conf/application.conf")
uri = config.get("MongoDB", "mongo_uri")
port_no = config.get("MongoDB", "port")
link_to_fastapi = config.get("FastAPI", "link_to_fastapi")
# create a connection
from pymongo import MongoClient
from scripts.constants.application_config import uri
def db_connect():
try:
# Connect to MongoDB
connection = MongoClient(uri)
if connection:
print("Database Connected Successfully !")
# Create a database
db_name = 'inspection_db'
db = connection[db_name]
return db
except Exception as e:
print("Error ", e)
def violated_companies(app_api, collection):
try:
@app_api.get("/violated-company")
async def violated_company():
pipeline = [
{
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'count': -1
}
}, {
'$limit': 1
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
business_names = collection.aggregate(pipeline)
result = str(list(business_names))
print("Business names of companies having violation\n", result)
return {"Results": result}
except Exception as e:
print("Error-", e)
def no_violated_companies(app_api, collection):
try:
@app_api.get("/no-violated-company")
async def no_violated_company():
pipeline = [
{
'$match': {
'result': 'No Violation Issued'
}
}, {
'$group': {
'_id': None,
'business_name': {
'$push': '$business_name'
}
}
}
]
business_names = collection.aggregate(pipeline)
result = str(list(business_names))
print("Business names of companies having no violation\n", result)
return {"Results": result}
except Exception as e:
print("Error-", e)
import openpyxl
def create_excel(app_api, collection):
try:
@app_api.get("/generate-excel-file")
async def generate_excel_file():
# Fetch data from MongoDB
pipeline = [
{
'$project': {
'result': '$result',
'business_name': '$business_name',
'date': '$date',
'_id': 0
}
}
]
records = list(collection.aggregate(pipeline))
# Create an Excel Workbook and Worksheet
wb = openpyxl.Workbook()
ws = wb.active
# Write column headers to Excel Worksheet
column_headers = list(records[0].keys())
for i, header in enumerate(column_headers):
ws.cell(row=1, column=i + 1, value=header)
# Write data from MongoDB to Excel Worksheet
for i, record in enumerate(records):
for j, key in enumerate(record.keys()):
ws.cell(row=i + 2, column=j + 1, value=record[key])
# Save the Excel Workbook
wb.save("temp/mongodb_data.xlsx")
return {"Message": "Excel file created"}
except Exception as e:
print("Error-", e)
def insert_data_to_mongodb(collection, data):
status = collection.insert_many(data["company"])
if status:
return True
else:
return False
import json
from fastapi import UploadFile
from scripts.core.handlers.insert_into_db import insert_data_to_mongodb
def upload_and_insert_into_db(app_api, collection):
@app_api.post("/upload-file/")
async def create_upload_file(file: UploadFile):
data_frame = file.file.read()
data_frame = data_frame.decode()
uploaded_path = "temp/uploaded.json"
with open(uploaded_path, "w") as json_file:
# Write data to the file
json_file.write(data_frame)
with open(uploaded_path, "r") as uploaded_json:
# Load the data from the file
data = json.load(uploaded_json)
inserted = insert_data_to_mongodb(collection, data)
if inserted:
print("Data inserted to DB")
return {"filename": file.filename}
from fastapi import FastAPI
from scripts.constants.db_connection import db_connect
from scripts.core.handlers.aggregate_operations import violated_companies, no_violated_companies
from scripts.core.handlers.create_excel_file import create_excel
from scripts.core.handlers.upload_and_insert_data import upload_and_insert_into_db
from scripts.utils.mongo_util import create_collection
# Creating FastAPI object
app = FastAPI()
# Root
@app.get("/")
async def root():
return "It's Working!"
# Create Database Connection
db = db_connect()
collection = create_collection(db)
# upload file and insert into db
upload_and_insert_into_db(app, collection)
# Printing Violated Companies names
violated_companies(app, collection)
# Printing no Violated Companies names
no_violated_companies(app, collection)
# Create Excel file
create_excel(app, collection)
# function for creating collection
def create_collection(db):
# Create Collection
collection_name = "tbl_company"
collection = db[collection_name]
return collection
{
"company":[
{
"id": "10021-2015-ENFO",
"certificate_number": 9278806,
"business_name": "ALIXCO DELI GROCERY INC.",
"date": "Feb 20 2015",
"result": "No Violation Issued",
"sector": "Cigarette Retail Dealer - 127",
"address": {
"city": "RIDGEWOOD",
"zip": 11385,
"street": "MENAHAN ST",
"number": 1712
}
},
{
"id": "10021-2016-ENFO",
"certificate_number": 9275506,
"business_name": "PHARAMACEUTICAL LTD.",
"date": "Jan 25 2016",
"result": "Violation Issued",
"sector": "DRUG - 308",
"address": {
"city": "LAKEWOOD",
"zip": 12000,
"street": "MEXICAN ST",
"number": 1011
}
},
{
"id": "12021-2021-ENFO",
"certificate_number": 9278906,
"business_name": "COOKIES GROCERY INC.",
"date": "Nov 25 2021",
"result": "No Violation Issued",
"sector": "Cookies - 307",
"address": {
"city": "PINEVALLEY",
"zip": 73233,
"street": "CHURCH ST",
"number": 1700
}
},
{
"id": "12021-2009-ENFO",
"certificate_number": 8295506,
"business_name": "BHARAT CHEMICALS PVT LTD.",
"date": "Jun 10 2009",
"result": "Violation Issued",
"sector": "Chemicals - 328",
"address": {
"city": "Tokyo",
"zip": 45600,
"street": "MEXICAN ST",
"number": 1331
}
},
{
"id": "19021-2010-ENFO",
"certificate_number": 9266916,
"business_name": "XYZ Food Importing PVT LTD.",
"date": "Nov 25 2011",
"result": "No Violation Issued",
"sector": "Consumable - 108",
"address": {
"city": "Berlin",
"zip": 71133,
"street": "Luther ST",
"number": 9876
}
},
{
"id": "12022-2022-ENFO",
"certificate_number": 8120006,
"business_name": "ABC METAL EXPORTING LTD.",
"date": "Jan 25 2022",
"result": "Violation Issued",
"sector": "Metals - 998",
"address": {
"city": "Rio",
"zip": 45680,
"street": "Industrial Zone",
"number": 1999
}
},
{
"id": "19021-2010-ENFO",
"certificate_number": 9266916,
"business_name": "XYZ Food Importing PVT LTD.",
"date": "Dec 30 2011",
"result": "Violation Issued",
"sector": "Consumable - 108",
"address": {
"city": "Berlin",
"zip": 71133,
"street": "Luther ST",
"number": 9876
}
},
{
"id": "12021-2009-ENFO",
"certificate_number": 8295506,
"business_name": "BHARAT CHEMICALS PVT LTD.",
"date": "Sep 10 2019",
"result": "Violation Issued",
"sector": "Chemicals - 328",
"address": {
"city": "Tokyo",
"zip": 45600,
"street": "MEXICAN ST",
"number": 1331
}
}
]
}
\ No newline at end of file
{
"company":[
{
"id": "10021-2015-ENFO",
"certificate_number": 9278806,
"business_name": "ALIXCO DELI GROCERY INC.",
"date": "Feb 20 2015",
"result": "No Violation Issued",
"sector": "Cigarette Retail Dealer - 127",
"address": {
"city": "RIDGEWOOD",
"zip": 11385,
"street": "MENAHAN ST",
"number": 1712
}
},
{
"id": "10021-2016-ENFO",
"certificate_number": 9275506,
"business_name": "PHARAMACEUTICAL LTD.",
"date": "Jan 25 2016",
"result": "Violation Issued",
"sector": "DRUG - 308",
"address": {
"city": "LAKEWOOD",
"zip": 12000,
"street": "MEXICAN ST",
"number": 1011
}
},
{
"id": "12021-2021-ENFO",
"certificate_number": 9278906,
"business_name": "COOKIES GROCERY INC.",
"date": "Nov 25 2021",
"result": "No Violation Issued",
"sector": "Cookies - 307",
"address": {
"city": "PINEVALLEY",
"zip": 73233,
"street": "CHURCH ST",
"number": 1700
}
},
{
"id": "12021-2009-ENFO",
"certificate_number": 8295506,
"business_name": "BHARAT CHEMICALS PVT LTD.",
"date": "Jun 10 2009",
"result": "Violation Issued",
"sector": "Chemicals - 328",
"address": {
"city": "Tokyo",
"zip": 45600,
"street": "MEXICAN ST",
"number": 1331
}
},
{
"id": "19021-2010-ENFO",
"certificate_number": 9266916,
"business_name": "XYZ Food Importing PVT LTD.",
"date": "Nov 25 2011",
"result": "No Violation Issued",
"sector": "Consumable - 108",
"address": {
"city": "Berlin",
"zip": 71133,
"street": "Luther ST",
"number": 9876
}
},
{
"id": "12022-2022-ENFO",
"certificate_number": 8120006,
"business_name": "ABC METAL EXPORTING LTD.",
"date": "Jan 25 2022",
"result": "Violation Issued",
"sector": "Metals - 998",
"address": {
"city": "Rio",
"zip": 45680,
"street": "Industrial Zone",
"number": 1999
}
},
{
"id": "19021-2010-ENFO",
"certificate_number": 9266916,
"business_name": "XYZ Food Importing PVT LTD.",
"date": "Dec 30 2011",
"result": "Violation Issued",
"sector": "Consumable - 108",
"address": {
"city": "Berlin",
"zip": 71133,
"street": "Luther ST",
"number": 9876
}
},
{
"id": "12021-2009-ENFO",
"certificate_number": 8295506,
"business_name": "BHARAT CHEMICALS PVT LTD.",
"date": "Sep 10 2019",
"result": "Violation Issued",
"sector": "Chemicals - 328",
"address": {
"city": "Tokyo",
"zip": 45600,
"street": "MEXICAN ST",
"number": 1331
}
}
]
}
\ No newline at end of file
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