Skip to content

Commit

Permalink
metrics: views: cache metrics evaluation status
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba committed Sep 22, 2023
1 parent 2b6ec1d commit 76ab46a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions site/zenodo_rdm/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import humanize
from flask import Blueprint, Response, current_app
from invenio_cache import current_cache

from zenodo_rdm.metrics import tasks, utils

Expand All @@ -26,8 +27,11 @@ def metrics(metric_id):
response = utils.formatted_response(metrics)
return Response(response, mimetype="text/plain")

# Send off task to compute metrics
tasks.calculate_metrics.delay(metric_id)
# Send off task to compute metrics only if it wasn't already requested
if not current_cache.get(f"METRICS_EVALUATING::{metric_id}"):
tasks.calculate_metrics.delay(metric_id)
current_cache.set(f"METRICS_EVALUATING::{metric_id}", True, timeout=60 * 2)

retry_after = current_app.config["METRICS_CACHE_UPDATE_INTERVAL"]
return Response(
f"Metrics not available. Try again after {humanize.naturaldelta(retry_after)}.",
Expand Down

0 comments on commit 76ab46a

Please sign in to comment.