Skip to content

Commit 544363c

Browse files
committed
fix: reporter tests
1 parent fe3885c commit 544363c

6 files changed

Lines changed: 99 additions & 43 deletions

File tree

osf/metrics/es8_metrics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ class MonthlyPublicItemUsageReportEs8(djelme.CyclicRecord):
508508
# https://cop5.projectcounter.org/en/5.1/appendices/a-glossary-of-terms.html
509509
# https://coprd.countermetrics.org/en/1.0.1/appendices/a-glossary.html
510510
item_osfid: str
511+
# fields built from aggregations -- more than one value unlikely, but possible
511512
item_type: list[str] # counter:Data-Type
512513
provider_id: list[str] # counter:Database(?)
513514
platform_iri: list[str] # counter:Platform

osf/metrics/reporters/public_item_usage.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,37 @@ def report(self, **report_kwargs):
6464
if _guid is None or _guid.referent is None:
6565
raise _SkipItem
6666
_obj = _guid.referent
67-
_reports = self._init_report(_obj)
68-
for _report in _reports:
69-
self._fill_report_counts(_report, _obj)
70-
if not any((
71-
_report.view_count,
72-
_report.view_session_count,
73-
_report.download_count,
74-
_report.download_session_count,
75-
)):
76-
raise _SkipItem
77-
return _reports
67+
_report = self._init_report(_obj)
68+
self._fill_report_counts(_report, _obj)
69+
if not any((
70+
_report.view_count,
71+
_report.view_session_count,
72+
_report.download_count,
73+
_report.download_session_count,
74+
)):
75+
raise _SkipItem
76+
_report_es6 = PublicItemUsageReport(
77+
item_osfid=_report.item_osfid,
78+
item_type=list(_report.item_type),
79+
provider_id=list(_report.provider_id),
80+
platform_iri=list(_report.platform_iri),
81+
view_count=_report.view_count,
82+
view_session_count=_report.view_session_count,
83+
download_count=_report.download_count,
84+
download_session_count=_report.download_session_count,
85+
)
86+
return [_report, _report_es6]
7887
except _SkipItem:
79-
return None
88+
return []
8089

