88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
|
|
import functions_framework
|
||
|
|
from firebase_functions import https_fn
|
||
|
|
# import firebase_admin
|
||
|
|
from firebase_admin import initialize_app, firestore
|
||
|
|
import pandas as pd
|
||
|
|
import datetime
|
||
|
|
import sys
|
||
|
|
import platform
|
||
|
|
import os
|
||
|
|
import io
|
||
|
|
from flask import send_file
|
||
|
|
|
||
|
|
initialize_app()
|
||
|
|
|
||
|
|
@functions_framework.http
|
||
|
|
def consolidation(request):
|
||
|
|
"""HTTP Cloud Function.
|
||
|
|
Args:
|
||
|
|
request (flask.Request): The request object.
|
||
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
|
||
|
|
Returns:
|
||
|
|
The response text, or any set of values that can be turned into a
|
||
|
|
Response object using `make_response`
|
||
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
|
||
|
|
"""
|
||
|
|
request_json = request.get_json(silent=True)
|
||
|
|
request_args = request.args
|
||
|
|
|
||
|
|
if request_json and 'start_date' in request_json and 'end_date' in request_json:
|
||
|
|
start_date = request_json['start_date']
|
||
|
|
end_date = request_json['end_date']
|
||
|
|
elif request_args and 'start_date' in request_args and 'end_date' in request_args:
|
||
|
|
start_date = request_args['start_date']
|
||
|
|
end_date = request_args['end_date']
|
||
|
|
else:
|
||
|
|
start_date = "2023-08-20"
|
||
|
|
end_date = "2023-08-21"
|
||
|
|
|
||
|
|
path_delim = "/"
|
||
|
|
|
||
|
|
db = firestore.client()
|
||
|
|
|
||
|
|
patient_collection = db.collection("patientData")
|
||
|
|
test_collection = db.collection("testData")
|
||
|
|
|
||
|
|
query = test_collection
|
||
|
|
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"
|
||
|
|
|
||
|
|
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.rename(columns={'deviceSerialNumber': "loginId", "calculatedRatio": "calibratedRatio"}, inplace = True)
|
||
|
|
df.to_excel(writer, sheet_name = 'data', index=False)
|
||
|
|
writer.close()
|
||
|
|
|
||
|
|
# firebase_admin.delete_app(firebase_admin.get_app())
|
||
|
|
|
||
|
|
# return 'df size {}!'.format(df.size)
|
||
|
|
|
||
|
|
with open(output_path,'rb') as f:
|
||
|
|
file_data = io.BytesIO(f.read())
|
||
|
|
|
||
|
|
# application/vnd.ms-excel
|
||
|
|
return send_file(file_data, download_name= "data.xlsx", mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|