Commit 368d79e5829f9ac470a9f1f7a57902830e3ecea9
1 parent
fef2e2fe
Exists in
master
Adicionado um método na API para recuperar as últimas respostas de um projeto.
Showing
4 changed files
with
48 additions
and
1 deletions
Show diff stats
pybossa/api/__init__.py
@@ -53,6 +53,7 @@ from result import ResultAPI | @@ -53,6 +53,7 @@ from result import ResultAPI | ||
53 | from pybossa.core import project_repo, task_repo | 53 | from pybossa.core import project_repo, task_repo |
54 | from pybossa.contributions_guard import ContributionsGuard | 54 | from pybossa.contributions_guard import ContributionsGuard |
55 | from pybossa.cache import users as cached_users | 55 | from pybossa.cache import users as cached_users |
56 | +from pybossa.cache import projects as cached_projects | ||
56 | 57 | ||
57 | blueprint = Blueprint('api', __name__) | 58 | blueprint = Blueprint('api', __name__) |
58 | 59 | ||
@@ -188,8 +189,18 @@ def user_progress(project_id=None, short_name=None): | @@ -188,8 +189,18 @@ def user_progress(project_id=None, short_name=None): | ||
188 | @blueprint.route('/leaderboard') | 189 | @blueprint.route('/leaderboard') |
189 | @crossdomain(origin='*', headers=cors_headers) | 190 | @crossdomain(origin='*', headers=cors_headers) |
190 | @ratelimit(limit=ratelimits.get('LIMIT'), per=ratelimits.get('PER')) | 191 | @ratelimit(limit=ratelimits.get('LIMIT'), per=ratelimits.get('PER')) |
191 | -def leaderboard(limit=None): | 192 | +def leaderboard(): |
192 | user_id = None if current_user.is_anonymous() else current_user.id | 193 | user_id = None if current_user.is_anonymous() else current_user.id |
193 | limit = request.args.get('limit') | 194 | limit = request.args.get('limit') |
194 | data = cached_users.get_complete_leaderboard(n=limit, user_id=user_id) | 195 | data = cached_users.get_complete_leaderboard(n=limit, user_id=user_id) |
195 | return Response(json.dumps(data), mimetype="application/json") | 196 | return Response(json.dumps(data), mimetype="application/json") |
197 | + | ||
198 | +@jsonpify | ||
199 | +@blueprint.route('/project_last_answers') | ||
200 | +@crossdomain(origin='*', headers=cors_headers) | ||
201 | +@ratelimit(limit=ratelimits.get('LIMIT'), per=ratelimits.get('PER')) | ||
202 | +def project_last_answers(): | ||
203 | + user_id = None if current_user.is_anonymous() else current_user.id | ||
204 | + project_id = request.args.get('project_id') | ||
205 | + data = cached_projects.project_last_answers(project_id) | ||
206 | + return Response(json.dumps(data), mimetype="application/json") |
pybossa/cache/projects.py
@@ -204,6 +204,39 @@ def last_activity(project_id): | @@ -204,6 +204,39 @@ def last_activity(project_id): | ||
204 | return None | 204 | return None |
205 | 205 | ||
206 | 206 | ||
207 | +def project_last_answers_data(project_id): | ||
208 | + """Return last answers from a project.""" | ||
209 | + sql = text(''' | ||
210 | + WITH last_answers AS (SELECT task_id, MAX(finish_time) AS timestamp | ||
211 | + FROM task_run WHERE project_id =:project_id GROUP BY task_id) | ||
212 | + SELECT task_run.info AS taskrun_info, task.info AS task_info FROM task_run | ||
213 | + JOIN last_answers ON task_run.task_id = last_answers.task_id AND finish_time = timestamp | ||
214 | + JOIN task ON task_run.task_id = task.id''') | ||
215 | + | ||
216 | + results = session.execute(sql, dict(project_id=project_id)) | ||
217 | + last_answers = [] | ||
218 | + for row in results: | ||
219 | + answer = dict( | ||
220 | + taskrun_info=row.taskrun_info, | ||
221 | + task_info=row.task_info) | ||
222 | + last_answers.append(answer) | ||
223 | + return last_answers | ||
224 | + | ||
225 | +@memoize(timeout=timeouts.get('PROJECT_LAST_ANSWERS_TIMEOUT')) | ||
226 | +def project_last_answers(project_id): | ||
227 | + projects = [] | ||
228 | + if project_id is None: | ||
229 | + projects = session.query(Project).all() | ||
230 | + else: | ||
231 | + projects.append(session.query(Project).get(project_id)) | ||
232 | + results = [] | ||
233 | + for project in projects: | ||
234 | + last_answers = dict() | ||
235 | + last_answers['project_name'] = project.short_name | ||
236 | + last_answers['last_answers'] = project_last_answers_data(project.id) | ||
237 | + result.append(last_answers) | ||
238 | + return results | ||
239 | + | ||
207 | @memoize(timeout=timeouts.get('APP_TIMEOUT')) | 240 | @memoize(timeout=timeouts.get('APP_TIMEOUT')) |
208 | def average_contribution_time(project_id): | 241 | def average_contribution_time(project_id): |
209 | sql = text('''SELECT | 242 | sql = text('''SELECT |
pybossa/core.py
@@ -567,6 +567,8 @@ def setup_cache_timeouts(app): | @@ -567,6 +567,8 @@ def setup_cache_timeouts(app): | ||
567 | timeouts['STATS_DRAFT_TIMEOUT'] = app.config['STATS_DRAFT_TIMEOUT'] | 567 | timeouts['STATS_DRAFT_TIMEOUT'] = app.config['STATS_DRAFT_TIMEOUT'] |
568 | timeouts['N_APPS_PER_CATEGORY_TIMEOUT'] = \ | 568 | timeouts['N_APPS_PER_CATEGORY_TIMEOUT'] = \ |
569 | app.config['N_APPS_PER_CATEGORY_TIMEOUT'] | 569 | app.config['N_APPS_PER_CATEGORY_TIMEOUT'] |
570 | + timeouts['PROJECT_LAST_ANSWERS_TIMEOUT'] = \ | ||
571 | + app.config['PROJECT_LAST_ANSWERS_TIMEOUT'] | ||
570 | # Categories | 572 | # Categories |
571 | timeouts['CATEGORY_TIMEOUT'] = app.config['CATEGORY_TIMEOUT'] | 573 | timeouts['CATEGORY_TIMEOUT'] = app.config['CATEGORY_TIMEOUT'] |
572 | # Users | 574 | # Users |
pybossa/default_settings.py
@@ -75,6 +75,7 @@ STATS_APP_TIMEOUT = 12 * 60 * 60 | @@ -75,6 +75,7 @@ STATS_APP_TIMEOUT = 12 * 60 * 60 | ||
75 | STATS_DRAFT_TIMEOUT = 24 * 60 * 60 | 75 | STATS_DRAFT_TIMEOUT = 24 * 60 * 60 |
76 | N_APPS_PER_CATEGORY_TIMEOUT = 60 * 60 | 76 | N_APPS_PER_CATEGORY_TIMEOUT = 60 * 60 |
77 | BROWSE_TASKS_TIMEOUT = 3 * 60 * 60 | 77 | BROWSE_TASKS_TIMEOUT = 3 * 60 * 60 |
78 | +PROJECT_LAST_ANSWERS_TIMEOUT = 5 * 60 | ||
78 | # Category cache | 79 | # Category cache |
79 | CATEGORY_TIMEOUT = 24 * 60 * 60 | 80 | CATEGORY_TIMEOUT = 24 * 60 * 60 |
80 | # User cache | 81 | # User cache |