Commit 7f2a8b89 authored by iLensDevOps's avatar iLensDevOps

Pre-checks

parent 72f96be0
custom_report_json = [ custom_report_json = [{}]
{
}
]
""" """
This __init__ file contains the configuration settings for the application This __init__ file contains the configuration settings for the application
""" """
import os
import shutil
from pydantic import BaseSettings from pydantic import BaseSettings
from dotenv import load_dotenv from dotenv import load_dotenv
...@@ -15,6 +13,7 @@ class SecurityDetails(BaseSettings): ...@@ -15,6 +13,7 @@ class SecurityDetails(BaseSettings):
""" """
Configuration class for security token details Configuration class for security token details
""" """
secure_cookie: bool secure_cookie: bool
lock_out_time_mins: int lock_out_time_mins: int
leeway_in_mins: int leeway_in_mins: int
...@@ -29,6 +28,7 @@ class ServiceDetails(BaseSettings): ...@@ -29,6 +28,7 @@ class ServiceDetails(BaseSettings):
""" """
Configuration class for application service details Configuration class for application service details
""" """
host_name: str host_name: str
port_name: str port_name: str
env: str env: str
...@@ -39,6 +39,7 @@ class LoggingDetails(BaseSettings): ...@@ -39,6 +39,7 @@ class LoggingDetails(BaseSettings):
""" """
Configuration class for logging database details Configuration class for logging database details
""" """
log_base_path: str log_base_path: str
log_level: str log_level: str
max_bytes: int max_bytes: int
...@@ -54,6 +55,7 @@ class PostgresDetails(BaseSettings): ...@@ -54,6 +55,7 @@ class PostgresDetails(BaseSettings):
Config: Config:
env_prefix (str): The environment variable prefix for PostgreSQL settings. env_prefix (str): The environment variable prefix for PostgreSQL settings.
""" """
uri: str uri: str
host: str host: str
port: str port: str
......
...@@ -19,6 +19,7 @@ class APIConstants: ...@@ -19,6 +19,7 @@ class APIConstants:
""" """
Constants related to API endpoints Constants related to API endpoints
""" """
STARTUP = "startup" STARTUP = "startup"
SHUTDOWN = "shutdown" SHUTDOWN = "shutdown"
HEALTH_CHECK = "/healthcheck" HEALTH_CHECK = "/healthcheck"
...@@ -30,6 +31,7 @@ class CommonConstants: ...@@ -30,6 +31,7 @@ class CommonConstants:
""" """
Common constants used in the application Common constants used in the application
""" """
GET = "GET" GET = "GET"
POST = "POST" POST = "POST"
CUSTOM_REPORT_TAG = "Event Handler Endpoints" CUSTOM_REPORT_TAG = "Event Handler Endpoints"
...@@ -49,6 +51,7 @@ class ReportType: ...@@ -49,6 +51,7 @@ class ReportType:
""" """
Constants related to ReportType Constants related to ReportType
""" """
REFINERY_REPORT = "refinery_report" REFINERY_REPORT = "refinery_report"
......
...@@ -7,14 +7,11 @@ class GeneralException(Exception): ...@@ -7,14 +7,11 @@ class GeneralException(Exception):
class ErrorMessages: class ErrorMessages:
""" """ """
"""
CONNECTION_EXCEPTION = "" CONNECTION_EXCEPTION = ""
COMMON_MESSAGE = "" COMMON_MESSAGE = ""
class PostgresDBError: class PostgresDBError:
""" """ """
"""
...@@ -28,7 +28,7 @@ class CustomReportHandler: ...@@ -28,7 +28,7 @@ class CustomReportHandler:
) )
logger.info(f"Creating list of dates starting from {start_date} to {end_date}") logger.info(f"Creating list of dates starting from {start_date} to {end_date}")
date_list = pd.date_range(start_date, end_date, freq='D') date_list = pd.date_range(start_date, end_date, freq="D")
# Iterating and creating where clause filters # Iterating and creating where clause filters
# Output - {'day_start_date': 'YYYY-MM-DD', 'day_end_date': 'YYYY-MM-DD', # Output - {'day_start_date': 'YYYY-MM-DD', 'day_end_date': 'YYYY-MM-DD',
...@@ -36,8 +36,8 @@ class CustomReportHandler: ...@@ -36,8 +36,8 @@ class CustomReportHandler:
# 'year_start_date': 'YYYY-MM-DD', 'year_end_date': 'YYYY-MM-DD'} # 'year_start_date': 'YYYY-MM-DD', 'year_end_date': 'YYYY-MM-DD'}
for each_dates in date_list.strftime( for each_dates in date_list.strftime(
CommonConstants.DATE_TIME_FORMAT).to_list(): CommonConstants.DATE_TIME_FORMAT
).to_list():
# To get the financial year # To get the financial year
financial_year = None financial_year = None
date_obj = datetime.strptime(each_dates, CommonConstants.DATE_TIME_FORMAT) date_obj = datetime.strptime(each_dates, CommonConstants.DATE_TIME_FORMAT)
...@@ -73,18 +73,20 @@ class CustomReportHandler: ...@@ -73,18 +73,20 @@ class CustomReportHandler:
temp_data_dict = dict() temp_data_dict = dict()
# Iterating each query for each KPI # Iterating each query for each KPI
for each_query in \ for each_query in input_json[each_blocks][CommonConstants.QUERY][
input_json[each_blocks][CommonConstants.QUERY][each_kpi]: each_kpi
query = each_query. \ ]:
format( query = each_query.format(
day_start_date=date_filter[CommonConstants.DAY_START_DATE], day_start_date=date_filter[CommonConstants.DAY_START_DATE],
day_end_date=date_filter[CommonConstants.DAY_END_DATE], day_end_date=date_filter[CommonConstants.DAY_END_DATE],
month_start_date=date_filter[ month_start_date=date_filter[
CommonConstants.MONTH_START_DATE], CommonConstants.MONTH_START_DATE
],
month_end_date=date_filter[CommonConstants.MONTH_END_DATE], month_end_date=date_filter[CommonConstants.MONTH_END_DATE],
year_start_date=date_filter[ year_start_date=date_filter[
CommonConstants.YEAR_START_DATE], CommonConstants.YEAR_START_DATE
year_end_date=date_filter[CommonConstants.YEAR_END_DATE] ],
year_end_date=date_filter[CommonConstants.YEAR_END_DATE],
) )
response = self.postgres_db_obj.fetch_data(query=query) response = self.postgres_db_obj.fetch_data(query=query)
...@@ -100,9 +102,7 @@ class CustomReportHandler: ...@@ -100,9 +102,7 @@ class CustomReportHandler:
else: else:
temp_data_dict = dict() temp_data_dict = dict()
for each_columns in input_json[each_blocks]["data_column"]: for each_columns in input_json[each_blocks]["data_column"]:
temp_data_dict.update( temp_data_dict.update({each_columns: None})
{each_columns: None}
)
input_json[each_blocks]["data"].append(temp_data_dict) input_json[each_blocks]["data"].append(temp_data_dict)
return input_json return input_json
......
...@@ -17,7 +17,7 @@ from scripts.core.logging.application_logging import logger ...@@ -17,7 +17,7 @@ from scripts.core.logging.application_logging import logger
from scripts.constants import APIConstants, CommonConstants, figlet from scripts.constants import APIConstants, CommonConstants, figlet
from scripts.core.schemas.api import ( from scripts.core.schemas.api import (
custom_report_input_model, custom_report_input_model,
custom_report_output_model custom_report_output_model,
) )
report_handler_obj = CustomReportHandler() report_handler_obj = CustomReportHandler()
...@@ -40,8 +40,7 @@ async def shutdown_event(): ...@@ -40,8 +40,7 @@ async def shutdown_event():
print("Exiting!") print("Exiting!")
@event_router.get( @event_router.get(APIConstants.HEALTH_CHECK)
APIConstants.HEALTH_CHECK)
async def ping(): async def ping():
""" """
This function returns the `200 status message`. This function returns the `200 status message`.
...@@ -50,7 +49,8 @@ async def ping(): ...@@ -50,7 +49,8 @@ async def ping():
@event_router.post( @event_router.post(
APIConstants.CUSTOM_REPORT_ENDPOINT, response_model=custom_report_output_model) APIConstants.CUSTOM_REPORT_ENDPOINT, response_model=custom_report_output_model
)
async def custom_report_function(input_json: custom_report_input_model): async def custom_report_function(input_json: custom_report_input_model):
""" """
Initiate postgres db and create tables Initiate postgres db and create tables
......
...@@ -23,8 +23,11 @@ class PostgresDBUtility: ...@@ -23,8 +23,11 @@ class PostgresDBUtility:
def create_connection(self, db=None): def create_connection(self, db=None):
_db = db or self.db_name _db = db or self.db_name
conn = psycopg2.connect( conn = psycopg2.connect(
database=_db, user=self.username, password=self.password, host=self.host, database=_db,
port=self.port user=self.username,
password=self.password,
host=self.host,
port=self.port,
) )
logger.debug("Connection established successfully") logger.debug("Connection established successfully")
return conn return conn
...@@ -125,8 +128,11 @@ class PostgresDBUtility: ...@@ -125,8 +128,11 @@ class PostgresDBUtility:
cursor.execute(query) cursor.execute(query)
json = cursor.fetchall() json = cursor.fetchall()
list_of_lists = list( list_of_lists = list(
map(lambda each: [each["plant"], each["line"], each["machine"]], map(
json)) lambda each: [each["plant"], each["line"], each["machine"]],
json,
)
)
except Exception as e: except Exception as e:
logger.error(f"Exception while fetching {str(e)}") logger.error(f"Exception while fetching {str(e)}")
except Exception as e: except Exception as e:
...@@ -232,7 +238,8 @@ class PostgresDBUtility: ...@@ -232,7 +238,8 @@ class PostgresDBUtility:
except Exception as e: except Exception as e:
logger.exception( logger.exception(
f"Exception in the check table existance definition{str(e)}") f"Exception in the check table existance definition{str(e)}"
)
finally: finally:
try: try:
......
...@@ -11,13 +11,13 @@ class SterliteRefineryTemplate: ...@@ -11,13 +11,13 @@ class SterliteRefineryTemplate:
"NORMS (Month)", "NORMS (Month)",
"ON DATE(Day)", "ON DATE(Day)",
"MTD", "MTD",
"YTD" "YTD",
], ],
"query": { "query": {
"ANODE_AVAILABILITY": [ "ANODE_AVAILABILITY": [
SterliteRefineryQuery.AnodeAvailability.QUERY_AA, SterliteRefineryQuery.AnodeAvailability.QUERY_AA,
SterliteRefineryQuery.AnodeAvailability.QUERY_MTD, SterliteRefineryQuery.AnodeAvailability.QUERY_MTD,
SterliteRefineryQuery.AnodeAvailability.QUERY_YTD SterliteRefineryQuery.AnodeAvailability.QUERY_YTD,
], ],
"DO CELLS IN OPERATION": [ "DO CELLS IN OPERATION": [
SterliteRefineryQuery.DOCellsInOperation.QUERY_AA SterliteRefineryQuery.DOCellsInOperation.QUERY_AA
...@@ -25,34 +25,37 @@ class SterliteRefineryTemplate: ...@@ -25,34 +25,37 @@ class SterliteRefineryTemplate:
"Total Cells In Operation": [ "Total Cells In Operation": [
SterliteRefineryQuery.TotalCellsInOperation.QUERY_1, SterliteRefineryQuery.TotalCellsInOperation.QUERY_1,
SterliteRefineryQuery.TotalCellsInOperation.QUERY_2, SterliteRefineryQuery.TotalCellsInOperation.QUERY_2,
SterliteRefineryQuery.TotalCellsInOperation.QUERY_3 SterliteRefineryQuery.TotalCellsInOperation.QUERY_3,
] ],
}, },
"data": [], "data": [],
"data_column": ["anode_availability", "uom", "data_column": [
"norms", "on_date", "mtd", "anode_availability",
"ytd"], "uom",
"norms",
"on_date",
"mtd",
"ytd",
],
"description": "", "description": "",
"format": "" "format": "",
}, },
"SAFETY REPORT": { "SAFETY REPORT": {
"columns": [ "columns": ["SAFETY REPORT", "UOM", "NORMS", "ON DATE", "MTD", "YTD"],
"SAFETY REPORT",
"UOM",
"NORMS",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"data_column": ["safety_report", "uom", "data_column": [
"norms", "on_date", "mtd", "safety_report",
"ytd"], "uom",
"norms",
"on_date",
"mtd",
"ytd",
],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"POWER AVAILABILITY": { "POWER AVAILABILITY": {
...@@ -62,13 +65,13 @@ class SterliteRefineryTemplate: ...@@ -62,13 +65,13 @@ class SterliteRefineryTemplate:
"NORMS", "NORMS",
"ON DATE", "ON DATE",
"MTD", "MTD",
"YTD" "YTD",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"RM ANODE RECEIPT DETAILS": { "RM ANODE RECEIPT DETAILS": {
"columns": [ "columns": [
...@@ -77,30 +80,23 @@ class SterliteRefineryTemplate: ...@@ -77,30 +80,23 @@ class SterliteRefineryTemplate:
"MTD", "MTD",
"YTD", "YTD",
"Material", "Material",
"OPENING STOCK" "OPENING STOCK",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"EFFICIENCIES": { "EFFICIENCIES": {
"columns": [ "columns": ["EFFICIENCIES", "UOM", "NORMS", "ON DATE", "MTD", "YTD"],
"EFFICIENCIES",
"UOM",
"NORMS",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"BANK / CROP": { "BANK / CROP": {
"columns": [ "columns": [
...@@ -108,72 +104,50 @@ class SterliteRefineryTemplate: ...@@ -108,72 +104,50 @@ class SterliteRefineryTemplate:
"CE (%)", "CE (%)",
"THEOR. WEIGHT", "THEOR. WEIGHT",
"ACTUAL WEIGHT", "ACTUAL WEIGHT",
"STRIPPING TIME" "STRIPPING TIME",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"PRODUCTION": { "PRODUCTION": {
"columns": [ "columns": ["PRODUCTION", "UOM", "NORMS", "ON DATE", "MTD", "YTD"],
"PRODUCTION",
"UOM",
"NORMS",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"CIRCULATION": { "CIRCULATION": {
"columns": [ "columns": ["CIRCULATION", "CE%", "THEOR. WEIGHT", "ACTUAL WEIGHT"],
"CIRCULATION",
"CE%",
"THEOR. WEIGHT",
"ACTUAL WEIGHT"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"FG INVENTORY": { "FG INVENTORY": {
"columns": [ "columns": ["FG INVENTORY", "ON DATE", "MTD", "YTD"],
"FG INVENTORY",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"PRODUCTION": { "PRODUCTION": {
"columns": [ "columns": ["PRODUCTION", "ON DATE", "MTD", "YTD"],
"PRODUCTION",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"CROP": { "CROP": {
...@@ -182,13 +156,13 @@ class SterliteRefineryTemplate: ...@@ -182,13 +156,13 @@ class SterliteRefineryTemplate:
"BANKS", "BANKS",
"TOTAL CELLS", "TOTAL CELLS",
"CELL VOLTAGE (V)", "CELL VOLTAGE (V)",
"T. SHORTS / CELL / CHECK" "T. SHORTS / CELL / CHECK",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} }
}, },
{ {
...@@ -199,13 +173,13 @@ class SterliteRefineryTemplate: ...@@ -199,13 +173,13 @@ class SterliteRefineryTemplate:
"1st CROP - Sttripping time", "1st CROP - Sttripping time",
"2nd CROP - Change over time", "2nd CROP - Change over time",
"3rd CROP", "3rd CROP",
"CSM Plate Rejection" "CSM Plate Rejection",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} }
}, },
{ {
...@@ -216,13 +190,13 @@ class SterliteRefineryTemplate: ...@@ -216,13 +190,13 @@ class SterliteRefineryTemplate:
"MTD", "MTD",
"Filtered Volume", "Filtered Volume",
"Total Volume Refinery", "Total Volume Refinery",
"ON DATE" "ON DATE",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} }
}, },
{ {
...@@ -233,29 +207,22 @@ class SterliteRefineryTemplate: ...@@ -233,29 +207,22 @@ class SterliteRefineryTemplate:
"NORMS", "NORMS",
"ON DATE", "ON DATE",
"MTD", "MTD",
"YTD" "YTD",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"SPECIFIC CONSUMPTION": { "SPECIFIC CONSUMPTION": {
"columns": [ "columns": ["CONSUMABLES", "UOM", "NORMS", "ON DATE", "MTD", "YTD"],
"CONSUMABLES",
"UOM",
"NORMS",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"ELECTROLYTE COMPOSITION": { "ELECTROLYTE COMPOSITION": {
...@@ -265,56 +232,40 @@ class SterliteRefineryTemplate: ...@@ -265,56 +232,40 @@ class SterliteRefineryTemplate:
"NORMS", "NORMS",
"DATE", "DATE",
"CIR-1", "CIR-1",
"CIR-2" "CIR-2",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"SUSPENDED SOLIDS": { "SUSPENDED SOLIDS": {
"columns": [ "columns": ["SUSPENDED SOLIDS"],
"SUSPENDED SOLIDS"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"BLEEDING SECTION": { "BLEEDING SECTION": {
"columns": [ "columns": ["BLEEDING SECTION", "UOM", "ON DATE", "MTD", "YTD"],
"BLEEDING SECTION",
"UOM",
"ON DATE",
"MTD",
"YTD"
],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}, },
"BANK / CROP": { "BANK / CROP": {
"columns": [ "columns": ["BANK / CROP", "TIME", "WEIGHT (MT)", "CELLS", "READING"],
"BANK / CROP", "query": [],
"TIME",
"WEIGHT (MT)",
"CELLS",
"READING"
],
"query": [
],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
} },
}, },
{ {
"DEPARTMENT(M4)": { "DEPARTMENT(M4)": {
...@@ -324,13 +275,13 @@ class SterliteRefineryTemplate: ...@@ -324,13 +275,13 @@ class SterliteRefineryTemplate:
"EQUIPMENT LOCATION", "EQUIPMENT LOCATION",
"DURATION", "DURATION",
"(MT) PRODUCTION", "(MT) PRODUCTION",
"CAUSE OF THE BREAK DOWN" "CAUSE OF THE BREAK DOWN",
], ],
"query": [], "query": [],
"data": [], "data": [],
"additions": [], "additions": [],
"description": "", "description": "",
"format": "" "format": "",
}
} }
},
] ]
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