Files
hpos-data/scripts/consolidated_data.py

98 lines
3.9 KiB
Python
Raw Normal View History

import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
2023-08-21 14:53:56 +05:30
from google.cloud.firestore_v1.base_query import FieldFilter
import pandas as pd
2023-09-04 07:43:03 +05:30
# import datetime
2023-08-21 14:53:56 +05:30
import sys
import platform
2023-09-04 07:43:03 +05:30
from datetime import datetime, timedelta
2023-08-21 14:53:56 +05:30
2023-12-10 13:49:59 +05:30
environment = "preprod"
2023-11-19 19:01:44 +05:30
2023-08-21 14:53:56 +05:30
if __name__ == "__main__":
if platform.system() == 'Windows':
2023-08-21 14:53:56 +05:30
path_delim = '\\'
else:
path_delim = '/'
2023-08-21 14:53:56 +05:30
2023-11-19 19:01:44 +05:30
key_file_path = os.getcwd() + path_delim + "keys" + path_delim + f"hpos-{environment}-firebase-adminsdk.json"
2023-08-21 14:53:56 +05:30
cred = credentials.Certificate(key_file_path) # 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): ")
2023-09-04 07:43:03 +05:30
# 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)
2023-08-25 13:31:28 +05:30
# patient_collection = db.collection("patientData")
2023-09-04 07:43:03 +05:30
query = db.collection("testData").where(filter=FieldFilter("testTime", ">=", start_date)).where(filter=FieldFilter("testTime", "<", end_date))
2023-08-25 13:31:28 +05:30
# query = test_collection
# patient_docs = query.stream()
2023-08-21 14:53:56 +05:30
data = []
2023-08-25 13:31:28 +05:30
# for patient_doc in patient_docs:
# patient_data = patient_doc.to_dict()
# patient_id = patient_data["_id"]
# test_docs = test_collection.where("_id", "==", patient_id).stream()
2023-08-21 14:53:56 +05:30
2023-08-25 13:31:28 +05:30
# for test_doc in test_docs:
# test_data = test_doc.to_dict()
2023-08-21 14:53:56 +05:30
2023-08-25 13:31:28 +05:30
# combined_data = {**patient_data, **test_data}
2023-08-21 14:53:56 +05:30
2023-08-25 13:31:28 +05:30
# for key, value in combined_data.items():
# if value == "" and key in patient_data:
# combined_data[key] = patient_data[key]
2023-08-21 14:53:56 +05:30
2023-08-25 13:31:28 +05:30
# data.append(combined_data)
2023-08-21 14:53:56 +05:30
2023-09-04 07:43:03 +05:30
test_docs = query.stream()
2023-08-25 13:31:28 +05:30
for test_doc in test_docs:
test_data = test_doc.to_dict()
data.append(test_data)
2023-08-21 14:53:56 +05:30
# Convert the data to a DataFrame
df = pd.DataFrame(data)
2023-09-25 10:42:30 +05:30
print(df)
2023-08-21 14:53:56 +05:30
print(df.size)
2023-12-10 13:49:59 +05:30
2023-08-21 14:53:56 +05:30
# Save the DataFrame to a CSV file
2023-11-19 19:01:44 +05:30
output_filename = f'data_{environment}_{datetime.today().strftime("%d_%m_%Y_%H_%M")}.xlsx'
2023-08-21 14:53:56 +05:30
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)
writer = pd.ExcelWriter(output_path, engine = 'openpyxl')
df = df[(df['testTime'] > start_date) & (df['testTime'] <= end_date)]
df = df.sort_values(by=['testTime'], ascending=False)
2023-08-28 19:27:10 +05:30
2023-12-10 13:49:59 +05:30
df = df[["_id", "errorMessages", "classificationResult", "prdClassification", "predictedDenovixRatio", "calculatedRatio", "deviceRatio", "kitSerial", "abs1", "led1Average", "led1Buffer", "led1Sample", "abs2", "led2Average", "led2Buffer", "led2Sample", "abs3", "led3Average", "led3Buffer", "led3Sample", "abs4", "led4Average", "led4Buffer", "led4Sample", "batteryLevel", "batteryVoltage", "deviceId", "deviceSerialNumber", "name", "testTime"]]
2023-09-04 07:43:03 +05:30
# 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)
2023-11-19 19:01:44 +05:30
# df = df.reindex(sorted(df.columns), axis=1)
2023-12-10 13:49:59 +05:30
df = df.drop_duplicates()
df.to_excel(writer, sheet_name = 'data', index=False)
writer.close()
2023-08-21 14:53:56 +05:30
print(f"Data saved to '{output_path}'")
# Close the Firebase Admin SDK
2023-08-22 09:05:30 +05:30
firebase_admin.delete_app(firebase_admin.get_app())