Skip to content

Commit

Permalink
Merge pull request #522 from EverythingMe/feature/new_home
Browse files Browse the repository at this point in the history
Feature: "personalized" homepage with recent queries and dashboards
  • Loading branch information
arikfr committed Aug 6, 2015
2 parents 6f0ac1e + 292d31e commit 1ef94b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions rd_ui/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ angular.module('redash', [
});

$routeProvider.when('/', {
templateUrl: '/views/index.html',
controller: 'IndexCtrl'
templateUrl: '/views/personal.html',
controller: 'PersonalIndexCtrl'
});

$routeProvider.when('/personal', {
templateUrl: '/views/personal.html',
controller: 'PersonalIndexCtrl'
Expand Down
9 changes: 8 additions & 1 deletion rd_ui/app/views/personal.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<div class="container">
<div class="row">
<p>
<a href="/queries/new" class="btn btn-default">New Query</a>
<button ng-show="currentUser.hasPermission('create_dashboard')" type="button" class="btn btn-default" data-toggle="modal" href="#new_dashboard_dialog">New Dashboard</button>
<a href="/alerts/new" class="btn btn-default">New Alert</a>
</p>
</div>

<div class="row">
<div class="list-group col-md-6">
<div class="list-group-item active">
Recent Dashboards
<button ng-show="currentUser.hasPermission('create_dashboard')" type="button" class="btn btn-sm btn-link" data-toggle="modal" href="#new_dashboard_dialog" tooltip="New Dashboard"><span class="glyphicon glyphicon-plus-sign"></span></button>
</div>
<div class="list-group-item" ng-repeat="dashboard in recentDashboards" >
<button type="button" class="close delete-button" aria-hidden="true" ng-show="dashboard.canEdit()" ng-click="archiveDashboard(dashboard)" tooltip="Delete Dashboard">&times;</button>
Expand Down
18 changes: 16 additions & 2 deletions redash/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from funcy import project
import sqlparse

from itertools import chain
from funcy import distinct

from redash import statsd_client, models, settings, utils
from redash.wsgi import app, api
from redash.tasks import QueryTask, record_event
Expand Down Expand Up @@ -249,9 +252,15 @@ def get(self, data_source_id):

api.add_resource(DataSourceSchemaAPI, '/api/data_sources/<data_source_id>/schema')


class DashboardRecentAPI(BaseResource):
def get(self):
return [d.to_dict() for d in models.Dashboard.recent(current_user.id).limit(20)]
recent = [d.to_dict() for d in models.Dashboard.recent(current_user.id)]

if len(recent) < 10:
global_recent = [d.to_dict() for d in models.Dashboard.recent()]

return distinct(chain(recent, global_recent), key=lambda d: d['id'])


class DashboardListAPI(BaseResource):
Expand Down Expand Up @@ -355,7 +364,12 @@ def get(self):
class QueryRecentAPI(BaseResource):
@require_permission('view_query')
def get(self):
return [q.to_dict() for q in models.Query.recent(current_user.id).limit(20)]
recent = [d.to_dict() for d in models.Query.recent(current_user.id)]

if len(recent) < 10:
global_recent = [d.to_dict() for d in models.Query.recent()]

return distinct(chain(recent, global_recent), key=lambda d: d['id'])


class QueryListAPI(BaseResource):
Expand Down
24 changes: 18 additions & 6 deletions redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,24 @@ def search(cls, term):
return cls.select().where(where).order_by(cls.created_at.desc())

@classmethod
def recent(cls, user_id):
def recent(cls, user_id=None, limit=20):
# TODO: instead of t2 here, we should define table_alias for Query table
return cls.select().where(Event.created_at > peewee.SQL("current_date - 7")).\
query = cls.select().where(Event.created_at > peewee.SQL("current_date - 7")).\
join(Event, on=(Query.id == peewee.SQL("t2.object_id::integer"))).\
where(Event.action << ('edit', 'execute', 'edit_name', 'edit_description', 'view_source')).\
where(Event.user == user_id).\
where(~(Event.object_id >> None)).\
where(Event.object_type == 'query'). \
where(cls.is_archived == False).\
group_by(Event.object_id, Query.id).\
order_by(peewee.SQL("count(0) desc"))

if user_id:
query = query.where(Event.user == user_id)

query = query.limit(limit)

return query

@classmethod
def update_instance(cls, query_id, **kwargs):
if 'query' in kwargs:
Expand Down Expand Up @@ -703,16 +709,22 @@ def get_by_slug(cls, slug):
return cls.get(cls.slug == slug)

@classmethod
def recent(cls, user_id):
return cls.select().where(Event.created_at > peewee.SQL("current_date - 7")). \
def recent(cls, user_id=None, limit=20):
query = cls.select().where(Event.created_at > peewee.SQL("current_date - 7")). \
join(Event, on=(Dashboard.id == peewee.SQL("t2.object_id::integer"))). \
where(Event.action << ('edit', 'view')).\
where(Event.user == user_id). \
where(~(Event.object_id >> None)). \
where(Event.object_type == 'dashboard'). \
group_by(Event.object_id, Dashboard.id). \
order_by(peewee.SQL("count(0) desc"))

if user_id:
query = query.where(Event.user == user_id)

query = query.limit(limit)

return query

def save(self, *args, **kwargs):
if not self.slug:
self.slug = utils.slugify(self.name)
Expand Down

0 comments on commit 1ef94b7

Please sign in to comment.