2020-11-06 21:53:58 +00:00
|
|
|
|
/* eslint-disable ember/no-incorrect-calls-with-inline-anonymous-functions */
|
2020-10-29 12:46:42 +00:00
|
|
|
|
import Controller from '@ember/controller';
|
2020-11-04 18:22:24 +00:00
|
|
|
|
import { action } from '@ember/object';
|
2020-11-06 21:53:58 +00:00
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2020-11-04 18:22:24 +00:00
|
|
|
|
import { inject as controller } from '@ember/controller';
|
2020-11-30 14:18:44 +00:00
|
|
|
|
import { inject as service } from '@ember/service';
|
2020-11-06 21:53:58 +00:00
|
|
|
|
import { scheduleOnce } from '@ember/runloop';
|
2020-11-04 18:22:24 +00:00
|
|
|
|
import { task } from 'ember-concurrency';
|
2020-11-06 21:53:58 +00:00
|
|
|
|
import intersection from 'lodash.intersection';
|
2021-12-28 16:08:12 +00:00
|
|
|
|
import {
|
|
|
|
|
serialize,
|
|
|
|
|
deserializedQueryParam as selection,
|
|
|
|
|
} from 'nomad-ui/utils/qp-serialize';
|
2020-10-29 12:46:42 +00:00
|
|
|
|
|
2020-11-09 15:28:40 +00:00
|
|
|
|
import EmberObject, { computed } from '@ember/object';
|
|
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
|
import Searchable from 'nomad-ui/mixins/searchable';
|
|
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
|
export default class OptimizeController extends Controller {
|
2020-11-04 18:22:24 +00:00
|
|
|
|
@controller('optimize/summary') summaryController;
|
2020-11-30 14:18:44 +00:00
|
|
|
|
@service router;
|
|
|
|
|
@service system;
|
2020-10-29 12:46:42 +00:00
|
|
|
|
|
2020-11-06 21:53:58 +00:00
|
|
|
|
queryParams = [
|
2020-11-30 14:18:44 +00:00
|
|
|
|
{
|
2021-04-29 20:00:59 +00:00
|
|
|
|
searchTerm: 'search',
|
2020-11-30 14:18:44 +00:00
|
|
|
|
},
|
2020-11-09 15:28:40 +00:00
|
|
|
|
{
|
2021-04-29 20:00:59 +00:00
|
|
|
|
qpNamespace: 'namespacefilter',
|
2020-11-09 15:28:40 +00:00
|
|
|
|
},
|
2020-11-06 21:53:58 +00:00
|
|
|
|
{
|
|
|
|
|
qpType: 'type',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
qpStatus: 'status',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
qpDatacenter: 'dc',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
qpPrefix: 'prefix',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2020-11-09 15:28:40 +00:00
|
|
|
|
constructor() {
|
|
|
|
|
super(...arguments);
|
|
|
|
|
|
|
|
|
|
this.summarySearch = RecommendationSummarySearch.create({
|
|
|
|
|
dataSource: this,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 20:00:59 +00:00
|
|
|
|
get namespaces() {
|
|
|
|
|
return this.model.namespaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get summaries() {
|
|
|
|
|
return this.model.summaries;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 15:28:40 +00:00
|
|
|
|
@tracked searchTerm = '';
|
|
|
|
|
|
2020-11-06 21:53:58 +00:00
|
|
|
|
@tracked qpType = '';
|
|
|
|
|
@tracked qpStatus = '';
|
|
|
|
|
@tracked qpDatacenter = '';
|
|
|
|
|
@tracked qpPrefix = '';
|
2021-04-29 20:00:59 +00:00
|
|
|
|
@tracked qpNamespace = '*';
|
2020-11-30 14:18:44 +00:00
|
|
|
|
|
2020-11-06 21:53:58 +00:00
|
|
|
|
@selection('qpType') selectionType;
|
|
|
|
|
@selection('qpStatus') selectionStatus;
|
|
|
|
|
@selection('qpDatacenter') selectionDatacenter;
|
|
|
|
|
@selection('qpPrefix') selectionPrefix;
|
|
|
|
|
|
2021-04-29 20:00:59 +00:00
|
|
|
|
get optionsNamespaces() {
|
2021-12-28 14:45:20 +00:00
|
|
|
|
const availableNamespaces = this.namespaces.map((namespace) => ({
|
2021-04-29 20:00:59 +00:00
|
|
|
|
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
|
2021-10-21 14:24:07 +00:00
|
|
|
|
this.qpNamespace = this.system.cachedNamespace || '*';
|
2021-04-29 20:00:59 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return availableNamespaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optionsType = [
|
|
|
|
|
{ key: 'service', label: 'Service' },
|
|
|
|
|
{ key: 'system', label: 'System' },
|
|
|
|
|
];
|
2020-11-06 21:53:58 +00:00
|
|
|
|
|
|
|
|
|
optionsStatus = [
|
|
|
|
|
{ key: 'pending', label: 'Pending' },
|
|
|
|
|
{ key: 'running', label: 'Running' },
|
|
|
|
|
{ key: 'dead', label: 'Dead' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
get optionsDatacenter() {
|
|
|
|
|
const flatten = (acc, val) => acc.concat(val);
|
2021-12-28 16:08:12 +00:00
|
|
|
|
const allDatacenters = new Set(
|
|
|
|
|
this.summaries.mapBy('job.datacenters').reduce(flatten, [])
|
|
|
|
|
);
|
2020-11-06 21:53:58 +00:00
|
|
|
|
|
|
|
|
|
// 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
|
2021-12-28 16:08:12 +00:00
|
|
|
|
this.qpDatacenter = serialize(
|
|
|
|
|
intersection(availableDatacenters, this.selectionDatacenter)
|
|
|
|
|
);
|
2020-11-06 21:53:58 +00:00
|
|
|
|
});
|
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
|
return availableDatacenters.sort().map((dc) => ({ key: dc, label: dc }));
|
2020-11-06 21:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2021-04-29 20:00:59 +00:00
|
|
|
|
const allNames = this.summaries.mapBy('job.name');
|
2020-11-06 21:53:58 +00:00
|
|
|
|
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
|
2021-12-28 14:45:20 +00:00
|
|
|
|
const nameTable = Object.keys(nameHistogram).map((key) => ({
|
2020-11-06 21:53:58 +00:00
|
|
|
|
prefix: key,
|
|
|
|
|
count: nameHistogram[key],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Only consider prefixes that match more than one name
|
2021-12-28 14:45:20 +00:00
|
|
|
|
const prefixes = nameTable.filter((name) => name.count > 1);
|
2020-11-06 21:53:58 +00:00
|
|
|
|
|
|
|
|
|
// Remove any invalid prefixes from the query param/selection
|
|
|
|
|
const availablePrefixes = prefixes.mapBy('prefix');
|
|
|
|
|
scheduleOnce('actions', () => {
|
|
|
|
|
// eslint-disable-next-line ember/no-side-effects
|
2021-12-28 16:08:12 +00:00
|
|
|
|
this.qpPrefix = serialize(
|
|
|
|
|
intersection(availablePrefixes, this.selectionPrefix)
|
|
|
|
|
);
|
2020-11-06 21:53:58 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Sort, format, and include the count in the label
|
2021-12-28 14:45:20 +00:00
|
|
|
|
return prefixes.sortBy('prefix').map((name) => ({
|
2020-11-06 21:53:58 +00:00
|
|
|
|
key: name.prefix,
|
|
|
|
|
label: `${name.prefix} (${name.count})`,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get filteredSummaries() {
|
|
|
|
|
const {
|
|
|
|
|
selectionType: types,
|
|
|
|
|
selectionStatus: statuses,
|
|
|
|
|
selectionDatacenter: datacenters,
|
|
|
|
|
selectionPrefix: prefixes,
|
|
|
|
|
} = this;
|
|
|
|
|
|
|
|
|
|
// A summary’s job must match ALL filter facets, but it can match ANY selection within a facet
|
|
|
|
|
// Always return early to prevent unnecessary facet predicates.
|
2021-12-28 14:45:20 +00:00
|
|
|
|
return this.summarySearch.listSearched.filter((summary) => {
|
2020-11-06 21:53:58 +00:00
|
|
|
|
const job = summary.get('job');
|
|
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
|
if (job.isDestroying) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
|
if (
|
|
|
|
|
this.qpNamespace !== '*' &&
|
|
|
|
|
job.get('namespace.name') !== this.qpNamespace
|
|
|
|
|
) {
|
2020-11-30 14:18:44 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 21:53:58 +00:00
|
|
|
|
if (types.length && !types.includes(job.get('displayType'))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (statuses.length && !statuses.includes(job.get('status'))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
|
if (
|
|
|
|
|
datacenters.length &&
|
|
|
|
|
!job.get('datacenters').find((dc) => datacenters.includes(dc))
|
|
|
|
|
) {
|
2020-11-06 21:53:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = job.get('name');
|
2021-12-28 16:08:12 +00:00
|
|
|
|
if (
|
|
|
|
|
prefixes.length &&
|
|
|
|
|
!prefixes.find((prefix) => name.startsWith(prefix))
|
|
|
|
|
) {
|
2020-11-06 21:53:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
|
get activeRecommendationSummary() {
|
2020-11-30 14:18:44 +00:00
|
|
|
|
if (this.router.currentRouteName === 'optimize.summary') {
|
|
|
|
|
return this.summaryController.model;
|
|
|
|
|
} else {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2020-10-29 12:46:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 18:22:24 +00:00
|
|
|
|
// This is a task because the accordion uses timeouts for animation
|
|
|
|
|
// eslint-disable-next-line require-yield
|
2021-12-28 14:45:20 +00:00
|
|
|
|
@(task(function* () {
|
2021-12-28 16:08:12 +00:00
|
|
|
|
const currentSummaryIndex = this.filteredSummaries.indexOf(
|
|
|
|
|
this.activeRecommendationSummary
|
|
|
|
|
);
|
|
|
|
|
const nextSummary = this.filteredSummaries.objectAt(
|
|
|
|
|
currentSummaryIndex + 1
|
|
|
|
|
);
|
2020-10-29 12:46:42 +00:00
|
|
|
|
|
2020-11-04 18:22:24 +00:00
|
|
|
|
if (nextSummary) {
|
|
|
|
|
this.transitionToSummary(nextSummary);
|
|
|
|
|
} else {
|
|
|
|
|
this.send('reachedEnd');
|
2020-10-29 12:46:42 +00:00
|
|
|
|
}
|
|
|
|
|
}).drop())
|
|
|
|
|
proceed;
|
2020-11-04 18:22:24 +00:00
|
|
|
|
|
|
|
|
|
@action
|
|
|
|
|
transitionToSummary(summary) {
|
|
|
|
|
this.transitionToRoute('optimize.summary', summary.slug, {
|
|
|
|
|
queryParams: { jobNamespace: summary.jobNamespace },
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-11-06 21:53:58 +00:00
|
|
|
|
|
|
|
|
|
@action
|
2021-04-29 20:00:59 +00:00
|
|
|
|
cacheNamespace(namespace) {
|
|
|
|
|
this.system.cachedNamespace = namespace;
|
2020-11-30 14:18:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
2021-04-29 20:00:59 +00:00
|
|
|
|
setFacetQueryParam(queryParam, selection) {
|
|
|
|
|
this[queryParam] = serialize(selection);
|
2020-11-30 14:18:44 +00:00
|
|
|
|
this.syncActiveSummary();
|
2020-11-09 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
2020-11-30 14:18:44 +00:00
|
|
|
|
syncActiveSummary() {
|
2020-11-09 17:47:54 +00:00
|
|
|
|
scheduleOnce('actions', () => {
|
2020-11-30 14:18:44 +00:00
|
|
|
|
if (
|
|
|
|
|
!this.activeRecommendationSummary ||
|
|
|
|
|
!this.filteredSummaries.includes(this.activeRecommendationSummary)
|
|
|
|
|
) {
|
2020-11-09 17:47:54 +00:00
|
|
|
|
const firstFilteredSummary = this.filteredSummaries.objectAt(0);
|
|
|
|
|
|
|
|
|
|
if (firstFilteredSummary) {
|
|
|
|
|
this.transitionToSummary(firstFilteredSummary);
|
2020-11-10 15:38:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.transitionToRoute('optimize');
|
2020-11-09 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-11-06 21:53:58 +00:00
|
|
|
|
}
|
2020-10-29 12:46:42 +00:00
|
|
|
|
}
|
2020-11-09 15:28:40 +00:00
|
|
|
|
|
|
|
|
|
@classic
|
|
|
|
|
class RecommendationSummarySearch extends EmberObject.extend(Searchable) {
|
|
|
|
|
@computed
|
|
|
|
|
get fuzzySearchProps() {
|
|
|
|
|
return ['slug'];
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 20:00:59 +00:00
|
|
|
|
@alias('dataSource.summaries') listToSearch;
|
2020-11-09 15:28:40 +00:00
|
|
|
|
@alias('dataSource.searchTerm') searchTerm;
|
|
|
|
|
|
|
|
|
|
exactMatchEnabled = false;
|
|
|
|
|
fuzzySearchEnabled = true;
|
|
|
|
|
includeFuzzySearchMatches = true;
|
|
|
|
|
}
|