From 907129d6736df90a0c5b3d0efe902009e553a82b Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Mon, 4 Sep 2023 07:43:03 +0530 Subject: [PATCH] nagpur changes --- cloud-functions/nodejs/functions/index.js | 78 +++++++ scripts/combine_hemocube_denovix.py | 27 +++ scripts/consolidated_data.py | 18 +- scripts/pdf_report.py | 265 ++++++++++++++++++++++ scripts/test_collection.py | 69 ++++++ scripts/user_collection.py | 69 ++++++ 6 files changed, 520 insertions(+), 6 deletions(-) create mode 100644 scripts/combine_hemocube_denovix.py create mode 100644 scripts/pdf_report.py create mode 100644 scripts/test_collection.py create mode 100644 scripts/user_collection.py diff --git a/cloud-functions/nodejs/functions/index.js b/cloud-functions/nodejs/functions/index.js index f61d66c..a44da35 100644 --- a/cloud-functions/nodejs/functions/index.js +++ b/cloud-functions/nodejs/functions/index.js @@ -3,6 +3,9 @@ const logger = require("firebase-functions/logger"); const functions = require("firebase-functions"); const admin = require("firebase-admin"); const moment = require('moment'); +const fs = require('fs'); +const csv = require('csv-parser'); +const html_to_pdf = require('html-pdf-node'); admin.initializeApp(); @@ -40,3 +43,78 @@ exports.registerUser = functions.region("asia-south1").https.onRequest(async (re res.json({ status: "400", message: "some error.", result: "true" }); } }); + +exports.addBloodTest = functions.region("asia-south1").https.onRequest(async (req, res) => { + res.set('Access-Control-Allow-Origin', "*") + res.set('Access-Control-Allow-Methods', 'GET, POST'); + + if (req.method === "OPTIONS") { + // stop preflight requests here + res.set("Access-Control-Allow-Headers", "Content-Type, x-client"); + res.status(204).send(''); + return; + } + + try { + const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27"; + logger.info(`User-Agent ${req.get("User-Agent")}`); + logger.info(`token ${req.get("x-client")}`); + if (req.get("x-client") !== token) { + res.json({ status: "403", message: "auth error", result: "true" }); + } + else { + const { body } = req; + const { _id } = body; + logger.info(`_id ${_id}`); + await admin + .firestore() + .collection("testData") + .add(Object.assign(Object.assign({}, body), { createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss") })) + res.json({ status: "200" }); + } + } + catch (err) { + logger.info(`error ${err}`); + res.json({ status: "400", message: "some error.", result: "true" }); + } +}); + +exports.reportDownload = functions.region("asia-south1").https.onRequest(async (req, res) => { + res.set('Access-Control-Allow-Origin', "*") + res.set('Access-Control-Allow-Methods', 'GET, POST'); + + if (req.method === "OPTIONS") { + // stop preflight requests here + res.set("Access-Control-Allow-Headers", "Content-Type, x-client"); + res.status(204).send(''); + return; + } + + try { + const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27"; + logger.info(`User-Agent ${req.get("User-Agent")}`); + logger.info(`token ${req.get("x-client")}`); + if (req.get("x-client") !== token) { + res.json({ status: "403", message: "auth error", result: "true" }); + } + else { + const { body } = req; + const { data } = body; + logger.info(`_id ${_id}`); + const fileName = `${data["_id"]}.pdf`; + // await makePdf(); + res.sendFile(fileName, options, function (err) { + if (err) { + next(err); + } else { + console.log('Sent:', fileName); + } + }); + } + } + catch (err) { + logger.info(`error ${err}`); + res.json({ status: "400", message: "some error.", result: "true" }); + } +}); + diff --git a/scripts/combine_hemocube_denovix.py b/scripts/combine_hemocube_denovix.py new file mode 100644 index 0000000..45507c9 --- /dev/null +++ b/scripts/combine_hemocube_denovix.py @@ -0,0 +1,27 @@ +import pandas as pd +import os + +curdir = os.getcwd() +path_delim = '/' +df1 = pd.read_excel(curdir + path_delim + "NagpurData_FinalResults_Aug1-a.xlsx", sheet_name="Sheet1") +df2 = pd.read_excel(curdir + path_delim + "NagpurData_FinalResults_Aug1-a.xlsx", sheet_name="Sheet3") + +print(df1) +print(df2) + +df = df1.merge(df2, on="_id") +df['Age'] = 2023 - df['birthYear'] +print(df) + +df = df[["_id", "name", "abhaId", "aadharId", "Age", "gender", "category", "maritalStatus", "house", "district", "state", "pinCode", "phoneNumber", "Result", "bloodGroup"]] +print(df) + +# df = df.sort_values(by=['testTime'], ascending=True) + +df.rename(columns={'_id': "Sample ID", "name": "Name", "abhaId": "ABHA ID", "aadharId": "Aadhaar ID", "gender": "Gender", "category": "Category", "maritalStatus": "Marital Status", "house": "Address", "district": "District", "state": "State", "pinCode": "Pincode", "phoneNumber": "Mobile Number", "Result": "Test Result", "bloodGroup": "Blood Group"}, inplace = True) +df = df.reindex(["Sample ID", "Name", "ABHA ID", "Aadhaar ID", "Age", "Gender", "Category", "Marital Status", "Address", "District", "State", "Pincode", "Mobile Number", "Test Result", "Blood Group"], axis=1) +print(df) + +writer = pd.ExcelWriter(curdir + path_delim + "NagpurData_FinalResults_Aug1-op.xlsx", engine = 'openpyxl') +df.to_excel(writer, sheet_name = 'op', index=False) +writer.close() \ No newline at end of file diff --git a/scripts/consolidated_data.py b/scripts/consolidated_data.py index 4038256..7c8251b 100644 --- a/scripts/consolidated_data.py +++ b/scripts/consolidated_data.py @@ -4,10 +4,10 @@ from firebase_admin import credentials from firebase_admin import firestore from google.cloud.firestore_v1.base_query import FieldFilter import pandas as pd -import datetime +# import datetime import sys import platform -from datetime import datetime +from datetime import datetime, timedelta if __name__ == "__main__": @@ -16,7 +16,7 @@ if __name__ == "__main__": else: path_delim = '/' - key_file_path = os.getcwd() + path_delim + "keys" + path_delim + "hpos-af3cc-firebase-adminsdk-n261k-2bfd463ec0.json" + key_file_path = os.getcwd() + path_delim + "keys" + path_delim + "hpos-prod-firebase-adminsdk-bionp-7af43fc5d6.json" cred = credentials.Certificate(key_file_path) # Replace with your own service account key path firebase_admin.initialize_app(cred) @@ -29,8 +29,14 @@ if __name__ == "__main__": start_date = sys.argv[1] # '2023-01-12' #input("Please enter the start date (yyyy-mm-dd): ") end_date = sys.argv[2] #'2023-07-16' #input("Please enter the end date (yyyy-mm-dd): ") + # if start_date == end_date: + # start_date = datetime.strptime(start_date, "%Y-%m-%d") + # end_date = (start_date + timedelta(days=1)).strftime("%Y-%m-%d") + + print("end_date", end_date) + # patient_collection = db.collection("patientData") - test_collection = db.collection("testData") + query = db.collection("testData").where(filter=FieldFilter("testTime", ">=", start_date)).where(filter=FieldFilter("testTime", "<", end_date)) # query = test_collection # patient_docs = query.stream() @@ -53,7 +59,7 @@ if __name__ == "__main__": # data.append(combined_data) - test_docs = test_collection.stream() + test_docs = query.stream() for test_doc in test_docs: test_data = test_doc.to_dict() data.append(test_data) @@ -74,7 +80,7 @@ if __name__ == "__main__": df = df.sort_values(by=['testTime'], ascending=False) df = df[["_id", "calculatedRatio", "deviceId", "deviceRatio", "deviceType", "kitSerial", "led1Average", "led1Buffer", "led1Sample", "led2Average", "led2Buffer", "led2Sample", "location", "deviceSerialNumber", "name", "testTime"]] - df.rename(columns={'deviceSerialNumber': "login_id", "calculatedRatio": "calibrated_ratio", "led1Buffer": "427_buffer_intensity", "led2Buffer": "555_buffer_intensity", "led1Sample": "427_sample_intensity", "led2Sample": "555_sample_intensity", "led1Average": "427_absorbance", "led2Average": "555_absorbance"}, inplace = True) + # df.rename(columns={'deviceSerialNumber': "login_id", "calculatedRatio": "calibrated_ratio", "led1Buffer": "427_buffer_intensity", "led2Buffer": "555_buffer_intensity", "led1Sample": "427_sample_intensity", "led2Sample": "555_sample_intensity", "led1Average": "427_absorbance", "led2Average": "555_absorbance"}, inplace = True) df = df.reindex(sorted(df.columns), axis=1) df.to_excel(writer, sheet_name = 'data', index=False) diff --git a/scripts/pdf_report.py b/scripts/pdf_report.py new file mode 100644 index 0000000..f28e502 --- /dev/null +++ b/scripts/pdf_report.py @@ -0,0 +1,265 @@ +import sys +import csv +from xhtml2pdf import pisa +import os +import pandas as pd + +rptData = {} + +rptData['Person Name'] = "row['Name']" +rptData['Age'] = "row['Age']" +rptData['Gender'] = "row['Gender']" +rptData['Age / Sex'] = "Male" +rptData['Sample type'] = 'Capillary Whole Blood' +rptData['Family History of Sickle Cell Anemia'] = 'Unknown' +rptData['Marital Status'] = "row['Marital Status']" +rptData['Test Date'] = "date" +rptData['Patient ID'] = "row['ABHA ID']" +rptData['Sample ID'] = "row['_id']" +rptData['value'] = "row['Measured Deovix Ratio']" + +rd = rptData +# template_path = os.path.join(THIS_DIR, 'report_template.html') + +report_template =r''' + + + + + + + + +
+ + + + + + +
+
Person Name: {{ reportData["Person Name"] }}
+
Age / Sex: {{reportData['Age']}} / {{reportData['Gender']}}
+
Sample type:{{ reportData["Sample type"] }}
+
Family History of Sickle Cell Anemia:{{ reportData["Family History of Sickle Cell Anemia"] }}
+
+
Marital Status:{{ reportData["Marital Status"] }}
+
Test Date:{{ reportData["Test Date"] }}
+
ABHA ID:{{ reportData["Patient ID"] }}
+
Sample ID:{{ reportData["Sample ID"] }}
+
+
+ + +
+ + +
+
POINT OF CARE SICKLE CELL ANEMIA TEST
+ + + + + + + + + + + + +
+ Test Description + + RESULT + + REFERENCE RANGES +
+
Sickle Cell + Anemia +
(Method: HPOS)
+
+
+
Ra = {{ reportData["value"] }}
+ +
+
+ < 0.16: Normal (HbA)
+
0.165 – 0.235: Sickle-cell Trait (HbAS)
+
> 0.24: Sickle-cell Disease (HbSS)
+
0.16-0.165: Inconclusive (Negative Borderline)
+
0.235 – 0.24: Inconclusive (Positive Borderline)
+
+
+ +
+ + +
+ Test Principle: This point of care quantitative diagnostic test for sickle-cell anemia + works on the principle of absorption + spectroscopy. The test helps in differentiating heterozygous/homozygous hemoglobin from normal hemoglobin. +
+ +
+ Method: High Performance Optical Spectroscopy (HPOS) for detection of Sickle cell trait and + sickle cell disease in whole blood capillary blood samples. +
+ +
+ Note: + + + Borderline cases are reported as inconclusive. It may occur due to several factors such as medication, + transfusion, field conditions and assay process. Further clinical tests are recommended in these cases for + diagnosis. +
+ + + + +
+
+ *** END OF REPORT ***
+
+
+ This is an electronically generated report. Generated at HH:MM hrs on DD-MMM-YYYY. +
+
+ Note: Assay results should be correlated clinically with other clinical findings +
+
+
+ + + +''' + +# rptHtml = j2_env.from_string(report_template).render(reportData=rd) +reportFile = open('report.pdf','w+b') +pisa_status = pisa.CreatePDF(report_template, dest=reportFile) +if not pisa_status.err: + print("Created PDF report %s." % outFilename) +#os.remove(rd['qrcodeImgFile']) diff --git a/scripts/test_collection.py b/scripts/test_collection.py new file mode 100644 index 0000000..9702bc9 --- /dev/null +++ b/scripts/test_collection.py @@ -0,0 +1,69 @@ +import os +import firebase_admin +from firebase_admin import credentials +from firebase_admin import firestore +from google.cloud.firestore_v1.base_query import FieldFilter +import pandas as pd +import datetime +import sys + +# Initialize Firebase Admin SDK +# cred = credentials.Certificate(r'C:\Users\smila\PycharmProjects\pythonProject\hpos-af3cc-firebase-adminsdk-n261k-2bfd463ec0.json') # Replace with your own service account key path +cred = credentials.Certificate(r'/Users/apple/Downloads/work/pythonProject-master/hpos-prod-firebase-adminsdk-bionp-3c041b2300.json') # Replace with your own service account key path +firebase_admin.initialize_app(cred) + +# Get a reference to the Firestore database +db = firestore.client() + +# Specify the collections +patient_collection = db.collection("patientData") +test_collection = db.collection("testData") +start_date = sys.argv[1] # '2023-01-12' #input("Please enter the start date (yyyy-mm-dd): ") +end_date = sys.argv[2] #'2023-07-16' #input("Please enter the end date (yyyy-mm-dd): ") + +query = patient_collection.where(filter=FieldFilter("createdAt", ">=", start_date)).where(filter=FieldFilter("createdAt", "<", end_date)) +patient_docs = query.stream() + +# Prepare data to store in CSV +data = [] +for patient_doc in patient_docs: + print(patient_doc) + patient_data = patient_doc.to_dict() + # patient_id = patient_data["_id"] + + # # Query the document from testData collection based on the common _id + # test_docs = test_collection.where("_id", "==", patient_id).stream() + + # for test_doc in test_docs: + # print(test_doc) + # test_data = test_doc.to_dict() + + # # Combine the data from both collections into a single dictionary + # combined_data = {**patient_data, **test_data} + + # # Fill empty fields in test_data with corresponding values from patient_data + # for key, value in combined_data.items(): + # if value == "" and key in patient_data: + # combined_data[key] = patient_data[key] + + # data.append(combined_data) + data.append(patient_data) + +# Convert the data to a DataFrame +df = pd.DataFrame(data) +print(df) +print(df.size) + +# Save the DataFrame to a CSV file +output_filename = "data.csv" +df.to_csv(output_filename, index=False) + +downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads") +output_path = os.path.join(downloads_dir, output_filename) +df.to_csv(output_path, index=False) + +print(f"Data saved to '{output_path}'") + +# Close the Firebase Admin SDK +firebase_admin.delete_app(firebase_admin.get_app()) + diff --git a/scripts/user_collection.py b/scripts/user_collection.py new file mode 100644 index 0000000..9702bc9 --- /dev/null +++ b/scripts/user_collection.py @@ -0,0 +1,69 @@ +import os +import firebase_admin +from firebase_admin import credentials +from firebase_admin import firestore +from google.cloud.firestore_v1.base_query import FieldFilter +import pandas as pd +import datetime +import sys + +# Initialize Firebase Admin SDK +# cred = credentials.Certificate(r'C:\Users\smila\PycharmProjects\pythonProject\hpos-af3cc-firebase-adminsdk-n261k-2bfd463ec0.json') # Replace with your own service account key path +cred = credentials.Certificate(r'/Users/apple/Downloads/work/pythonProject-master/hpos-prod-firebase-adminsdk-bionp-3c041b2300.json') # Replace with your own service account key path +firebase_admin.initialize_app(cred) + +# Get a reference to the Firestore database +db = firestore.client() + +# Specify the collections +patient_collection = db.collection("patientData") +test_collection = db.collection("testData") +start_date = sys.argv[1] # '2023-01-12' #input("Please enter the start date (yyyy-mm-dd): ") +end_date = sys.argv[2] #'2023-07-16' #input("Please enter the end date (yyyy-mm-dd): ") + +query = patient_collection.where(filter=FieldFilter("createdAt", ">=", start_date)).where(filter=FieldFilter("createdAt", "<", end_date)) +patient_docs = query.stream() + +# Prepare data to store in CSV +data = [] +for patient_doc in patient_docs: + print(patient_doc) + patient_data = patient_doc.to_dict() + # patient_id = patient_data["_id"] + + # # Query the document from testData collection based on the common _id + # test_docs = test_collection.where("_id", "==", patient_id).stream() + + # for test_doc in test_docs: + # print(test_doc) + # test_data = test_doc.to_dict() + + # # Combine the data from both collections into a single dictionary + # combined_data = {**patient_data, **test_data} + + # # Fill empty fields in test_data with corresponding values from patient_data + # for key, value in combined_data.items(): + # if value == "" and key in patient_data: + # combined_data[key] = patient_data[key] + + # data.append(combined_data) + data.append(patient_data) + +# Convert the data to a DataFrame +df = pd.DataFrame(data) +print(df) +print(df.size) + +# Save the DataFrame to a CSV file +output_filename = "data.csv" +df.to_csv(output_filename, index=False) + +downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads") +output_path = os.path.join(downloads_dir, output_filename) +df.to_csv(output_path, index=False) + +print(f"Data saved to '{output_path}'") + +# Close the Firebase Admin SDK +firebase_admin.delete_app(firebase_admin.get_app()) +