Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #78 - Implement server side pagination and sorting for queries #2566

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/app/components/app-header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import './app-header.css';

const logger = debug('redash:appHeader');

function controller($rootScope, $location, $uibModal, Auth, currentUser, clientConfig, Dashboard) {
function controller($rootScope, $location, $route, $uibModal, Auth, currentUser, clientConfig, Dashboard) {
this.logoUrl = logoUrl;
this.basePath = clientConfig.basePath;
this.currentUser = currentUser;
Expand Down Expand Up @@ -35,7 +35,8 @@ function controller($rootScope, $location, $uibModal, Auth, currentUser, clientC
};

this.searchQueries = () => {
$location.path('/queries/search').search({ q: this.term });
$location.path('/queries').search({ search: this.term });
$route.reload();
};

this.logout = () => {
Expand Down
40 changes: 36 additions & 4 deletions client/app/lib/pagination/live-paginator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
export default class LivePaginator {
constructor(rowsFetcher, { page = 1, itemsPerPage = 20 } = {}) {
constructor(rowsFetcher, {
page = 1,
itemsPerPage = 20,
orderByField,
orderByReverse = false,
params = {},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be ...params instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm, maybe?

} = {}) {
this.page = page;
this.itemsPerPage = itemsPerPage;
this.orderByField = orderByField;
this.orderByReverse = orderByReverse;
this.params = params;
this.rowsFetcher = rowsFetcher;
this.rowsFetcher(this.page, this.itemsPerPage, this);
this.fetch(this.page);
}

fetch(page) {
this.rowsFetcher(
page,
this.itemsPerPage,
this.orderByField,
this.orderByReverse,
this.params,
this,
);
}

setPage(page) {
this.page = page;
this.rowsFetcher(page, this.itemsPerPage, this);
this.fetch(page);
}

getPageRows() {
Expand All @@ -23,5 +43,17 @@ export default class LivePaginator {
this.totalCount = 0;
}
}
}

orderBy(column) {
if (column === this.orderByField) {
this.orderByReverse = !this.orderByReverse;
} else {
this.orderByField = column;
this.orderByReverse = false;
}

if (this.orderByField) {
this.fetch(this.page);
}
}
}
91 changes: 83 additions & 8 deletions client/app/pages/queries-list/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import moment from 'moment';
import { isString } from 'underscore';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the React pull request gets merged before this one, we need to remember to replace this with lodash...

import startsWith from 'underscore.string/startsWith';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use String's own startsWith.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about IE11 is very benevolent of you :) But I would assume Babel takes care of this for us?


import { LivePaginator } from '@/lib/pagination';
import template from './queries-list.html';


class QueriesListCtrl {
constructor($location, Title, Query) {
constructor($location, $log, $route, Title, Query) {
const page = parseInt($location.search().page || 1, 10);

const orderSeparator = '-';
const pageOrder = $location.search().order || 'created_at';
const pageOrderReverse = startsWith(pageOrder, orderSeparator);
this.showEmptyState = false;
this.showDrafts = false;
this.pageSize = parseInt($location.search().page_size || 20, 10);
this.pageSizeOptions = [5, 10, 20, 50, 100];
this.searchTerm = $location.search().search || '';
this.oldSearchTerm = $location.search().q;
this.defaultOptions = {};

const self = this;
Expand All @@ -20,16 +31,42 @@ class QueriesListCtrl {
Title.set('My Queries');
this.resource = Query.myQueries;
break;
// Redirect to the real search view.
// TODO: check if that really always works.
case '/queries/search':
window.location.replace('/queries?q=' + this.oldSearchTerm);
break;
default:
break;
}

function queriesFetcher(requestedPage, itemsPerPage, paginator) {
const setSearchOrClear = (name, value) => {
if (value) {
$location.search(name, value);
} else {
$location.search(name, undefined);
}
};

function queriesFetcher(requestedPage, itemsPerPage, orderByField, orderByReverse, params, paginator) {
$location.search('page', requestedPage);
$location.search('page_size', itemsPerPage);
if (orderByReverse && !startsWith(orderByField, orderSeparator)) {
orderByField = orderSeparator + orderByField;
}
setSearchOrClear('order', orderByField);
setSearchOrClear('search', params.searchTerm);
setSearchOrClear('drafts', params.showDrafts);

const request = Object.assign(
{}, self.defaultOptions,
{ page: requestedPage, page_size: itemsPerPage },
{
page: requestedPage,
page_size: itemsPerPage,
order: orderByField,
search: params.searchTerm,
drafts: params.showDrafts,
},
);

return self.resource(request).$promise.then((data) => {
Expand All @@ -43,16 +80,49 @@ class QueriesListCtrl {
});
}

this.paginator = new LivePaginator(queriesFetcher, { page });
this.paginator = new LivePaginator(
queriesFetcher,
{
page,
itemsPerPage: this.pageSize,
orderByField: pageOrder,
orderByReverse: pageOrderReverse,
params: this.parameters,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this.parameters have a value at this point? (I see where update() assigns to it later.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a kerfuffle, I'll clean this up while porting this to #2573

},
);

this.parameters = () => ({
searchTerm: this.searchTerm,
showDrafts: this.showDrafts,
});

this.tabs = [
{ path: 'queries', name: 'All Queries', isActive: path => path === '/queries' },
{ name: 'My Queries', path: 'queries/my' },
{ name: 'Search', path: 'queries/search' },
];

this.showList = () => this.paginator.getPageRows() !== undefined && this.paginator.getPageRows().length > 0;
this.showEmptyState = () => this.paginator.getPageRows() !== undefined && this.paginator.getPageRows().length === 0;
this.searchUsed = () => this.searchTerm !== undefined || this.searchTerm !== '';

this.hasResults = () => this.paginator.getPageRows() !== undefined &&
this.paginator.getPageRows().length > 0;

this.showEmptyState = () => !this.hasResults() && !this.searchUsed();

this.showDraftsCheckbox = () => $location.path() !== '/queries/my';

this.clearSearch = () => {
this.searchTerm = '';
this.update();
};

this.update = () => {
if (!isString(this.searchTerm) || this.searchTerm.trim() === '') {
this.searchTerm = '';
}
this.paginator.itemsPerPage = this.pageSize;
this.paginator.params = this.parameters();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to just inline the parameters function? I found the similar name to params confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good idea.

this.paginator.fetch(page);
};
}
}

Expand All @@ -71,5 +141,10 @@ export default function init(ngModule) {
template: '<page-queries-list></page-queries-list>',
reloadOnSearch: false,
},
// just for backward-compatible routes
'/queries/search': {
template: '<page-queries-list></page-queries-list>',
reloadOnSearch: false,
},
};
}
100 changes: 68 additions & 32 deletions client/app/pages/queries-list/queries-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,75 @@
<empty-state icon="fa fa-code" description="Getting the data from your datasources" illustration="query" help-link="http://help.redash.io/category/21-querying"
ng-if="$ctrl.showEmptyState()"></empty-state>

<div ng-if="$ctrl.showList()">
<tab-nav tabs="$ctrl.tabs"></tab-nav>
<tab-nav tabs="$ctrl.tabs"></tab-nav>

<div class="bg-white tiled">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created By</th>
<th>Created At</th>
<th>Runtime</th>
<th>Last Executed At</th>
<th>Update Schedule</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="query in $ctrl.paginator.getPageRows()">
<td>
<a href="queries/{{query.id}}">{{query.name}}</a>
<span class="label label-default" ng-if="query.is_draft">Unpublished</span>
</td>
<td>
<img ng-src="{{ query.user.profile_image_url }}" class="profile__image_thumb" />
<span ng-class="{'text-muted': query.user.is_disabled}">{{query.user.name}}</span>
</td>
<td>{{query.created_at | dateTime}}</td>
<td>{{query.runtime | durationHumanize}}</td>
<td>{{query.retrieved_at | dateTime}}</td>
<td>{{query.schedule | scheduleHumanize}}</td>
</tr>
</tbody>
</table>
<paginator paginator="$ctrl.paginator"></paginator>
<div class="bg-white tiled p-10">
<div class="row">
<div class="col-md-3 p-t-5">
<form class="form-inline" role="form" ng-submit="$ctrl.update()">
<div class="input-group">
<input class="form-control input-sm" placeholder="Search..." ng-model="$ctrl.searchTerm" autofocus>
<span class="input-group-btn">
<button class="btn btn-secondary" type="button" ng-click="$ctrl.clearSearch()">
<span class="zmdi zmdi-close"></span>
</button>
<button class="btn btn-primary" type="submit">
<span class="zmdi zmdi-search"></span>
</button>
</span>
</div>
</form>
</div>
<div class="col-md-9 p-t-5">
<form class="form-inline" role="form">
<div class="form-group">
<label class="control-label" for="page_size">Show results:</label>
<select id="page_size" ng-change="$ctrl.update()" ng-model="$ctrl.pageSize" class="form-control input-sm"
ng-options="value for value in $ctrl.pageSizeOptions"></select>
</div>
<div class="form-group p-l-20 p-r-20" ng-if="$ctrl.showDraftsCheckbox()">
<label class="checkbox" for="show_drafts">
<input id="show_drafts" type="checkbox" ng-model="$ctrl.showDrafts" ng-change="$ctrl.update()">
Show drafts
</label>
</div>
</form>
</div>
</div>
</div>

<div class="bg-white tiled">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('name')">Name <sort-icon column="'name'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('created_by')">Created By <sort-icon column="'created_by'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('created_at')">Created At <sort-icon column="'created_at'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('runtime')">Runtime <sort-icon column="'runtime'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('executed_at')">Last Executed At <sort-icon column="'executed_at'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.paginator.orderBy('schedule')">Update Schedule <sort-icon column="'schedule'" sort-column="$ctrl.paginator.orderByField" reverse="$ctrl.paginator.orderByReverse"></sort-icon></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="query in $ctrl.paginator.getPageRows()">
<td>
<a href="queries/{{query.id}}">{{query.name}}</a>
<span class="label label-default" ng-if="query.is_draft">Unpublished</span>
</td>
<td>
<img ng-src="{{ query.user.profile_image_url }}" class="profile__image_thumb" />
<span ng-class="{'text-muted': query.user.is_disabled}">{{query.user.name}}</span>
</td>
<td>{{query.created_at | dateTime}}</td>
<td>{{query.runtime | durationHumanize}}</td>
<td>{{query.retrieved_at | dateTime}}</td>
<td>{{query.schedule | scheduleHumanize}}</td>
</tr>
<tr ng-if="$ctrl.paginator.rows.length == 0 && $ctrl.searchTerm !== null">
<td>No queries found.</td>
</tr>
</tbody>
</table>
<paginator paginator="$ctrl.paginator"></paginator>
</div>
</div>
43 changes: 0 additions & 43 deletions client/app/pages/queries/queries-search-results-page.html

This file was deleted.

Loading