diff --git a/swh/web/misc/__init__.py b/swh/web/misc/__init__.py deleted file mode 100644 diff --git a/swh/web/tests/test_urls.py b/swh/web/tests/test_urls.py --- a/swh/web/tests/test_urls.py +++ b/swh/web/tests/test_urls.py @@ -7,6 +7,9 @@ from django.urls import get_resolver +from swh.web.tests.helpers import check_http_get_response +from swh.web.utils import reverse + def test_swh_web_urls_have_trailing_slash(): urls = set( @@ -25,3 +28,8 @@ AssertionError, match=f"Django application {app_name} not found !" ): django_settings.SWH_DJANGO_APPS = django_settings.SWH_DJANGO_APPS + [app_name] + + +def test_stat_counters_view(client): + url = reverse("stat-counters") + check_http_get_response(client, url, status_code=200) diff --git a/swh/web/misc/urls.py b/swh/web/tests/urls.py rename from swh/web/misc/urls.py rename to swh/web/tests/urls.py --- a/swh/web/misc/urls.py +++ b/swh/web/tests/urls.py @@ -1,86 +1,47 @@ -# Copyright (C) 2019-2022 The Software Heritage developers +# Copyright (C) 2018-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import json - -import requests - -from django.http import JsonResponse from django.urls import re_path as url from swh.web.config import get_config -from swh.web.utils import archive -from swh.web.utils.exc import sentry_capture_exception - - -def _stat_counters(request): - stat_counters = archive.stat_counters() - url = get_config()["history_counters_url"] - stat_counters_history = {} - try: - response = requests.get(url, timeout=5) - stat_counters_history = json.loads(response.text) - except Exception as exc: - sentry_capture_exception(exc) - - counters = { - "stat_counters": stat_counters, - "stat_counters_history": stat_counters_history, - } - return JsonResponse(counters) - - -urlpatterns = [ - url(r"^stat_counters/$", _stat_counters, name="stat-counters"), -] - - +from swh.web.tests.views import ( + get_content_code_data_all_exts, + get_content_code_data_all_filenames, + get_content_code_data_by_ext, + get_content_code_data_by_filename, + get_content_other_data_by_ext, +) + +urlpatterns = [] # when running end to end tests through cypress, declare some extra # endpoints to provide input data for some of those tests if get_config()["e2e_tests_mode"]: - - from swh.web.tests.views import ( - get_content_code_data_all_exts, - get_content_code_data_all_filenames, - get_content_code_data_by_ext, - get_content_code_data_by_filename, - get_content_other_data_by_ext, - ) - - urlpatterns.append( + urlpatterns = [ url( r"^tests/data/content/code/extension/(?P.+)/$", get_content_code_data_by_ext, name="tests-content-code-extension", - ) - ) - urlpatterns.append( + ), url( r"^tests/data/content/other/extension/(?P.+)/$", get_content_other_data_by_ext, name="tests-content-other-extension", - ) - ) - urlpatterns.append( + ), url( r"^tests/data/content/code/extensions/$", get_content_code_data_all_exts, name="tests-content-code-extensions", - ) - ) - urlpatterns.append( + ), url( r"^tests/data/content/code/filename/(?P.+)/$", get_content_code_data_by_filename, name="tests-content-code-filename", - ) - ) - urlpatterns.append( + ), url( r"^tests/data/content/code/filenames/$", get_content_code_data_all_filenames, name="tests-content-code-filenames", - ) - ) + ), + ] diff --git a/swh/web/urls.py b/swh/web/urls.py --- a/swh/web/urls.py +++ b/swh/web/urls.py @@ -4,13 +4,16 @@ # See top-level LICENSE file for more information from importlib.util import find_spec +import json from typing import List, Union from django_js_reverse.views import urls_js +import requests from django.conf import settings from django.conf.urls import handler400, handler403, handler404, handler500, include from django.contrib.staticfiles.views import serve +from django.http import JsonResponse from django.shortcuts import render from django.urls import URLPattern, URLResolver from django.urls import re_path as url @@ -18,7 +21,7 @@ from swh.web.browse.identifiers import swhid_browse from swh.web.config import get_config -from swh.web.utils import origin_visit_types +from swh.web.utils import archive, origin_visit_types from swh.web.utils.exc import swh_handle400, swh_handle403, swh_handle404, swh_handle500 swh_web_config = get_config() @@ -32,6 +35,22 @@ return render(request, "homepage.html", {"visit_types": origin_visit_types()}) +def _stat_counters(request): + stat_counters = archive.stat_counters() + url = get_config()["history_counters_url"] + stat_counters_history = {} + + if url: + response = requests.get(url, timeout=5) + stat_counters_history = json.loads(response.text) + + counters = { + "stat_counters": stat_counters, + "stat_counters_history": stat_counters_history, + } + return JsonResponse(counters) + + urlpatterns: List[Union[URLPattern, URLResolver]] = [] # Register URLs for each SWH Django application @@ -59,7 +78,8 @@ swhid_browse, name="browse-swhid", ), - url(r"^", include("swh.web.misc.urls")), + url(r"^stat_counters/$", _stat_counters, name="stat-counters"), + url(r"^", include("swh.web.tests.urls")), ]