From a1f91c31cb910295a43260894f6ceb532aa53c42 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Tue, 13 Feb 2024 20:08:12 +0530 Subject: [PATCH] add deviceratio and classificationResult plot --- .../python/functions/crdboxplot/main.py | 82 +++++++++++++++++++ .../functions/crdboxplot/requirements.txt | 7 ++ scripts/delete_junk_samples.py | 4 +- 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 cloud-functions/python/functions/crdboxplot/main.py create mode 100644 cloud-functions/python/functions/crdboxplot/requirements.txt diff --git a/cloud-functions/python/functions/crdboxplot/main.py b/cloud-functions/python/functions/crdboxplot/main.py new file mode 100644 index 0000000..96c0b18 --- /dev/null +++ b/cloud-functions/python/functions/crdboxplot/main.py @@ -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. + + Returns: + The response text, or any set of values that can be turned into a + Response object using `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') diff --git a/cloud-functions/python/functions/crdboxplot/requirements.txt b/cloud-functions/python/functions/crdboxplot/requirements.txt new file mode 100644 index 0000000..310878b --- /dev/null +++ b/cloud-functions/python/functions/crdboxplot/requirements.txt @@ -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 \ No newline at end of file diff --git a/scripts/delete_junk_samples.py b/scripts/delete_junk_samples.py index ab6d21f..7fbf541 100644 --- a/scripts/delete_junk_samples.py +++ b/scripts/delete_junk_samples.py @@ -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)