open-nomad/ui/app/controllers/jobs/index.js

253 lines
6.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable ember/no-incorrect-calls-with-inline-anonymous-functions */
import { inject as service } from '@ember/service';
import { alias, readOnly } from '@ember/object/computed';
import Controller from '@ember/controller';
import { action, computed } from '@ember/object';
import { scheduleOnce } from '@ember/runloop';
import intersection from 'lodash.intersection';
2017-09-19 14:47:10 +00:00
import Sortable from 'nomad-ui/mixins/sortable';
import Searchable from 'nomad-ui/mixins/searchable';
import { serialize, deserializedQueryParam as selection } from 'nomad-ui/utils/qp-serialize';
import classic from 'ember-classic-decorator';
@classic
export default class IndexController extends Controller.extend(Sortable, Searchable) {
@service system;
@service userSettings;
isForbidden = false;
queryParams = [
{
currentPage: 'page',
},
{
searchTerm: 'search',
},
{
sortProperty: 'sort',
},
{
sortDescending: 'desc',
},
{
qpType: 'type',
},
{
qpStatus: 'status',
},
{
qpDatacenter: 'dc',
},
{
qpPrefix: 'prefix',
},
{
qpNamespace: 'namespace',
},
];
2017-09-19 14:47:10 +00:00
currentPage = 1;
@readOnly('userSettings.pageSize') pageSize;
2017-09-19 14:47:10 +00:00
sortProperty = 'modifyIndex';
sortDescending = true;
2017-09-19 14:47:10 +00:00
@computed
get searchProps() {
return ['id', 'name'];
}
@computed
get fuzzySearchProps() {
return ['name'];
}
fuzzySearchEnabled = true;
2017-09-19 14:47:10 +00:00
qpType = '';
qpStatus = '';
qpDatacenter = '';
qpPrefix = '';
2019-02-11 22:37:36 +00:00
@selection('qpType') selectionType;
@selection('qpStatus') selectionStatus;
@selection('qpDatacenter') selectionDatacenter;
@selection('qpPrefix') selectionPrefix;
2019-02-11 22:37:36 +00:00
@computed
get optionsType() {
return [
{ key: 'batch', label: 'Batch' },
{ key: 'parameterized', label: 'Parameterized' },
{ key: 'periodic', label: 'Periodic' },
{ key: 'service', label: 'Service' },
{ key: 'system', label: 'System' },
{ key: 'sysbatch', label: 'System Batch' },
];
}
@computed
get optionsStatus() {
return [
{ key: 'pending', label: 'Pending' },
{ key: 'running', label: 'Running' },
{ key: 'dead', label: 'Dead' },
];
}
@computed('selectionDatacenter', 'visibleJobs.[]')
get optionsDatacenter() {
const flatten = (acc, val) => acc.concat(val);
const allDatacenters = new Set(this.visibleJobs.mapBy('datacenters').reduce(flatten, []));
// Remove any invalid datacenters from the query param/selection
const availableDatacenters = Array.from(allDatacenters).compact();
scheduleOnce('actions', () => {
// eslint-disable-next-line ember/no-side-effects
this.set(
'qpDatacenter',
2019-03-26 07:46:44 +00:00
serialize(intersection(availableDatacenters, this.selectionDatacenter))
);
});
return availableDatacenters.sort().map(dc => ({ key: dc, label: dc }));
}
@computed('selectionPrefix', 'visibleJobs.[]')
get optionsPrefix() {
// A prefix is defined as the start of a job name up to the first - or .
// ex: mktg-analytics -> mktg, ds.supermodel.classifier -> ds
const hasPrefix = /.[-._]/;
// Collect and count all the prefixes
2019-03-26 07:46:44 +00:00
const allNames = this.visibleJobs.mapBy('name');
const nameHistogram = allNames.reduce((hist, name) => {
if (hasPrefix.test(name)) {
const prefix = name.match(/(.+?)[-._]/)[1];
hist[prefix] = hist[prefix] ? hist[prefix] + 1 : 1;
}
return hist;
}, {});
// Convert to an array
const nameTable = Object.keys(nameHistogram).map(key => ({
prefix: key,
count: nameHistogram[key],
}));
// Only consider prefixes that match more than one name
const prefixes = nameTable.filter(name => name.count > 1);
// Remove any invalid prefixes from the query param/selection
const availablePrefixes = prefixes.mapBy('prefix');
scheduleOnce('actions', () => {
// eslint-disable-next-line ember/no-side-effects
2019-03-26 07:46:44 +00:00
this.set('qpPrefix', serialize(intersection(availablePrefixes, this.selectionPrefix)));
});
// Sort, format, and include the count in the label
2019-01-24 04:35:51 +00:00
return prefixes.sortBy('prefix').map(name => ({
key: name.prefix,
label: `${name.prefix} (${name.count})`,
}));
}
@computed('qpNamespace', 'model.namespaces.[]', 'system.cachedNamespace')
get optionsNamespaces() {
const availableNamespaces = this.model.namespaces.map(namespace => ({
key: namespace.name,
label: namespace.name,
}));
availableNamespaces.unshift({
key: '*',
label: 'All (*)',
});
// Unset the namespace selection if it was server-side deleted
if (!availableNamespaces.mapBy('key').includes(this.qpNamespace)) {
scheduleOnce('actions', () => {
// eslint-disable-next-line ember/no-side-effects
this.set('qpNamespace', this.system.cachedNamespace || '*');
});
}
return availableNamespaces;
}
/**
2019-02-11 22:37:36 +00:00
Visible jobs are those that match the selected namespace and aren't children
of periodic or parameterized jobs.
*/
@computed('model.jobs.@each.parent')
get visibleJobs() {
if (!this.model || !this.model.jobs) return [];
return this.model.jobs
.compact()
.filter(job => !job.isNew)
.filter(job => !job.get('parent.content'));
}
@computed(
'visibleJobs.[]',
2019-02-01 22:08:55 +00:00
'selectionType',
'selectionStatus',
'selectionDatacenter',
'selectionPrefix'
)
get filteredJobs() {
const {
selectionType: types,
selectionStatus: statuses,
selectionDatacenter: datacenters,
selectionPrefix: prefixes,
} = this;
// A job must match ALL filter facets, but it can match ANY selection within a facet
// Always return early to prevent unnecessary facet predicates.
return this.visibleJobs.filter(job => {
if (types.length && !types.includes(job.get('displayType'))) {
return false;
}
if (statuses.length && !statuses.includes(job.get('status'))) {
return false;
}
if (datacenters.length && !job.get('datacenters').find(dc => datacenters.includes(dc))) {
return false;
}
const name = job.get('name');
if (prefixes.length && !prefixes.find(prefix => name.startsWith(prefix))) {
return false;
}
return true;
});
}
@alias('filteredJobs') listToSort;
@alias('listSorted') listToSearch;
@alias('listSearched') sortedJobs;
isShowingDeploymentDetails = false;
2017-09-19 14:47:10 +00:00
@action
cacheNamespace(namespace) {
this.system.cachedNamespace = namespace;
}
setFacetQueryParam(queryParam, selection) {
this.set(queryParam, serialize(selection));
}
@action
gotoJob(job) {
this.transitionToRoute('jobs.job', job.get('plainId'), {
queryParams: { namespace: job.get('namespace.name') },
});
}
}