def home(): """Render home page with the cached projects and users.""" page = 1 per_page = current_app.config.get('APPS_PER_PAGE') if per_page is None: # pragma: no cover per_page = 5 d = {'top_projects': cached_projects.get_top(), 'top_users': None} # Get all the categories with projects categories = cached_cat.get_used() d['categories'] = categories d['categories_projects'] = {} for c in categories: tmp_projects = cached_projects.get(c['short_name'], page, per_page) d['categories_projects'][c['short_name']] = rank(tmp_projects) # Add featured tmp_projects = cached_projects.get_featured('featured', page, per_page) if len(tmp_projects) > 0: featured = Category(name='Featured', short_name='featured') d['categories'].insert(0, featured) d['categories_projects']['featured'] = rank(tmp_projects) if (current_app.config['ENFORCE_PRIVACY'] and current_user.is_authenticated()): if current_user.admin: d['top_users'] = cached_users.get_leaderboard(10) if not current_app.config['ENFORCE_PRIVACY']: d['top_users'] = cached_users.get_leaderboard(10) return render_template('/home/index.html', **d)
def home(): """Render home page with the cached projects and users.""" page = 1 per_page = current_app.config.get('APPS_PER_PAGE') if per_page is None: # pragma: no cover per_page = 5 d = {'top_projects': cached_projects.get_top(), 'top_users': None} # Get all the categories with projects categories = cached_cat.get_used() d['categories'] = categories d['categories_projects'] = {} for c in categories: tmp_projects = cached_projects.get(c['short_name'], page, per_page) d['categories_projects'][c['short_name']] = rank(tmp_projects) # Add featured tmp_projects = cached_projects.get_featured('featured', page, per_page) if len(tmp_projects) > 0: featured = Category(name='Featured', short_name='featured') d['categories'].insert(0, featured) d['categories_projects']['featured'] = rank(tmp_projects) if (current_app.config['ENFORCE_PRIVACY'] and current_user.is_authenticated()): if current_user.admin: d['top_users'] = cached_users.get_leaderboard(10) if not current_app.config['ENFORCE_PRIVACY']: d['top_users'] = cached_users.get_leaderboard(10) response = dict(template='/home/index.html', **d) return handle_content_type(response)
def warm_cache(): # pragma: no cover """Background job to warm cache.""" from pybossa.core import create_app app = create_app(run_as_server=False) # Cache 3 pages apps_cached = [] pages = range(1, 4) import pybossa.cache.apps as cached_apps import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.cache.project_stats as stats 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) # Cache top projects apps = cached_apps.get_top() for a in apps: warm_app(a['id'], a['short_name']) for page in pages: apps = cached_apps.get_featured('featured', page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name'], featured=True) # Categories categories = cached_cat.get_used() for c in categories: for page in pages: apps = cached_apps.get(c['short_name'], page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name']) # Users cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous') cached_users.get_top() return True
def warm_cache(): '''Warm cache''' # Disable cache, so we can refresh the data in Redis os.environ['PYBOSSA_REDIS_CACHE_DISABLED'] = '1' # Cache 3 pages apps_cached = [] pages = range(1, 4) with app.app_context(): import pybossa.cache.projects as cached_apps import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.cache.project_stats as stats 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) # Cache top projects apps = cached_apps.get_top() for a in apps: warm_app(a['id'], a['short_name']) for page in pages: apps = cached_apps.get_featured('featured', page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name'], featured=True) # Categories categories = cached_cat.get_used() for c in categories: for page in pages: apps = cached_apps.get(c['short_name'], page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name']) # Users cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous') cached_users.get_top()
def warm_cache(): '''Warm cache''' # Disable cache, so we can refresh the data in Redis os.environ['PYBOSSA_REDIS_CACHE_DISABLED'] = '1' # Cache 3 pages apps_cached = [] pages = range(1, 4) with app.app_context(): import pybossa.cache.projects as cached_apps import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.cache.project_stats as stats 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) # Cache top projects apps = cached_apps.get_top() for a in apps: warm_app(a['id'], a['short_name']) for page in pages: apps = cached_apps.get_featured('featured', page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name'], featured=True) # Categories categories = cached_cat.get_used() for c in categories: for page in pages: apps = cached_apps.get(c['short_name'], page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name']) # Users cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous') cached_users.get_top()
def index(page): """ Index page for all PyBossa registered users. Returns a Jinja2 rendered template with the users. """ update_feed = get_update_feed() per_page = 24 count = cached_users.get_total_users() accounts = cached_users.get_users_page(page, per_page) if not accounts and page != 1: abort(404) pagination = Pagination(page, per_page, count) if current_user.is_authenticated(): user_id = current_user.id else: user_id = None top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id) return render_template('account/index.html', accounts=accounts, total=count, top_users=top_users, title="Community", pagination=pagination, update_feed=update_feed)
def index(window=0): """Get the last activity from users and projects.""" if current_user.is_authenticated(): user_id = current_user.id else: user_id = None if window >= 10: window = 10 info = request.args.get('info') leaderboards = current_app.config.get('LEADERBOARDS') if info is not None: if leaderboards is None or info not in leaderboards: return abort(404) top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id=user_id, window=window, info=info) response = dict(template='/stats/index.html', title="Community Leaderboard", top_users=top_users) return handle_content_type(response)
def test_get_leaderboard_no_users_returns_empty_list(self): """Test CACHE USERS get_leaderboard returns an empty list if there are no users""" users = cached_users.get_leaderboard(10) assert users == [], users
def index(page): """ Index page for all PyBossa registered users. Returns a Jinja2 rendered template with the users. """ update_feed = get_update_feed() per_page = 24 count = cached_users.get_total_users() accounts = cached_users.get_users_page(page, per_page) if not accounts and page != 1: abort(404) pagination = Pagination(page, per_page, count) if current_user.is_authenticated(): user_id = current_user.id else: user_id = 'anonymous' top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id) return render_template('account/index.html', accounts=accounts, total=count, top_users=top_users, title="Community", pagination=pagination, update_feed=update_feed)
def index(): """Return Global Statistics for the site.""" title = "Statistics" description = """Statistical analysis of contributions made via the LibCrowds crowdsourcing platform.""" # User stats n_anon = extra_stats.n_anon_users() n_auth = site_stats.n_auth_users() top_5_users_1_week = extra_stats.get_top_n_users_k_days(5, 7) n_tr_top_10_percent = extra_stats.get_top_n_percent(10) users_daily = extra_stats.get_users_daily() leaderboard = cached_users.get_leaderboard(10) n_avg_days_active = extra_stats.n_avg_days_active() # Task stats n_tasks = site_stats.n_tasks_site() n_task_runs = site_stats.n_task_runs_site() n_auth_task_runs = extra_stats.n_auth_task_runs_site() n_tasks_completed = extra_stats.n_tasks_completed() task_runs_daily = extra_stats.get_task_runs_daily() dow = extra_stats.get_dow() hourly_activity = extra_stats.site_hourly_activity() # Project stats n_published_projects = cached_projects.n_published() top_5_pr_1_week = extra_stats.get_top_n_projects_k_days(5, 7) # Location stats locs = extra_stats.get_locations() n_continents = extra_stats.n_continents() n_cities = extra_stats.n_cities() n_countries = extra_stats.n_countries() top_countries = extra_stats.get_top_n_countries() stats = dict(n_auth=n_auth, n_anon=n_anon, n_published_projects=n_published_projects, n_tasks=n_tasks, n_task_runs=n_task_runs, n_auth_task_runs=n_auth_task_runs, n_tasks_completed=n_tasks_completed, n_countries=n_countries, n_cities=n_cities, n_continents=n_continents, n_avg_days_active=n_avg_days_active, n_tr_top_10_percent=n_tr_top_10_percent) return render_template('/stats.html', title=title, description=description, stats=json.dumps(stats), locs=json.dumps(locs), users_daily=json.dumps(users_daily), task_runs_daily=json.dumps(task_runs_daily), top_countries=json.dumps(top_countries), top_5_pr_1_week=json.dumps(top_5_pr_1_week), top_5_users_1_week=json.dumps(top_5_users_1_week), hourly_activity=json.dumps(hourly_activity), dow=json.dumps(dow), leaderboard=json.dumps(leaderboard))
def index(page=1): """Index page for all PYBOSSA registered users.""" update_feed = get_update_feed() per_page = 24 count = cached_users.get_total_users() print(count) accounts = cached_users.get_users_page(page, per_page) print(accounts) if not accounts and page != 1: abort(404) pagination = Pagination(page, per_page, count) if current_user.is_authenticated: user_id = current_user.id else: user_id = None top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id) tmp = dict(template='account/index.html', accounts=accounts, total=count, top_users=top_users, title="Community", pagination=pagination, update_feed=update_feed) return handle_content_type(tmp)
def warm_cache(): # pragma: no cover """Background job to warm cache.""" from pybossa.core import create_app app = create_app(run_as_server=False) # Cache 3 pages projects_cached = [] pages = range(1, 4) import pybossa.cache.projects as cached_projects import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.cache.project_stats as stats 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) # Cache top projects projects = cached_projects.get_top() for p in projects: warm_project(p['id'], p['short_name']) for page in pages: projects = cached_projects.get_featured('featured', page, app.config['APPS_PER_PAGE']) for p in projects: warm_project(p['id'], p['short_name'], featured=True) # Categories categories = cached_cat.get_used() for c in categories: for page in pages: projects = cached_projects.get(c['short_name'], page, app.config['APPS_PER_PAGE']) for p in projects: warm_project(p['id'], p['short_name']) # Users users = cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous') for user in users: # print "Getting stats for %s" % user['name'] cached_users.get_user_summary(user['name']) cached_users.projects_contributed_cached(user['id']) cached_users.published_projects_cached(user['id']) cached_users.draft_projects_cached(user['id']) cached_users.get_top() return True
def warm_cache(): # pragma: no cover """Background job to warm cache.""" from pybossa.core import create_app app = create_app(run_as_server=False) projects_cached = [] import pybossa.cache.projects as cached_projects import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.cache.project_stats as stats from pybossa.util import rank from pybossa.core import user_repo 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) #cached_projects.browse_tasks(_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.update_stats(_id, app.config.get('GEO')) projects_cached.append(_id) # Cache top projects projects = cached_projects.get_top() for p in projects: warm_project(p['id'], p['short_name']) # Cache 3 pages to_cache = 3 * app.config['APPS_PER_PAGE'] projects = rank(cached_projects.get_all_featured('featured'))[:to_cache] for p in projects: warm_project(p['id'], p['short_name'], featured=True) # Categories categories = cached_cat.get_used() for c in categories: projects = rank(cached_projects.get_all(c['short_name']))[:to_cache] for p in projects: warm_project(p['id'], p['short_name']) # Users users = cached_users.get_leaderboard(app.config['LEADERBOARD']) for user in users: # print "Getting stats for %s" % user['name'] print user_repo u = user_repo.get_by_name(user['name']) cached_users.get_user_summary(user['name']) cached_users.projects_contributed_cached(u.id) cached_users.published_projects_cached(u.id) cached_users.draft_projects_cached(u.id) return True
def render_leaderboard(): """Renders the leaderboard page. Returns: unicode: The page's rendered HTML. """ user_id = current_user.id if current_user.is_authenticated() else None users = cached_users.get_leaderboard(current_app.config["LEADERBOARD"], user_id=user_id) return render_template("/community/leaderboard.html", users=users)
def test_get_leaderboard_returns_fields(self): """Test CACHE USERS get_leaderboard returns user fields""" user = UserFactory.create() TaskRunFactory.create(user=user) fields = User.public_attributes() leaderboard = cached_users.get_leaderboard(1) for field in fields: assert field in leaderboard[0].keys(), field assert len(leaderboard[0].keys()) == len(fields)
def index(): """Get the last activity from users and projects.""" if current_user.is_authenticated(): user_id = current_user.id else: user_id = None top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id=user_id) return render_template('/stats/index.html', title="Community Leaderboard", top_users=top_users)
def test_get_leaderboard_returns_fields(self): """Test CACHE USERS get_leaderboard returns user fields""" user = UserFactory.create() TaskRunFactory.create(user=user) fields = ('rank', 'id', 'name', 'fullname', 'email_addr', 'info', 'created', 'score') leaderboard = cached_users.get_leaderboard(1) for field in fields: assert field in leaderboard[0].keys(), field assert len(leaderboard[0].keys()) == len(fields)
def test_get_leaderboard_returns_fields(self): """Test CACHE USERS get_leaderboard returns user fields""" user = UserFactory.create() TaskRunFactory.create(user=user) fields = User.public_attributes() update_leaderboard() leaderboard = cached_users.get_leaderboard(1) for field in fields: assert field in leaderboard[0].keys(), field assert len(leaderboard[0].keys()) == len(fields)
def test_get_leaderboard_returns_users_ordered_by_rank(self): leader = UserFactory.create() second = UserFactory.create() third = UserFactory.create() project = ProjectFactory.create() tasks = TaskFactory.create_batch(3, project=project) i = 3 for user in [leader, second, third]: TaskRunFactory.create_batch(i, user=user, task=tasks[i - 1]) i -= 1 leaderboard = cached_users.get_leaderboard(3) assert leaderboard[0]['id'] == leader.id assert leaderboard[1]['id'] == second.id assert leaderboard[2]['id'] == third.id
def test_get_leaderboard_returns_users_ordered_by_rank(self): leader = UserFactory.create() second = UserFactory.create() third = UserFactory.create() project = ProjectFactory.create() tasks = TaskFactory.create_batch(3, project=project) i = 3 for user in [leader, second, third]: TaskRunFactory.create_batch(i, user=user, task=tasks[i-1]) i -= 1 leaderboard = cached_users.get_leaderboard(3) assert leaderboard[0]['id'] == leader.id assert leaderboard[1]['id'] == second.id assert leaderboard[2]['id'] == third.id
def test_get_leaderboard_includes_specific_user_even_is_not_in_top(self): leader = UserFactory.create() second = UserFactory.create() third = UserFactory.create() project = ProjectFactory.create() tasks = TaskFactory.create_batch(3, project=project) i = 3 for user in [leader, second, third]: TaskRunFactory.create_batch(i, user=user, task=tasks[i-1]) i -= 1 user_out_of_top = UserFactory.create() leaderboard = cached_users.get_leaderboard(3, user_id=user_out_of_top.id) assert len(leaderboard) is 4 assert leaderboard[-1]['id'] == user_out_of_top.id
def test_get_leaderboard_includes_specific_user_even_is_not_in_top(self): leader = UserFactory.create() second = UserFactory.create() third = UserFactory.create() project = ProjectFactory.create() tasks = TaskFactory.create_batch(3, project=project) i = 3 for user in [leader, second, third]: TaskRunFactory.create_batch(i, user=user, task=tasks[i - 1]) i -= 1 user_out_of_top = UserFactory.create() leaderboard = cached_users.get_leaderboard(3, user_id=user_out_of_top.id) assert len(leaderboard) is 4 assert leaderboard[-1]['id'] == user_out_of_top.id
def index(window=0): """Get the last activity from users and projects.""" if current_user.is_authenticated(): user_id = current_user.id else: user_id = None if window >= 10: window = 10 top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id=user_id, window=window) response = dict(template='/stats/index.html', title="Community Leaderboard", top_users=top_users) return handle_content_type(response)
def index(page=1): """Index page for all PYBOSSA registered users.""" update_feed = get_update_feed() per_page = 24 count = cached_users.get_total_users() accounts = cached_users.get_users_page(page, per_page) if not accounts and page != 1: abort(404) pagination = Pagination(page, per_page, count) if current_user.is_authenticated(): user_id = current_user.id else: user_id = None top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id) tmp = dict(template='account/index.html', accounts=accounts, total=count, top_users=top_users, title="Community", pagination=pagination, update_feed=update_feed) return handle_content_type(tmp)
def index(window=0): """Get the last activity from users and projects.""" if current_user.is_authenticated: user_id = current_user.id rank_and_score = cached_users.rank_and_score(user_id) current_user.rank = rank_and_score['rank'] else: user_id = None if window >= 10: window = 10 info = request.args.get('info') leaderboards = current_app.config.get('LEADERBOARDS') if info is not None: if leaderboards is None or info not in leaderboards: return abort(404) underscore = info.find("_") answer = info[underscore + 1:] dept = info[:underscore] if (current_user.info is None or current_user.info['container'] is None or current_user.info['container'].get(dept) is None or current_user.info['container'][dept] != answer): return abort(401) top_users = cached_users.get_leaderboard(current_app.config['LEADERBOARD'], user_id=user_id, window=window, info=info) response = dict(template='/stats/index.html', title="Community Leaderboard", top_users=top_users) return handle_content_type(response)
def get_top_project_contributors(project_id, limit): user_id = None if current_user.is_anonymous() else current_user.id top_contributors = cached_users.get_leaderboard(limit, user_id, project_id, True) return Response(json.dumps(top_contributors), 200, mimetype='application/json')