fix: repairs poor performance for assets (#2174)

- fixes #2158
This commit is contained in:
Wyatt Johnson
2019-02-05 22:06:05 +01:00
committed by Kiwi
parent 456abaf862
commit 94786c2711
4 changed files with 58 additions and 17 deletions
@@ -11,6 +11,7 @@ const initialState = {
criteria: {
asc: 'false',
filter: 'all',
limit: 20,
},
};
+10 -2
View File
@@ -16,7 +16,8 @@
text-align: center;
vertical-align: middle;
line-height: 30px;
width: 30px;
min-width: 30px;
padding: 5px;
user-select: none;
}
@@ -24,7 +25,14 @@
font-size: 1.8em;
}
.active {
.page:hover {
background-color: #ababab;
box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
a {
color: white;
}
}
.active, .active:hover {
background-color: #696969;
box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
a {
+4
View File
@@ -81,6 +81,10 @@ Asset.index(
}
);
// Indexes for listing the assets on the admin page.
Asset.index({ created_at: -1, publication_date: -1 }, { background: true });
Asset.index({ created_at: 1, publication_date: 1 }, { background: true });
/**
* Returns true if the asset is closed, false else.
*/
+43 -15
View File
@@ -1,4 +1,5 @@
const express = require('express');
const Joi = require('joi');
const router = express.Router();
const authorization = require('../../../middleware/authorization');
const { ErrHTTPNotFound } = require('../../../errors');
@@ -31,36 +32,63 @@ const FilterOpenAssets = (query, filter) => {
}
};
const ListAssetsSchema = Joi.object({
value: Joi.string()
.empty('')
.default(''),
field: Joi.string()
.empty('')
.default('publication_date'),
page: Joi.number()
.empty('')
.default(1)
.min(1),
asc: Joi.bool()
.empty('')
.default(false),
filter: Joi.string()
.empty('')
.valid(['all', 'open', 'closed'])
.default('all'),
limit: Joi.number()
.empty('')
.default(20)
.max(500)
.min(0),
});
// List assets.
router.get(
'/',
authorization.needed('ADMIN', 'MODERATOR'),
async (req, res, next) => {
const {
value = '',
field = 'publication_date',
page = 1,
asc = 'false',
filter = 'all',
limit = 20,
} = req.query;
const { value: query, error: err } = Joi.validate(
req.query,
ListAssetsSchema,
{}
);
if (err) {
return next(err);
}
let { value, field, page, asc, filter, limit } = query;
try {
const order = asc === 'true' ? 1 : -1;
const order = asc ? 1 : -1;
const queryOpts = {
sort: { [field]: order, created_at: order },
skip: (page - 1) * limit,
limit,
limit: limit,
};
// Find all the assets.
let [result, count] = await Promise.all([
// Find the actuall assets.
// Find the actual assets.
FilterOpenAssets(AssetsService.search({ value }), filter)
.sort(queryOpts.sort)
.skip(parseInt(queryOpts.skip))
.limit(parseInt(queryOpts.limit))
.skip(queryOpts.skip)
.limit(queryOpts.limit)
.lean(),
// Get the count of actual assets.
@@ -70,9 +98,9 @@ router.get(
// Send back the asset data.
res.json({
result,
limit: Number(limit),
limit,
count,
page: Number(page),
page,
totalPages: Math.ceil(count / (limit === 0 ? 1 : limit)),
});
} catch (e) {