-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
179 lines (146 loc) · 5.7 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Views delivering statistics.
- GET /statistics/competences
- GET /statistics/hours
- GET /statistics/innopoints
"""
from datetime import datetime
from flask import jsonify, request
from innopoints.blueprints import api
from innopoints.core.helpers import abort, admin_required
from innopoints.core.timezone import tz_aware_now, unix_epoch
from innopoints.extensions import db
from innopoints.models import (
Account,
Activity,
Application,
Competence,
Feedback,
LifetimeStage,
Project,
StockChange,
Transaction,
feedback_competence,
project_tags,
)
@api.route('/statistics/competences')
@admin_required
def get_competence_stats():
"""Return the statistics on the developed competences."""
if 'start_date' in request.args:
try:
start_date = datetime.fromisoformat(request.args['start_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if start_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
start_date = unix_epoch
if 'end_date' in request.args:
try:
end_date = datetime.fromisoformat(request.args['end_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if end_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
end_date = tz_aware_now()
student_groups = request.args.getlist('group')
project_tag = request.args.get('tag')
competences = (
db.session
.query(Competence.id.label('competence_id'))
.join(feedback_competence,
Competence.id == feedback_competence.c.competence_id)
.join(Feedback,
feedback_competence.c.feedback_id == Feedback.application_id)
.join(Feedback.application)
.join(Application.activity)
.join(Activity.project)
.outerjoin(project_tags,
Project.id == project_tags.c.project_id)
.join(Application.applicant)
.add_columns(db.func.count(Feedback.application_id).label('amount'))
.filter(Feedback.time > start_date,
Feedback.time < end_date)
.group_by(Competence.id)
)
if student_groups:
competences = competences.filter(Account.group.in_(student_groups))
if project_tag is not None:
competences = competences.filter(project_tags.c.tag_id == project_tag)
return jsonify([{'id': row[0], 'amount': row[1]} for row in competences.all()])
@api.route('/statistics/hours')
@admin_required
def get_hour_stats():
"""Return the statistics on the volunteering hours."""
if 'start_date' in request.args:
try:
start_date = datetime.fromisoformat(request.args['start_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if start_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
start_date = unix_epoch
if 'end_date' in request.args:
try:
end_date = datetime.fromisoformat(request.args['end_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if end_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
end_date = tz_aware_now()
student_groups = request.args.getlist('group')
project_tag = request.args.get('tag')
hours = (
db.session
.query(db.func.sum(Application.actual_hours))
.join(Application.activity)
.join(Activity.project)
.outerjoin(project_tags,
Project.id == project_tags.c.project_id)
.join(Application.applicant)
.filter(Project.lifetime_stage == LifetimeStage.finished)
.filter(Activity.start_date > start_date,
Activity.end_date < end_date)
)
if student_groups:
hours = hours.filter(Account.group.in_(student_groups))
if project_tag is not None:
hours = hours.filter(project_tags.c.tag_id == project_tag)
return jsonify(hours.scalar() or 0)
@api.route('/statistics/innopoints')
@admin_required
def get_innopoint_stats():
"""Return the statistics on the amount of innopoints spent."""
if 'start_date' in request.args:
try:
start_date = datetime.fromisoformat(request.args['start_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if start_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
start_date = unix_epoch
if 'end_date' in request.args:
try:
end_date = datetime.fromisoformat(request.args['end_date'])
except ValueError:
abort(400, {'message': 'The datetime must be in ISO format with timezone.'})
if end_date.tzinfo is None:
abort(400, {'message': 'The timezone must be passed.'})
else:
end_date = tz_aware_now()
student_groups = request.args.getlist('group')
innopoints = (
db.session
.query(db.func.sum(Transaction.change))
.join(Transaction.account)
.join(Transaction.stock_change)
.filter(StockChange.time > start_date,
StockChange.time < end_date)
)
if student_groups:
innopoints = innopoints.filter(Account.group.in_(student_groups))
return jsonify(-(innopoints.scalar() or 0))