forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake_metrics_reports.py
More file actions
81 lines (70 loc) · 2.85 KB
/
fake_metrics_reports.py
File metadata and controls
81 lines (70 loc) · 2.85 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
from datetime import date, timedelta
from random import randint
from django.conf import settings
from django.core.management.base import BaseCommand
from osf.metrics import (
UserSummaryReport,
PreprintSummaryReport,
)
from osf.metrics.reports import PublicItemUsageReport
from osf.metrics.utils import YearMonth
from osf.models import PreprintProvider
def fake_user_counts(days_back):
yesterday = date.today() - timedelta(days=1)
first_report = UserSummaryReport(
report_date=(yesterday - timedelta(days=days_back)),
active=randint(0, 23),
deactivated=randint(0, 2),
merged=randint(0, 4),
new_users_daily=randint(0, 7),
new_users_with_institution_daily=randint(0, 5),
unconfirmed=randint(0, 3),
)
first_report.save()
last_report = first_report
while last_report.report_date < yesterday:
new_user_count = randint(0, 500)
new_report = UserSummaryReport(
report_date=(last_report.report_date + timedelta(days=1)),
active=(last_report.active + randint(0, new_user_count)),
deactivated=(last_report.deactivated + randint(0, new_user_count)),
merged=(last_report.merged + randint(0, new_user_count)),
new_users_daily=new_user_count,
new_users_with_institution_daily=randint(0, new_user_count),
unconfirmed=(last_report.unconfirmed + randint(0, new_user_count)),
)
new_report.save()
last_report = new_report
def fake_preprint_counts(days_back):
yesterday = date.today() - timedelta(days=1)
provider_keys = PreprintProvider.objects.all().values_list('_id', flat=True)
for day_delta in range(days_back):
for provider_key in provider_keys:
preprint_count = randint(100, 5000) * (days_back - day_delta)
PreprintSummaryReport(
report_date=yesterday - timedelta(days=day_delta),
provider_key=provider_key,
preprint_count=preprint_count,
).save()
def fake_usage_reports(osfid: str, count: int):
_ym = YearMonth.from_date(date.today()).prior()
for _months in range(count):
PublicItemUsageReport.record(
item_osfid=osfid,
report_yearmonth=_ym,
view_count=(_vc := randint(0, 500)),
view_session_count=randint(0, _vc),
download_count=(_dc := randint(0, 300)),
download_session_count=randint(0, _dc),
)
_ym = _ym.prior()
class Command(BaseCommand):
def handle(self, *args, **kwargs):
if not settings.DEBUG:
raise NotImplementedError('fake_reports requires DEBUG mode')
fake_user_counts(1000)
fake_preprint_counts(1000)
fake_usage_reports('blarg', 100)
fake_usage_reports('blerg', 50)
fake_usage_reports('bleg', 50)
# TODO: more reports