nagpur changes

This commit is contained in:
Pritimay Sarkar
2023-09-04 07:43:03 +05:30
parent cfe3492a80
commit 907129d673
6 changed files with 520 additions and 6 deletions

View File

@@ -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" });
}
});

View File

@@ -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()

View File

@@ -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)

265
scripts/pdf_report.py Normal file
View File

@@ -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'''
<!DOCTYPE html>
<html>
<head>
<style>
@media print {
body {
-webkit-print-color-adjust: exact;
}
}
.person-details-row {
display: flex;
flex-direction: row;
}
.person-details-col {
display: flex;
flex-direction: column;
height: 90px;
width: 50%;
margin: 1px;
border: 0;
}
.test-details-row {
border: 0;
}
.test-details-col {
height: 100px;
}
.test-cell-div {
/* border: 1px solid black; */
border-collapse: collapse;
height: 20px;
padding: 10px;
}
.table-header {
border: 1px solid black;
margin: 0 0 -10px 10px;
background-color: rgb(191, 191, 191);
text-align: center;
justify-content: center;
align-items: center;
display: flex;
font-weight: 600;
height: 50px;
width: 98%;
}
@media print {
.table-header {
background-color: rgb(191, 191, 191) !important;
print-color-adjust: exact;
}
}
@media print {
.vendorListHeading th {
color: white !important;
}
}
.test-method {
font-weight: 200 !important;
color: rgb(191, 191, 191);
}
.result-value {
justify-content: center;
align-items: center;
display: flex;
}
.end-of-report {
display: flex;
align-items: center;
justify-content: center;
}
.logo {
background-color: red;
width: 10;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
ul {
list-style-type: none;
/* margin: 0; */
/* padding: 0; */
}
</style>
</head>
<body>
<div>
<div class="logo">
<img src="logo_nobg.png" width="138" height="128" alt="sickle cell logo" />
</div>
<table style="border: 1px solid black; margin: 10px; width: 98%;">
<tr class="person-details-row">
<td class="person-details-col" style="border-right: 2; width: 70%;">
<div><strong>Person Name:</strong> {{ reportData["Person Name"] }}</div>
<div><strong>Age / Sex:</strong> {{reportData['Age']}} / {{reportData['Gender']}}</div>
<div><strong>Sample type:</strong>{{ reportData["Sample type"] }}</div>
<div><strong>Family History of Sickle Cell Anemia:</strong>{{ reportData["Family History of Sickle Cell Anemia"] }}</div>
</td>
<td class="person-details-col" style="width: 30%;">
<div><strong>Marital Status:</strong>{{ reportData["Marital Status"] }}</div>
<div><strong>Test Date:</strong>{{ reportData["Test Date"] }}</div>
<div><strong>ABHA ID:</strong>{{ reportData["Patient ID"] }}</div>
<div><strong>Sample ID:</strong>{{ reportData["Sample ID"] }}</div>
</td>
</tr>
</table>
</div>
<div style="height: 2px;width: 98%; background-color: rgb(56, 105, 166); margin: 30px 10px 40px 10px;"></div>
<div>
<div class="table-header">POINT OF CARE SICKLE CELL ANEMIA TEST</div>
<table class="test-details-table" style="border: 1px solid black; margin: 10px; width: 98%;">
<tr style="height: 50px;">
<th>
Test Description
</th>
<th>
RESULT
</th>
<th>
REFERENCE RANGES
</th>
</tr>
<tr class="test-details-row">
<td class="test-details-col" style="width: 20%; margin: 10px;">
<div style="margin: 10px;">Sickle Cell
Anemia
<div class="test-method">(Method: HPOS)</div>
</div>
</td>
<td style="width: 30%;">
<div class="result-value">Ra = {{ reportData["value"] }}</div>
<!-- <div class="test-cell-div">Normal</div>
<div class="test-cell-div">Sickle Cell Trait</div>
<div class="test-cell-div">Sickle Cell Disease</div>
<div class="test-cell-div">Negative Borderline</div>
<div class="test-cell-div">Positive Borderline</div> -->
</td>
<td style="width: 50%;">
<div class="test-cell-div">
< 0.16: Normal (HbA)</div>
<div class="test-cell-div">0.165 0.235: Sickle-cell Trait (HbAS)</div>
<div class="test-cell-div">> 0.24: Sickle-cell Disease (HbSS)</div>
<div class="test-cell-div">0.16-0.165: Inconclusive (Negative Borderline)</div>
<div class="test-cell-div">0.235 0.24: Inconclusive (Positive Borderline)</div>
</td>
<!-- <td style="width: 25%;">
<div class="test-cell-div">Normal</div>
<div class="test-cell-div">Sickle Cell Trait</div>
<div class="test-cell-div">Sickle Cell Disease</div>
<div class="test-cell-div">Recommended for HPLC or Electrophoresis Tests</div>
<div class="test-cell-div">Recommended for HPLC or Electrophore</div>
</td> -->
</tr>
</table>
</div>
<div>
<!-- <div style="font-weight: 600; margin: 10px;">INTERPRETATION:</div> -->
<div style="margin: 10px;">
<strong>Test Principle:</strong> 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.
</div>
<div style="margin: 10px;">
<strong>Method:</strong> High Performance Optical Spectroscopy (HPOS) for detection of Sickle cell trait and
sickle cell disease in whole blood capillary blood samples.
</div>
<div style="margin: 10px;">
<strong> Note:</strong>
<!-- <ul>
<li>a. Blood transfusion may have an impact on the test results.</li>
<li>
b. Patients already on sickle-cell medications may impact test results.
</li>
</ul> -->
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.
</div>
<!-- <div
style="margin: 80px 10px 20px 50px; width: 88%; display: flex; flex-direction: row; justify-content: space-between; align-items: center;">
<div style="margin: 5px;">DATE:</div>
<div style="margin: 5px;">Hematologist</div>
</div> -->
<!-- <div style="margin: 30px;">
This report is for the perusal of doctor only. Not for medico legal cases. Clinical correlation is
essential.
Please contact us in case of unexpected result.
</div> -->
<div style="height: 3px;width: 98%; background-color: black; margin-top: 150px;"></div>
<div class="end-of-report">
*** END OF REPORT ***</div>
<div style="display: flex; justify-content: center; align-items: center; flex-direction: column;">
<div style="font-style: italic; color: rgb(191, 191, 191);">
This is an electronically generated report. Generated at HH:MM hrs on DD-MMM-YYYY.
</div>
<div>
Note: Assay results should be correlated clinically with other clinical findings
</div>
</div>
</div>
</body>
</html>
'''
# 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'])

View File

@@ -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())

View File

@@ -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())