forked from sfono96/ponyv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgdb.py
195 lines (159 loc) · 6.74 KB
/
pgdb.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import psycopg2, os, urlparse
from math import trunc
############ DB CONNECTION ############
production = False # set this to true if pushing changes to production server (heroku) else keep false on development box
if production == False:
# development server (local box)
host = 'localhost'
dbname = 'inquis'
user = 'postgres'
password = 'password'
conn_string = 'host=%s dbname=%s user=%s password=%s' %(host,dbname,user,password)
conn = psycopg2.connect(conn_string)
else:
#production server (heroku)
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
# cursor object
cursor = conn.cursor()
############ DEFAULT LISTS ############
# PROFICIENCY GROUPS
cursor.execute('select distinct PRIOR_YEAR_PROFICIENCY from data;')
proficiency_groups = [r[0] for r in cursor.fetchall()]
proficiency_groups.append('All')
# GRADE LEVELS
cursor.execute('select distinct GRADE from data order by 1;')
grades = [r[0] for r in cursor.fetchall()]
# ATTEMPTS
cursor.execute('select distinct attempt from data order by 1;')
attempts = [r[0] for r in cursor.fetchall()]
############ HELPER METHODS ############
def round_me(number):
#return Decimal(number).quantize(Decimal('0.01'))
return (trunc(round(number,3)*1000)+.0)/10
def relevant_quiz(grade):
sql = 'SELECT DISTINCT ASSESSMENT FROM DATA WHERE GRADE = \'%s\' ORDER BY 1;' % grade
cursor.execute(sql)
quizzes = [r[0] for r in cursor.fetchall()]
return quizzes
def relevant_teachers(grade):
sql = 'SELECT DISTINCT TEACHER FROM DATA WHERE GRADE = \'%s\' ORDER BY 1;' % grade
cursor.execute(sql)
rs = [r[0] for r in cursor.fetchall()]
rs.append('All') # Allow for all teachers within the grade
return rs
############ MAIN METHODS ############
def quiz(grade,proficiency):
if proficiency == 'All':
sql = 'SELECT ASSESSMENT, AVG(SCORE) FROM DATA WHERE GRADE = \'%s\' AND ATTEMPT = 1 GROUP BY ASSESSMENT ORDER BY 1;' % grade
else:
sql = 'SELECT ASSESSMENT, AVG(SCORE) FROM DATA WHERE GRADE = \'%s\' AND ATTEMPT = 1 AND PRIOR_YEAR_PROFICIENCY = \'%s\' GROUP BY ASSESSMENT ORDER BY 1;' % (grade,proficiency)
cursor.execute(sql)
rs = cursor.fetchall()
assessments = [r[0] for r in rs]
#scores = [round_me(r[1]) for r in rs]
scores = [float(r[1]) for r in rs]
return assessments, scores
def teacher(grade,proficiency,quiz):
if proficiency == 'All':
sql = 'SELECT TEACHER, AVG(SCORE) FROM DATA WHERE GRADE = \'%s\' AND ASSESSMENT = \'%s\' AND ATTEMPT = 1 GROUP BY TEACHER ORDER BY 1;' % (grade,quiz)
else:
sql = 'SELECT TEACHER, AVG(SCORE) FROM DATA WHERE GRADE = \'%s\' AND ASSESSMENT = \'%s\' AND ATTEMPT = 1 AND PRIOR_YEAR_PROFICIENCY = \'%s\' GROUP BY TEACHER ORDER BY 1;' % (grade,quiz,proficiency)
cursor.execute(sql)
rs = cursor.fetchall()
teachers = [r[0] for r in rs]
#scores = [round_me(r[1]) for r in rs]
scores = [float(r[1]) for r in rs]
return teachers, scores
def students_list(grade,teacher,proficiency):
if proficiency == 'All':
if teacher == 'All':
sql = 'SELECT STUDENT, ASSESSMENT, SCORE, TEACHER FROM DATA WHERE GRADE = \'%s\' AND ATTEMPT = 1 ORDER BY 1,2;' % grade
else:
sql = 'SELECT STUDENT, ASSESSMENT, SCORE, TEACHER FROM DATA WHERE TEACHER = \'%s\' AND ATTEMPT = 1 ORDER BY 1,2;' % teacher
else:
if teacher == 'All':
sql = 'SELECT STUDENT, ASSESSMENT, SCORE, TEACHER FROM DATA WHERE GRADE = \'%s\' AND ATTEMPT = 1 AND PRIOR_YEAR_PROFICIENCY = \'%s\' ORDER BY 1,2;' % (grade,proficiency)
else:
sql = 'SELECT STUDENT, ASSESSMENT, SCORE, TEACHER FROM DATA WHERE TEACHER = \'%s\' AND ATTEMPT = 1 AND PRIOR_YEAR_PROFICIENCY = \'%s\' ORDER BY 1,2;' % (teacher,proficiency)
quizzes = relevant_quiz(grade)
cursor.execute(sql)
rs = cursor.fetchall()
data_series = []
sdict = {}
for r in rs:
if r[0] not in sdict: # if student not in the dictionary
sdict[r[0]] = [] # will be a list where first value is teacher name and second value is dictionary of quiz scores
sdict[r[0]].append(r[3]) # teacher
sdict[r[0]].append({}) # dictionary of quiz scores
for quiz in quizzes:
if quiz not in sdict[r[0]][1]: # if quiz score in the dictionary for student
sdict[r[0]][1][quiz] = '' # blank place holder quiz score
if r[1] in sdict[r[0]][1]:
sdict[r[0]][1][r[1]] = float(r[2]) # add real quiz score
for k in sdict:
list = []
list.append(sdict[k][0]) # teacher
list.append(k) # student
for q in quizzes:
if sdict[k][1][q] <> '':
list.append(float(sdict[k][1][q]))
else:
list.append('')
data_series.append(list)
data_series = sorted(data_series)
return data_series
def student_attempts(grade,teacher,proficiency,quiz):
# SQL depending on selections
if proficiency == 'All':
if teacher == 'All':
sql = 'SELECT STUDENT, ATTEMPT, SCORE FROM DATA WHERE GRADE = \'%s\' AND ASSESSMENT = \'%s\' ORDER BY 1,2;' %(grade, quiz)
else:
sql = 'SELECT STUDENT, ATTEMPT, SCORE FROM DATA WHERE TEACHER = \'%s\' AND ASSESSMENT = \'%s\' ORDER BY 1,2;' %(teacher, quiz)
else:
if teacher == 'All':
sql = 'SELECT STUDENT, ATTEMPT, SCORE FROM DATA WHERE GRADE = \'%s\' AND ASSESSMENT = \'%s\' AND PRIOR_YEAR_PROFICIENCY = \'%s\' ORDER BY 1,2;' % (grade,quiz,proficiency)
else:
sql = 'SELECT STUDENT, ATTEMPT, SCORE FROM DATA WHERE TEACHER = \'%s\' AND ASSESSMENT = \'%s\' AND PRIOR_YEAR_PROFICIENCY = \'%s\' ORDER BY 1,2;' % (teacher,quiz,proficiency)
cursor.execute(sql)
rs = cursor.fetchall()
# get list of scores (val) for each student (key)
sdict = {}
for r in rs:
if r[0] not in sdict:
sdict[r[0]] = []
sdict[r[0]].append(r[2])
# now combine in key, val into 1 row and toss into data series list for returning
data_series = []
for k in sdict:
list = []
list.append(k)
for v in sdict[k]:
if v is not None:
list.append(round_me(v))
else:
list.append(None)
data_series.append(list)
data_series = sorted(data_series)
return data_series
# st = student_attempts('0 - Kindergarten','Mr. Boler','All','K.CC.A.1')
# for s in st:
# print s
# ASSESSMENTS BY GRADE By SCORE (LAST 3)
def assessments_by_grade(proficiency):
sql = 'select grade, score, count(*) students from ('+
'select grade,student,round(avg(score)) score from ('+
'select student, grade, assessment, max(round(score)) score from data_new '+
'where score <= 4 and assessment in (select assessment from ('+
'select s.*, row_number() over (partition by grade order by assessment) rowCnt from ('+
'select distinct grade, assessment from data_new) s) t where rowCnt <= 3) and PRIOR_YEAR_PROFICIENCY = \'%s\' '+
'group by student, grade, assessment) i group by grade,student) j where score > 0 group by grade, score order by 1,2;' % (proficiency)
cursor.execute(sql)
rs = cursor.fetchall()