Skip to content

Commit

Permalink
/_/metrics/openapi.json
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Feb 29, 2024
1 parent ba3f8e1 commit 37eae3f
Show file tree
Hide file tree
Showing 5 changed files with 505 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/metrics/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls import re_path
from django.urls import re_path, path

from . import views

Expand Down Expand Up @@ -40,4 +40,9 @@
views.UniqueUserVisitsQuery.as_view(),
name=views.UniqueUserVisitsQuery.view_name,
),
path(
'openapi.json',
views.MetricsOpenapiView.as_view(),
name=views.MetricsOpenapiView.view_name,
),
]
26 changes: 25 additions & 1 deletion api/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
RegistriesModerationMetrics,
CountedAuthUsage,
)
from osf.metrics.openapi import get_metrics_openapi_json_dict
from osf.models import AbstractNode


Expand Down Expand Up @@ -561,7 +562,7 @@ class UserVisitsQuery(JSONAPIBaseView):
DAYS_PER_PERIOD = {'day': 1, 'month': 31, 'year': 365}

def get(self, request, *args):
report_date = {'gte': f'now/d-1d'}
report_date = {'gte': 'now/d-1d'}

if request.GET.get('timeframe', False):
timeframe = request.GET.get('timeframe')
Expand Down Expand Up @@ -633,3 +634,26 @@ def _build_query_payload(self, timespan):
payload = super()._build_query_payload(timespan)
payload['query']['bool']['filter'].insert(0, {'term': {'user_is_authenticated': True}})
return payload


class MetricsOpenapiView(GenericAPIView):
permission_classes = (
TokenHasScope,
drf_permissions.IsAuthenticatedOrReadOnly,
)

required_read_scopes = [CoreScopes.ALWAYS_PUBLIC]
required_write_scopes = [CoreScopes.NULL]

view_category = 'metrics'
view_name = 'openapi-json'

def get(self, request):
_openapi_json = get_metrics_openapi_json_dict(reports=VIEWABLE_REPORTS)
return JsonResponse(
_openapi_json,
json_dumps_params={'indent': 2},
headers={
'Cache-Control': f'immutable, public, max-age={60*60*24*7}', # pls cache for a week
},
)
8 changes: 7 additions & 1 deletion api_tests/base/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from api.base.settings.defaults import API_BASE
from api.search.permissions import IsAuthenticatedOrReadOnlyForSearch
from api.crossref.views import ParseCrossRefConfirmation
from api.metrics.views import RawMetricsView, RegistriesModerationMetricsView, CountedAuthUsageView
from api.metrics.views import (
RawMetricsView,
RegistriesModerationMetricsView,
CountedAuthUsageView,
MetricsOpenapiView,
)
from api.users.views import ClaimUser
from api.wb.views import MoveFileMetadataView, CopyFileMetadataView
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
Expand Down Expand Up @@ -58,6 +63,7 @@ def setUp(self):
ParseCrossRefConfirmation,
RawMetricsView,
RegistriesModerationMetricsView,
MetricsOpenapiView,
]

def test_root_returns_200(self):
Expand Down
Loading

0 comments on commit 37eae3f

Please sign in to comment.