Skip to content

Commit

Permalink
metrics: move and rename config vars
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba committed Sep 18, 2023
1 parent 51fafa0 commit 9678d1b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
8 changes: 6 additions & 2 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ from zenodo_rdm.legacy.resources import record_serializers
from zenodo_rdm.tokens import RATSubjectSchema
from zenodo_rdm import providers as zenodo_providers
from zenodo_rdm import facets as zenodo_community_facets
from zenodo_rdm.metrics.config import ZENODO_METRICS_CACHE_UPDATE_INTERVAL
from zenodo_rdm.metrics.config import METRICS_CACHE_UPDATE_INTERVAL

# Flask
# =====
Expand Down Expand Up @@ -119,7 +119,7 @@ CELERY_BEAT_SCHEDULE = {
"kwargs": {
"metric_id": "openaire-nexus",
},
"schedule": ZENODO_METRICS_CACHE_UPDATE_INTERVAL,
"schedule": METRICS_CACHE_UPDATE_INTERVAL,
},
}

Expand Down Expand Up @@ -515,3 +515,7 @@ ZENODO_RECORDS_UI_CITATIONS_ENABLE = True

OPENAIRE_PORTAL_URL = 'https://explore.openaire.eu'
"""URL to OpenAIRE portal."""

METRICS_UPTIME_ROBOT_METRIC_IDS = {}
METRICS_UPTIME_ROBOT_URL = "https://api.uptimerobot.com/v2/getMonitors"
METRICS_UPTIME_ROBOT_API_KEY = None
6 changes: 3 additions & 3 deletions site/zenodo_rdm/metrics/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def get_visitors():
@staticmethod
def get_uptime():
"""Get Zenodo uptime."""
metrics = current_app.config["ZENODO_METRICS_UPTIME_ROBOT_METRIC_IDS"]
url = current_app.config["ZENODO_METRICS_UPTIME_ROBOT_URL"]
api_key = current_app.config["ZENODO_METRICS_UPTIME_ROBOT_API_KEY"]
metrics = current_app.config["METRICS_UPTIME_ROBOT_METRIC_IDS"]
url = current_app.config["METRICS_UPTIME_ROBOT_URL"]
api_key = current_app.config["METRICS_UPTIME_ROBOT_API_KEY"]

end = datetime.utcnow().replace(
day=1, hour=0, minute=0, second=0, microsecond=0
Expand Down
12 changes: 4 additions & 8 deletions site/zenodo_rdm/metrics/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

from .api import ZenodoMetric

ZENODO_METRICS_START_DATE = datetime.datetime(2021, 1, 1)
ZENODO_METRICS_CACHE_TIMEOUT = int(datetime.timedelta(hours=1).total_seconds())
ZENODO_METRICS_CACHE_UPDATE_INTERVAL = datetime.timedelta(minutes=30)
METRICS_START_DATE = datetime.datetime(2021, 1, 1)
METRICS_CACHE_TIMEOUT = int(datetime.timedelta(hours=1).total_seconds())
METRICS_CACHE_UPDATE_INTERVAL = datetime.timedelta(minutes=30)

ZENODO_METRICS_UPTIME_ROBOT_METRIC_IDS = {}
ZENODO_METRICS_UPTIME_ROBOT_URL = "https://api.uptimerobot.com/v2/getMonitors"
ZENODO_METRICS_UPTIME_ROBOT_API_KEY = None

ZENODO_METRICS_DATA = {
METRICS_DATA = {
"openaire-nexus": [
{
"name": "zenodo_nexus_data_transfer_bytes_total",
Expand Down
4 changes: 2 additions & 2 deletions site/zenodo_rdm/metrics/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, app=None):
def init_config(app):
"""Initialize configuration."""
for k in dir(config):
if k.startswith("ZENODO_METRICS"):
if k.startswith("METRICS"):
app.config.setdefault(k, getattr(config, k))

def init_app(self, app):
Expand All @@ -35,4 +35,4 @@ def init_app(self, app):
@property
def metrics_start_date(self):
"""Get get metrics start date from config."""
return current_app.config["ZENODO_METRICS_START_DATE"]
return current_app.config["METRICS_START_DATE"]
8 changes: 4 additions & 4 deletions site/zenodo_rdm/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@


def get_metrics(metric_id):
cached_data = current_cache.get(f"ZENODO_METRICS_CACHE::{metric_id}")
cached_data = current_cache.get(f"METRICS_CACHE::{metric_id}")
if cached_data is not None:
return cached_data


def calculate_metrics(metric_id, cache=True):
"""Calculate a metric's result."""
result = deepcopy(current_app.config["ZENODO_METRICS_DATA"][metric_id])
result = deepcopy(current_app.config["METRICS_DATA"][metric_id])

for metric in result:
metric["value"] = metric["value"]()

if cache:
current_cache.set(
f"ZENODO_METRICS_CACHE::{metric_id}",
f"METRICS_CACHE::{metric_id}",
result,
timeout=current_app.config["ZENODO_METRICS_CACHE_TIMEOUT"],
timeout=current_app.config["METRICS_CACHE_TIMEOUT"],
)

return result
Expand Down
6 changes: 3 additions & 3 deletions site/zenodo_rdm/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

from . import tasks, utils

blueprint = Blueprint("zenodo_metrics", __name__)
blueprint = Blueprint("METRICS", __name__)


@blueprint.route("/metrics/<string:metric_id>")
def metrics(metric_id):
"""Metrics endpoint."""
if metric_id not in current_app.config["ZENODO_METRICS_DATA"]:
if metric_id not in current_app.config["METRICS_DATA"]:
return Response("Invalid key", status=404, mimetype="text/plain")

metrics = utils.get_metrics(metric_id)
Expand All @@ -28,7 +28,7 @@ def metrics(metric_id):

# Send off task to compute metrics
tasks.calculate_metrics.delay(metric_id)
retry_after = current_app.config["ZENODO_METRICS_CACHE_UPDATE_INTERVAL"]
retry_after = current_app.config["METRICS_CACHE_UPDATE_INTERVAL"]
return Response(
f"Metrics not available. Try again after {humanize.naturaldelta(retry_after)}.",
status=503,
Expand Down

0 comments on commit 9678d1b

Please sign in to comment.