-
Notifications
You must be signed in to change notification settings - Fork 24
/
usage_statistics.py
108 lines (87 loc) · 3.93 KB
/
usage_statistics.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import os
import argparse
import django
from datetime import datetime
import arrow
from django.contrib.auth import get_user_model
from django.utils import timezone
def get_number_of_deliveries(from_datetime, to_datetime):
"""
Get the number of deliveries made.
Simply counts the number of comments posted by students with files on
all FeedbackSets with deadlines within the from and to datetime arguments.
"""
from devilry.devilry_comment.models import CommentFile
from devilry.devilry_group.models import GroupComment, FeedbackSet
#: Get all `FeedbackSets` with deadlines within the from and to datetime range.
feedbackset_queryset = FeedbackSet.objects\
.filter(deadline_datetime__gte=from_datetime,
deadline_datetime__lte=to_datetime)
#: UNCOMMENT THIS IF YOU WANT TO:
#:
#: Filter only the last FeedbackSet for the AssignmentGroup.
# feedbackset_queryset = feedbackset_queryset \
# .filter(group__cached_data__last_feedbackset_id=models.F('id'))
# Get all comments for all `FeedbackSet`s with deadline within the
# from and to datetime posted by a student.
group_comment_queryset = GroupComment.objects\
.filter(user_role=GroupComment.USER_ROLE_STUDENT)\
.filter(feedback_set_id__in=feedbackset_queryset.values_list('id', flat=True))
#: UNCOMMENT THIS IF YOU WANT TO:
#:
#: Filter only comments posted before the deadline expired on the
#: feedbackset the comment belongs to.
# group_comment_queryset = group_comment_queryset\
# .filter(published_datetime__gte=models.F('feedback_set__deadline_datetime'))
#: Get all Comments with files from the fetched comments.
comment_file_queryset = CommentFile.objects\
.filter(comment_id__in=group_comment_queryset.values_list('id', flat=True))
return comment_file_queryset.count()
def get_unique_logins(from_datetime):
"""
Get the number of unique logins since a specified datetime.
"""
unique_logins = get_user_model().objects\
.filter(last_login__gte=from_datetime)
return unique_logins.count()
def populate_arguments_and_get_parser():
parser = argparse.ArgumentParser(description='Set up department permission groups for missing subjects.')
parser.add_argument(
'--from-date',
dest='from_date',
default='1900-01-01',
help='A %%Y-%%m-%%d formatted from-date. Defaults to 1900-01-01.')
parser.add_argument(
'--to-date',
dest='to_date',
default='5999-12-31',
help='A %%Y-%%m-%%d formatted to-date. Defaults to 5999-12-31.')
return parser
if __name__ == "__main__":
# For development:
os.environ.setdefault('DJANGOENV', 'develop')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "devilry.project.settingsproxy")
django.setup()
# For production: Specify python path to your settings file here
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'devilry_settings')
# django.setup()
parser = populate_arguments_and_get_parser()
args = parser.parse_args()
arguments_dict = vars(args)
from_datetime = timezone.make_aware(datetime.strptime(arguments_dict['from_date'], '%Y-%m-%d')).replace(
hour=0, minute=0, second=0)
to_datetime = timezone.make_aware(datetime.strptime(arguments_dict['to_date'], '%Y-%m-%d')).replace(
hour=23, minute=59, second=59)
# Get unique logins
unique_login_count = get_unique_logins(from_datetime=from_datetime)
print('Unique logins since {}: {}'.format(
arrow.get(from_datetime).format('MMM D. YYYY HH:mm:ss'),
unique_login_count))
# Get number of deliveries
delivery_count = get_number_of_deliveries(from_datetime, to_datetime)
print('Deliveries made between {} and {}: {}'.format(
arrow.get(from_datetime).format('MMM D. YYYY HH:mm:ss'),
arrow.get(to_datetime).format('MMM D. YYYY HH:mm:ss'),
delivery_count
))