Example #1
0
def index():
    """Return Global Statistics for the site."""
    title = "Global Statistics"

    n_auth = site_stats.n_auth_users()

    n_anon = site_stats.n_anon_users()

    n_total_users = n_anon + n_auth

    n_published_projects = cached_projects.n_published()
    n_draft_projects = cached_projects.n_count('draft')
    n_total_projects = n_published_projects + n_draft_projects

    n_tasks = site_stats.n_tasks_site()

    n_task_runs = site_stats.n_task_runs_site()

    top5_projects_24_hours = site_stats.get_top5_projects_24_hours()

    top5_users_24_hours = site_stats.get_top5_users_24_hours()

    stats = dict(n_total_users=n_total_users,
                 n_auth=n_auth,
                 n_anon=n_anon,
                 n_published_projects=n_published_projects,
                 n_draft_projects=n_draft_projects,
                 n_total_projects=n_total_projects,
                 n_tasks=n_tasks,
                 n_task_runs=n_task_runs)

    users = dict(label="User Statistics",
                 values=[
                     dict(label='Anonymous', value=[0, n_anon]),
                     dict(label='Authenticated', value=[0, n_auth])
                 ])

    projects = dict(label="Projects Statistics",
                    values=[
                        dict(label='Published',
                             value=[0, n_published_projects]),
                        dict(label='Draft', value=[0, n_draft_projects])
                    ])

    tasks = dict(label="Task and Task Run Statistics",
                 values=[
                     dict(label='Tasks', value=[0, n_tasks]),
                     dict(label='Answers', value=[1, n_task_runs])
                 ])

    response = dict(template='/stats/global.html',
                    title=title,
                    users=json.dumps(users),
                    projects=json.dumps(projects),
                    tasks=json.dumps(tasks),
                    show_locs=False,
                    top5_users_24_hours=top5_users_24_hours,
                    top5_projects_24_hours=top5_projects_24_hours,
                    stats=stats)
    return handle_content_type(response)
Example #2
0
def warm_up_stats():  # pragma: no cover
    """Background job for warming stats."""
    from pybossa.cache.site_stats import (n_auth_users, n_anon_users,
                                          n_tasks_site, n_total_tasks_site,
                                          n_task_runs_site,
                                          get_top5_projects_24_hours,
                                          get_top5_users_24_hours)
    n_auth_users()
    n_anon_users()
    n_tasks_site()
    n_total_tasks_site()
    n_task_runs_site()
    get_top5_projects_24_hours()
    get_top5_users_24_hours()

    return True
Example #3
0
def index():
    """Return Global Statistics for the site."""
    title = "Global Statistics"

    n_auth = site_stats.n_auth_users()

    n_anon = site_stats.n_anon_users()

    n_total_users = n_anon + n_auth

    n_published_projects = cached_projects.n_published()
    n_draft_projects = cached_projects.n_count('draft')
    n_total_projects = n_published_projects + n_draft_projects

    n_tasks = site_stats.n_tasks_site()

    n_task_runs = site_stats.n_task_runs_site()

    top5_projects_24_hours = site_stats.get_top5_projects_24_hours()

    top5_users_24_hours = site_stats.get_top5_users_24_hours()

    locs = site_stats.get_locs()

    show_locs = False
    if len(locs) > 0:
        show_locs = True

    stats = dict(n_total_users=n_total_users, n_auth=n_auth, n_anon=n_anon,
                 n_published_projects=n_published_projects,
                 n_draft_projects=n_draft_projects,
                 n_total_projects=n_total_projects,
                 n_tasks=n_tasks,
                 n_task_runs=n_task_runs)

    users = dict(label="User Statistics",
                 values=[
                     dict(label='Anonymous', value=[0, n_anon]),
                     dict(label='Authenticated', value=[0, n_auth])])

    projects = dict(label="Projects Statistics",
                    values=[
                        dict(label='Published',
                             value=[0, n_published_projects]),
                        dict(label='Draft', value=[0, n_draft_projects])])

    tasks = dict(label="Task and Task Run Statistics",
                 values=[
                     dict(label='Tasks', value=[0, n_tasks]),
                     dict(label='Answers', value=[1, n_task_runs])])

    return render_template('/stats/global.html', title=title,
                           users=json.dumps(users),
                           projects=json.dumps(projects),
                           tasks=json.dumps(tasks),
                           locs=json.dumps(locs),
                           show_locs=show_locs,
                           top5_users_24_hours=top5_users_24_hours,
                           top5_projects_24_hours=top5_projects_24_hours,
                           stats=stats)
