Skip to content

Commit ca11b39

Browse files
Megha PrasannanMegha Prasannan
authored andcommitted
updated fit_kit_logged util with new functions
1 parent db57845 commit ca11b39

File tree

2 files changed

+91
-62
lines changed

2 files changed

+91
-62
lines changed

utils/fit_kit_logged.py

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
from oracle import OracleDB
22
import pandas as pd
33
from datetime import datetime
4+
import logging
5+
6+
7+
def process_kit_data():
8+
# Get test data for compartment 3
9+
kit_id_df = get_kit_id_logged_from_db()
10+
11+
# Split dataframe into two different dataframes, normal and abnormal
12+
normal_fit_kit_df, abnormal_fit_kit_df = split_fit_kits(kit_id_df)
13+
14+
# Process normal kits (only 1)
15+
if not normal_fit_kit_df.empty:
16+
device_id = normal_fit_kit_df["device_id"].iloc[0]
17+
logging.info(f"Processing normal kit with Device ID: {device_id}") # Logging normal device_id
18+
update_kit_service_management_entity(device_id, True)
19+
else:
20+
logging.warning("No normal kits found for processing.") # Log warning
21+
22+
# Process abnormal kits (multiple, loop through)
23+
if not abnormal_fit_kit_df.empty:
24+
for index, row in abnormal_fit_kit_df.iterrows():
25+
device_id = row["device_id"]
26+
logging.info(f"Processing abnormal kit with Device ID: {device_id}") # Logging abnormal device_id
27+
update_kit_service_management_entity(device_id, False)
28+
else:
29+
logging.warning("No abnormal kits found for processing.") # Log warning
430

531

