I have a scenario where in I need to compare two simulation coming from same table, i.e say simulation_1 and simulation_2. For all completed simulation theer would be a unique id generated "sim_sttus_id" in the "__TABLE1" as shown in the code snippet. Below are the code snippet for reference.
class SimRes:
__TABLE1 = 'SIM_STTUS'
__TABLE2 = 'COMP_SIM_CONF'
__TABLE3 = 'COMP'
def __init__(self, prog: str, plan: int, versn: float, sim_sttus: int, tab_name: str = None, comp_id: int = None):
super().__init__(prog)
self.__prog = prog
self.__plan = plan
self.__versn = versn
self.__sim_sttus = sim_sttus
self.__tab_name = tab_name
self.__comp_id = comp_id
self.__sim_sttus_tbl = Table(f'{self.schema_name}.{self.__TABLE1}')
self.__comp_sim_conf_tbl = Table(f'{self.schema_name}.{self.__TABLE2}')
self.__comp_tbl = Table(f'{self.schema_name}.{self.__TABLE3}')
def get_select_comp(self):
try:
SSD = self.__sim_sttus_tbl
CSCD = self.__comp_sim_conf_tbl
ICD = self.__comp_tbl
query = SnowflakeQuery.from_(CSCD) \
.join(SSD).on_field('PLAN_SIM_CONF_ID') \
.join(ICD).on_field('COMP_ID') \
.select(CSCD.COMP_SIM_CONF_ID, SSD.sim_sttus_id, CSCD.PLAN_SIM_CONF_ID, \
CSCD.COMP_ID, ICD.COMP_NAME) \
.where(Criterion.all([SSD.sim_sttus_id == self.sim_sttus_id])) \
.get_sql()
comp_lst = self.execute_query(query)
if isinstance(component_lists['result'], str):
raise QueryException(component_lists)
return comp_lst["result"]
except Exception as e:
raise
My challenge lies in the below function where I need to compare
def compare_sim_sttus(self, sttus_id1, sttus_id2):
try:
selected_comp = self.get_select_comp()
matching_comp = []
for component in selected_components:
if component['sim_sttus_id'] == sttus_id1:
comp_id = component['comp_id']
comp_name = component['comp_name']
if self.__check_component_status(comp_id, sttus_id2):
matching_comp.append((comp_id, comp_name))
return matching_comp
except Exception as e:
raise
def __check_comp_sttus(self, comp_id, sttus_id):
try:
query = SnowflakeQuery \
.from_(self.__sim_sttus_tbl) \
.where(self.__sim_sttus_tbl.sim_sttus_id == sttus_id) \
.where(self.__comp_tbl.COMP_ID == comp_id) \
.get_sql()
comp_sttus = self.execute_query(query)
return len(comp_sttus['result']) > 0
except Exception as e:
raise
"
get_select_comp" method is using PyPika query to get list ofcomp_nameandcomp_idbased onsim_sttus_id. Its working as per the method requirement.compare_sim_sttuswhich internally calls the existing "get_select_comp" method to retrieve the list of selected components. Then, we iterate over each component and check if its sim_sttus_id matches sttus_id1. If it does, we retrieve the comp_id and comp_name and call the "check_comp_sttus" method to verify if the same comp_id exists for sttus_id2. If it does, we add the component to the matching_components list.
Below is the below endpoint code.
import traceback
from flasgger import swag_from
from flask_restful import Resource
from pathlib import Path
from <path_to_module import SimRes
__all__ = ['SimRes']
class SimResResource(Resource):
@classmethod
def get(cls, prog: str, plan: int, versn: float, sim_sttus_id: int):
try:
sttus_id1 = 1 # Set the first sim_sttus_id
sttus_id2 = 2 # Set the second sim_sttus_id
components_compare = SimRes(prog=prog, plan=plan, versn=versn, sim_sttus_id=sim_sttus_id)
response = components_compare.compare_sim_sttus(sttus_id1, sttus_id2)
return success_response(response)
except Exception as e:
app.logger.error(f'{str(e)}: {traceback.format_exc()}')
return error_response(e)
As you can see that in the above GET method I have passed the values for sttus_id1 and status_id2 which is hard coded. But I need to execute the API in POSTMAN, and I wanted to pass the sttus_id1 and sttus_id2 in the POSTMAN API and get the response.
below would be the endpoint route
/programs/<prog>/plans/<plan>/<versn>/sim_compare.
I am not able to figure out how to do that in my endpoint. I couldn't pass "sim_sttus_id" twice in my endpoint path, as it is giving me error saying "identical col name" and when I am passing <sttus_id1>/<sttus_id2> in the endpoint path, I am getting below error.
{
"error": "__init__() got an unexpected keyword argument 'sttus_id1'",
"success": false
}
So, I tried adding those two variables sttus_id1, and sttus_id2 in the class constructor, and removed them as a parameters from the compare_sim_sttus method and call them with "self" at the required positions. In the endpoint class I added them as parameters in the GET method, but no help. As, when I did, I am getting below error
"error": "__init__() missing 1 required positional argument: 'sim_sttus_id'",
"success": false
}
modified endpoint path /programs/<prog_id>/plans/<plan>/<versn>/<sttus_id1>/<sttus_id2>/sim_compare
I cannot pass sim_sttus_id in the endpoint as said earlier. Kindly suggest