move to folder
This commit is contained in:
107
cloud-functions/python/functions/consolidation/consolidation.py
Normal file
107
cloud-functions/python/functions/consolidation/consolidation.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import functions_framework
|
||||
from firebase_functions import https_fn
|
||||
# import firebase_admin
|
||||
from firebase_admin import initialize_app, firestore
|
||||
from google.cloud.firestore import FieldFilter
|
||||
import pandas as pd
|
||||
import datetime
|
||||
import sys
|
||||
import platform
|
||||
import os
|
||||
import io
|
||||
from flask import send_file
|
||||
from datetime import datetime
|
||||
|
||||
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>.
|
||||
"""
|
||||
|
||||
if request.method == 'OPTIONS':
|
||||
headers = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
# 'Access-Control-Max-Age': '3600'
|
||||
}
|
||||
|
||||
return ('', 204, headers)
|
||||
|
||||
headers = {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
test_collection = db.collection("testData")
|
||||
|
||||
data = []
|
||||
|
||||
test_docs = test_collection.stream()
|
||||
for test_doc in test_docs:
|
||||
test_data = test_doc.to_dict()
|
||||
data.append(test_data)
|
||||
|
||||
test_df = pd.DataFrame(data)
|
||||
|
||||
query = db.collection("patientData").where(filter=FieldFilter("createdAt", ">=", start_date)).where(filter=FieldFilter("createdAt", "<", end_date))
|
||||
data = []
|
||||
docs = query.stream()
|
||||
for doc in docs:
|
||||
row_data = doc.to_dict()
|
||||
data.append(row_data)
|
||||
|
||||
user_df = pd.DataFrame(data)
|
||||
|
||||
df = pd.merge(test_df, user_df, on='_id')
|
||||
|
||||
output_filename = f'data_{datetime.today().strftime("%d_%m_%Y_%H_%M")}.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)
|
||||
|
||||
column_names = ["_id", "classificationResult", "deviceRatioClass", "prdClassification", "slopeRatioClass", "predictedDenovixRatio", "slopeRatio", "calculatedRatio", "deviceRatio", "kitSerial", "led1Gain1", "led1Gain2", "led1Gain4", "abs1", "led1Average", "led1Buffer", "led1Sample", "led2Gain1", "led2Gain2", "led2Gain4", "abs2", "led2Average", "led2Buffer", "led2Sample", "led3Gain1", "led3Gain2", "led3Gain4", "hb3", "abs3", "led3Average", "led3Buffer", "led3Sample", "led4Gain1", "led4Gain2", "led4Gain4", "hb4", "abs4", "led4Average", "led4Average", "led4Buffer", "led4Sample", "batteryLevel", "batteryVoltage", "solution", "concentration", "volume", "errorMessages", "deviceId", "deviceSerialNumber", "appVersion", "name_y", "testTime"]
|
||||
common_columns = [col for col in column_names if col in df.columns]
|
||||
df = df[common_columns]
|
||||
|
||||
df.rename(columns={'deviceSerialNumber': "loginId", "name_y": "name"}, inplace = True)
|
||||
# df = df.reindex(sorted(df.columns), axis=1)
|
||||
|
||||
df = df.drop_duplicates()
|
||||
|
||||
df.to_excel(writer, sheet_name = 'data', index=False)
|
||||
writer.close()
|
||||
|
||||
# firebase_admin.delete_app(firebase_admin.get_app())
|
||||
|
||||
with open(output_path,'rb') as f:
|
||||
file_data = io.BytesIO(f.read())
|
||||
|
||||
# application/vnd.ms-excel
|
||||
return send_file(file_data, download_name= output_filename, mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
Reference in New Issue
Block a user