632
def get_kit_id_logged_from_db():
733
kit_id_df = OracleDB().execute_query("""SELECT tk.kitid,tk.device_id,tk.screening_subject_id
8-
FROM tk_items_t tk
34+
FROM tk_items_t tk
935
INNER JOIN kit_queue kq ON kq.device_id = tk.device_id
1036
INNER JOIN ep_subject_episode_t se ON se.screening_subject_id = tk.screening_subject_id
1137
WHERE tk.logged_in_flag = 'Y'
@@ -30,7 +56,7 @@ def split_fit_kits(kit_id_df):
3056
return normal_fit_kit_df, abnormal_fit_kit_df
3157

3258

33-
def get_service_management_by_device_id(self, deviceid):
59+
def get_service_management_by_device_id(deviceid):
3460
get_service_management_df = OracleDB().execute_query(f"""SELECT kq.device_id, kq.test_kit_name, kq.test_kit_type, kq.test_kit_status,
3561
CASE WHEN tki.logged_in_flag = 'Y' THEN kq.logged_by_hub END AS logged_by_hub,
3662
CASE WHEN tki.logged_in_flag = 'Y' THEN kq.date_time_logged END AS date_time_logged,
@@ -59,74 +85,77 @@ def get_service_management_by_device_id(self, deviceid):
5985
LEFT OUTER JOIN valid_values mta ON mta.valid_value_id = mt.message_attribute_id AND mta.valid_value_id = 305482
6086
LEFT OUTER JOIN ORG o ON ep.start_hub_id = o.org_id
6187
LEFT OUTER JOIN ORG lo ON lo.org_code = kq.logged_by_hub
62-
# WHERE kq.test_kit_type = 'FIT' AND kq.device_id = {deviceid}
88+
WHERE kq.test_kit_type = 'FIT' AND kq.device_id = '{deviceid}'
6389
""")
6490
return get_service_management_df
6591

66-
# # normal kits (only 1)
67-
# device_id = normal_fit_kit_list["device_id"].iloc[0]
68-
# update_kit_service_management_entity(device_id, True)
69-
#
70-
# # abnormal kits (9 we need to loop through)
71-
# for index, row in abnormal_fit_kit_list.iterrows(): # only be for abnormal
72-
# device_id = row["device_id"]
73-
# update_kit_service_management_entity(device_id, False)
74-
#
75-
# execute_stored_procedure('PKG_TEST_KIT_QUEUE.p_validate_kit_queue')
76-
# execute_stored_procedure('PKG_TEST_KIT_QUEUE.p_calculate_result')
77-
78-
79-
def update_kit_service_management_entity(self, device_id, normal):
80-
# (device_id, test_kit_name, test_kit_type, logged_by_hub, date_time_logged, test_result,
81-
# calculated_result, date_time_authorised, post_response, post_attempts, put_response, put_attempts)
92+
93+
def execute_stored_procedures():
94+
db_instance = OracleDB() # Create an instance of the OracleDB class
95+
db_instance.execute_stored_procedure('PKG_TEST_KIT_QUEUE.p_validate_kit_queue')
96+
db_instance.execute_stored_procedure('PKG_TEST_KIT_QUEUE.p_calculate_result')
97+
98+
99+
def update_kit_service_management_entity(device_id, normal):
82100
get_service_management_df = get_service_management_by_device_id(device_id)
83101
test_kit_name = get_service_management_df["test_kit_name"].iloc[0]
84-
test_kit_type = get_service_management_df["test_kit_Type"].iloc[0]
102+
test_kit_type = get_service_management_df["test_kit_type"].iloc[0]
85103
logged_by_hub = get_service_management_df["logged_by_hub"].iloc[0]
86-
date_time_logged=get_service_management_df["date_time_logged"].iloc[0]
87-
calculated_result=get_service_management_df["calculated_result"].iloc[0]
88-
post_response=get_service_management_df["post_response"].iloc[0]
89-
post_attempts=get_service_management_df["post_attempts"].iloc[0]
90-
put_response=get_service_management_df["put_response"].iloc[0]
91-
put_attempts=get_service_management_df["put_attempts"].iloc[0]
92-
date_time_authorised = datetime.now().strftime("%#d-%b-%y %H.%M.%S.%f").upper() + "000"
104+
date_time_logged = get_service_management_df["date_time_logged"].iloc[0]
105+
calculated_result = get_service_management_df["calculated_result"].iloc[0]
106+
post_response = get_service_management_df["post_response"].iloc[0]
107+
post_attempts = get_service_management_df["post_attempts"].iloc[0]
108+
put_response = get_service_management_df["put_response"].iloc[0]
109+
put_attempts = get_service_management_df["put_attempts"].iloc[0]
110+
# format date
111+
date_time_authorised = datetime.now().strftime("%d-%b-%y %H.%M.%S.") + f"{datetime.now().microsecond:06d}000"
93112
if normal:
94113
test_result = 75
95114
else:
96115
test_result = 150
97-
update_query = f"""
98-
UPDATE kit_queue kq
99-
SET kq.test_kit_name = {test_kit_name},
100-
kq.test_kit_type = {test_kit_type},
101-
kq.test_kit_status = 'BCSS Ready',
102-
kq.logged_by_hub = {logged_by_hub},
103-
kq.date_time_logged = {date_time_logged},
104-
kq.test_result = {test_result},
105-
kq.calculated_result = {calculated_result},
106-
kq.error_code = NULL,
107-
kq.analyser_code = 'HMJackalt1',
108-
kq.date_time_authorised = {date_time_authorised},
109-
kq.authoriser_user_code = 'AUTO1',
110-
kq.post_response = {post_response},
111-
kq.post_attempts = {post_attempts},
112-
kq.put_response = {put_response},
113-
kq.put_attempts = {put_attempts}
114-
WHERE kq.device_id = {device_id}
115-
"""
116-
# values = {
117-
# "test_kit_name": test_kit_name,
118-
# "test_kit_type": test_kit_type,
119-
# "logged_by_hub": logged_by_hub,
120-
# "date_time_logged": date_time_logged,
121-
# "test_result": test_result,
122-
# "calculated_result": calculated_result,
123-
# "date_time_authorised": date_time_authorised,
124-
# "post_response": post_response,
125-
# "post_attempts": post_attempts,
126-
# "put_response": put_response,
127-
# "put_attempts": put_attempts,
128-
# "device_id": device_id
129-
# }
116+
# Parameterized query
117+
update_query = """
118+
UPDATE kit_queue kq
119+
SET kq.test_kit_name = :test_kit_name,
120+
kq.test_kit_type = :test_kit_type,
121+
kq.test_kit_status =:test_kit_status,
122+
kq.logged_by_hub = :logged_by_hub,
123+
kq.date_time_logged = :date_time_logged,
124+
kq.test_result = :test_result,
125+
kq.calculated_result = :calculated_result,
126+
kq.error_code = NULL,
127+
kq.analyser_code = 'HMJackalt1',
128+
kq.date_time_authorised = TO_TIMESTAMP(:date_time_authorised, 'DD-Mon-YY HH24.MI.SS.FF9'),
129+
kq.authoriser_user_code = 'AUTO1',
130+
kq.post_response = :post_response,
131+
kq.post_attempts = :post_attempts,
132+
kq.put_response = :put_response,
133+
kq.put_attempts = :put_attempts
134+
WHERE kq.device_id = :device_id
135+
"""
136+
137+
# Parameters dictionary
138+
params = {
139+
"test_kit_name": test_kit_name,
140+
"test_kit_type": test_kit_type,
141+
"test_kit_status": 'BCSS_READY',
142+
"logged_by_hub": logged_by_hub,
143+
"date_time_logged": date_time_logged,
144+
"test_result": int(test_result),
145+
"calculated_result": calculated_result,
146+
"date_time_authorised": str(date_time_authorised),
147+
"post_response": int(post_response),
148+
"post_attempts": int(post_attempts),
149+
"put_response": put_response,
150+
"put_attempts": put_attempts,
151+
"device_id": device_id
152+
}
153+
154+
# Execute query
155+
print("Parameters before execution:", params)
156+
rows_affected=OracleDB().update_or_insert_data_to_table(update_query, params)
157+
print(f"Rows affected: {rows_affected}")
158+
130159

131160
def get_nhs_number_from_subject_id(subject_ids, df):
132161
temp_df = OracleDB().execute_query(

utils/oracle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (s
114114
except Exception as connectionError:
115115
print(f"Failed to connect to the DB! with connection error: {connectionError}")
116116

117-
def update_or_insert_data_to_table(self, statement): # To update or insert data into a table
117+
def update_or_insert_data_to_table(self, statement,params): # To update or insert data into a table
118118
try:
119119
print("Attempting DB connection...")
120120
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
121121
print(conn.version, "DB connection successful!")
122122
try:
123123
print("Attempting to insert/update table")
124124
cursor = conn.cursor()
125-
cursor.execute(statement)
125+
cursor.execute(statement,params)
126126
conn.commit()
127127
print(conn.version, "DB table successfully updated!")
128128
except Exception as dbUpdateInsertError:

0 commit comments

Comments
 (0)