Skip to content

Commit

Permalink
Add spam/ham metrics for OOPSpam and Akismet with FK join fix, test c…
Browse files Browse the repository at this point in the history
…leanup
  • Loading branch information
Uditi Mehta authored and Uditi Mehta committed Oct 28, 2024
1 parent 0095797 commit 1c35276
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
4 changes: 2 additions & 2 deletions osf/external/askismet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def get_flagged_count(self, start_date, end_date):
action=NodeLog.FLAG_SPAM,
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='akismet'
node__spam_data__who_flagged__in=['akismet', 'both']
).count()

return flagged_count
Expand All @@ -153,7 +153,7 @@ def get_hammed_count(self, start_date, end_date):
action=NodeLog.CONFIRM_HAM,
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='akismet'
node__spam_data__who_flagged__in=['akismet', 'both']
).count()

return hammed_count
4 changes: 2 additions & 2 deletions osf/external/oopspam/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_flagged_count(self, start_date, end_date):
action=NodeLog.FLAG_SPAM,
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='oopspam'
node__spam_data__who_flagged__in=['oopspam', 'both']
).count()

return flagged_count
Expand All @@ -65,7 +65,7 @@ def get_hammed_count(self, start_date, end_date):
action=NodeLog.CONFIRM_HAM,
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='oopspam'
node__spam_data__who_flagged__in=['oopspam', 'both']
).count()

return hammed_count
15 changes: 4 additions & 11 deletions osf_tests/external/akismet/test_akismet.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,40 +243,33 @@ def test_get_flagged_count(self, mock_filter, user):
from osf.external.askismet.client import AkismetClient
from datetime import datetime

mock_filter.return_value.count.return_value = 7

client = AkismetClient()
start_date = datetime(2024, 10, 1)
end_date = datetime(2024, 10, 31)

flagged_count = client.get_flagged_count(start_date, end_date)
client.get_flagged_count(start_date, end_date)

mock_filter.assert_called_with(
action='flag_spam',
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='akismet'
node__spam_data__who_flagged__in=['akismet', 'both']
)
assert flagged_count == 7

@mock.patch('osf.models.NodeLog.objects.filter')
def test_get_hammed_count(self, mock_filter, user):
from osf.external.askismet.client import AkismetClient
from datetime import datetime

mock_filter.return_value.count.return_value = 4

client = AkismetClient()
start_date = datetime(2024, 10, 1)
end_date = datetime(2024, 10, 31)

hammed_count = client.get_hammed_count(start_date, end_date)
client.get_hammed_count(start_date, end_date)

mock_filter.assert_called_with(
action='confirm_ham',
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='akismet'
node__spam_data__who_flagged__in=['akismet', 'both']
)
assert hammed_count == 4

14 changes: 4 additions & 10 deletions osf_tests/external/oopspam/test_oopspam.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,39 +131,33 @@ def test_get_flagged_count(self, mock_filter, user):
from osf.external.oopspam.client import OOPSpamClient
from datetime import datetime

mock_filter.return_value.count.return_value = 5

client = OOPSpamClient()
start_date = datetime(2024, 10, 1)
end_date = datetime(2024, 10, 31)

flagged_count = client.get_flagged_count(start_date, end_date)
client.get_flagged_count(start_date, end_date)

mock_filter.assert_called_with(
action='flag_spam',
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='oopspam'
node__spam_data__who_flagged__in=['oopspam', 'both']
)
assert flagged_count == 5

@mock.patch('osf.models.NodeLog.objects.filter')
def test_get_hammed_count(self, mock_filter, user):
from osf.external.oopspam.client import OOPSpamClient
from datetime import datetime

mock_filter.return_value.count.return_value = 3

client = OOPSpamClient()
start_date = datetime(2024, 10, 1)
end_date = datetime(2024, 10, 31)

hammed_count = client.get_hammed_count(start_date, end_date)
client.get_hammed_count(start_date, end_date)

mock_filter.assert_called_with(
action='confirm_ham',
created__gt=start_date,
created__lt=end_date,
spam_data__who_flagged='oopspam'
node__spam_data__who_flagged__in=['oopspam', 'both']
)
assert hammed_count == 3
56 changes: 28 additions & 28 deletions osf_tests/metrics/test_spam_count_reporter.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import datetime
import pytest
from datetime import datetime
from osf.metrics.reporters.spam_count import SpamCountReporter
from osf.external.oopspam.client import OOPSpamClient
from osf.external.askismet.client import AkismetClient
from unittest import mock
from osf.metrics.utils import YearMonth
from osf_tests.factories import NodeLogFactory, NodeFactory

@pytest.fixture
def mock_oopspam_client(mocker):
mock = mocker.patch('osf.external.oopspam.client.OOPSpamClient')
mock.get_flagged_count.return_value = 10
mock.get_hammed_count.return_value = 5
return mock
def mock_oopspam_client():
with mock.patch('osf.external.oopspam.client.OOPSpamClient') as mock_client:
instance = mock_client.return_value
instance.get_flagged_count.return_value = 10
instance.get_hammed_count.return_value = 5
yield instance

@pytest.fixture
def mock_akismet_client(mocker):
mock = mocker.patch('osf.external.askismet.client.AkismetClient')
mock.get_flagged_count.return_value = 20
mock.get_hammed_count.return_value = 10
return mock
def mock_akismet_client():
with mock.patch('osf.external.askismet.client.AkismetClient') as mock_client:
instance = mock_client.return_value
instance.get_flagged_count.return_value = 20
instance.get_hammed_count.return_value = 10
yield instance

@pytest.fixture
def mock_nodelog_model(mocker):
mock = mocker.patch('osf.models.NodeLog')
mock.filter.return_value.count.return_value = 100
return mock
@pytest.mark.django_db
def test_spam_count_reporter():
start_date = datetime(2024, 10, 1)

@pytest.fixture
def mock_preprintlog_model(mocker):
mock = mocker.patch('osf.models.PreprintLog')
mock.filter.return_value.count.return_value = 50
return mock
oopspam_node = NodeFactory(spam_data={'who_flagged': 'oopspam'})
akismet_node = NodeFactory(spam_data={'who_flagged': 'akismet'})

NodeLogFactory.create_batch(10, action='flag_spam', created=start_date, node=oopspam_node)
NodeLogFactory.create_batch(5, action='confirm_ham', created=start_date, node=oopspam_node)
NodeLogFactory.create_batch(20, action='flag_spam', created=start_date, node=akismet_node)
NodeLogFactory.create_batch(10, action='confirm_ham', created=start_date, node=akismet_node)

def test_spam_count_reporter(mock_oopspam_client, mock_akismet_client, mock_nodelog_model, mock_preprintlog_model):
report_month = datetime.datetime(2024, 10, 1)
report_yearmonth = YearMonth(2024, 10)
reporter = SpamCountReporter()
report = reporter.report(report_month)
report = reporter.report(report_yearmonth)

assert report[0].oopspam_flagged == 10
assert report[0].oopspam_hammed == 5
assert report[0].akismet_flagged == 20
assert report[0].akismet_hammed == 10
assert report[0].node_confirmed_spam == 100
assert report[0].preprint_confirmed_spam == 50

0 comments on commit 1c35276

Please sign in to comment.