add deviceratio and classificationResult plot
This commit is contained in:
82
cloud-functions/python/functions/crdboxplot/main.py
Normal file
82
cloud-functions/python/functions/crdboxplot/main.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import functions_framework
|
||||
from flask import Flask, jsonify, send_file
|
||||
from flask_cors import CORS
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.backends.backend_svg import FigureCanvasSVG
|
||||
import io
|
||||
import os
|
||||
from firebase_admin import initialize_app, firestore
|
||||
import datetime
|
||||
import pandas as pd
|
||||
from google.cloud.firestore import FieldFilter
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
initialize_app()
|
||||
|
||||
def create_svg_plot():
|
||||
db = firestore.client()
|
||||
|
||||
test_collection = db.collection("testData")
|
||||
|
||||
data = []
|
||||
|
||||
current_datetime = datetime.datetime.now()
|
||||
|
||||
start_datetime = current_datetime.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
start_date = start_datetime.strftime('%Y-%m-%d')
|
||||
|
||||
end_datetime = start_datetime + datetime.timedelta(days=1)
|
||||
end_date = end_datetime.strftime('%Y-%m-%d')
|
||||
|
||||
test_docs = test_collection.where(filter=FieldFilter('testTime', '>=', start_date)).where(filter=FieldFilter('testTime', '<', end_date)).stream()
|
||||
for test_doc in test_docs:
|
||||
test_data = test_doc.to_dict()
|
||||
data.append(test_data)
|
||||
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
box_plot = df.boxplot(column='deviceRatio', by='classificationResult', vert=True, ax=ax)
|
||||
|
||||
# plt.title('Box Plot of Device Ratio by Classification Result')
|
||||
plt.xlabel('Classification Result')
|
||||
plt.ylabel('Device Ratio')
|
||||
|
||||
svg_output = io.StringIO()
|
||||
canvas = FigureCanvasSVG(fig)
|
||||
canvas.print_svg(svg_output)
|
||||
|
||||
plt.close(fig)
|
||||
|
||||
return svg_output.getvalue()
|
||||
|
||||
@functions_framework.http
|
||||
def crdboxplot(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, POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
# 'Access-Control-Max-Age': '3600'
|
||||
}
|
||||
|
||||
return ('', 204, headers)
|
||||
|
||||
headers = {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
|
||||
svg_plot = create_svg_plot()
|
||||
|
||||
return send_file(io.BytesIO(svg_plot.encode()), as_attachment=False, download_name="plot.svg", mimetype='image/svg+xml')
|
||||
@@ -0,0 +1,7 @@
|
||||
functions-framework==3.*
|
||||
firebase_functions~=0.1.0
|
||||
pandas==2.0.3
|
||||
openpyxl==3.1.2
|
||||
firebase-admin==6.2.0
|
||||
matplotlib==3.8.0
|
||||
google-cloud-firestore==2.14.0
|
||||
@@ -48,8 +48,8 @@ if __name__ == "__main__":
|
||||
# Specify your Firestore collection name
|
||||
collection_name = 'testData'
|
||||
|
||||
num_documents = 10000
|
||||
batch_size = 500
|
||||
num_documents = 100
|
||||
batch_size = 5
|
||||
|
||||
# Call the function to delete the latest documents in batches
|
||||
delete_latest_documents(collection_name, num_documents, batch_size)
|
||||
|
||||
Reference in New Issue
Block a user