From 9678d1b34678051d8aaee3c40e9bed4db2cf38f6 Mon Sep 17 00:00:00 2001 From: yashlamba Date: Wed, 13 Sep 2023 16:01:32 +0200 Subject: [PATCH] metrics: move and rename config vars --- invenio.cfg | 8 ++++++-- site/zenodo_rdm/metrics/api.py | 6 +++--- site/zenodo_rdm/metrics/config.py | 12 ++++-------- site/zenodo_rdm/metrics/ext.py | 4 ++-- site/zenodo_rdm/metrics/utils.py | 8 ++++---- site/zenodo_rdm/metrics/views.py | 6 +++--- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/invenio.cfg b/invenio.cfg index abae919b..d70c7556 100644 --- a/invenio.cfg +++ b/invenio.cfg @@ -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 # ===== @@ -119,7 +119,7 @@ CELERY_BEAT_SCHEDULE = { "kwargs": { "metric_id": "openaire-nexus", }, - "schedule": ZENODO_METRICS_CACHE_UPDATE_INTERVAL, + "schedule": METRICS_CACHE_UPDATE_INTERVAL, }, } @@ -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 diff --git a/site/zenodo_rdm/metrics/api.py b/site/zenodo_rdm/metrics/api.py index 0284fe13..7c205362 100644 --- a/site/zenodo_rdm/metrics/api.py +++ b/site/zenodo_rdm/metrics/api.py @@ -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 diff --git a/site/zenodo_rdm/metrics/config.py b/site/zenodo_rdm/metrics/config.py index d36f5afe..24cae482 100644 --- a/site/zenodo_rdm/metrics/config.py +++ b/site/zenodo_rdm/metrics/config.py @@ -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", diff --git a/site/zenodo_rdm/metrics/ext.py b/site/zenodo_rdm/metrics/ext.py index 9b1d37ac..1caacc9e 100644 --- a/site/zenodo_rdm/metrics/ext.py +++ b/site/zenodo_rdm/metrics/ext.py @@ -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): @@ -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"] diff --git a/site/zenodo_rdm/metrics/utils.py b/site/zenodo_rdm/metrics/utils.py index a7e81ab4..c5cf7692 100644 --- a/site/zenodo_rdm/metrics/utils.py +++ b/site/zenodo_rdm/metrics/utils.py @@ -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 diff --git a/site/zenodo_rdm/metrics/views.py b/site/zenodo_rdm/metrics/views.py index fc2c4492..cbb6555d 100644 --- a/site/zenodo_rdm/metrics/views.py +++ b/site/zenodo_rdm/metrics/views.py @@ -12,13 +12,13 @@ from . import tasks, utils -blueprint = Blueprint("zenodo_metrics", __name__) +blueprint = Blueprint("METRICS", __name__) @blueprint.route("/metrics/") 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) @@ -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,