Files
hpos-data/scripts/test_collection.py

71 lines
2.6 KiB
Python
Raw Normal View History

2023-09-04 07:43:03 +05:30
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
2023-09-21 14:09:02 +05:30
from datetime import datetime
2023-09-04 07:43:03 +05:30
import sys
# Initialize Firebase Admin SDK
2023-09-25 10:42:30 +05:30
cred = credentials.Certificate(os.getcwd() + '/' + 'keys/hpos-prod-firebase-adminsdk.json') # Replace with your own service account key path
2023-09-04 07:43:03 +05:30
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")
2023-09-04 08:18:26 +05:30
start_date = sys.argv[1] # '2023-09-12' #input("Please enter the start date (yyyy-mm-dd): ")
2023-09-04 07:43:03 +05:30
end_date = sys.argv[2] #'2023-07-16' #input("Please enter the end date (yyyy-mm-dd): ")
2023-09-04 08:18:26 +05:30
query = test_collection.where(filter=FieldFilter("testTime", ">=", start_date)).where(filter=FieldFilter("testTime", "<", end_date))
docs = query.stream()
2023-09-04 07:43:03 +05:30
# Prepare data to store in CSV
data = []
2023-09-04 08:18:26 +05:30
for doc in docs:
doc_data = doc.to_dict()
2023-09-04 07:43:03 +05:30
# 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)
2023-09-04 08:18:26 +05:30
data.append(doc_data)
2023-09-04 07:43:03 +05:30
# Convert the data to a DataFrame
df = pd.DataFrame(data)
print(df)
print(df.size)
2023-09-25 10:42:30 +05:30
df = df[["_id", "classificationResult", "calculatedRatio", "deviceId", "deviceRatio", "deviceType", "kitSerial", "led1Average", "led1Buffer", "led1Sample", "led2Average", "led2Buffer", "led2Sample", "location", "deviceSerialNumber", "name", "testTime", "resultData"]]
2023-09-21 14:09:02 +05:30
2023-09-04 07:43:03 +05:30
# Save the DataFrame to a CSV file
2023-09-21 14:09:02 +05:30
output_filename = f'tests_{datetime.today().strftime("%d_%m_%Y_%H_%M")}.xlsx'
2023-09-04 07:43:03 +05:30
downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads")
output_path = os.path.join(downloads_dir, output_filename)
2023-09-21 14:09:02 +05:30
writer = pd.ExcelWriter(output_path, engine = 'openpyxl')
df.to_excel(writer, sheet_name = 'data', index=False)
writer.close()
2023-09-04 07:43:03 +05:30
print(f"Data saved to '{output_path}'")
# Close the Firebase Admin SDK
firebase_admin.delete_app(firebase_admin.get_app())