open-nomad/ui/app/routes/evaluations/index.js
Phil Renaud 6a8f98723e
Chronological most-recent evals by default (#12847)
* Chronological most-recent evals by default

* Adding reverse: true to the list of expected queryparams in test

* changelog
2022-05-05 16:11:27 -04:00

104 lines
2.6 KiB
JavaScript

import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';
export default class EvaluationsIndexRoute extends Route {
@service store;
queryParams = {
pageSize: {
refreshModel: true,
},
nextToken: {
refreshModel: true,
},
status: {
refreshModel: true,
},
triggeredBy: {
refreshModel: true,
},
qpNamespace: {
refreshModel: true,
},
searchTerm: {
refreshModel: true,
},
type: {
refreshModel: true,
},
};
model({
nextToken,
pageSize,
searchTerm,
status,
triggeredBy,
type,
qpNamespace: namespace,
}) {
/*
We use our own DSL for filter expressions. This function takes our query parameters and builds a query that matches our DSL.
Documentation can be found here: https://www.nomadproject.io/api-docs#filtering
*/
const generateFilterExpression = () => {
const searchFilter = searchTerm
? `ID contains "${searchTerm}" or JobID contains "${searchTerm}" or NodeID contains "${searchTerm}" or TriggeredBy contains "${searchTerm}"`
: null;
const typeFilter =
type === 'client' ? `NodeID is not empty` : `NodeID is empty`;
const triggeredByFilter = `TriggeredBy contains "${triggeredBy}"`;
const statusFilter = `Status contains "${status}"`;
let filterExp;
if (searchTerm) {
if (!type && !status && !triggeredBy) {
return searchFilter;
}
filterExp = `(${searchFilter})`;
if (type) {
filterExp = `${filterExp} and ${typeFilter}`;
}
if (triggeredBy) {
filterExp = `${filterExp} and ${triggeredByFilter}`;
}
if (status) {
filterExp = `${filterExp} and ${statusFilter}`;
}
return filterExp;
}
if (type || status || triggeredBy) {
const lookup = {
[type]: typeFilter,
[status]: statusFilter,
[triggeredBy]: triggeredByFilter,
};
filterExp = [type, status, triggeredBy].reduce((result, filter) => {
const expression = lookup[filter];
if (!!filter && result !== '') {
result = result.concat(` and ${expression}`);
} else if (filter) {
result = expression;
}
return result;
}, '');
return filterExp;
}
return null;
};
this.store.findAll('namespace');
return this.store.query('evaluation', {
namespace,
reverse: true,
per_page: pageSize,
next_token: nextToken,
filter: generateFilterExpression(),
});
}
}