refactor for

- test date
	- sorting
	- excel save
This commit is contained in:
Pritimay Sarkar
2023-08-21 15:44:58 +05:30
parent df40847109
commit 3b61e4aef8

View File

@@ -11,10 +11,10 @@ import platform
if __name__ == "__main__": if __name__ == "__main__":
path_delim = "" path_delim = ""
if platform.system() == 'Darwin': if platform.system() == 'Windows':
path_delim = '/'
else:
path_delim = '\\' path_delim = '\\'
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-af3cc-firebase-adminsdk-n261k-2bfd463ec0.json"
cred = credentials.Certificate(key_file_path) # Replace with your own service account key path cred = credentials.Certificate(key_file_path) # Replace with your own service account key path
@@ -35,7 +35,7 @@ if __name__ == "__main__":
# Prepare data to store in CSV # Prepare data to store in CSV
data = [] data = []
for patient_doc in patient_docs: for patient_doc in patient_docs:
print(patient_doc) # print(patient_doc)
patient_data = patient_doc.to_dict() patient_data = patient_doc.to_dict()
patient_id = patient_data["_id"] patient_id = patient_data["_id"]
@@ -43,7 +43,7 @@ if __name__ == "__main__":
test_docs = test_collection.where("_id", "==", patient_id).stream() test_docs = test_collection.where("_id", "==", patient_id).stream()
for test_doc in test_docs: for test_doc in test_docs:
print(test_doc) # print(test_doc)
test_data = test_doc.to_dict() test_data = test_doc.to_dict()
# Combine the data from both collections into a single dictionary # Combine the data from both collections into a single dictionary
@@ -62,12 +62,17 @@ if __name__ == "__main__":
print(df.size) print(df.size)
# Save the DataFrame to a CSV file # Save the DataFrame to a CSV file
output_filename = "data.csv" output_filename = "data.xlsx"
df.to_csv(output_filename, index=False) df.to_csv(output_filename, index=False)
downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads") downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads")
output_path = os.path.join(downloads_dir, output_filename) output_path = os.path.join(downloads_dir, output_filename)
df.to_csv(output_path, index=False) 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)
df = df.drop(['resultData', "reportUploadTime", "userImageURL", "testType", "birthYear", "testStatus", "reportPath", "createdBy", "csvPath", "result", "mobileId", "resultRatio", "localFlag", "led2", "led1"], axis=1)
df.to_excel(writer, sheet_name = 'data', index=False)
writer.close()
print(f"Data saved to '{output_path}'") print(f"Data saved to '{output_path}'")