forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
678 lines (571 loc) · 22.7 KB
/
views.py
File metadata and controls
678 lines (571 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import re
import json
import logging
from enum import Enum
from django.http import JsonResponse, HttpResponse, Http404
from django.utils import timezone
from elasticsearch6.exceptions import NotFoundError, RequestError
from elasticsearch6_dsl.connections import get_connection
from elasticsearch_metrics.registry import djelme_registry
from framework.auth.oauth_scopes import CoreScopes
from rest_framework.exceptions import ValidationError
from rest_framework import permissions as drf_permissions
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from rest_framework.settings import api_settings as drf_api_settings
from api.base.views import JSONAPIBaseView
from api.base.permissions import TokenHasScope
from api.base.waffle_decorators import require_switch
from api.metrics.permissions import (
IsPreprintMetricsUser,
IsRawMetricsUser,
IsRegistriesModerationMetricsUser,
)
from api.metrics.renderers import (
MetricsReportsCsvRenderer,
MetricsReportsTsvRenderer,
)
from api.metrics.serializers import (
PreprintMetricSerializer,
RawMetricsSerializer,
DailyReportSerializer,
MonthlyReportSerializer,
ReportNameSerializer,
NodeAnalyticsSerializer,
UserVisitsSerializer,
UniqueUserVisitsSerializer,
CountedAuthUsageSerializer,
)
from api.metrics.utils import (
parse_datetimes,
parse_date_range,
)
from api.nodes.permissions import MustBePublic
from osf.features import ENABLE_RAW_METRICS
from osf.metrics import (
utils,
reports,
PreprintDownload,
PreprintView,
RegistriesModerationMetrics,
CountedAuthUsage,
)
from osf.metrics.openapi import get_metrics_openapi_json_dict
from osf.models import AbstractNode
logger = logging.getLogger(__name__)
class PreprintMetricMixin(JSONAPIBaseView):
permission_classes = (
drf_permissions.IsAuthenticated,
drf_permissions.IsAdminUser,
IsPreprintMetricsUser,
TokenHasScope,
)
required_read_scopes = [CoreScopes.METRICS_BASIC]
required_write_scopes = [CoreScopes.METRICS_RESTRICTED]
serializer_class = PreprintMetricSerializer
@property
def metric_type(self):
raise NotImplementedError
@property
def metric(self):
raise NotImplementedError
def add_search(self, search, query_params, **kwargs):
"""
get list of guids from the kwargs
use that in a query to narrow down metrics results
"""
preprint_guid_string = query_params.get('guids')
if not preprint_guid_string:
raise ValidationError(
'To gather metrics for preprints, you must provide one or more preprint ' +
'guids in the `guids` query parameter.',
)
preprint_guids = preprint_guid_string.split(',')
return search.filter('terms', preprint_id=preprint_guids)
def format_response(self, response, query_params):
data = []
if getattr(response, 'aggregations') and response.aggregations:
for result in response.aggregations.dates.buckets:
guid_results = {}
for preprint_result in result.preprints.buckets:
guid_results[preprint_result['key']] = preprint_result['total']['value']
# return 0 for the guids with no results for consistent payloads
guids = query_params['guids'].split(',')
if guid_results.keys() != guids:
for guid in guids:
if not guid_results.get(guid):
guid_results[guid] = 0
result_dict = {result.key_as_string: guid_results}
data.append(result_dict)
return {
'metric_type': self.metric_type,
'data': data,
}
def execute_search(self, search, query=None):
try:
# There's a bug in the ES python library the prevents us from updating the search object, so lets just make
# the raw query. If we have it.
if query:
es = get_connection(search._using)
response = search._response_class(
search,
es.search(
index=search._index,
body=query,
),
)
else:
response = search.execute()
except NotFoundError:
# _get_relevant_indices returned 1 or more indices
# that doesn't exist. Fall back to unoptimized query
search = search.index().index(self.metric._default_index())
response = search.execute()
return response
def get(self, *args, **kwargs):
query_params = getattr(self.request, 'query_params', self.request.GET)
interval = query_params.get('interval', 'day')
start_datetime, end_datetime = parse_datetimes(query_params)
search = self.metric.search(after=start_datetime)
search = search.filter('range', timestamp={'gte': start_datetime, 'lt': end_datetime})
search.aggs.bucket('dates', 'date_histogram', field='timestamp', interval=interval) \
.bucket('preprints', 'terms', field='preprint_id') \
.metric('total', 'sum', field='count')
search = self.add_search(search, query_params, **kwargs)
response = self.execute_search(search)
resp_dict = self.format_response(response, query_params)
return JsonResponse(resp_dict)
def post(self, request, *args, **kwargs):
"""
For a bit of future proofing, accept custom elasticsearch aggregation queries in JSON form.
Caution - this could be slow if a very large query is executed, so use with care!
"""
search = self.metric.search()
query = request.data.get('query')
try:
results = self.execute_search(search, query)
except RequestError as e:
if e.args:
raise ValidationError(e.info['error']['root_cause'][0]['reason'])
raise ValidationError('Malformed elasticsearch query.')
return JsonResponse(results.to_dict())
class PreprintViewMetrics(PreprintMetricMixin):
view_category = 'preprint-metrics'
view_name = 'preprint-view-metrics'
@property
def metric_type(self):
return 'views'
@property
def metric(self):
return PreprintView
class PreprintDownloadMetrics(PreprintMetricMixin):
view_category = 'preprint-metrics'
view_name = 'preprint-download-metrics'
@property
def metric_type(self):
return 'downloads'
@property
def metric(self):
return PreprintDownload
class RawMetricsView(GenericAPIView):
permission_classes = (
drf_permissions.IsAuthenticated,
IsRawMetricsUser,
TokenHasScope,
)
required_read_scopes = [CoreScopes.METRICS_BASIC]
required_write_scopes = [CoreScopes.METRICS_RESTRICTED]
view_category = 'raw-metrics'
view_name = 'raw-metrics-view'
serializer_class = RawMetricsSerializer
@require_switch(ENABLE_RAW_METRICS)
def delete(self, request, *args, **kwargs):
raise ValidationError('DELETE not supported. Use GET/POST/PUT')
@require_switch(ENABLE_RAW_METRICS)
def get(self, request, *args, djelme_backend_name, url_path, **kwargs):
connection = self._get_es_connection(djelme_backend_name)
_response = connection.transport.perform_request('GET', f'/{url_path}')
return JsonResponse(_response if isinstance(_response, dict) else _response.body)
@require_switch(ENABLE_RAW_METRICS)
def post(self, request, *args, djelme_backend_name, url_path, **kwargs):
connection = self._get_es_connection(djelme_backend_name)
body = json.loads(request.body)
_response = connection.transport.perform_request('POST', f'/{url_path}', body=body)
return JsonResponse(_response if isinstance(_response, dict) else _response.body)
@require_switch(ENABLE_RAW_METRICS)
def put(self, request, *args, djelme_backend_name, url_path, **kwargs):
connection = self._get_es_connection(djelme_backend_name)
body = json.loads(request.body)
_response = connection.transport.perform_request('PUT', f'/{url_path}', body=body)
return JsonResponse(_response if isinstance(_response, dict) else _response.body)
def _get_es_connection(self, djelme_backend_name):
try:
_backend = djelme_registry.get_backend(djelme_backend_name)
except LookupError:
raise Http404
return _backend.elastic_client
class RegistriesModerationMetricsView(GenericAPIView):
permission_classes = (
drf_permissions.IsAuthenticated,
IsRegistriesModerationMetricsUser,
TokenHasScope,
)
required_read_scopes = [CoreScopes.METRICS_BASIC]
required_write_scopes = [CoreScopes.METRICS_RESTRICTED]
view_category = 'raw-metrics'
view_name = 'raw-metrics-view'
def get(self, request, *args, **kwargs):
return JsonResponse(RegistriesModerationMetrics.get_registries_info())
VIEWABLE_REPORTS = {
'download_count': reports.DownloadCountReport,
'institution_summary': reports.InstitutionSummaryReport,
'node_summary': reports.NodeSummaryReport,
'osfstorage_file_count': reports.OsfstorageFileCountReport,
'preprint_summary': reports.PreprintSummaryReport,
'storage_addon_usage': reports.StorageAddonUsage,
'user_summary': reports.UserSummaryReport,
'spam_summary': reports.SpamSummaryReport,
'new_user_domains': reports.NewUserDomainReport,
}
class ReportNameList(JSONAPIBaseView):
permission_classes = (
TokenHasScope,
drf_permissions.IsAuthenticatedOrReadOnly,
)
required_read_scopes = [CoreScopes.ALWAYS_PUBLIC]
required_write_scopes = [CoreScopes.NULL]
view_category = 'metrics'
view_name = 'report-name-list'
serializer_class = ReportNameSerializer
def get(self, request, *args, **kwargs):
serializer = self.serializer_class(
VIEWABLE_REPORTS.keys(),
many=True,
)
return Response({'data': serializer.data})
class RecentReportList(JSONAPIBaseView):
MAX_COUNT = 10000
DEFAULT_DAYS_BACK = 13
permission_classes = (
TokenHasScope,
drf_permissions.IsAuthenticatedOrReadOnly,
)
required_read_scopes = [CoreScopes.ALWAYS_PUBLIC]
required_write_scopes = [CoreScopes.NULL]
view_category = 'metrics'
view_name = 'recent-report-list'
serializer_class = DailyReportSerializer
renderer_classes = (
*drf_api_settings.DEFAULT_RENDERER_CLASSES,
MetricsReportsCsvRenderer,
MetricsReportsTsvRenderer,
)
def get(self, request, *args, report_name):
try:
report_class = VIEWABLE_REPORTS[report_name]
except KeyError:
return Response(
{
'errors': [{
'title': 'unknown report name',
'detail': f'unknown report: "{report_name}"',
}],
},
status=404,
)
is_daily = issubclass(report_class, reports.DailyReport)
days_back = request.GET.get('days_back', self.DEFAULT_DAYS_BACK if is_daily else None)
is_monthly = issubclass(report_class, reports.MonthlyReport)
if is_daily:
serializer_class = DailyReportSerializer
range_field_name = 'report_date'
elif is_monthly:
serializer_class = MonthlyReportSerializer
range_field_name = 'report_yearmonth'
else:
raise ValueError(f'report class must subclass DailyReport or MonthlyReport: {report_class}')
range_filter = parse_date_range(request.GET, is_monthly=is_monthly)
search_recent = (
report_class.search()
.filter('range', **{range_field_name: range_filter})
.sort(range_field_name)
[:self.MAX_COUNT]
)
if days_back:
search_recent.filter('range', report_date={'gte': f'now/d-{days_back}d'})
report_date_range = parse_date_range(request.GET)
search_response = search_recent.execute()
serializer = serializer_class(
search_response,
many=True,
context={'report_name': report_name},
)
accepted_format = request.accepted_renderer.format
response_headers = {}
if accepted_format in ('tsv', 'csv'):
from_date = report_date_range['gte']
until_date = report_date_range['lte']
filename = (
f'{report_name}__'
f'until_{until_date}__'
f'from_{from_date}.{accepted_format}'
)
response_headers['Content-Disposition'] = f'attachment; filename={filename}'
return Response(
{'data': serializer.data},
headers=response_headers,
)
class CountedAuthUsageView(JSONAPIBaseView):
view_category = 'metrics'
view_name = 'counted-usage'
serializer_class = CountedAuthUsageSerializer
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(
data=request.data,
context={
'user_id': request.user._id if request.user.is_authenticated else None,
'request_host': request.get_host(),
'request_useragent': request.META.get('HTTP_USER_AGENT', ''),
},
)
serializer.is_valid(raise_exception=True)
session_id, user_is_authenticated = self._get_session_id(
request,
client_session_id=serializer.validated_data.get('client_session_id'),
)
serializer.save(session_id=session_id, user_is_authenticated=user_is_authenticated)
return HttpResponse(status=201)
def _get_session_id(self, request, client_session_id=None):
# NOTE: to remove after osfmetrics 6to8 migration -- logic moved to djelme
# get a session id as described in the COUNTER code of practice:
# https://cop5.projectcounter.org/en/5.0.2/07-processing/03-counting-unique-items.html
# -- different from the "login session" tracked by `osf.models.Session` (which
# lasts about a month), this session lasts at most a day and may time out after
# minutes or hours of inactivity
now = timezone.now()
current_date_str = now.date().isoformat()
user_is_authenticated = request.user.is_authenticated
if client_session_id:
session_id_parts = [
client_session_id,
current_date_str,
]
elif user_is_authenticated:
session_id_parts = [
request.user._id,
current_date_str,
now.hour,
]
else:
session_id_parts = [
request.get_host(),
request.META.get('HTTP_USER_AGENT', ''),
current_date_str,
now.hour,
]
user_is_authenticated = False
return utils.stable_key(*session_id_parts), user_is_authenticated
class NodeAnalyticsQuery(JSONAPIBaseView):
permission_classes = (
MustBePublic,
TokenHasScope,
drf_permissions.IsAuthenticatedOrReadOnly,
)
required_read_scopes = [CoreScopes.ALWAYS_PUBLIC]
required_write_scopes = [CoreScopes.NULL]
view_category = 'metrics'
view_name = 'node-analytics-query'
serializer_class = NodeAnalyticsSerializer
class Timespan(Enum):
WEEK = 'week'
FORTNIGHT = 'fortnight'
MONTH = 'month'
def get(self, request, *args, node_guid, timespan):
try:
node = AbstractNode.load(node_guid)
except AbstractNode.DoesNotExist:
raise Http404
self.check_object_permissions(request, node)
analytics_result = self._run_query(node_guid, timespan)
serializer = self.serializer_class(
analytics_result,
context={
'node_guid': node_guid,
'timespan': timespan,
},
)
return Response({'data': serializer.data})
def _run_query(self, node_guid, timespan):
query_dict = self._build_query_payload(node_guid, NodeAnalyticsQuery.Timespan(timespan))
analytics_search = CountedAuthUsage.search().update_from_dict(query_dict)
return analytics_search.execute()
def _build_query_payload(self, node_guid, timespan):
return {
'size': 0, # don't return hits, just the aggregations
'query': {
'bool': {
'minimum_should_match': 1,
'should': [
{'term': {'item_guid': node_guid}},
{'term': {'surrounding_guids': node_guid}},
],
'filter': [
{'term': {'item_public': True}},
{'term': {'action_labels': 'view'}},
{'term': {'action_labels': 'web'}},
self._build_timespan_filter(timespan),
],
},
},
'aggs': {
'unique-visits': {
'date_histogram': {
'field': 'timestamp',
'interval': 'day',
},
},
'time-of-day': {
'terms': {
'field': 'pageview_info.hour_of_day',
'size': 24,
},
},
'referer-domain': {
'terms': {
'field': 'pageview_info.referer_domain',
'size': 10,
},
},
'popular-pages': {
'terms': {
'field': 'pageview_info.page_path',
'size': 10,
},
'aggs': {
'route-for-path': {
'terms': {
'field': 'pageview_info.route_name',
'size': 1,
},
},
'title-for-path': {
'terms': {
'field': 'pageview_info.page_title',
'size': 1,
},
},
},
},
},
}
def _build_timespan_filter(self, timespan):
if timespan == NodeAnalyticsQuery.Timespan.WEEK:
from_date = 'now-1w/d'
elif timespan == NodeAnalyticsQuery.Timespan.FORTNIGHT:
from_date = 'now-2w/d'
elif timespan == NodeAnalyticsQuery.Timespan.MONTH:
from_date = 'now-1M/d'
else:
raise NotImplementedError
return {
'range': {
'timestamp': {
'gte': from_date,
},
},
}
class UserVisitsQuery(JSONAPIBaseView):
permission_classes = (
MustBePublic,
TokenHasScope,
drf_permissions.IsAuthenticatedOrReadOnly,
)
required_read_scopes = [CoreScopes.ALWAYS_PUBLIC]
required_write_scopes = [CoreScopes.NULL]
view_category = 'metrics'
view_name = 'user-visits-query'
serializer_class = UserVisitsSerializer
DAYS_PER_PERIOD = {'day': 1, 'month': 31, 'year': 365}
def get(self, request, *args):
report_date = {'gte': 'now/d-1d'}
if request.GET.get('timeframe', False):
timeframe = request.GET.get('timeframe')
if timeframe is not None:
m = re.match(r'previous_(\d+)_(day|month|year)s?', timeframe)
if m:
period_count = m.group(1)
period = m.group(2)
days_back = int(period_count) * self.DAYS_PER_PERIOD[period]
else:
raise Exception(f'Unsupported timeframe format: "{timeframe}"')
report_date = {'gte': f'now/d-{days_back}d'}
elif request.GET.get('timeframeStart'):
tsStart = request.GET.get('timeframeStart')
tsEnd = request.GET.get('timeframeEnd')
report_date = {'gte': tsStart, 'lt': tsEnd}
else:
pass # just fall back to days_back for now
timespan = report_date
analytics_result = self._run_query(timespan)
serializer = self.serializer_class(
analytics_result,
context={
'timespan': timespan,
},
)
return JsonResponse({'data': serializer.data})
def _run_query(self, timespan):
query_dict = self._build_query_payload(timespan)
analytics_search = CountedAuthUsage.search().update_from_dict(query_dict)
return analytics_search.execute()
def _build_query_payload(self, timespan):
return {
'size': 0, # don't return hits, just the aggregations
'query': {
'bool': {
'filter': [
{'range': {'timestamp': timespan}},
],
},
},
'aggs': {
'unique-visits': {
'date_histogram': {
'field': 'timestamp',
'interval': 'day',
},
'aggs': {
'user-visits': {
'cardinality': {
'field': 'session_id',
},
},
},
},
},
}
class UniqueUserVisitsQuery(UserVisitsQuery):
view_name = 'unique-user-visits-query'
serializer_class = UniqueUserVisitsSerializer
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
},
)