Skip to content

Commit 4c60755

Browse files
Megha PrasannanMegha Prasannan
authored andcommitted
Workiing on task 1,2,3 for compartment 3
1 parent 5c96fec commit 4c60755

File tree

2 files changed

+172
-18
lines changed

2 files changed

+172
-18
lines changed

utils/fit_kit_logged.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from oracle import OracleDB
2+
import pandas as pd
3+
from datetime import datetime
4+
5+
6+
def get_kit_id_logged_from_db():
7+
kit_id_df = OracleDB().execute_query("""SELECT tk.kitid,tk.device_id,tk.screening_subject_id
8+
FROM tk_items_t tk
9+
INNER JOIN kit_queue kq ON kq.device_id = tk.device_id
10+
INNER JOIN ep_subject_episode_t se ON se.screening_subject_id = tk.screening_subject_id
11+
WHERE tk.logged_in_flag = 'Y'
12+
AND kq.test_kit_status IN ('LOGGED', 'POSTED')
13+
AND se.episode_status_id = 11352
14+
AND tk.tk_type_id = 3
15+
AND se.latest_event_status_id = 11223
16+
AND tk.logged_in_at = 23159
17+
AND tk.reading_flag = 'N'
18+
AND tk.test_results IS NULL
19+
fetch first 10 rows only""")
20+
21+
return kit_id_df
22+
23+
24+
def split_fit_kits(kit_id_df):
25+
number_of_normal = 1
26+
number_of_abnormal = 9
27+
# Split dataframe into two dataframes
28+
normal_fit_kit_df = kit_id_df.iloc[:number_of_normal]
29+
abnormal_fit_kit_df = kit_id_df.iloc[number_of_normal:number_of_normal + number_of_abnormal]
30+
return normal_fit_kit_df, abnormal_fit_kit_df
31+
32+
33+
def get_service_management_by_device_id(self, deviceid):
34+
get_service_management_df = OracleDB().execute_query(f"""SELECT kq.device_id, kq.test_kit_name, kq.test_kit_type, kq.test_kit_status,
35+
CASE WHEN tki.logged_in_flag = 'Y' THEN kq.logged_by_hub END AS logged_by_hub,
36+
CASE WHEN tki.logged_in_flag = 'Y' THEN kq.date_time_logged END AS date_time_logged,
37+
tki.logged_in_on AS tk_logged_date_time, kq.test_result, kq.calculated_result,
38+
kq.error_code,
39+
(SELECT vvt.description
40+
FROM tk_analyser_t tka
41+
INNER JOIN tk_analyser_type_error tkate ON tkate.tk_analyser_type_id = tka.tk_analyser_type_id
42+
INNER JOIN valid_values vvt ON tkate.tk_analyser_error_type_id = vvt.valid_value_id
43+
WHERE tka.analyser_code = kq.analyser_code AND tkate.error_code = kq.error_code)
44+
AS analyser_error_description, kq.analyser_code, kq.date_time_authorised,
45+
kq.authoriser_user_code, kq.datestamp, kq.bcss_error_id,
46+
REPLACE(mt.description, 'ERROR - ', '') AS error_type,
47+
NVL(mta.allowed_value, 'N') AS error_ok_to_archive,
48+
kq.post_response, kq.post_attempts, kq.put_response,
49+
kq.put_attempts, kq.date_time_error_archived,
50+
kq.error_archived_user_code, sst.screening_subject_id,
51+
sst.subject_nhs_number, tki.test_results, tki.issue_date,
52+
o.org_code AS issued_by_hub
53+
FROM kit_queue kq
54+
LEFT OUTER JOIN tk_items_t tki ON tki.device_id = kq.device_id
55+
OR (tki.device_id IS NULL AND tki.kitid = pkg_test_kit.f_get_kit_id_from_device_id(kq.device_id))
56+
LEFT OUTER JOIN screening_subject_t sst ON sst.screening_subject_id = tki.screening_subject_id
57+
LEFT OUTER JOIN ep_subject_episode_t ep ON ep.subject_epis_id = tki.subject_epis_id
58+
LEFT OUTER JOIN message_types mt ON kq.bcss_error_id = mt.message_type_id
59+
LEFT OUTER JOIN valid_values mta ON mta.valid_value_id = mt.message_attribute_id AND mta.valid_value_id = 305482
60+
LEFT OUTER JOIN ORG o ON ep.start_hub_id = o.org_id
61+
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}
63+
""")
64+
return get_service_management_df
65+
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)
82+
get_service_management_df = get_service_management_by_device_id(device_id)
83+
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]
85+
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"
93+
if normal:
94+
test_result = 75
95+
else:
96+
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+
# }
130+
131+
def get_nhs_number_from_subject_id(subject_ids, df):
132+
temp_df = OracleDB().execute_query(
133+
f"SELECT SCREENING_SUBJECT_ID, SUBJECT_NHS_NUMBER FROM SCREENING_SUBJECT_T WHERE SCREENING_SUBJECT_ID = {subject_ids}")
134+
df = df.merge(temp_df[["screening_subject_id", "subject_nhs_number"]], on="screening_subject_id", how="left")
135+
return df

utils/oracle.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,41 @@ def execute_query(self, query: str,): # To use when "select xxxx" (stored proced
9494
except Exception as connectionError:
9595
print(f"Failed to connect to the DB! with connection error {connectionError}")
9696

97-
# The following function is commented out as it is not used currently, but may be needed in future compartments
9897

99-
# def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures)
100-
# try:
101-
# print("Attempting DB connection...")
102-
# conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
103-
# print(conn.version, "DB connection successful!")
104-
# try:
105-
# print(f"Attempting to execute stored procedure: {procedure}")
106-
# cursor = conn.cursor()
107-
# cursor.callproc(procedure)
108-
# print(conn.version, "stored procedure execution successful!")
109-
# except Exception as executionError:
110-
# print(f"Failed to execute stored procedure with execution error: {executionError}")
111-
# finally:
112-
# if conn is not None:
113-
# conn.close()
114-
# except Exception as connectionError:
115-
# print(f"Failed to connect to the DB! with connection error: {connectionError}")
98+
def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures)
99+
try:
100+
print("Attempting DB connection...")
101+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
102+
print(conn.version, "DB connection successful!")
103+
try:
104+
print(f"Attempting to execute stored procedure: {procedure}")
105+
cursor = conn.cursor()
106+
cursor.callproc(procedure)
107+
conn.commit()
108+
print(conn.version, "stored procedure execution successful!")
109+
except Exception as executionError:
110+
print(f"Failed to execute stored procedure with execution error: {executionError}")
111+
finally:
112+
if conn is not None:
113+
conn.close()
114+
except Exception as connectionError:
115+
print(f"Failed to connect to the DB! with connection error: {connectionError}")
116+
117+
def update_or_insert_data_to_table(self, statement): # To update or insert data into a table
118+
try:
119+
print("Attempting DB connection...")
120+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
121+
print(conn.version, "DB connection successful!")
122+
try:
123+
print("Attempting to insert/update table")
124+
cursor = conn.cursor()
125+
cursor.execute(statement)
126+
conn.commit()
127+
print(conn.version, "DB table successfully updated!")
128+
except Exception as dbUpdateInsertError:
129+
print(f"Failed to insert/update values from the DB table! with error {dbUpdateInsertError}")
130+
finally:
131+
if conn is not None:
132+
conn.close()
133+
except Exception as connectionError:
134+
print(f"Failed to connect to the DB! with connection error {connectionError}")

0 commit comments

Comments
 (0)