8190
def followup_task(self, report):
82-
_is_last_month = report.report_yearmonth.next() == YearMonth.from_date(datetime.date.today())
91+
_last_month = YearMonth.from_date(datetime.date.today()).prior()
92+
if isinstance(report, MonthlyPublicItemUsageReportEs8):
93+
_is_last_month = (report.cycle_coverage == cycle_coverage_yearmonth(_last_month))
94+
elif isinstance(report, PublicItemUsageReport):
95+
_is_last_month = (report.report_yearmonth == _last_month)
96+
else:
97+
raise ValueError(report)
8398
if _is_last_month:
8499
from api.share.utils import task__update_share
85100
return task__update_share.signature(
@@ -135,27 +150,17 @@ def _preprintdownload_osfids(self, after_osfid: str | None) -> typing.Iterator[s
135150
)
136151
return _iter_composite_bucket_keys(_search, 'agg_osfid', 'osfid', after=after_osfid)
137152

138-
def _init_report(self, osf_obj) -> typing.List[PublicItemUsageReport | MonthlyPublicItemUsageReportEs8]:
153+
def _init_report(self, osf_obj) -> MonthlyPublicItemUsageReportEs8:
139154
if not _is_item_public(osf_obj):
140155
raise _SkipItem
141-
reports = []
142-
report_es8 = MonthlyPublicItemUsageReportEs8(
156+
return MonthlyPublicItemUsageReportEs8(
143157
cycle_coverage=cycle_coverage_yearmonth(self.yearmonth),
144158
item_osfid=osf_obj._id,
145159
item_type=[get_item_type(osf_obj)],
146160
provider_id=[get_provider_id(osf_obj)],
147161
platform_iri=[website_settings.DOMAIN],
148-
)
149-
reports.append(report_es8)
150-
report = PublicItemUsageReport(
151-
item_osfid=report_es8.item_osfid,
152-
item_type=report_es8.item_type,
153-
provider_id=report_es8.provider_id,
154-
platform_iri=report_es8.platform_iri,
155162
# leave counts null; will be set if there's data
156163
)
157-
reports.append(report)
158-
return reports
159164

160165
def _fill_report_counts(self, report, osf_obj):
161166
if (
Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
import datetime
2+
13
from django.core.management import call_command
24
from django.test import TestCase
35
from elasticsearch_metrics.tests.util import djelme_test_backends
46

57
from framework.celery_tasks import app as celery_app
8+
from osf.metrics import reports as es6_reports
9+
from osf.metrics.es8_metrics import (
10+
MonthlyInstitutionSummaryReportEs8,
11+
MonthlyInstitutionalUserReportEs8,
12+
MonthlyPrivateSpamMetricsReportEs8,
13+
MonthlyPublicItemUsageReportEs8,
14+
MonthlySpamSummaryReportEs8,
15+
)
16+
from osf.metrics.counted_usage import CountedAuthUsage
17+
from osf.metrics.utils import YearMonth
618
from osf_tests import factories
19+
from website import settings as website_settings
720

821

922
class TestMonthlyReportersGo(TestCase):
@@ -13,12 +26,55 @@ def setUp(self):
1326
'task_always_eager': True,
1427
'task_eager_propagates': True,
1528
})
16-
# set up data, so each reporter outputs something
29+
self._report_yearmonth = YearMonth.from_date(datetime.date.today())
30+
# set up for institutional-user report
1731
_inst = factories.InstitutionFactory()
1832
_user = factories.UserFactory()
1933
_user.add_or_update_affiliated_institution(_inst)
20-
factories.PreprintFactory()
34+
# set up for public item usage report
35+
_reg = factories.RegistrationFactory(is_public=True)
36+
CountedAuthUsage.record(
37+
platform_iri=website_settings.DOMAIN,
38+
item_guid=_reg._id,
39+
session_id='blarg',
40+
user_is_authenticated=True,
41+
action_labels=['view', 'web'],
42+
)
43+
CountedAuthUsage._get_connection().indices.refresh(CountedAuthUsage._template_pattern)
44+
# TODO when switching to use es8 data
45+
# OsfCountedUsageEvent.record(
46+
# item_osfid=_preprint._id,
47+
# action_labels=['view', 'web'],
48+
# user_id=_user._id,
49+
# )
50+
# OsfCountedUsageEvent.refresh()
2151

2252
def test_for_smoke(self):
23-
call_command('monthly_reporters_go')
24-
# TODO: assert more specifically
53+
self._assert_count(MonthlyInstitutionSummaryReportEs8, 0)
54+
self._assert_count(MonthlyInstitutionalUserReportEs8, 0)
55+
self._assert_count(MonthlyPrivateSpamMetricsReportEs8, 0)
56+
self._assert_count(MonthlyPublicItemUsageReportEs8, 0)
57+
self._assert_count(MonthlySpamSummaryReportEs8, 0)
58+
self._assert_count(es6_reports.SpamSummaryReport, 0)
59+
self._assert_count(es6_reports.InstitutionalUserReport, 0)
60+
self._assert_count(es6_reports.InstitutionMonthlySummaryReport, 0)
61+
self._assert_count(es6_reports.PublicItemUsageReport, 0)
62+
self._assert_count(es6_reports.PrivateSpamMetricsReport, 0)
63+
call_command('monthly_reporters_go', yearmonth=str(self._report_yearmonth))
64+
self._assert_count(MonthlyInstitutionSummaryReportEs8, 1)
65+
self._assert_count(MonthlyInstitutionalUserReportEs8, 1)
66+
self._assert_count(MonthlyPrivateSpamMetricsReportEs8, 1)
67+
self._assert_count(MonthlyPublicItemUsageReportEs8, 1)
68+
self._assert_count(MonthlySpamSummaryReportEs8, 1)
69+
self._assert_count(es6_reports.SpamSummaryReport, 1)
70+
self._assert_count(es6_reports.InstitutionalUserReport, 1)
71+
self._assert_count(es6_reports.InstitutionMonthlySummaryReport, 1)
72+
self._assert_count(es6_reports.PublicItemUsageReport, 1)
73+
self._assert_count(es6_reports.PrivateSpamMetricsReport, 1)
74+
75+
def _assert_count(self, recordtype, expected_count):
76+
if hasattr(recordtype, 'refresh'):
77+
recordtype.refresh()
78+
else: # elasticsearch_metrics.imps.elastic6
79+
recordtype._get_connection().indices.refresh(recordtype._template_pattern)
80+
self.assertEqual(recordtype.search().count(), expected_count)

osf_tests/metrics/reporters/_testutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from osf.metrics.reports import MonthlyReport
33

44

5-
def list_monthly_reports(reporter: MonthlyReporter) -> list[MonthlyReport]:
5+
def list_monthly_reports(reporter: MonthlyReporter, *, flat=False) -> list[MonthlyReport]:
66
_each_reports_list = (
77
reporter.report(**_kwargs)
88
for _kwargs in reporter.iter_report_kwargs()

osf_tests/metrics/reporters/test_institutional_summary_reporter.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
from django.test import TestCase
55
from osf.metrics.reporters import InstitutionalSummaryMonthlyReporter
6-
from osf.metrics.reports import InstitutionMonthlySummaryReport
76
from osf.metrics.utils import YearMonth
87
from osf_tests.factories import (
98
InstitutionFactory,
@@ -80,10 +79,9 @@ def _create_active_user(cls, institution, date_confirmed):
8079

8180
def test_report_generation(self):
8281
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
83-
reports_raw = list_monthly_reports(reporter)
84-
self.assertEqual(len(reports_raw[0]), 2)
85-
86-
report = next(r for r in reports_raw[0] if isinstance(r, InstitutionMonthlySummaryReport))
82+
reports = list_monthly_reports(reporter)
83+
self.assertEqual(len(reports), 1)
84+
report = reports[0]
8785
self.assertEqual(report.institution_id, self._institution._id)
8886
self.assertEqual(report.user_count, 2) # _logged_in_user and _active_user
8987
self.assertEqual(report.public_project_count, 1)
@@ -116,8 +114,7 @@ def test_report_generation_multiple_institutions(self):
116114

117115
# Run the reporter for the current month (February 2018)
118116
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
119-
reports_raw = list_monthly_reports(reporter)
120-
reports = [item for sublist in reports_raw for item in sublist if isinstance(item, InstitutionMonthlySummaryReport)]
117+
reports = list_monthly_reports(reporter)
121118
self.assertEqual(len(reports), 3) # Reports for self._institution, institution2, institution3
122119

123120
# Extract reports by institution
@@ -266,8 +263,7 @@ def test_high_counts_multiple_institutions(self):
266263
if enable_benchmarking:
267264
reporter_start_time = time.time()
268265
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
269-
reports_raw = list_monthly_reports(reporter)
270-
reports = [item for sublist in reports_raw for item in sublist if isinstance(item, InstitutionMonthlySummaryReport)]
266+
reports = list_monthly_reports(reporter)
271267
assert len(reports) == additional_institution_count + 1
272268

273269
if enable_benchmarking:

osf_tests/metrics/reporters/test_public_item_usage_reporter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ def test_no_data(self, ym_empty):
174174

175175
def test_reporter(self, ym_empty, ym_sparse, ym_busy, sparse_month_usage, busy_month_item0, busy_month_item1, busy_month_item2, item0):
176176
_empty = list_monthly_reports(PublicItemUsageReporter(ym_empty))
177-
_sparse_raw = list_monthly_reports(PublicItemUsageReporter(ym_sparse))
178-
_sparse = [item for sublist in _sparse_raw for item in sublist if isinstance(item, PublicItemUsageReport)]
179-
_busy_raw = list_monthly_reports(PublicItemUsageReporter(ym_busy))
180-
_busy = [item for sublist in _busy_raw for item in sublist if isinstance(item, PublicItemUsageReport)]
177+
_sparse = list_monthly_reports(PublicItemUsageReporter(ym_sparse))
178+
_busy = list_monthly_reports(PublicItemUsageReporter(ym_busy))
181179

182180
# empty month:
183181
assert _empty == []

0 commit comments

Comments
 (0)