Skip to content

Commit

Permalink
Merge pull request #1391 from Scifabric/issue-1357-admin-dashboard
Browse files Browse the repository at this point in the history
Issue 1357 admin dashboard
  • Loading branch information
teleyinex committed Jan 24, 2017
2 parents 48f3da0 + f05fd28 commit ce58252
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 9 deletions.
160 changes: 160 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1747,3 +1747,163 @@ to update it.
"next": "/admin/categories",
"status": "success"
}
Admin dashboard
~~~~~~~~~~~~~~~
**Endpoint: /admin/dashboard/
*Allowed methods*: **GET**
**GET**
It shows the server statistics. You can use the argument *?refresh=1* to update the
data, as this data is only updated every 24 hours.
* **active_anon_last_week**: Active number of anonymous users in the server.
* **published_projects_last_week**: Published projects from the last week.
* **new_tasks_week**: Number of new tasks created on the last week.
* **update_feed**: Activity feed of actions in the server.
* **draft_projects_last_week**: List of new draft projects created in the last week.
* **update_projects_last_week**: List of updated projects in the last week.
* **new_users_week**: Number of new registered users in the last week.
* **new_task_runs_week**: Number of new task runs in the last week.
* **returning_users_week**: Number of returning users per number of days in a row in the last week.
* **active_users_last_week**: Number of active users in the last week.
* **wait**: This will be False if there's data, otherwise it will be True.
**Example output**
.. code-block:: python
{
"active_anon_last_week": {
"labels": [
"2016-04-28"
],
"series": [
[
0
]
]
},
"active_users_last_week": {
"labels": [
"2016-04-28"
],
"series": [
[
1
]
]
},
"draft_projects_last_week": [
{
"day": "2016-04-27",
"email_addr": "email",
"id": id,
"owner_id": id,
"p_name": "name",
"short_name": "name",
"u_name": "name"
},
{
"day": "2016-04-26",
"email_addr": "email",
"id": id,
"owner_id": id,
"p_name": "name",
"short_name": "name",
"u_name": "name"
}
],
"new_task_runs_week": {
"labels": [
"2016-04-28"
],
"series": [
[
4
]
]
},
"new_tasks_week": {
"labels": [
"2016-04-26",
"2016-04-28"
],
"series": [
[
57,
4
]
]
},
"new_users_week": {
"labels": [
"2016-04-27"
],
"series": [
[
1
]
]
},
"published_projects_last_week": [],
"returning_users_week": {
"labels": [
"1 day",
"2 days",
"3 days",
"4 days",
"5 days",
"6 days",
"7 days"
],
"series": [
[
0,
0,
0,
0,
0,
0,
0
]
]
},
"template": "admin/dashboard.html",
"title": "Dashboard",
"update_feed": [],
"update_projects_last_week": [
{
"day": "2016-04-28",
"email_addr": "email",
"id": id,
"owner_id": id,
"p_name": "name",
"short_name": "name",
"u_name": "name"
},
{
"day": "2016-04-27",
"email_addr": "email",
"id": id,
"owner_id": id,
"p_name": "name",
"short_name": "name",
"u_name": "name"
},
{
"day": "2016-04-26",
"email_addr": "email",
"id": id,
"owner_id": id,
"p_name": "name",
"short_name": "name",
"u_name": "name"
},
],
"wait": false
}
2 changes: 1 addition & 1 deletion pybossa/dashboard/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _graph_data_from_query(results, column, label=None):
def _format_projects_data(results):
formatted_projects = []
for row in results:
datum = dict(day=row.day, id=row.id, short_name=row.short_name,
datum = dict(day=row.day.strftime('%Y-%m-%d'), id=row.id, short_name=row.short_name,
p_name=row.name, owner_id=row.owner_id, u_name=row.u_name,
email_addr=row.email_addr)
formatted_projects.append(datum)
Expand Down
12 changes: 7 additions & 5 deletions pybossa/view/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ def dashboard():
returning_users_week = dashb.format_returning_users()
update_feed = get_update_feed()

return render_template(
'admin/dashboard.html',
response = dict(
template='admin/dashboard.html',
title=gettext('Dashboard'),
active_users_last_week=active_users_last_week,
active_anon_last_week=active_anon_last_week,
Expand All @@ -411,10 +411,12 @@ def dashboard():
returning_users_week=returning_users_week,
update_feed=update_feed,
wait=False)
return handle_content_type(response)
except ProgrammingError as e:
return render_template('admin/dashboard.html',
title=gettext('Dashboard'),
wait=True)
response = dict(template='admin/dashboard.html',
title=gettext('Dashboard'),
wait=True)
return handle_content_type(response)
except Exception as e: # pragma: no cover
current_app.logger.error(e)
return abort(500)
98 changes: 98 additions & 0 deletions test/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,15 @@ def test_admin_dashboard(self):
err_msg = "It should require login"
assert "Sign in" in res.data, err_msg

@with_context
def test_admin_dashboard_json(self):
"""Test ADMIN JSON dashboard requires admin"""
url = '/admin/dashboard/'
res = self.app_get_json(url, follow_redirects=True)
err_msg = "It should require login"
assert "Sign in" in res.data, err_msg


@with_context
def test_admin_dashboard_auth_user(self):
"""Test ADMIN dashboard requires admin"""
Expand All @@ -1081,6 +1090,20 @@ def test_admin_dashboard_auth_user(self):
err_msg = "It should return 403"
assert res.status_code == 403, err_msg

@with_context
def test_admin_dashboard_auth_user_json(self):
"""Test ADMIN JSON dashboard requires admin"""
url = '/admin/dashboard/'
self.register()
self.signout()
self.register(fullname="juan", name="juan")
res = self.app_get_json(url)
err_msg = "It should return 403"
assert res.status_code == 403, err_msg
data = json.loads(res.data)
assert data.get('code') == 403, err_msg


@with_context
def test_admin_dashboard_admin_user(self):
"""Test ADMIN dashboard admins can access it"""
Expand All @@ -1092,6 +1115,49 @@ def test_admin_dashboard_admin_user(self):
assert res.status_code == 200, err_msg
assert "No data" in res.data, res.data

@with_context
def test_admin_dashboard_admin_user_json(self):
"""Test ADMIN JSON dashboard admins can access it"""
url = '/admin/dashboard/'
self.register()
self.new_project()
res = self.app_get_json(url)
print res.data
err_msg = "It should return 200"
data = json.loads(res.data)
assert res.status_code == 200, err_msg
assert data.get('wait') == True, data

@with_context
def test_admin_dashboard_admin_user_data_json(self):
"""Test ADMIN JSON dashboard admins can access it with data"""
url = '/admin/dashboard/'
self.register()
self.new_project()
self.new_task(1)
import pybossa.dashboard.jobs as dashboard
dashboard.active_anon_week()
dashboard.active_users_week()
dashboard.new_users_week()
dashboard.new_tasks_week()
dashboard.new_task_runs_week()
dashboard.draft_projects_week()
dashboard.published_projects_week()
dashboard.update_projects_week()
dashboard.returning_users_week()
res = self.app_get_json(url)
data = json.loads(res.data)
err_msg = "It should return 200"
assert res.status_code == 200, err_msg
keys = ['active_anon_last_week', 'published_projects_last_week',
'new_tasks_week', 'title', 'update_feed',
'draft_projects_last_week', 'update_projects_last_week',
'new_users_week', 'template', 'new_task_runs_week',
'returning_users_week', 'active_users_last_week', 'wait']
for key in keys:
assert key in data.keys(), data


@with_context
def test_admin_dashboard_admin_user_data(self):
"""Test ADMIN dashboard admins can access it with data"""
Expand Down Expand Up @@ -1139,3 +1205,35 @@ def test_admin_dashboard_admin_refresh_user_data(self, mock):
assert "No data" not in res.data, res.data
assert "New Users" in res.data, res.data
assert mock.enqueue.called

@with_context
@patch('pybossa.view.admin.DASHBOARD_QUEUE')
def test_admin_dashboard_admin_refresh_user_data_json(self, mock):
"""Test ADMIN JSON dashboard admins refresh can access it with data"""
url = '/admin/dashboard/?refresh=1'
self.register()
self.new_project()
self.new_task(1)
import pybossa.dashboard.jobs as dashboard
dashboard.active_anon_week()
dashboard.active_users_week()
dashboard.new_users_week()
dashboard.new_tasks_week()
dashboard.new_task_runs_week()
dashboard.draft_projects_week()
dashboard.published_projects_week()
dashboard.update_projects_week()
dashboard.returning_users_week()
res = self.app_get_json(url)
data = json.loads(res.data)
err_msg = "It should return 200"
assert res.status_code == 200, err_msg
assert mock.enqueue.called
keys = ['active_anon_last_week', 'published_projects_last_week',
'new_tasks_week', 'title', 'update_feed',
'draft_projects_last_week', 'update_projects_last_week',
'new_users_week', 'template', 'new_task_runs_week',
'returning_users_week', 'active_users_last_week', 'wait']
for key in keys:
assert key in data.keys(), data

6 changes: 3 additions & 3 deletions test/test_jobs/test_dashboard_projects_week.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_format_new_projects(self):

assert len(res) == 1, res
res = res[0]
assert res['day'].strftime('%Y-%m-%d') == day, res['day']
assert res['day'] == day, res['day']
assert res['id'] == p.id
assert res['short_name'] == p.short_name
assert res['p_name'] == p.name
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_format_published_projects_week(self):

assert len(res) == 1, res
res = res[0]
assert res['day'].strftime('%Y-%m-%d') == day, res['day']
assert res['day'] == day, res['day']
assert res['id'] == p.id
assert res['short_name'] == p.short_name
assert res['p_name'] == p.name
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_format_updated_projects(self):

assert len(res) == 1, res
res = res[0]
assert res['day'].strftime('%Y-%m-%d') == day, res['day']
assert res['day'] == day, res['day']
assert res['id'] == p.id
assert res['short_name'] == p.short_name
assert res['p_name'] == p.name
Expand Down

0 comments on commit ce58252

Please sign in to comment.