author | Sverre Rabbelier <srabbelier@gmail.com> |
Thu, 02 Apr 2009 23:05:56 +0000 | |
changeset 2058 | 773b13d86309 |
parent 2052 | a723a2509e21 |
child 2060 | 45029d87be4a |
permissions | -rwxr-xr-x |
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Starts an interactive shell with statistic helpers. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
import cPickle |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
import operator |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
import sys |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
import interactive |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
|
2058 | 32 |
def dateFetch(queryGen, last=None, batchSize=100): |
33 |
"""Iterator that yields an entity in batches. |
|
34 |
||
35 |
Args: |
|
36 |
queryGen: should return a Query object |
|
37 |
last: used to .filter() for last_modified_on |
|
38 |
batchSize: how many entities to retrieve in one datastore call |
|
39 |
||
40 |
Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook). |
|
41 |
""" |
|
42 |
||
43 |
from google.appengine.ext import db |
|
44 |
||
45 |
# AppEngine will not fetch more than 1000 results |
|
46 |
batchSize = min(batchSize,1000) |
|
47 |
||
48 |
query = None |
|
49 |
done = False |
|
50 |
count = 0 |
|
51 |
||
52 |
while not done: |
|
53 |
print count |
|
54 |
query = queryGen() |
|
55 |
query.order('last_modified_on') |
|
56 |
if last: |
|
57 |
query.filter("last_modified_on > ", last) |
|
58 |
results = query.fetch(batchSize) |
|
59 |
for result in results: |
|
60 |
count += 1 |
|
61 |
yield result |
|
62 |
if batchSize > len(results): |
|
63 |
done = True |
|
64 |
else: |
|
65 |
last = results[-1].last_modified_on |
|
66 |
||
67 |
||
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
def addKey(target, fieldname): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
"""Adds the key of the specified field. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
result = target.copy() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
result['%s_key' % fieldname] = target[fieldname].key().name() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
return result |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
def getOrgs(): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
"""Returns all orgs as dictionary. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
from soc.models.organization import Organization |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
gen = lambda: Organization.all() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
it = interactive.deepFetch(gen) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
orgs = [(i.key().name(), i) for i in it] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
return dict(orgs) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
|
2052
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
89 |
def getUsers(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
90 |
"""Returns all Users as dictionary. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
91 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
92 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
93 |
from soc.models.user import User |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
94 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
95 |
gen = lambda: User.all() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
96 |
it = interactive.deepFetch(gen) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
97 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
98 |
users = [(i.key().name(), i) for i in it] |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
99 |
return dict(users) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
100 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
101 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
102 |
def getStudents(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
103 |
"""Returns all Students as dictionary. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
104 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
105 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
106 |
from soc.models.student import Student |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
107 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
108 |
gen = lambda: Student.all() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
109 |
it = interactive.deepFetch(gen) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
110 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
111 |
students = [(i.key().name(), i) for i in it] |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
112 |
return dict(students) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
113 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
114 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
115 |
def getMentors(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
116 |
"""Returns all Mentors as dictionary. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
117 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
118 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
119 |
from soc.models.mentor import Mentor |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
120 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
121 |
gen = lambda: Mentor.all() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
122 |
it = interactive.deepFetch(gen) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
123 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
124 |
mentors = [(i.key().name(), i) for i in it] |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
125 |
return dict(mentors) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
126 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
127 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
128 |
def getOrgAdmins(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
129 |
"""Returns all Organization Administrators as dictionary. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
130 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
131 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
132 |
from soc.models.org_admin import OrgAdmin |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
133 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
134 |
gen = lambda: OrgAdmin.all() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
135 |
it = interactive.deepFetch(gen) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
136 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
137 |
orgAdmins = [(i.key().name(), i) for i in it] |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
138 |
return dict(orgAdmins) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
139 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
140 |
|
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
def getProps(): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
"""Returns all proposals as a list of dictionaries. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
key_order = [ |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
'link_id', 'scope_path', 'title', 'abstract', 'content', |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
'additional_info', 'mentor', 'possible_mentors', 'score', |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
'status', 'org'] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
150 |
from soc.models.student_proposal import StudentProposal |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
151 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
gen = lambda: StudentProposal.all() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
153 |
it = interactive.deepFetch(gen) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
proposals = [i.toDict(key_order) for i in it] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
return proposals |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
|
2052
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
160 |
def getStudentProposals(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
161 |
"""Returns all Student Proposals as dictionary. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
162 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
163 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
164 |
from soc.models.student_proposal import StudentProposal |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
165 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
166 |
gen = lambda: StudentProposal.all() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
167 |
it = interactive.deepFetch(gen) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
168 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
169 |
studentProposals = [(i.key().name(), i) for i in it] |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
170 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
171 |
return dict(studentProposals) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
172 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
173 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
174 |
def orgStats(target): |
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
175 |
"""Retrieves org stats. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
176 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
177 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
178 |
from soc.logic import dicts |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
179 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
180 |
target = [addKey(i, 'org') for i in target] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
181 |
grouped = dicts.groupby(target, 'org_key') |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
182 |
popularity = [(k, len(v)) for k,v in grouped.iteritems()] |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
183 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
184 |
return grouped, dict(popularity) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
185 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
186 |
|
2052
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
187 |
def countStudentsWithProposals(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
188 |
"""Retrieves number of Students who have submitted at least one Student Proposal. |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
189 |
""" |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
190 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
191 |
proposals = getStudentProposals() |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
192 |
students = {} |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
193 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
194 |
for proposal_key in proposals.keys(): |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
195 |
students[proposals[proposal_key].scope_path] = True |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
196 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
197 |
return len(students) |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
198 |
|
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
199 |
|
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
200 |
def printPopularity(popularity): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
201 |
"""Prints the popularity for the specified proposals. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
202 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
203 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
204 |
g = operator.itemgetter(1) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
205 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
206 |
for item in sorted(popularity.iteritems(), key=g, reverse=True): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
print "%s: %d" % item |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
208 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
209 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
210 |
def savePopularity(popularities): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
211 |
"""Saves the specified popularities. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
212 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
214 |
import logging |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
215 |
from google.appengine.ext import db |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
216 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
217 |
from soc.models.organization import Organization |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
218 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
219 |
def txn(key_name, popularity): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
220 |
org = Organization.get_by_key_name(key_name) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
221 |
org.nr_applications = popularity |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
222 |
org.put() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
223 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
for key, value in sorted(popularities.iteritems()): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
225 |
print key |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
226 |
db.run_in_transaction_custom_retries(10, txn, key, value) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
227 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
228 |
print "done" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
229 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
230 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
231 |
def loadPickle(name): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
232 |
"""Loads a pickle. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
233 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
234 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
235 |
f = open(name + '.dat') |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
236 |
return cPickle.load(f) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
237 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
238 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
239 |
def dumpPickle(target, name): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
240 |
"""Dumps a pickle" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
241 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
242 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
243 |
f = open("%s.dat" % name, 'w') |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
244 |
cPickle.dump(target, f) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
245 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
246 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
247 |
def main(args): |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
248 |
"""Main routine. |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
249 |
""" |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
250 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
251 |
interactive.setup() |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
252 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
253 |
context = { |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
254 |
'load': loadPickle, |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
255 |
'dump': dumpPickle, |
2052
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
256 |
'orgStats': orgStats, |
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
257 |
'printPopularity': printPopularity, |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
258 |
'savePopularity': savePopularity, |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
259 |
'getOrgs': getOrgs, |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
260 |
'getProps': getProps, |
2052
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
261 |
'getStudents': getStudents, |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
262 |
'getMentors': getMentors, |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
263 |
'getOrgAdmins': getOrgAdmins, |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
264 |
'getUsers': getUsers, |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
265 |
'getStudentProposals': getStudentProposals, |
a723a2509e21
Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2047
diff
changeset
|
266 |
'countStudentsWithProposals': countStudentsWithProposals |
2047
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
267 |
} |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
268 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
269 |
interactive.remote(args, context) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
270 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
271 |
if __name__ == '__main__': |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
272 |
if len(sys.argv) < 2: |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
273 |
print "Usage: %s app_id [host]" % (sys.argv[0],) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
274 |
sys.exit(1) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
275 |
|
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
276 |
main(sys.argv[1:]) |
7e9656691c8e
Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
277 |