From ae56900540815ee42e243d2256c6f72249308c2d Mon Sep 17 00:00:00 2001 From: Nicolas Ledez Date: Tue, 3 Oct 2023 17:02:31 +0200 Subject: [PATCH] Stats refactoring --- zou/app/__init__.py | 19 +++++++++++++------ zou/app/services/stats_service.py | 29 +++++++++++++++++++---------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/zou/app/__init__.py b/zou/app/__init__.py index a419258c40..9597bcc5a1 100644 --- a/zou/app/__init__.py +++ b/zou/app/__init__.py @@ -10,9 +10,6 @@ from flask_migrate import Migrate from flask_mail import Mail -from prometheus_flask_exporter import RESTfulPrometheusMetrics -from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics - from jwt import ExpiredSignatureError from babel.core import UnknownLocaleError from meilisearch.errors import ( @@ -21,7 +18,6 @@ ) from zou.app import config, swagger -from zou import __version__ as zou_version from zou.app.stores import auth_tokens_store from zou.app.services.exception import ( ModelWithRelationsDeletionException, @@ -166,12 +162,23 @@ def add_permissions(_, payload): def load_api(): from zou.app import api + from zou import __version__ as zou_version api.configure(app) try: - metrics = GunicornPrometheusMetrics(app, defaults_prefix="zou") + from prometheus_flask_exporter.multiprocess import ( + GunicornPrometheusMetrics, + ) + + metrics = GunicornPrometheusMetrics( + app, defaults_prefix="zou", group_by="url_rule" + ) except ValueError: - metrics = RESTfulPrometheusMetrics(app, api, defaults_prefix="zou") + from prometheus_flask_exporter import RESTfulPrometheusMetrics + + metrics = RESTfulPrometheusMetrics( + app, api, defaults_prefix="zou", group_by="url_rule" + ) metrics.info("zou_info", "Application info", version=zou_version) fs.mkdir_p(app.config["TMP_DIR"]) diff --git a/zou/app/services/stats_service.py b/zou/app/services/stats_service.py index 51a417d2c7..0107258fd1 100644 --- a/zou/app/services/stats_service.py +++ b/zou/app/services/stats_service.py @@ -39,17 +39,26 @@ def get_main_stats(): + metric_number_of_video_previews = metrics.gauge( + "zou_number_of_video_previews", "Number of video previews" + ) + number_of_video_previews = PreviewFile.query.filter( + PreviewFile.extension == "mp4" + ).count() + number_of_picture_previews = PreviewFile.query.filter( + PreviewFile.extension == "png" + ).count() + number_of_model_previews = PreviewFile.query.filter( + PreviewFile.extension == "obj" + ).count() + number_of_comments = Comment.query.count() + + metric_number_of_video_previews.set(number_of_video_previews) return { - "number_of_video_previews": PreviewFile.query.filter( - PreviewFile.extension == "mp4" - ).count(), - "number_of_picture_previews": PreviewFile.query.filter( - PreviewFile.extension == "png" - ).count(), - "number_of_model_previews": PreviewFile.query.filter( - PreviewFile.extension == "obj" - ).count(), - "number_of_comments": Comment.query.count(), + "number_of_video_previews": number_of_video_previews, + "number_of_picture_previews": number_of_picture_previews, + "number_of_model_previews": number_of_model_previews, + "number_of_comments": number_of_comments, }