2020-10-29 12:46:42 +00:00
|
|
|
import Component from '@glimmer/component';
|
2020-11-04 18:22:24 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-10-29 12:46:42 +00:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import ResourcesDiffs from 'nomad-ui/utils/resources-diffs';
|
|
|
|
import { htmlSafe } from '@ember/template';
|
|
|
|
import { didCancel, task, timeout } from 'ember-concurrency';
|
|
|
|
import Ember from 'ember';
|
|
|
|
|
|
|
|
export default class DasRecommendationCardComponent extends Component {
|
2020-11-04 18:22:24 +00:00
|
|
|
@service router;
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
@tracked allCpuToggleActive = true;
|
|
|
|
@tracked allMemoryToggleActive = true;
|
|
|
|
|
|
|
|
@tracked activeTaskToggleRowIndex = 0;
|
|
|
|
|
2021-03-01 15:46:22 +00:00
|
|
|
element = null;
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
@tracked cardHeight;
|
|
|
|
@tracked interstitialComponent;
|
|
|
|
@tracked error;
|
|
|
|
|
|
|
|
@tracked proceedPromiseResolve;
|
|
|
|
|
|
|
|
get activeTaskToggleRow() {
|
|
|
|
return this.taskToggleRows[this.activeTaskToggleRowIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
get activeTask() {
|
|
|
|
return this.activeTaskToggleRow.task;
|
|
|
|
}
|
|
|
|
|
|
|
|
get narrative() {
|
|
|
|
const summary = this.args.summary;
|
|
|
|
const taskGroup = summary.taskGroup;
|
|
|
|
|
|
|
|
const diffs = new ResourcesDiffs(
|
|
|
|
taskGroup,
|
|
|
|
taskGroup.count,
|
|
|
|
this.args.summary.recommendations,
|
|
|
|
this.args.summary.excludedRecommendations
|
|
|
|
);
|
|
|
|
|
|
|
|
const cpuDelta = diffs.cpu.delta;
|
|
|
|
const memoryDelta = diffs.memory.delta;
|
|
|
|
|
|
|
|
const aggregate = taskGroup.count > 1;
|
|
|
|
const aggregateString = aggregate ? ' an aggregate' : '';
|
|
|
|
|
|
|
|
if (cpuDelta || memoryDelta) {
|
|
|
|
const deltasSameDirection =
|
|
|
|
(cpuDelta < 0 && memoryDelta < 0) || (cpuDelta > 0 && memoryDelta > 0);
|
|
|
|
|
|
|
|
let narrative = 'Applying the selected recommendations will';
|
|
|
|
|
|
|
|
if (deltasSameDirection) {
|
|
|
|
narrative += ` ${verbForDelta(cpuDelta)} ${aggregateString}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpuDelta) {
|
|
|
|
if (!deltasSameDirection) {
|
|
|
|
narrative += ` ${verbForDelta(cpuDelta)} ${aggregateString}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
narrative += ` <strong>${diffs.cpu.absoluteAggregateDiff} of CPU</strong>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpuDelta && memoryDelta) {
|
|
|
|
narrative += ' and';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memoryDelta) {
|
|
|
|
if (!deltasSameDirection) {
|
|
|
|
narrative += ` ${verbForDelta(memoryDelta)} ${aggregateString}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
narrative += ` <strong>${diffs.memory.absoluteAggregateDiff} of memory</strong>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (taskGroup.count === 1) {
|
|
|
|
narrative += '.';
|
|
|
|
} else {
|
|
|
|
narrative += ` across <strong>${taskGroup.count} allocations</strong>.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlSafe(narrative);
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get taskToggleRows() {
|
|
|
|
const taskNameToTaskToggles = {};
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
return this.args.summary.recommendations.reduce(
|
|
|
|
(taskToggleRows, recommendation) => {
|
|
|
|
let taskToggleRow = taskNameToTaskToggles[recommendation.task.name];
|
|
|
|
|
|
|
|
if (!taskToggleRow) {
|
|
|
|
taskToggleRow = {
|
|
|
|
recommendations: [],
|
|
|
|
task: recommendation.task,
|
|
|
|
};
|
|
|
|
|
|
|
|
taskNameToTaskToggles[recommendation.task.name] = taskToggleRow;
|
|
|
|
taskToggleRows.push(taskToggleRow);
|
|
|
|
}
|
2020-10-29 12:46:42 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
const isCpu = recommendation.resource === 'CPU';
|
|
|
|
const rowResourceProperty = isCpu ? 'cpu' : 'memory';
|
2020-10-29 12:46:42 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
taskToggleRow[rowResourceProperty] = {
|
|
|
|
recommendation,
|
|
|
|
isActive:
|
|
|
|
!this.args.summary.excludedRecommendations.includes(recommendation),
|
|
|
|
};
|
2020-10-29 12:46:42 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
if (isCpu) {
|
|
|
|
taskToggleRow.recommendations.unshift(recommendation);
|
|
|
|
} else {
|
|
|
|
taskToggleRow.recommendations.push(recommendation);
|
|
|
|
}
|
2020-10-29 12:46:42 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
return taskToggleRows;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get showToggleAllToggles() {
|
|
|
|
return this.taskToggleRows.length > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get allCpuToggleDisabled() {
|
2021-12-28 16:08:12 +00:00
|
|
|
return !this.args.summary.recommendations.filterBy('resource', 'CPU')
|
|
|
|
.length;
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get allMemoryToggleDisabled() {
|
2021-12-28 16:08:12 +00:00
|
|
|
return !this.args.summary.recommendations.filterBy('resource', 'MemoryMB')
|
|
|
|
.length;
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get cannotAccept() {
|
|
|
|
return (
|
2021-12-28 16:08:12 +00:00
|
|
|
this.args.summary.excludedRecommendations.length ==
|
|
|
|
this.args.summary.recommendations.length
|
2020-10-29 12:46:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:22:24 +00:00
|
|
|
get copyButtonLink() {
|
2021-12-28 16:08:12 +00:00
|
|
|
const path = this.router.urlFor(
|
|
|
|
'optimize.summary',
|
|
|
|
this.args.summary.slug,
|
|
|
|
{
|
|
|
|
queryParams: { namespace: this.args.summary.jobNamespace },
|
|
|
|
}
|
|
|
|
);
|
2020-11-04 18:22:24 +00:00
|
|
|
const { origin } = window.location;
|
|
|
|
|
|
|
|
return `${origin}${path}`;
|
|
|
|
}
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
@action
|
|
|
|
toggleAllRecommendationsForResource(resource) {
|
|
|
|
let enabled;
|
|
|
|
|
|
|
|
if (resource === 'CPU') {
|
|
|
|
this.allCpuToggleActive = !this.allCpuToggleActive;
|
|
|
|
enabled = this.allCpuToggleActive;
|
|
|
|
} else {
|
|
|
|
this.allMemoryToggleActive = !this.allMemoryToggleActive;
|
|
|
|
enabled = this.allMemoryToggleActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.args.summary.toggleAllRecommendationsForResource(resource, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
accept() {
|
2021-03-01 15:46:22 +00:00
|
|
|
this.storeCardHeight();
|
2020-10-29 12:46:42 +00:00
|
|
|
this.args.summary
|
|
|
|
.save()
|
2021-03-01 15:46:22 +00:00
|
|
|
.then(
|
|
|
|
() => this.onApplied.perform(),
|
2021-12-28 14:45:20 +00:00
|
|
|
(e) => this.onError.perform(e)
|
2021-03-01 15:46:22 +00:00
|
|
|
)
|
2021-12-28 14:45:20 +00:00
|
|
|
.catch((e) => {
|
2020-10-29 12:46:42 +00:00
|
|
|
if (!didCancel(e)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-03-08 17:28:36 +00:00
|
|
|
async dismiss() {
|
2021-03-01 15:46:22 +00:00
|
|
|
this.storeCardHeight();
|
2022-03-08 17:28:36 +00:00
|
|
|
const recommendations = await this.args.summary.recommendations;
|
|
|
|
|
|
|
|
this.args.summary.excludedRecommendations.pushObjects(recommendations);
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
this.args.summary
|
|
|
|
.save()
|
2021-03-01 15:46:22 +00:00
|
|
|
.then(
|
|
|
|
() => this.onDismissed.perform(),
|
2021-12-28 14:45:20 +00:00
|
|
|
(e) => this.onError.perform(e)
|
2021-03-01 15:46:22 +00:00
|
|
|
)
|
2021-12-28 14:45:20 +00:00
|
|
|
.catch((e) => {
|
2020-10-29 12:46:42 +00:00
|
|
|
if (!didCancel(e)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@(task(function* () {
|
2020-10-29 12:46:42 +00:00
|
|
|
this.interstitialComponent = 'accepted';
|
|
|
|
yield timeout(Ember.testing ? 0 : 2000);
|
|
|
|
|
|
|
|
this.args.proceed.perform();
|
|
|
|
this.resetInterstitial();
|
|
|
|
}).drop())
|
|
|
|
onApplied;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@(task(function* () {
|
|
|
|
const { manuallyDismissed } = yield new Promise((resolve) => {
|
2020-10-29 12:46:42 +00:00
|
|
|
this.proceedPromiseResolve = resolve;
|
|
|
|
this.interstitialComponent = 'dismissed';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!manuallyDismissed) {
|
|
|
|
yield timeout(Ember.testing ? 0 : 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.args.proceed.perform();
|
|
|
|
this.resetInterstitial();
|
|
|
|
}).drop())
|
|
|
|
onDismissed;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@(task(function* (error) {
|
|
|
|
yield new Promise((resolve) => {
|
2020-10-29 12:46:42 +00:00
|
|
|
this.proceedPromiseResolve = resolve;
|
|
|
|
this.interstitialComponent = 'error';
|
|
|
|
this.error = error.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.args.proceed.perform();
|
|
|
|
this.resetInterstitial();
|
|
|
|
}).drop())
|
|
|
|
onError;
|
|
|
|
|
|
|
|
get interstitialStyle() {
|
|
|
|
return htmlSafe(`height: ${this.cardHeight}px`);
|
|
|
|
}
|
|
|
|
|
|
|
|
resetInterstitial() {
|
|
|
|
if (!this.args.skipReset) {
|
|
|
|
this.interstitialComponent = undefined;
|
|
|
|
this.error = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2021-03-01 15:46:22 +00:00
|
|
|
cardInserted(element) {
|
|
|
|
this.element = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
storeCardHeight() {
|
|
|
|
this.cardHeight = this.element.clientHeight;
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function verbForDelta(delta) {
|
|
|
|
if (delta > 0) {
|
|
|
|
return 'add';
|
|
|
|
} else {
|
|
|
|
return 'save';
|
|
|
|
}
|
|
|
|
}
|