1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
8 import { CrushRuleService } from '../../../shared/api/crush-rule.service';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
11 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
12 import { CdValidators } from '../../../shared/forms/cd-validators';
13 import { CrushNode } from '../../../shared/models/crush-node';
14 import { FinishedTask } from '../../../shared/models/finished-task';
15 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
18 selector: 'cd-crush-rule-form-modal',
19 templateUrl: './crush-rule-form-modal.component.html',
20 styleUrls: ['./crush-rule-form-modal.component.scss']
22 export class CrushRuleFormModalComponent implements OnInit {
24 submitAction = new EventEmitter();
26 buckets: CrushNode[] = [];
27 failureDomains: { [type: string]: CrushNode[] } = {};
28 devices: string[] = [];
29 tooltips = this.crushRuleService.formTooltips;
36 private nodes: CrushNode[] = [];
37 private easyNodes: { [id: number]: CrushNode } = {};
40 private formBuilder: CdFormBuilder,
41 public bsModalRef: BsModalRef,
42 private taskWrapper: TaskWrapperService,
43 private crushRuleService: CrushRuleService,
45 public actionLabels: ActionLabelsI18n
47 this.action = this.actionLabels.CREATE;
48 this.resource = this.i18n('Crush Rule');
53 this.form = this.formBuilder.group({
59 Validators.pattern('[A-Za-z0-9_-]+'),
62 (value: any) => this.names && this.names.indexOf(value) !== -1
67 root: null, // Replaced with first root
68 // failure_domain: string
69 failure_domain: '', // Replaced with most common type
70 // device_class: string
71 device_class: '' // Replaced with device type if only one exists beneath domain
78 .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
80 nodes.forEach((node) => {
81 this.easyNodes[node.id] = node;
83 this.buckets = _.sortBy(
84 nodes.filter((n) => n.children),
90 this.form.get('root').valueChanges.subscribe((root: CrushNode) => this.updateRoot(root));
92 .get('failure_domain')
93 .valueChanges.subscribe((domain: string) => this.updateDevices(domain));
96 private preSelectRoot() {
97 const rootNode = this.nodes.find((node) => node.type === 'root');
98 this.form.silentSet('root', rootNode);
99 this.updateRoot(rootNode);
102 private updateRoot(rootNode: CrushNode) {
103 const nodes = this.getSubNodes(rootNode);
105 nodes.forEach((node) => {
106 if (!domains[node.type]) {
107 domains[node.type] = [];
109 domains[node.type].push(node);
111 Object.keys(domains).forEach((type) => {
112 if (domains[type].length <= 1) {
113 delete domains[type];
116 this.failureDomains = domains;
117 this.updateFailureDomain();
120 private getSubNodes(node: CrushNode): CrushNode[] {
121 let subNodes = [node]; // Includes parent node
122 if (!node.children) {
125 node.children.forEach((id) => {
126 const childNode = this.easyNodes[id];
127 subNodes = subNodes.concat(this.getSubNodes(childNode));
132 private updateFailureDomain() {
133 let failureDomain = this.getIncludedCustomValue(
135 Object.keys(this.failureDomains)
137 if (failureDomain === '') {
138 failureDomain = this.setMostCommonDomain();
140 this.updateDevices(failureDomain);
143 private getIncludedCustomValue(controlName: string, includedIn: string[]) {
144 const control = this.form.get(controlName);
145 return control.dirty && includedIn.includes(control.value) ? control.value : '';
148 private setMostCommonDomain(): string {
149 let winner = { n: 0, type: '' };
150 Object.keys(this.failureDomains).forEach((type) => {
151 const n = this.failureDomains[type].length;
153 winner = { n, type };
156 this.form.silentSet('failure_domain', winner.type);
160 updateDevices(failureDomain: string) {
161 const subNodes = _.flatten(
162 this.failureDomains[failureDomain].map((node) => this.getSubNodes(node))
164 this.devices = _.uniq(subNodes.filter((n) => n.device_class).map((n) => n.device_class)).sort();
166 this.devices.length === 1
168 : this.getIncludedCustomValue('device_class', this.devices);
169 this.form.get('device_class').setValue(device);
172 failureDomainKeys(): string[] {
173 return Object.keys(this.failureDomains).sort();
177 if (this.form.invalid) {
178 this.form.setErrors({ cdSubmitButton: true });
181 const rule = _.cloneDeep(this.form.value);
182 rule.root = rule.root.name;
183 if (rule.device_class === '') {
184 delete rule.device_class;
187 .wrapTaskAroundCall({
188 task: new FinishedTask('crushRule/create', rule),
189 call: this.crushRuleService.create(rule)
194 this.form.setErrors({ cdSubmitButton: true });
197 this.bsModalRef.hide();
198 this.submitAction.emit(rule);