Commit 7199ecfe authored by arjun.b's avatar arjun.b

task completed

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
l python:S3776"TRefactor this function to reduce its Cognitive Complexity from 48 to the 15 allowed.(´Þæ×
\ No newline at end of file
Z
*venv/Lib/site-packages/fastapi/encoders.py,f\8\f8b29650c954ce033247479297694f06d62e2702
\ 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.mongo_services:app
port_no=8000
[mongo]
uri=mongodb://localhost:27017
[file_path]
path=temp/excel_data.xlsx
[logging]
file_name=scripts/external/logging_log
import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
# server path
server_path = config.get("server", "server_path")
port_no = config.get("server", "port_no")
# mongo pth
uri = config.get("mongo", "uri")
file_path = config.get("file_path", "path")
# logging file path
file_name = config.get("logging", "file_name")
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))
class APIEndpoints:
# mongo end points
root="/"
file_upload = "/upload"
max_violation = "/business_name with max violation issued"
no_violation = "/business name with no violation"
convert_excel = "/convert_to_excel"
import json
import pandas as pd
from scripts.config.application_config import file_path
from scripts.database.mongo.mongo_aggregation import inspection, project_fields
from scripts.logging.logging import logger
class MongoHandler:
@staticmethod
async def file_upload(file_content):
try:
data = json.loads(file_content.decode("utf-8"))
inspection.insert_many(data)
except Exception as e:
logger.error(f"uploading file is failed -> {e}")
print(e)
@staticmethod
def create_excel_file():
try:
file_data = project_fields()
df = pd.DataFrame(file_data)
df.to_excel(file_path, index=False)
except Exception as e:
logger.error(f"could not create an excel file -> {e}")
print(e)
@staticmethod
def open_excel():
try:
dataset = pd.read_excel("temp/excel_data.xlsx")
print(dataset)
except Exception as e:
logger.error(f"could not open the excel file -> {e}")
print(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]
# create database
from scripts.constants.db_connection import db_connect
from scripts.utils.mongo_utils import create_collection
# database connection
db = db_connect()
# create collection
inspection = create_collection(db)
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 result
except Exception as e:
print(str(e))
def max_violation():
try:
pipeline = [
{
'$match': {
'result': 'Violation issued'
}
}, {
'$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 result
except Exception as e:
print(str(e))
def project_fields():
try:
pipeline = [
{
'$project': {
'business_name': 1,
'result': 1,
'date': 1,
'_id': 0
}
}
]
result = list(inspection.aggregate(pipeline))
return result
except Exception as e:
print(e)
2023-02-06 12:56:33 - ERROR - [MainThread:business_max_violation():32] - cant find business name with max violation
2023-02-07 10:01:41 - ERROR - [MainThread:convert_excel():55] - cant create excel file
2023-02-09 18:29:33 - ERROR - [MainThread:file_upload():15] - uploading file is failed -> Expecting value: line 1 column 1 (char 0)
2023-02-09 18:32:50 - ERROR - [MainThread:file_upload():15] - uploading file is failed -> Expecting value: line 1 column 1 (char 0)
2023-02-09 18:35:55 - ERROR - [MainThread:file_upload():15] - uploading file is failed -> Expecting value: line 1 column 1 (char 0)
import logging
import os
from logging.handlers import RotatingFileHandler
from scripts.config.application_config import file_name
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel("ERROR")
log_formatter = '%(asctime)s - %(levelname)-6s - [%(threadName)5s:%(funcName)5s():%(lineno)s] - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(log_formatter, time_format)
log_file = os.path.join(f"{file_name}_ERROR.log")
temp_handler = RotatingFileHandler(log_file,
maxBytes=100000000,
backupCount=5)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
from fastapi import FastAPI, UploadFile
from scripts.constants.end_points import APIEndpoints
from scripts.core.handlers.mongo_handlers import MongoHandler
from scripts.database.mongo.mongo_aggregation import max_violation, no_violation
from scripts.logging.logging import logger
app = FastAPI(
title="Mongo Aggregations")
# root API
@app.get(APIEndpoints.root, tags=["root"])
async def root():
return {"data": "Mongo aggregation task"}
# upload file and store into database
@app.post(APIEndpoints.file_upload, tags=["upload file"])
async def upload_file(file: UploadFile):
file_content = file.file.read()
await MongoHandler.file_upload(file_content)
return {"data": "CSV data uploaded successfully"}
# business name with maximum violation
@app.get(APIEndpoints.max_violation, tags=["tasks"])
async def business_max_violation():
try:
re = max_violation()
return {"data": re}
except Exception as e:
logger.error("cant find business name with max violation")
print(e)
# business name with no violation
@app.get(APIEndpoints.no_violation, tags=["tasks"])
async def business_no_violation():
try:
re = no_violation()
return {"data": re}
except Exception as e:
logger.error("cant find business name with no violation")
print(e)
# create Excel file
@app.get(APIEndpoints.convert_excel, tags=["convert to excel"])
async def convert_excel():
try:
MongoHandler.create_excel_file()
MongoHandler.open_excel()
return {"data", "Excel file generated based on business name,result and date"}
except Exception as e:
logger.error("cant create excel file")
print(e)
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))
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