Skip to content

Commit

Permalink
Merge pull request #354 from EverythingMe/feature/personal_home
Browse files Browse the repository at this point in the history
Feature: personal home with recent queries & dashboards
  • Loading branch information
arikfr committed Jan 19, 2015
2 parents fd37188 + f51c232 commit 6bb43d0
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
4 changes: 4 additions & 0 deletions rd_ui/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ angular.module('redash', [
templateUrl: '/views/index.html',
controller: 'IndexCtrl'
});
$routeProvider.when('/personal', {
templateUrl: '/views/personal.html',
controller: 'PersonalIndexCtrl'
});
$routeProvider.otherwise({
redirectTo: '/'
});
Expand Down
22 changes: 20 additions & 2 deletions rd_ui/app/scripts/controllers/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
$(window).click(function () {
notifications.getPermissions();
});
}
};

var IndexCtrl = function ($scope, Events, Dashboard) {
Events.record(currentUser, "view", "page", "homepage");
Expand All @@ -206,11 +206,29 @@
});
}
}
}
};

var PersonalIndexCtrl = function ($scope, Events, Dashboard, Query) {
Events.record(currentUser, "view", "page", "homepage");
$scope.$parent.pageTitle = "Home";

$scope.recentQueries = Query.recent();
$scope.recentDashboards = Dashboard.recent();

$scope.archiveDashboard = function (dashboard) {
if (confirm('Are you sure you want to delete "' + dashboard.name + '" dashboard?')) {
Events.record(currentUser, "archive", "dashboard", dashboard.id);
dashboard.$delete(function () {
$scope.$parent.reloadDashboards();
});
}
}
};

angular.module('redash.controllers', [])
.controller('QueriesCtrl', ['$scope', '$http', '$location', '$filter', 'Query', QueriesCtrl])
.controller('IndexCtrl', ['$scope', 'Events', 'Dashboard', IndexCtrl])
.controller('PersonalIndexCtrl', ['$scope', 'Events', 'Dashboard', 'Query', PersonalIndexCtrl])
.controller('MainCtrl', ['$scope', '$location', 'Dashboard', 'notifications', MainCtrl])
.controller('QuerySearchCtrl', ['$scope', '$location', '$filter', 'Events', 'Query', QuerySearchCtrl]);
})();
8 changes: 7 additions & 1 deletion rd_ui/app/scripts/services/dashboards.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
(function () {
var Dashboard = function($resource) {
var resource = $resource('/api/dashboards/:slug', {slug: '@slug'});
var resource = $resource('/api/dashboards/:slug', {slug: '@slug'}, {
recent: {
method: 'get',
isArray: true,
url: "/api/dashboards/recent"
}});

resource.prototype.canEdit = function() {
return currentUser.hasPermission('admin') || currentUser.canEdit(this);
}
Expand Down
13 changes: 12 additions & 1 deletion rd_ui/app/scripts/services/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,18 @@
};

var Query = function ($resource, QueryResult, DataSource) {
var Query = $resource('/api/queries/:id', {id: '@id'}, {search: {method: 'get', isArray: true, url: "/api/queries/search"}});
var Query = $resource('/api/queries/:id', {id: '@id'},
{
search: {
method: 'get',
isArray: true,
url: "/api/queries/search"
},
recent: {
method: 'get',
isArray: true,
url: "/api/queries/recent"
}});

Query.newQuery = function () {
return new Query({
Expand Down
28 changes: 28 additions & 0 deletions rd_ui/app/views/personal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="container">
<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>
<a ng-href="/dashboard/{{dashboard.slug}}">{{dashboard.name}}</a>
</div>
</div>

<div class="list-group col-md-6">
<div class="list-group-item active">
Recent Queries
</div>
<a ng-href="/queries/{{query.id}}" class="list-group-item" ng-repeat="query in recentQueries">{{query.name}}</a>
</div>
</div>

<div ng-show="currentUser.hasPermission('admin')" class="row">
<div class="list-group">
<div class="list-group-item active">Admin</div>
<a href="/admin/status" class="list-group-item">Status</a>
</div>
</div>
</div>
14 changes: 14 additions & 0 deletions redash/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def ping():
@app.route('/queries')
@app.route('/queries/<query_id>')
@app.route('/queries/<query_id>/<anything>')
@app.route('/personal')
@app.route('/')
@auth.required
def index(**kwargs):
Expand Down Expand Up @@ -181,6 +182,11 @@ def get(self):
api.add_resource(DataSourceListAPI, '/api/data_sources', endpoint='data_sources')


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


class DashboardListAPI(BaseResource):
def get(self):
dashboards = [d.to_dict() for d in
Expand Down Expand Up @@ -225,6 +231,7 @@ def delete(self, dashboard_slug):
dashboard.save()

api.add_resource(DashboardListAPI, '/api/dashboards', endpoint='dashboards')
api.add_resource(DashboardRecentAPI, '/api/dashboards/recent', endpoint='recent_dashboards')
api.add_resource(DashboardAPI, '/api/dashboards/<dashboard_slug>', endpoint='dashboard')


Expand Down Expand Up @@ -285,6 +292,12 @@ def get(self):
return [q.to_dict() for q in models.Query.search(term)]


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)]


class QueryListAPI(BaseResource):
@require_permission('create_query')
def post(self):
Expand Down Expand Up @@ -334,6 +347,7 @@ def get(self, query_id):
abort(404, message="Query not found.")

api.add_resource(QuerySearchAPI, '/api/queries/search', endpoint='queries_search')
api.add_resource(QueryRecentAPI, '/api/queries/recent', endpoint='recent_queries')
api.add_resource(QueryListAPI, '/api/queries', endpoint='queries')
api.add_resource(QueryAPI, '/api/queries/<query_id>', endpoint='query')

Expand Down
22 changes: 22 additions & 0 deletions redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,17 @@ def search(cls, term):

return cls.select().where(where).order_by(cls.created_at.desc())

@classmethod
def recent(cls, user_id):
return 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').\
group_by(Event.object_id, Query.id).\
order_by(peewee.SQL("count(0) desc"))

@classmethod
def update_instance(cls, query_id, **kwargs):
if 'query' in kwargs:
Expand Down Expand Up @@ -449,6 +460,17 @@ def to_dict(self, with_widgets=False):
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")). \
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"))

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

0 comments on commit 6bb43d0

Please sign in to comment.