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 get_app_stats(id, short_name): # pragma: no cover """Get stats for app.""" import pybossa.cache.apps as cached_apps import pybossa.cache.project_stats as stats from flask import current_app cached_apps.get_app(short_name) cached_apps.n_tasks(id) 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) stats.get_stats(id, current_app.config.get('GEO'))
def warm_app(id, short_name): 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: print "Getting stats for %s as it has %s" % (id, n_task_runs) stats.get_stats(id, app.config.get('GEO')) apps_cached.append(id)
def hidden_apps(user_id): sql = text(''' SELECT app.id, app.name, app.short_name, app.description, app.owner_id, app.info FROM app, task WHERE app.id=task.app_id AND app.owner_id=:user_id AND app.hidden=1 AND app.info LIKE('%task_presenter%') GROUP BY app.id, app.name, app.short_name, app.description, app.info;''') apps_published = [] results = session.execute(sql, dict(user_id=user_id)) for row in results: app = dict(id=row.id, name=row.name, short_name=row.short_name, owner_id=row.owner_id, description=row.description, overall_progress=overall_progress(row.id), n_tasks=n_tasks(row.id), n_volunteers=n_volunteers(row.id), info=json.loads(row.info)) apps_published.append(app) return apps_published
def user_progress(app_id=None, short_name=None): """API endpoint for user progress. Return a JSON object with two fields regarding the tasks for the user: { 'done': 10, 'total: 100 } This will mean that the user has done a 10% of the available tasks for him """ if app_id or short_name: if short_name: app = project_repo.get_by_shortname(short_name) elif app_id: app = project_repo.get(app_id) if app: # For now, keep this version, but wait until redis cache is used here for task_runs too query_attrs = dict(app_id=app.id) if current_user.is_anonymous(): query_attrs['user_ip'] = request.remote_addr or '127.0.0.1' else: query_attrs['user_id'] = current_user.id taskrun_count = task_repo.count_task_runs_with(**query_attrs) tmp = dict(done=taskrun_count, total=n_tasks(app.id)) return Response(json.dumps(tmp), mimetype="application/json") else: return abort(404) else: # pragma: no cover return abort(404)
def apps_contributed(user_id): try: sql = text(''' WITH apps_contributed as (SELECT DISTINCT(app_id) FROM task_run WHERE user_id=:user_id) SELECT app.id, app.name, app.short_name, app.owner_id, app.description, app.info FROM app, apps_contributed WHERE app.id=apps_contributed.app_id ORDER BY app.name DESC; ''') session = get_session(db, bind='slave') results = session.execute(sql, dict(user_id=user_id)) apps_contributed = [] for row in results: app = dict(id=row.id, name=row.name, short_name=row.short_name, owner_id=row.owner_id, description=row.description, overall_progress=overall_progress(row.id), n_tasks=n_tasks(row.id), n_volunteers=n_volunteers(row.id), info=json.loads(row.info)) apps_contributed.append(app) return apps_contributed except: # pragma: no cover session.rollback() finally: session.close()
def hidden_apps(user_id): try: sql = text(''' SELECT app.id, app.name, app.short_name, app.description, app.owner_id, app.info FROM app, task WHERE app.id=task.app_id AND app.owner_id=:user_id AND app.hidden=1 AND app.info LIKE('%task_presenter%') GROUP BY app.id, app.name, app.short_name, app.description, app.info;''') apps_published = [] session = get_session(db, bind='slave') results = session.execute(sql, dict(user_id=user_id)) for row in results: app = dict(id=row.id, name=row.name, short_name=row.short_name, owner_id=row.owner_id, description=row.description, overall_progress=overall_progress(row.id), n_tasks=n_tasks(row.id), n_volunteers=n_volunteers(row.id), info=json.loads(row.info)) apps_published.append(app) return apps_published except: # pragma: no cover session.rollback() raise finally: session.close()
def app_index(page, lookup, category, fallback, use_count): """Show apps of app_type""" per_page = 5 # @FC to come back to project homepage from the application detail if 'cat_byapp_' in category: category = get_app_category(category.replace("cat_byapp_", "")) # @FC apps, count = lookup(category, page, per_page) data = [] #for app in apps: # if 'tutorial' in app['short_name']: # data.append(dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), # overall_progress=cached_apps.overall_progress(app['id']), # last_activity=cached_apps.last_activity(app['id']))) for app in apps: if 'tutorial' not in app['short_name']: data.append(dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), overall_progress=cached_apps.overall_progress(app['id']), last_activity=cached_apps.last_activity(app['id']))) if fallback and not apps: return redirect(url_for('.published')) pagination = Pagination(page, per_page, count) categories = cached_cat.get_all() # Check for pre-defined categories featured and draft featured_cat = model.Category(name='Featured', short_name='featured', description='Featured applications') if category == 'featured': active_cat = featured_cat elif category == 'draft': active_cat = model.Category(name='Draft', short_name='draft', description='Draft applications') else: active_cat = db.session.query(model.Category)\ .filter_by(short_name=category).first() # Check if we have to add the section Featured to local nav if cached_apps.n_featured() > 0: categories.insert(0, featured_cat) template_args = { "apps": data, "title": gettext("Applications"), "pagination": pagination, "active_cat": active_cat, "n_apps_per_category": None, "categories": categories} n_apps_per_category = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count(c.short_name) template_args['n_apps_per_category'] = n_apps_per_category if use_count: template_args.update({"count": count}) return render_template('/applications/index.html', **template_args)
def app_by_shortname(short_name): app = cached_apps.get_app(short_name) if app.id: # Populate CACHE with the data of the app return (app, cached_apps.n_tasks(app.id), cached_apps.n_task_runs(app.id), cached_apps.overall_progress(app.id), cached_apps.last_activity(app.id)) else: return abort(404)
def user_progress(app_id=None, short_name=None): """API endpoint for user progress. Return a JSON object with two fields regarding the tasks for the user: { 'done': 10, 'total: 100 } This will mean that the user has done a 10% of the available tasks for him """ if app_id or short_name: if short_name: app = _retrieve_app(short_name=short_name) elif app_id: app = _retrieve_app(app_id=app_id) if app: try: session = get_session(db, bind='slave') # get done tasks from DB if current_user.is_anonymous(): sql = text( '''SELECT COUNT(task_run.id) AS n_task_runs FROM task_run WHERE task_run.app_id=:app_id AND task_run.user_ip=:user_ip;''') user_ip = request.remote_addr if (user_ip == None): user_ip = '127.0.0.1' # set address to local host for internal tests (see AnonymousTaskRunFactory)! results = session.execute( sql, dict(app_id=app.id, user_ip=user_ip)) else: sql = text( '''SELECT COUNT(task_run.id) AS n_task_runs FROM task_run WHERE task_run.app_id=:app_id AND task_run.user_id=:user_id;''') results = session.execute( sql, dict(app_id=app.id, user_id=current_user.id)) n_task_runs = 0 for row in results: n_task_runs = row.n_task_runs # get total tasks from DB tmp = dict(done=n_task_runs, total=n_tasks(app.id)) return Response(json.dumps(tmp), mimetype="application/json") except: # pragma: no cover session.rollback() raise finally: session.close() else: return abort(404) else: # pragma: no cover return abort(404)
def app_index(page, lookup, category, fallback, use_count): """Show apps of app_type""" per_page = 5 apps, count = lookup(category, page, per_page) data = [] for app in apps: data.append( dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), overall_progress=cached_apps.overall_progress(app['id']), last_activity=app['last_activity'], last_activity_raw=app['last_activity_raw'])) if fallback and not apps: # pragma: no cover return redirect(url_for('.index')) pagination = Pagination(page, per_page, count) categories = cached_cat.get_all() # Check for pre-defined categories featured and draft featured_cat = model.Category(name='Featured', short_name='featured', description='Featured applications') if category == 'featured': active_cat = featured_cat elif category == 'draft': active_cat = model.Category(name='Draft', short_name='draft', description='Draft applications') else: active_cat = db.session.query(model.Category)\ .filter_by(short_name=category).first() # Check if we have to add the section Featured to local nav if cached_apps.n_featured() > 0: categories.insert(0, featured_cat) template_args = { "apps": data, "title": gettext("Applications"), "pagination": pagination, "active_cat": active_cat, "categories": categories } if use_count: template_args.update({"count": count}) return render_template('/applications/index.html', **template_args)
def user_progress(app_id=None, short_name=None): """API endpoint for user progress. Return a JSON object with two fields regarding the tasks for the user: { 'done': 10, 'total: 100 } This will mean that the user has done a 10% of the available tasks for him """ if app_id or short_name: if short_name: app = _retrieve_app(short_name=short_name) elif app_id: app = _retrieve_app(app_id=app_id) if app: try: session = get_session(db, bind='slave') # get done tasks from DB if current_user.is_anonymous(): sql = text('''SELECT COUNT(task_run.id) AS n_task_runs FROM task_run WHERE task_run.app_id=:app_id AND task_run.user_ip=:user_ip;''') user_ip = request.remote_addr if (user_ip == None): user_ip = '127.0.0.1' # set address to local host for internal tests (see AnonymousTaskRunFactory)! results = session.execute(sql, dict(app_id=app.id, user_ip=user_ip)) else: sql = text('''SELECT COUNT(task_run.id) AS n_task_runs FROM task_run WHERE task_run.app_id=:app_id AND task_run.user_id=:user_id;''') results = session.execute(sql, dict(app_id=app.id, user_id=current_user.id)) n_task_runs = 0 for row in results: n_task_runs = row.n_task_runs # get total tasks from DB tmp = dict(done=n_task_runs, total=n_tasks(app.id)) return Response(json.dumps(tmp), mimetype="application/json") except: # pragma: no cover session.rollback() raise finally: session.close() else: return abort(404) else: # pragma: no cover return abort(404)
def app_index(page, lookup, category, fallback, use_count): """Show apps of app_type""" per_page = 5 apps, count = lookup(category, page, per_page) data = [] for app in apps: data.append(dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), overall_progress=cached_apps.overall_progress(app['id']), last_activity=app['last_activity'], last_activity_raw=app['last_activity_raw'])) if fallback and not apps: # pragma: no cover return redirect(url_for('.index')) pagination = Pagination(page, per_page, count) categories = cached_cat.get_all() # Check for pre-defined categories featured and draft featured_cat = model.Category(name='Featured', short_name='featured', description='Featured applications') if category == 'featured': active_cat = featured_cat elif category == 'draft': active_cat = model.Category(name='Draft', short_name='draft', description='Draft applications') else: active_cat = db.session.query(model.Category)\ .filter_by(short_name=category).first() # Check if we have to add the section Featured to local nav if cached_apps.n_featured() > 0: categories.insert(0, featured_cat) template_args = { "apps": data, "title": gettext("Applications"), "pagination": pagination, "active_cat": active_cat, "categories": categories} if use_count: template_args.update({"count": count}) return render_template('/applications/index.html', **template_args)
def apps_contributed(user_id): sql = text(''' WITH apps_contributed as (SELECT DISTINCT(app_id) FROM task_run WHERE user_id=:user_id) SELECT app.id, app.name, app.short_name, app.owner_id, app.description, app.info FROM app, apps_contributed WHERE app.id=apps_contributed.app_id ORDER BY app.name DESC; ''') results = db.slave_session.execute(sql, dict(user_id=user_id)) apps_contributed = [] for row in results: app = dict(id=row.id, name=row.name, short_name=row.short_name, owner_id=row.owner_id, description=row.description, overall_progress=overall_progress(row.id), n_tasks=n_tasks(row.id), n_volunteers=n_volunteers(row.id), info=json.loads(row.info)) apps_contributed.append(app) return apps_contributed
def draft_apps(user_id): sql = text(''' SELECT app.id, app.name, app.short_name, app.description, owner_id, app.info FROM app WHERE app.owner_id=:user_id AND app.info NOT LIKE('%task_presenter%') GROUP BY app.id, app.name, app.short_name, app.description, app.info;''') apps_draft = [] results = db.slave_session.execute(sql, dict(user_id=user_id)) for row in results: app = dict(id=row.id, name=row.name, short_name=row.short_name, owner_id=row.owner_id, description=row.description, overall_progress=overall_progress(row.id), n_tasks=n_tasks(row.id), n_volunteers=n_volunteers(row.id), info=json.loads(row.info)) apps_draft.append(app) return apps_draft