Commit db1764cc by arjun.b

MongoDB task based on Aggregation

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>
</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_aggregate_task)" 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_aggregate_task.iml" filepath="$PROJECT_DIR$/.idea/mongo_aggregate_task.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
import uvicorn
from scripts.config.application_config import server_path, port_no
if __name__ == "__main__":
try:
uvicorn.run(server_path, port=int(port_no), reload=True)
except Exception as e:
print(str(e))
[server]
server_path=scripts.services.main:app
port_no=8000
[mongo]
uri=mongodb://localhost:27017
[file_path]
path=temp/inspection.json
import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
server_path = config.get("server", "server_path")
port_no = config.get("server", "port_no")
uri = config.get("mongo", "uri")
file_path = config.get("file_path", "path")
except configparser.NoOptionError:
print("could not find conf file")
from pymongo import MongoClient
from scripts.config.application_config import uri
def db_connect():
try:
conn = MongoClient(uri)
database = conn["Company"]
dblist = conn.list_database_names()
if database in dblist:
print("database with same name already exist")
print("database created successfully")
return database
except Exception as e:
print(str(e))
from typing import List
from pydantic import BaseModel
class Address(BaseModel):
city: str
zip: int
street: str
number: int
class CompanyItem(BaseModel):
id: str
certificate_number: str
business_name: str
date: str
result: str
sector: str
address: Address
class Model(BaseModel):
Company: List[CompanyItem]
import json
import pandas as pd
from scripts.config.application_config import file_path
def create_excel_file():
try:
with open(file_path) as data:
file_data = json.load(data)
df = pd.DataFrame(file_data, columns=["business_name", "result", "date"])
df.to_excel("temp/excel_data.xlsx", index=False)
except Exception as e:
print(str(e))
import json
from fastapi import FastAPI, UploadFile
from scripts.config.application_config import file_path
from scripts.constants.db_connection import db_connect
from scripts.core.handlers.create_excel_file import create_excel_file
from scripts.utils.mongo_utils import create_collection
app = FastAPI()
# create database
db = db_connect()
# create collection
inspection = create_collection(db)
@app.get("/", tags=["root"])
async def root():
return {"data": "Mongo aggregation task"}
# # upload file from user directory
# @app.post("/upload", tags=["file upload"])
# async def file_upload(file: UploadFile):
# dataset = file.file.read()
# file_data = dataset.decode()
# # print(file_data)
# return {"file": file}
# insert json data into database
@app.post("/insert_json", tags=["insert JSON"])
async def insert_data():
try:
with open(file_path) as data:
file_data = json.load(data)
inspection.insert_many(file_data)
return {"data": "JSON data successfully inserted"}
except Exception as e:
print(str(e))
@app.get("/business_name with max violation issued", tags=["tasks"])
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(inspection.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
@app.get("/business name with no violation", tags=["tasks"])
async def no_violation():
try:
pipeline = [
{
'$match': {
'result': 'No violation issued'
}
}, {
'$group': {
'_id': '$business_name'
}
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
}
]
result = list(inspection.aggregate(pipeline))
print(result)
return {"data": result}
except Exception as e:
print(str(e))
# create Excel file
@app.get("/convert_to_excel")
async def convert_excel():
create_excel_file()
return {"data", "Excel file generated based on business name,result and date"}
def create_collection(db):
try:
collection_name = "inspection"
collection = db[collection_name]
print("collection inspection created ")
return collection
except Exception as e:
print(str(e))
[
{
"id": "10021-2015-ENFO",
"certificate_number": "9278806",
"business_name": "ATLIXCO 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": "10022-2015-ENFO",
"certificate_number": "9278807",
"business_name": "KL Export INC.",
"date": "sep 30 2016",
"result": "No violation issued",
"sector": "Gadgets Retail Dealer",
"address": {
"city": "San Francisco",
"zip": 11387,
"street": " Chinatown ST",
"number": 1714
}
},
{
"id": "10023-2015-ENFO",
"certificate_number": "9278808",
"business_name": "SR Import INC.",
"date": "Jun 30 2017",
"result": "No violation issued",
"sector": "Shipping Dealer -128",
"address": {
"city": "Bengaluru",
"zip": 690651,
"street": " Neeladri ST",
"number": 1715
}
},
{
"id": "10024-2020-ENFO",
"certificate_number": "9278809",
"business_name": "TK Solution PVT LTD",
"date": "jan 30 2020",
"result": "Violation issued",
"sector": "Software Solution",
"address": {
"city": "Kasaragod",
"zip": 671541,
"street": "Paduppu",
"number": 1717
}
},
{
"id": "10025-2018-ENFO",
"certificate_number": "9278810",
"business_name": "KL Export INC.",
"date": "jan 30 2016",
"result": "Violation issued",
"sector": "Software Solution",
"address": {
"city": "Berlin",
"zip": 625232,
"street": "church ST",
"number": 1714
}
},
{
"id": "10025-2018-ENFO",
"certificate_number": "9278810",
"business_name": "KL Export INC.",
"date": "Dec 20 2017",
"result": "Violation issued",
"sector": "Software Solution",
"address": {
"city": "Berlin",
"zip": 625232,
"street": "church ST",
"number": 1714
}
}
]
\ 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