-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.py
122 lines (100 loc) · 3.91 KB
/
analytics.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
from flask import Flask, jsonify, request
from flask_cors import CORS
from mongoengine import connect
from models import HighSchool
import statistics as st
app = Flask(__name__)
CORS(app)
connect(
host='mongodb://admin:[email protected]:29450/sharkweb'
)
@app.route("/attendance/<sid>", methods=['GET'])
def get_average_attendance(sid):
response = {}
connect('high_school')
avg = 0
if not HighSchool.objects(id=sid):
response['error'] = "invalid id in request"
return jsonify(response), 400
else:
for school in HighSchool.objects(id=sid):
attendance = list(map(float, school.attendance_rates.values()))
if len(attendance) == 0:
return jsonify({}), 200
avg = st.harmonic_mean(attendance)
return jsonify({'avg_attendance': avg}), 200
@app.route("/enrollments/<sid>", methods=['GET'])
def get_average_enrollments(sid):
response = {}
connect('high_school')
avg = 0
if not HighSchool.objects(id=sid):
response['error'] = "invalid id in request"
return jsonify(response), 400
else:
for school in HighSchool.objects(id=sid):
enrollments = list(map(float, school.enrollments.values()))
if len(enrollments) == 0:
return jsonify({}), 200
avg = st.mean(enrollments)
return jsonify({'avg_enrollments': int(avg)}), 200
@app.route("/selective-score/<sid>", methods=['GET'])
def avg_min_selective_score(sid):
response = {}
connect('high_school')
avg = -1
if not HighSchool.objects(id=sid):
response['error'] = "invalid id in request"
return jsonify(response), 400
else:
for school in HighSchool.objects(id=sid):
if school.selective == "Not Selective":
return jsonify({}), 200
scores = list(map(float, school.selective_entry_scores.values()))
avg = st.mean(scores)
return jsonify({'avg_min_selective_scores': int(avg)}), 200
@app.route("/compare-attendances", methods=['GET'])
def compare_attendances():
schools = request.args.getlist('school_id')
connect('high_school')
average_attendances = {}
for sid in schools:
for school in HighSchool.objects(id=int(sid)):
# get the avg attendance_rates
attendance = list(map(float, school.attendance_rates.values()))
average_attendances[school.name] = st.harmonic_mean(attendance)
return jsonify(average_attendances), 200
@app.route("/compare-enrollments", methods=['GET'])
def compare_enrollments():
schools = request.args.getlist('school_id')
connect('high_school')
average_enrollments = {}
for sid in schools:
for school in HighSchool.objects(id=int(sid)):
# get the avg enrolments
enrollments = list(map(float, school.enrollments.values()))
average_enrollments[school.name] = st.mean(enrollments)
return jsonify(average_enrollments), 200
@app.route("/compare-students", methods=['GET'])
def compare_students():
schools = request.args.getlist('school_id')
connect('high_school')
num_students = {}
for sid in schools:
for school in HighSchool.objects(id=int(sid)):
# get current num. of students
num_students[school.name] = int(school.students)
return jsonify(num_students), 200
@app.route("/compare-selective-entries", methods=['GET'])
def compare_selective_entry():
schools = request.args.getlist('school_id')
connect('high_school')
average_selective_entry = {}
for sid in schools:
for school in HighSchool.objects(id=int(sid)):
if school.selective != 'Not Selective':
scores = list(map(float, school.selective_entry_scores.values()))
average_selective_entry[school.name] = st.mean(scores)
return jsonify(average_selective_entry), 200
if __name__ == '__main__':
app.run(port=5003)