84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
# Deploy with `firebase deploy`
|
|
|
|
from firebase_functions import https_fn
|
|
from firebase_admin import initialize_app, firestore
|
|
import pandas as pd
|
|
import datetime
|
|
import sys
|
|
import platform
|
|
import os
|
|
|
|
# initialize_app()
|
|
#
|
|
#
|
|
# @https_fn.on_request()
|
|
# def on_request_example(req: https_fn.Request) -> https_fn.Response:
|
|
# return https_fn.Response("Hello world!")
|
|
|
|
|
|
# @https_fn.on_request(
|
|
# cors=options.CorsOptions(
|
|
# cors_origins=[r"firebase\.com$", r"https://flutter\.com"],
|
|
# cors_methods=["get", "post"],
|
|
# )
|
|
# )
|
|
|
|
initialize_app()
|
|
|
|
@https_fn.on_request()
|
|
def consolidation(req: https_fn.Request) -> https_fn.Response:
|
|
|
|
path_delim = "/"
|
|
|
|
db = firestore.client()
|
|
|
|
patient_collection = db.collection("patientData")
|
|
test_collection = db.collection("testData")
|
|
|
|
start_date = req.query.start_date
|
|
print("start_date", start_date)
|
|
end_date = req.query.end_date
|
|
print("end_date", end_date)
|
|
|
|
|
|
query = test_collection # test_collection.where(filter=FieldFilter("testTime", ">=", start_date)).where(filter=FieldFilter("testTime", "<", end_date))
|
|
patient_docs = query.stream()
|
|
|
|
data = []
|
|
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()
|
|
|
|
for test_doc in test_docs:
|
|
test_data = test_doc.to_dict()
|
|
|
|
combined_data = {**patient_data, **test_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)
|
|
|
|
df = pd.DataFrame(data)
|
|
|
|
output_filename = "data.xlsx"
|
|
df.to_csv(output_filename, index=False)
|
|
|
|
output_path = os.getcwd() + path_delim + 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)
|
|
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()
|
|
|
|
firebase_admin.delete_app(firebase_admin.get_app())
|
|
|
|
|
|
# return https_fn.Response(response = send_file(output_path))
|
|
|
|
return https_fn.Response("Ok!")
|