Changeset View
Changeset View
Standalone View
Standalone View
swh/web/misc/urls.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import requests | import requests | ||||
import sentry_sdk | import sentry_sdk | ||||
from django.conf.urls import url, include | from django.conf.urls import url, include | ||||
from django.contrib.staticfiles import finders | from django.contrib.staticfiles import finders | ||||
from django.http import HttpResponse | from django.http import JsonResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
from swh.web.misc.metrics import prometheus_metrics | from swh.web.misc.metrics import prometheus_metrics | ||||
def _jslicenses(request): | def _jslicenses(request): | ||||
jslicenses_file = finders.find("jssources/jslicenses.json") | jslicenses_file = finders.find("jssources/jslicenses.json") | ||||
jslicenses_data = json.load(open(jslicenses_file)) | jslicenses_data = json.load(open(jslicenses_file)) | ||||
jslicenses_data = sorted( | jslicenses_data = sorted( | ||||
jslicenses_data.items(), key=lambda item: item[0].split("/")[-1] | jslicenses_data.items(), key=lambda item: item[0].split("/")[-1] | ||||
) | ) | ||||
return render(request, "misc/jslicenses.html", {"jslicenses_data": jslicenses_data}) | return render(request, "misc/jslicenses.html", {"jslicenses_data": jslicenses_data}) | ||||
def _stat_counters(request): | def _stat_counters(request): | ||||
stat = service.stat_counters() | stat_counters = service.stat_counters() | ||||
url = get_config()["history_counters_url"] | url = get_config()["history_counters_url"] | ||||
stat_counters_history = "null" | stat_counters_history = "null" | ||||
if url: | if url: | ||||
try: | try: | ||||
response = requests.get(url, timeout=5) | response = requests.get(url, timeout=5) | ||||
stat_counters_history = response.text | stat_counters_history = response.text | ||||
except Exception as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
json_data = '{"stat_counters": %s, "stat_counters_history": %s}' % ( | counters = { | ||||
json.dumps(stat), | "stat_counters": stat_counters, | ||||
stat_counters_history, | "stat_counters_history": stat_counters_history, | ||||
) | } | ||||
return HttpResponse(json_data, content_type="application/json") | return JsonResponse(counters) | ||||
urlpatterns = [ | urlpatterns = [ | ||||
url(r"^", include("swh.web.misc.coverage")), | url(r"^", include("swh.web.misc.coverage")), | ||||
url(r"^jslicenses/$", _jslicenses, name="jslicenses"), | url(r"^jslicenses/$", _jslicenses, name="jslicenses"), | ||||
url(r"^", include("swh.web.misc.origin_save")), | url(r"^", include("swh.web.misc.origin_save")), | ||||
url(r"^stat_counters/", _stat_counters, name="stat-counters"), | url(r"^stat_counters/", _stat_counters, name="stat-counters"), | ||||
url(r"^", include("swh.web.misc.badges")), | url(r"^", include("swh.web.misc.badges")), | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |