Skip to content

Commit

Permalink
Stats refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Ledez committed Oct 3, 2023
1 parent f1632a5 commit ae56900
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
19 changes: 13 additions & 6 deletions zou/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down
29 changes: 19 additions & 10 deletions zou/app/services/stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down

0 comments on commit ae56900

Please sign in to comment.