Example #4
0
    def test_get_top5_projects_24_hours_returns_required_fields(self):
        fields = ('id', 'name', 'short_name', 'info', 'n_answers')
        TaskRunFactory.create()

        top5 = stats.get_top5_projects_24_hours()

        for field in fields:
            assert field in top5[0].keys()
Example #5
0
    def test_get_top5_projects_24_hours_returns_required_fields(self):
        fields = ('id', 'name', 'short_name', 'info', 'n_answers')
        TaskRunFactory.create()

        top5 = stats.get_top5_projects_24_hours()

        for field in fields:
            assert field in top5[0].keys()
Example #6
0
def warm_up_stats():  # pragma: no cover
    """Background job for warming stats."""
    print "Running on the background warm_up_stats"
    from pybossa.cache.site_stats import (n_auth_users, n_anon_users,
                                          n_tasks_site, n_total_tasks_site,
                                          n_task_runs_site,
                                          get_top5_projects_24_hours,
                                          get_top5_users_24_hours)
    n_auth_users()
    n_anon_users()
    n_tasks_site()
    n_total_tasks_site()
    n_task_runs_site()
    get_top5_projects_24_hours()
    get_top5_users_24_hours()

    return True
Example #7
0
    def test_get_top5_projects_24_hours_does_not_include_hidden_projects(self):
        good_project = ProjectFactory.create()
        best_but_hidden_project = ProjectFactory.create(hidden=1)

        TaskRunFactory.create(project=good_project)
        TaskRunFactory.create_batch(2, project=best_but_hidden_project)

        top5 = stats.get_top5_projects_24_hours()
        top5_ids = [top['id'] for top in top5]

        assert good_project.id in top5_ids
        assert best_but_hidden_project.id not in top5_ids
Example #8
0
    def test_get_top5_projects_24_hours_considers_last_24_hours_contributions_only(self):
        recently_contributed_project = ProjectFactory.create()
        long_ago_contributed_project = ProjectFactory.create()
        two_days_ago = (datetime.datetime.utcnow() -  datetime.timedelta(2)).isoformat()

        TaskRunFactory.create(project=recently_contributed_project)
        TaskRunFactory.create(project=long_ago_contributed_project, finish_time=two_days_ago)

        top5 = stats.get_top5_projects_24_hours()
        top5_ids = [top['id'] for top in top5]

        assert recently_contributed_project.id in top5_ids
        assert long_ago_contributed_project.id not in top5_ids
Example #9
0
    def test_get_top5_projects_24_hours_considers_last_24_hours_contributions_only(self):
        recently_contributed_project = ProjectFactory.create()
        long_ago_contributed_project = ProjectFactory.create()
        two_days_ago = (datetime.datetime.utcnow() -  datetime.timedelta(2)).isoformat()

        TaskRunFactory.create(project=recently_contributed_project)
        TaskRunFactory.create(project=long_ago_contributed_project, finish_time=two_days_ago)

        top5 = stats.get_top5_projects_24_hours()
        top5_ids = [top['id'] for top in top5]

        assert recently_contributed_project.id in top5_ids
        assert long_ago_contributed_project.id not in top5_ids
Example #10
0
    def test_get_top5_projects_24_hours_returns_best_5_only(self):
        projects = ProjectFactory.create_batch(5)
        i = 5
        for project in projects:
            TaskRunFactory.create_batch(i, project=project)
            i -= 1

        worst_project = ProjectFactory.create()

        top5 = stats.get_top5_projects_24_hours()
        top5_ids = [top['id'] for top in top5]

        assert len(top5) == 5
        assert worst_project.id not in top5_ids
        for i in range(len(top5)):
            assert projects[i].id == top5_ids[i]
Example #11
0
    def test_get_top5_projects_24_hours_returns_best_5_only(self):
        projects = ProjectFactory.create_batch(5)
        i = 5
        for project in projects:
            TaskRunFactory.create_batch(i, project=project)
            i -= 1

        worst_project = ProjectFactory.create()

        top5 = stats.get_top5_projects_24_hours()
        top5_ids = [top['id'] for top in top5]

        assert len(top5) == 5
        assert worst_project.id not in top5_ids
        for i in range(len(top5)):
            assert projects[i].id == top5_ids[i]