Try stabilizing Mastodon weekly counts with more guessing

Mastodon has a tricky way of reporting weekly logins according to the
going week. Multiply the count depending on the weekday to see if we
can stabilize the stats a bit.
merge-requests/130/head
Jason Robinson 2018-06-04 22:59:49 +03:00
rodzic 8111f2d4e4
commit 907d1d60db
2 zmienionych plików z 47 dodań i 4 usunięć

Wyświetl plik

@ -2,6 +2,8 @@ import json
import re
from copy import deepcopy
from django.utils.timezone import now
from federation.utils.diaspora import generate_diaspora_profile_id
from federation.utils.network import fetch_document
@ -51,6 +53,12 @@ def int_or_none(value):
return None
def mastodon_weekly_from_day_logins(logins):
dow = now().date().weekday()
multiplier = 7 - dow
return logins * multiplier
def parse_mastodon_document(doc, host):
result = deepcopy(defaults)
result['host'] = host
@ -80,8 +88,9 @@ def parse_mastodon_document(doc, host):
return
else:
weekly_count = int_or_none(activity_doc[0].get('logins'))
result['activity']['users']['weekly'] = weekly_count
if weekly_count:
weekly_count = mastodon_weekly_from_day_logins(weekly_count)
result['activity']['users']['weekly'] = weekly_count
result['activity']['users']['half_year'] = int(weekly_count * WEEKLY_USERS_HALFYEAR_MULTIPLIER)
result['activity']['users']['monthly'] = int(weekly_count * WEEKLY_USERS_MONTHLY_MULTIPLIER)

Wyświetl plik

@ -1,9 +1,11 @@
import json
from unittest.mock import patch
from freezegun import freeze_time
from federation.hostmeta.parsers import (
parse_nodeinfo_document, parse_nodeinfo2_document, parse_statisticsjson_document, int_or_none,
parse_mastodon_document)
parse_mastodon_document, mastodon_weekly_from_day_logins)
from federation.tests.fixtures.hostmeta import (
NODEINFO2_10_DOC, NODEINFO_10_DOC, NODEINFO_20_DOC, STATISTICS_JSON_DOC, MASTODON_DOC, MASTODON_ACTIVITY_DOC,
MASTODON_RC_DOC)
@ -14,9 +16,40 @@ class TestIntOrNone:
assert int_or_none(-1) is None
class TestMastodonWeeklyFromDayLogins:
@freeze_time('2018-06-04')
def test_monday(self):
assert mastodon_weekly_from_day_logins(1) == 7
@freeze_time('2018-06-05')
def test_tuesday(self):
assert mastodon_weekly_from_day_logins(1) == 6
@freeze_time('2018-06-06')
def test_wednesday(self):
assert mastodon_weekly_from_day_logins(1) == 5
@freeze_time('2018-06-07')
def test_thursday(self):
assert mastodon_weekly_from_day_logins(1) == 4
@freeze_time('2018-06-08')
def test_friday(self):
assert mastodon_weekly_from_day_logins(1) == 3
@freeze_time('2018-06-09')
def test_saturday(self):
assert mastodon_weekly_from_day_logins(1) == 2
@freeze_time('2018-06-10')
def test_sunday(self):
assert mastodon_weekly_from_day_logins(1) == 1
class TestParseMastodonDocument:
@patch('federation.hostmeta.parsers.fetch_document')
def test_parse_mastodon_document(self, mock_fetch):
@patch('federation.hostmeta.parsers.mastodon_weekly_from_day_logins', side_effect=lambda x: x)
def test_parse_mastodon_document(self, mock_weekly, mock_fetch):
mock_fetch.return_value = MASTODON_ACTIVITY_DOC, 200, None
result = parse_mastodon_document(json.loads(MASTODON_DOC), 'example.com')
assert result == {
@ -48,7 +81,8 @@ class TestParseMastodonDocument:
}
@patch('federation.hostmeta.parsers.fetch_document')
def test_parse_mastodon_document__rc_version(self, mock_fetch):
@patch('federation.hostmeta.parsers.mastodon_weekly_from_day_logins', side_effect=lambda x: x)
def test_parse_mastodon_document__rc_version(self, mock_weekly, mock_fetch):
mock_fetch.return_value = MASTODON_ACTIVITY_DOC, 200, None
result = parse_mastodon_document(json.loads(MASTODON_RC_DOC), 'example.com')
assert result == {