Skip to content

Commit

Permalink
Add private spam metrics report with preprint inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Uditi Mehta authored and Uditi Mehta committed Nov 4, 2024
1 parent 1c35276 commit 0a32b02
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 64 deletions.
24 changes: 14 additions & 10 deletions osf/external/askismet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,30 @@ def submit_ham(self, user_ip, user_agent, **kwargs):
if res.status_code != requests.codes.ok:
raise AkismetClientError(reason=res.text)

def get_flagged_count(self, start_date, end_date):
from osf.models import NodeLog
def get_flagged_count(self, start_date, end_date, category='node'):
from osf.models import NodeLog, PreprintLog

flagged_count = NodeLog.objects.filter(
action=NodeLog.FLAG_SPAM,
log_model = NodeLog if category == 'node' else PreprintLog

flagged_count = log_model.objects.filter(
action=log_model.FLAG_SPAM,
created__gt=start_date,
created__lt=end_date,
node__spam_data__who_flagged__in=['akismet', 'both']
**{f'{category}__spam_data__who_flagged__in': ['akismet', 'both']}
).count()

return flagged_count

def get_hammed_count(self, start_date, end_date):
from osf.models import NodeLog
def get_hammed_count(self, start_date, end_date, category='node'):
from osf.models import NodeLog, PreprintLog

log_model = NodeLog if category == 'node' else PreprintLog

hammed_count = NodeLog.objects.filter(
action=NodeLog.CONFIRM_HAM,
hammed_count = log_model.objects.filter(
action=log_model.CONFIRM_HAM,
created__gt=start_date,
created__lt=end_date,
node__spam_data__who_flagged__in=['akismet', 'both']
**{f'{category}__spam_data__who_flagged__in': ['akismet', 'both']}
).count()

return hammed_count
24 changes: 14 additions & 10 deletions osf/external/oopspam/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,30 @@ def check_content(self, user_ip, content, **kwargs):
# OOPSpam returns a spam score out of 6. 3 or higher indicates spam
return spam_score >= settings.OOPSPAM_SPAM_LEVEL, resp_json

def get_flagged_count(self, start_date, end_date):
from osf.models import NodeLog
def get_flagged_count(self, start_date, end_date, category='node'):
from osf.models import NodeLog, PreprintLog

flagged_count = NodeLog.objects.filter(
action=NodeLog.FLAG_SPAM,
log_model = NodeLog if category == 'node' else PreprintLog

flagged_count = log_model.objects.filter(
action=log_model.FLAG_SPAM,
created__gt=start_date,
created__lt=end_date,
node__spam_data__who_flagged__in=['oopspam', 'both']
**{f'{category}__spam_data__who_flagged__in': ['oopspam', 'both']}
).count()

return flagged_count

def get_hammed_count(self, start_date, end_date):
from osf.models import NodeLog
def get_hammed_count(self, start_date, end_date, category='node'):
from osf.models import NodeLog, PreprintLog

log_model = NodeLog if category == 'node' else PreprintLog

hammed_count = NodeLog.objects.filter(
action=NodeLog.CONFIRM_HAM,
hammed_count = log_model.objects.filter(
action=log_model.CONFIRM_HAM,
created__gt=start_date,
created__lt=end_date,
node__spam_data__who_flagged__in=['oopspam', 'both']
**{f'{category}__spam_data__who_flagged__in': ['oopspam', 'both']}
).count()

return hammed_count
35 changes: 35 additions & 0 deletions osf/metrics/reporters/private_spam_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from osf.metrics.reports import MonthlyReport
from osf.external.oopspam.client import OOPSpamClient
from osf.external.askismet.client import AkismetClient

class PrivateSpamMetricsReporter(MonthlyReport):
report_name = 'Private Spam Metrics'

def report(self, report_yearmonth):
target_month = report_yearmonth.target_month()
next_month = report_yearmonth.next_month()

oopspam_client = OOPSpamClient()
akismet_client = AkismetClient()

node_oopspam_flagged = oopspam_client.get_flagged_count(target_month, next_month, category='node')
node_oopspam_hammed = oopspam_client.get_hammed_count(target_month, next_month, category='node')
node_akismet_flagged = akismet_client.get_flagged_count(target_month, next_month, category='node')
node_akismet_hammed = akismet_client.get_hammed_count(target_month, next_month, category='node')

preprint_oopspam_flagged = oopspam_client.get_flagged_count(target_month, next_month, category='preprint')
preprint_oopspam_hammed = oopspam_client.get_hammed_count(target_month, next_month, category='preprint')
preprint_akismet_flagged = akismet_client.get_flagged_count(target_month, next_month, category='preprint')
preprint_akismet_hammed = akismet_client.get_hammed_count(target_month, next_month, category='preprint')

return [self.create_report(
report_yearmonth=str(report_yearmonth),
node_oopspam_flagged=node_oopspam_flagged,
node_oopspam_hammed=node_oopspam_hammed,
node_akismet_flagged=node_akismet_flagged,
node_akismet_hammed=node_akismet_hammed,
preprint_oopspam_flagged=preprint_oopspam_flagged,
preprint_oopspam_hammed=preprint_oopspam_hammed,
preprint_akismet_flagged=preprint_akismet_flagged,
preprint_akismet_hammed=preprint_akismet_hammed,
)]
16 changes: 0 additions & 16 deletions osf/metrics/reporters/spam_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,13 @@
from ._base import MonthlyReporter
from osf.models import PreprintLog, NodeLog
from osf.models.spam import SpamStatus
from osf.external.oopspam.client import OOPSpamClient
from osf.external.askismet.client import AkismetClient


class SpamCountReporter(MonthlyReporter):

def report(self, report_yearmonth):
target_month = report_yearmonth.target_month()
next_month = report_yearmonth.next_month()

oopspam_client = OOPSpamClient()
akismet_client = AkismetClient()

oopspam_flagged = oopspam_client.get_flagged_count(target_month, next_month)
oopspam_hammed = oopspam_client.get_hammed_count(target_month, next_month)

akismet_flagged = akismet_client.get_flagged_count(target_month, next_month)
akismet_hammed = akismet_client.get_hammed_count(target_month, next_month)

report = SpamSummaryReport(
report_yearmonth=str(report_yearmonth),
# Node Log entries
Expand All @@ -44,10 +32,6 @@ def report(self, report_yearmonth):
created__lt=next_month,
node__type='osf.node',
).count(),
oopspam_flagged=oopspam_flagged,
oopspam_hammed=oopspam_hammed,
akismet_flagged=akismet_flagged,
akismet_hammed=akismet_hammed,
# Registration Log entries
registration_confirmed_spam=NodeLog.objects.filter(
action=NodeLog.CONFIRM_SPAM,
Expand Down
4 changes: 0 additions & 4 deletions osf/metrics/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,3 @@ class SpamSummaryReport(MonthlyReport):
preprint_flagged = metrics.Integer()
user_marked_as_spam = metrics.Integer()
user_marked_as_ham = metrics.Integer()
oopspam_flagged = metrics.Integer()
oopspam_hammed = metrics.Integer()
akismet_flagged = metrics.Integer()
akismet_hammed = metrics.Integer()
31 changes: 7 additions & 24 deletions osf_tests/metrics/test_spam_count_reporter.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import pytest
from datetime import datetime
from osf.metrics.reporters.spam_count import SpamCountReporter
from unittest import mock
from osf.metrics.reporters.private_spam_metrics import PrivateSpamMetricsReporter
from osf.metrics.utils import YearMonth
from osf_tests.factories import NodeLogFactory, NodeFactory

@pytest.fixture
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():
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.mark.django_db
def test_spam_count_reporter():
def test_private_spam_metrics_reporter():
start_date = datetime(2024, 10, 1)

oopspam_node = NodeFactory(spam_data={'who_flagged': 'oopspam'})
Expand All @@ -34,10 +17,10 @@ def test_spam_count_reporter():
NodeLogFactory.create_batch(10, action='confirm_ham', created=start_date, node=akismet_node)

report_yearmonth = YearMonth(2024, 10)
reporter = SpamCountReporter()
reporter = PrivateSpamMetricsReporter()
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_oopspam_flagged'] == 10
assert report[0]['node_oopspam_hammed'] == 5
assert report[0]['node_akismet_flagged'] == 20
assert report[0]['node_akismet_hammed'] == 10

0 comments on commit 0a32b02

Please sign in to comment.