def warm_app(id, short_name, featured=False): if id not in apps_cached: cached_apps.get_app(short_name) cached_apps.n_tasks(id) n_task_runs = cached_apps.n_task_runs(id) cached_apps.overall_progress(id) cached_apps.last_activity(id) cached_apps.n_completed_tasks(id) cached_apps.n_volunteers(id) if n_task_runs >= 1000 or featured: print "Getting stats for %s as it has %s task runs" % (short_name, n_task_runs) stats.get_stats(id, app.config.get('GEO')) apps_cached.append(id)
def warm_app(id, short_name, featured=False): if id not in apps_cached: cached_apps.get_app(short_name) cached_apps.n_tasks(id) n_task_runs = cached_apps.n_task_runs(id) cached_apps.overall_progress(id) cached_apps.last_activity(id) cached_apps.n_completed_tasks(id) cached_apps.n_volunteers(id) if n_task_runs >= 1000 or featured: print("Getting stats for %s as it has %s task runs" % (short_name, n_task_runs)) stats.get_stats(id) apps_cached.append(id)
def warm_project(_id, short_name, featured=False): if _id not in projects_cached: cached_projects.get_project(short_name) cached_projects.n_tasks(_id) n_task_runs = cached_projects.n_task_runs(_id) cached_projects.overall_progress(_id) cached_projects.last_activity(_id) cached_projects.n_completed_tasks(_id) cached_projects.n_volunteers(_id) if n_task_runs >= 1000 or featured: # print ("Getting stats for %s as it has %s task runs" % # (short_name, n_task_runs)) stats.get_stats(_id, app.config.get('GEO')) projects_cached.append(_id)
def get_project_stats(_id, short_name): # pragma: no cover """Get stats for project.""" import pybossa.cache.projects as cached_projects import pybossa.cache.project_stats as stats from flask import current_app cached_projects.get_project(short_name) cached_projects.n_tasks(_id) cached_projects.n_task_runs(_id) cached_projects.overall_progress(_id) cached_projects.last_activity(_id) cached_projects.n_completed_tasks(_id) cached_projects.n_volunteers(_id) stats.get_stats(_id, current_app.config.get('GEO'))
def update_stats(project_id, period='2 week'): """Update the stats of a given project.""" hours, hours_anon, hours_auth, max_hours, \ max_hours_anon, max_hours_auth = stats_hours(project_id, period) users, anon_users, auth_users = stats_users(project_id, period) dates, dates_anon, dates_auth = stats_dates(project_id, period) sum(dates.values()) sorted(dates.iteritems(), key=operator.itemgetter(0)) dates_stats = stats_format_dates(project_id, dates, dates_anon, dates_auth) hours_stats = stats_format_hours(project_id, hours, hours_anon, hours_auth, max_hours, max_hours_anon, max_hours_auth) users_stats = stats_format_users(project_id, users, anon_users, auth_users) data = dict(dates_stats=dates_stats, hours_stats=hours_stats, users_stats=users_stats) ps = session.query(ProjectStats).filter_by(project_id=project_id).first() n_tasks = cached_projects.n_tasks(project_id) n_task_runs = cached_projects.n_task_runs(project_id) n_results = cached_projects.n_results(project_id) overall_progress = cached_projects.overall_progress(project_id) last_activity = cached_projects.last_activity(project_id) n_volunteers = cached_projects.n_volunteers(project_id) n_completed_tasks = cached_projects.n_completed_tasks(project_id) average_time = cached_projects.average_contribution_time(project_id) n_blogposts = cached_projects.n_blogposts(project_id) if ps is None: ps = ProjectStats(project_id=project_id, info=data, n_tasks=n_tasks, n_task_runs=n_task_runs, n_results=n_results, n_volunteers=n_volunteers, n_completed_tasks=n_completed_tasks, average_time=average_time, overall_progress=overall_progress, n_blogposts=n_blogposts, last_activity=last_activity) db.session.add(ps) else: ps.info = data ps.n_tasks = n_tasks ps.n_task_runs = n_task_runs ps.overall_progress = overall_progress ps.last_activity = last_activity ps.n_results = n_results ps.n_completed_tasks = n_completed_tasks ps.n_volunteers = n_volunteers ps.average_time = average_time ps.n_blogposts = n_blogposts db.session.commit() return dates_stats, hours_stats, users_stats
def update_stats(project_id, period='2 week'): """Update the stats of a given project.""" hours, hours_anon, hours_auth, max_hours, \ max_hours_anon, max_hours_auth = stats_hours(project_id, period) users, anon_users, auth_users = stats_users(project_id, period) dates, dates_anon, dates_auth = stats_dates(project_id, period) sum(dates.values()) sorted(iter(dates.items()), key=operator.itemgetter(0)) dates_stats = stats_format_dates(project_id, dates, dates_anon, dates_auth) hours_stats = stats_format_hours(project_id, hours, hours_anon, hours_auth, max_hours, max_hours_anon, max_hours_auth) users_stats = stats_format_users(project_id, users, anon_users, auth_users) data = dict(dates_stats=dates_stats, hours_stats=hours_stats, users_stats=users_stats) ps = session.query(ProjectStats).filter_by(project_id=project_id).first() n_tasks = cached_projects.n_tasks(project_id) n_task_runs = cached_projects.n_task_runs(project_id) n_results = cached_projects.n_results(project_id) overall_progress = cached_projects.overall_progress(project_id) last_activity = cached_projects.last_activity(project_id) n_volunteers = cached_projects.n_volunteers(project_id) n_completed_tasks = cached_projects.n_completed_tasks(project_id) average_time = cached_projects.average_contribution_time(project_id) n_blogposts = cached_projects.n_blogposts(project_id) if ps is None: ps = ProjectStats(project_id=project_id, info=data, n_tasks=n_tasks, n_task_runs=n_task_runs, n_results=n_results, n_volunteers=n_volunteers, n_completed_tasks=n_completed_tasks, average_time=average_time, overall_progress=overall_progress, n_blogposts=n_blogposts, last_activity=last_activity) db.session.add(ps) else: ps.info = data ps.n_tasks = n_tasks ps.n_task_runs = n_task_runs ps.overall_progress = overall_progress ps.last_activity = last_activity ps.n_results = n_results ps.n_completed_tasks = n_completed_tasks ps.n_volunteers = n_volunteers ps.average_time = average_time ps.n_blogposts = n_blogposts db.session.commit() return dates_stats, hours_stats, users_stats
def test_n_completed_tasks_no_completed_tasks(self): """Test CACHE PROJECTS n_completed_tasks returns 0 if no completed tasks""" project = self.create_project_with_tasks(completed_tasks=0, ongoing_tasks=5) completed_tasks = cached_projects.n_completed_tasks(project.id) err_msg = "Completed tasks is %s, it should be 0" % completed_tasks assert completed_tasks == 0, err_msg
def test_n_completed_tasks_with_all_tasks_completed(self): """Test CACHE PROJECTS n_completed_tasks returns number of tasks if all tasks are completed""" project = self.create_project_with_tasks(completed_tasks=4, ongoing_tasks=0) completed_tasks = cached_projects.n_completed_tasks(project.id) err_msg = "Completed tasks is %s, it should be 4" % completed_tasks assert completed_tasks == 4, err_msg
def test_n_completed_tasks_with_completed_tasks(self): """Test CACHE PROJECTS n_completed_tasks returns number of completed tasks if there are any""" project = self.create_project_with_tasks(completed_tasks=5, ongoing_tasks=5) completed_tasks = cached_projects.n_completed_tasks(project.id) err_msg = "Completed tasks is %s, it should be 5" % completed_tasks assert completed_tasks == 5, err_msg
def user_progress(project_id=None, short_name=None): """API endpoint for user progress. Return a JSON object with four fields regarding the tasks for the user: { 'done': 10, 'total: 100, 'remaining': 90, 'remaining_for_user': 45 } This will mean that the user has done 10% of the available tasks for the project, 90 tasks are yet to be submitted and the user can access 45 of them based on user preferences. """ if current_user.is_anonymous: return abort(401) if project_id or short_name: if short_name: project = project_repo.get_by_shortname(short_name) elif project_id: project = project_repo.get(project_id) if project: # For now, keep this version, but wait until redis cache is # used here for task_runs too query_attrs = dict(project_id=project.id, user_id=current_user.id) guidelines_updated = _guidelines_updated(project.id, current_user.id) taskrun_count = task_repo.count_task_runs_with(**query_attrs) num_available_tasks = n_available_tasks(project.id, include_gold_task=True) num_available_tasks_for_user = n_available_tasks_for_user( project, current_user.id) response = dict(done=taskrun_count, total=n_tasks(project.id), completed=n_completed_tasks(project.id), remaining=num_available_tasks, locked=len({ task["task_id"] for task in get_locked_tasks(project) }), remaining_for_user=num_available_tasks_for_user, quiz=current_user.get_quiz_for_project(project), guidelines_updated=guidelines_updated) if current_user.admin or (current_user.subadmin and current_user.id in project.owners_ids): num_gold_tasks = n_unexpired_gold_tasks(project.id) response['available_gold_tasks'] = num_gold_tasks return Response(json.dumps(response), mimetype="application/json") else: return abort(404) else: # pragma: no cover return abort(404)
def flush_task_runs(project_short_name, confirmed): project = cached_projects.get_project(project_short_name) if current_user.admin or project.owner_id == current_user.id: if confirmed == "confirmed": associated_task_runs = TaskRun.query.filter_by(project_id=project.id).all() for task_run in associated_task_runs: db.session.delete(task_run) pass db.session.commit() # Iterate over all tasks associated with the project, and mark them as 'ongoing' # Some tasks might be marked as 'completed' if enough task_runs were done associated_tasks = Task.query.filter_by(project_id=project.id).all() for task in associated_tasks: if task.state != u"ongoing": task.state = u"ongoing" db.session.commit() # Reset project data in the cache cached_projects.clean_project(project.id) # Note: The cache will hold the old data about the users who contributed # to the tasks associated with this projects till the User Cache Timeout. # Querying the list of contributors to this project, and then individually updating # their cache after that will be a very expensive query, hence we will avoid that # for the time being. flash('All Task Runs associated with this project have been successfully deleted.', 'success') return redirect(url_for('project.task_settings', short_name = project_short_name)) elif confirmed == "unconfirmed": # Obtain data required by the project profile renderer (project, owner, n_tasks, n_task_runs, overall_progress, last_activity, n_results) = projects_view.project_by_shortname(project_short_name) return render_template('geotagx/projects/delete_task_run_confirmation.html', project=project, owner=owner, n_tasks=n_tasks, n_task_runs=n_task_runs, overall_progress=overall_progress, last_activity=last_activity, n_results=n_results, n_completed_tasks=cached_projects.n_completed_tasks(project.id), n_volunteers=cached_projects.n_volunteers(project.id)) else: abort(404) else: abort(404)
def visualize(short_name, task_id): """Return a file with all the TaskRuns for a given Task""" # Check if it a supported geotagx project whose schema we know if 'GEOTAGX_SUPPORTED_PROJECTS_SCHEMA' in current_app.config.keys() \ and short_name in current_app.config['GEOTAGX_SUPPORTED_PROJECTS_SCHEMA'].keys(): # Check if the project exists (project, owner, n_tasks, n_task_runs, overall_progress, last_activity) = projects_view.project_by_shortname(short_name) ensure_authorized_to('read', project) redirect_to_password = projects_view._check_if_redirect_to_password(project) if redirect_to_password: return redirect_to_password # Check if the task belongs to the project and exists task = task_repo.get_task_by(project_id=project.id, id=task_id) if task: taskruns = task_repo.filter_task_runs_by(task_id=task_id, project_id=project.id) results = [tr.dictize() for tr in taskruns] return render_template('geotagx/projects/task_runs_visualize.html', project=project, owner=owner, n_tasks=n_tasks, n_task_runs=n_task_runs, overall_progress=overall_progress, last_activity=last_activity, n_completed_tasks=cached_projects.n_completed_tasks(project.id), n_volunteers=cached_projects.n_volunteers(project.id), task_info = task.info, task_runs_json = results, geotagx_project_template_schema = \ current_app.config['GEOTAGX_SUPPORTED_PROJECTS_SCHEMA'][short_name]) else: return abort(404) else: return abort(404)