Commit e1c392af authored by suryakant's avatar suryakant

Query Update

parent a682a952
...@@ -9,7 +9,6 @@ POSTGRES_HOST = 20.219.42.36 ...@@ -9,7 +9,6 @@ POSTGRES_HOST = 20.219.42.36
POSTGRES_PORT = 5786 POSTGRES_PORT = 5786
POSTGRES_USERNAME = svc_sterlite POSTGRES_USERNAME = svc_sterlite
POSTGRES_PASSWORD = sterliteSvc#1247 POSTGRES_PASSWORD = sterliteSvc#1247
POSTGRES_DATABASE = strelite-datalakedev
# Log Details # Log Details
LOG_BASE_PATH=logs/ LOG_BASE_PATH=logs/
......
...@@ -60,7 +60,6 @@ class PostgresDetails(BaseSettings): ...@@ -60,7 +60,6 @@ class PostgresDetails(BaseSettings):
port: str port: str
username: str username: str
password: str password: str
database: str
class Config: class Config:
env_prefix = "POSTGRES_" env_prefix = "POSTGRES_"
......
...@@ -65,6 +65,12 @@ class ReportType: ...@@ -65,6 +65,12 @@ class ReportType:
REFINERY_WORKSHEET_NAME = "REFINERY DAILY PERFORMANCE REPORT" REFINERY_WORKSHEET_NAME = "REFINERY DAILY PERFORMANCE REPORT"
class PostgresConstant:
""" Constant used in Postgre SQL """
STRELITE_DATALAKE_DEV = "strelite-datalakedev"
PROJECT_101__ILENS_AI = "project_101__ilens_ai"
figlet = """ figlet = """
____ _ _ _ _ ____ _ _ _ _
/ ___| | |_ ___ _ __ | | (_) | |_ ___ / ___| | |_ ___ _ __ | | (_) | |_ ___
......
...@@ -3699,3 +3699,17 @@ class SterliteRefineryQuery: ...@@ -3699,3 +3699,17 @@ class SterliteRefineryQuery:
WHERE WHERE
DATE = '{day_start_date}' DATE = '{day_start_date}'
""" """
class BankCrop:
""" BANK / CROP """
QUERY_1 = """
SELECT
"Bank" AS BANK_CROP,
"Date"::VARCHAR AS TIME,
"Op" AS OP,
"Priority" AS PRIORITY,
"Copper Prod" AS COPPER_PROD,
"KA_new" AS KA_NEW
FROM PUBLIC.REFINERY_PRODUCTION_DAY_WISE RPDW
WHERE DATE("Date") = '{day_start_date}'
"""
...@@ -2,7 +2,7 @@ import copy ...@@ -2,7 +2,7 @@ import copy
import pandas as pd import pandas as pd
from datetime import datetime from datetime import datetime
from scripts.constants import ReportType, CommonConstants from scripts.constants import ReportType, CommonConstants, PostgresConstant
from scripts.template.sterlite_report_template import SterliteRefineryTemplate from scripts.template.sterlite_report_template import SterliteRefineryTemplate
from scripts.core.logging.application_logging import logger from scripts.core.logging.application_logging import logger
from scripts.core.exception.app_exceptions import GeneralException from scripts.core.exception.app_exceptions import GeneralException
...@@ -76,6 +76,10 @@ class CustomReportHandler: ...@@ -76,6 +76,10 @@ class CustomReportHandler:
append_flag = False append_flag = False
data_list = [] data_list = []
database = PostgresConstant.STRELITE_DATALAKE_DEV
if "database" in input_json[each_blocks]:
database = input_json[each_blocks]["database"]
# Iterating each query for each KPI # Iterating each query for each KPI
if input_json[each_blocks][CommonConstants.QUERY][each_kpi]: if input_json[each_blocks][CommonConstants.QUERY][each_kpi]:
for each_query in \ for each_query in \
...@@ -95,7 +99,9 @@ class CustomReportHandler: ...@@ -95,7 +99,9 @@ class CustomReportHandler:
year_end_date=date_filter[ year_end_date=date_filter[
CommonConstants.YEAR_END_DATE]) CommonConstants.YEAR_END_DATE])
response = self.postgres_db_obj.fetch_data(query=query) response = self.postgres_db_obj. \
select_postgres_table(query=query, db=database
)[1]
if response and "addition" not in input_json[each_blocks]: if response and "addition" not in input_json[each_blocks]:
if len(response) <= 1: if len(response) <= 1:
...@@ -217,6 +223,7 @@ class CustomReportHandler: ...@@ -217,6 +223,7 @@ class CustomReportHandler:
if str(input_json["job_type"]).lower() == ReportType.REFINERY_REPORT: if str(input_json["job_type"]).lower() == ReportType.REFINERY_REPORT:
logger.info("Generating custom date filter with in the range") logger.info("Generating custom date filter with in the range")
# Getting custom date range using start date and end date # Getting custom date range using start date and end date
date_filter = self.create_custom_date_filter(input_json=input_json) date_filter = self.create_custom_date_filter(input_json=input_json)
...@@ -234,9 +241,9 @@ class CustomReportHandler: ...@@ -234,9 +241,9 @@ class CustomReportHandler:
start_row = CommonConstants.START_ROW start_row = CommonConstants.START_ROW
total_column = 0 total_column = 0
workbook = writer.book workbook = writer.book
sheet_name = datetime.strptime(each_date_range[ sheet_name = datetime.strptime(
CommonConstants.DAY_START_DATE], "%Y-%m-%d").\ each_date_range[CommonConstants.DAY_START_DATE],
strftime("%d %b %Y") "%Y-%m-%d").strftime("%d %b %Y")
worksheet = None worksheet = None
...@@ -244,6 +251,9 @@ class CustomReportHandler: ...@@ -244,6 +251,9 @@ class CustomReportHandler:
for each_blocks in report_template: for each_blocks in report_template:
logger.info("Fetching each KPI data from queries") logger.info("Fetching each KPI data from queries")
print("--------------------")
print(each_blocks)
print("-- \n")
each_blocks = self.get_queries_from_db( each_blocks = self.get_queries_from_db(
input_json=each_blocks, date_filter=each_date_range input_json=each_blocks, date_filter=each_date_range
) )
......
...@@ -11,17 +11,18 @@ class PostgresDBUtility: ...@@ -11,17 +11,18 @@ class PostgresDBUtility:
def __init__(self): def __init__(self):
try: try:
logger.debug("Meta DB connection initialization") logger.debug("Meta DB connection initialization")
self.cursor_type = RealDictCursor
self.host = postgres_details.host self.host = postgres_details.host
self.port = postgres_details.port self.port = postgres_details.port
self.username = postgres_details.username self.username = postgres_details.username
self.password = postgres_details.password self.password = postgres_details.password
self.db_name = postgres_details.database self.db_name = postgres_details.database
self.cursor_type = RealDictCursor
except Exception as e: except Exception as e:
logger.error(f"Exception in the Meta DB initialization : {str(e)}") logger.error(f"Exception in the Meta DB initialization : {str(e)}")
def create_connection(self, db=None): def create_connection(self, db=None):
_db = db or self.db_name _db = db or self.db_name
print("Database ====================================> ", _db)
conn = psycopg2.connect( conn = psycopg2.connect(
database=_db, database=_db,
user=self.username, user=self.username,
...@@ -42,7 +43,7 @@ class PostgresDBUtility: ...@@ -42,7 +43,7 @@ class PostgresDBUtility:
""" """
logger.debug(f" SQL QUERY {query}") logger.debug(f" SQL QUERY {query}")
connection = None connection = None
result = "" result = []
flag = False flag = False
try: try:
connection = self.create_connection(db) connection = self.create_connection(db)
......
from scripts.constants import PostgresConstant
from scripts.core.db.postgres.custom_report_query import SterliteRefineryQuery from scripts.core.db.postgres.custom_report_query import SterliteRefineryQuery
...@@ -572,10 +573,17 @@ class SterliteRefineryTemplate: ...@@ -572,10 +573,17 @@ class SterliteRefineryTemplate:
}, },
{ {
"BANK / CROP": { "BANK / CROP": {
"columns": ["BANK / CROP", "TIME", "WEIGHT (MT)", "CELLS", "READING"], "columns": ["BANK / CROP", "TIME", "OP", "PRIORITY",
"query": {}, "COPPER_PROD", "KA_NEW"],
"query": {
"BankCrop": [
SterliteRefineryQuery.BankCrop.QUERY_1,
]
},
"data": [], "data": [],
"data_column": ["bank_crop", "time", "weight", "cells", "reading"], "data_column": ["bank_crop", "time", "op", "priority",
"copper_prod", "ka_new"],
"database": PostgresConstant.PROJECT_101__ILENS_AI
}, },
}, },
{ {
......
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