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(nodes.filter((n) => n.children), 'name');
87 this.form.get('root').valueChanges.subscribe((root: CrushNode) => this.updateRoot(root));
89 .get('failure_domain')
90 .valueChanges.subscribe((domain: string) => this.updateDevices(domain));
93 private preSelectRoot() {
94 const rootNode = this.nodes.find((node) => node.type === 'root');
95 this.form.silentSet('root', rootNode);
96 this.updateRoot(rootNode);
99 private updateRoot(rootNode: CrushNode) {
100 const nodes = this.getSubNodes(rootNode);
102 nodes.forEach((node) => {
103 if (!domains[node.type]) {
104 domains[node.type] = [];
106 domains[node.type].push(node);
108 Object.keys(domains).forEach((type) => {
109 if (domains[type].length <= 1) {
110 delete domains[type];
113 this.failureDomains = domains;
114 this.updateFailureDomain();
117 private getSubNodes(node: CrushNode): CrushNode[] {
118 let subNodes = [node]; // Includes parent node
119 if (!node.children) {
122 node.children.forEach((id) => {
123 const childNode = this.easyNodes[id];
124 subNodes = subNodes.concat(this.getSubNodes(childNode));
129 private updateFailureDomain() {
130 let failureDomain = this.getIncludedCustomValue(
132 Object.keys(this.failureDomains)
134 if (failureDomain === '') {
135 failureDomain = this.setMostCommonDomain();
137 this.updateDevices(failureDomain);
140 private getIncludedCustomValue(controlName: string, includedIn: string[]) {
141 const control = this.form.get(controlName);
142 return control.dirty && includedIn.includes(control.value) ? control.value : '';
145 private setMostCommonDomain(): string {
146 let winner = { n: 0, type: '' };
147 Object.keys(this.failureDomains).forEach((type) => {
148 const n = this.failureDomains[type].length;
150 winner = { n, type };
153 this.form.silentSet('failure_domain', winner.type);
157 updateDevices(failureDomain: string) {
158 const subNodes = _.flatten(
159 this.failureDomains[failureDomain].map((node) => this.getSubNodes(node))
161 this.devices = _.uniq(subNodes.filter((n) => n.device_class).map((n) => n.device_class)).sort();
163 this.devices.length === 1
165 : this.getIncludedCustomValue('device_class', this.devices);
166 this.form.get('device_class').setValue(device);
169 failureDomainKeys(): string[] {
170 return Object.keys(this.failureDomains).sort();
174 if (this.form.invalid) {
175 this.form.setErrors({ cdSubmitButton: true });
178 const rule = _.cloneDeep(this.form.value);
179 rule.root = rule.root.name;
180 if (rule.device_class === '') {
181 delete rule.device_class;
184 .wrapTaskAroundCall({
185 task: new FinishedTask('crushRule/create', rule),
186 call: this.crushRuleService.create(rule)
191 this.form.setErrors({ cdSubmitButton: true });
194 this.bsModalRef.hide();
195 this.submitAction.emit(rule);