]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / telemetry / telemetry.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
4
5 import _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
7
8 import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
9 import { TelemetryService } from '~/app/shared/api/telemetry.service';
10 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
11 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
12 import { CdForm } from '~/app/shared/forms/cd-form';
13 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
14 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
15 import { CdValidators } from '~/app/shared/forms/cd-validators';
16 import { NotificationService } from '~/app/shared/services/notification.service';
17 import { TelemetryNotificationService } from '~/app/shared/services/telemetry-notification.service';
18
19 @Component({
20 selector: 'cd-telemetry',
21 templateUrl: './telemetry.component.html',
22 styleUrls: ['./telemetry.component.scss']
23 })
24 export class TelemetryComponent extends CdForm implements OnInit {
25 configForm: CdFormGroup;
26 licenseAgrmt = false;
27 moduleEnabled: boolean;
28 options: Object = {};
29 previewForm: CdFormGroup;
30 requiredFields = [
31 'channel_basic',
32 'channel_crash',
33 'channel_device',
34 'channel_ident',
35 'interval',
36 'proxy',
37 'contact',
38 'description'
39 ];
40 report: object = undefined;
41 reportId: number = undefined;
42 sendToUrl = '';
43 sendToDeviceUrl = '';
44 step = 1;
45
46 constructor(
47 public actionLabels: ActionLabelsI18n,
48 private formBuilder: CdFormBuilder,
49 private mgrModuleService: MgrModuleService,
50 private notificationService: NotificationService,
51 private router: Router,
52 private telemetryService: TelemetryService,
53 private telemetryNotificationService: TelemetryNotificationService
54 ) {
55 super();
56 }
57
58 ngOnInit() {
59 const observables = [
60 this.mgrModuleService.getOptions('telemetry'),
61 this.mgrModuleService.getConfig('telemetry')
62 ];
63 observableForkJoin(observables).subscribe(
64 (resp: object) => {
65 const configResp = resp[1];
66 this.moduleEnabled = configResp['enabled'];
67 this.sendToUrl = configResp['url'];
68 this.sendToDeviceUrl = configResp['device_url'];
69 this.options = _.pick(resp[0], this.requiredFields);
70 const configs = _.pick(configResp, this.requiredFields);
71 this.createConfigForm();
72 this.configForm.setValue(configs);
73 this.loadingReady();
74 },
75 (_error) => {
76 this.loadingError();
77 }
78 );
79 }
80
81 private createConfigForm() {
82 const controlsConfig = {};
83 _.forEach(Object.values(this.options), (option) => {
84 controlsConfig[option.name] = [option.default_value, this.getValidators(option)];
85 });
86 this.configForm = this.formBuilder.group(controlsConfig);
87 }
88
89 private createPreviewForm() {
90 const controls = {
91 report: JSON.stringify(this.report, null, 2),
92 reportId: this.reportId,
93 licenseAgrmt: [this.licenseAgrmt, Validators.requiredTrue]
94 };
95 this.previewForm = this.formBuilder.group(controls);
96 }
97
98 private getValidators(option: any): ValidatorFn[] {
99 const result = [];
100 switch (option.type) {
101 case 'int':
102 result.push(CdValidators.number());
103 result.push(Validators.required);
104 if (_.isNumber(option.min)) {
105 result.push(Validators.min(option.min));
106 }
107 if (_.isNumber(option.max)) {
108 result.push(Validators.max(option.max));
109 }
110 break;
111 case 'str':
112 if (_.isNumber(option.min)) {
113 result.push(Validators.minLength(option.min));
114 }
115 if (_.isNumber(option.max)) {
116 result.push(Validators.maxLength(option.max));
117 }
118 break;
119 }
120 return result;
121 }
122
123 private getReport() {
124 this.loadingStart();
125
126 this.telemetryService.getReport().subscribe(
127 (resp: object) => {
128 this.report = resp;
129 this.reportId = resp['report']['report_id'];
130 this.createPreviewForm();
131 this.loadingReady();
132 this.step++;
133 },
134 (_error) => {
135 this.loadingError();
136 }
137 );
138 }
139
140 updateConfig() {
141 const config = {};
142 _.forEach(Object.values(this.options), (option) => {
143 const control = this.configForm.get(option.name);
144 // Append the option only if the value has been modified.
145 if (control.dirty && control.valid) {
146 config[option.name] = control.value;
147 }
148 });
149 this.mgrModuleService.updateConfig('telemetry', config).subscribe(
150 () => {
151 this.disableModule(
152 $localize`Your settings have been applied successfully. \
153 Due to privacy/legal reasons the Telemetry module is now disabled until you \
154 complete the next step and accept the license.`,
155 () => {
156 this.getReport();
157 }
158 );
159 },
160 () => {
161 // Reset the 'Submit' button.
162 this.configForm.setErrors({ cdSubmitButton: true });
163 }
164 );
165 }
166
167 disableModule(message: string = null, followUpFunc: Function = null) {
168 this.telemetryService.enable(false).subscribe(() => {
169 this.telemetryNotificationService.setVisibility(true);
170 if (message) {
171 this.notificationService.show(NotificationType.success, message);
172 }
173 if (followUpFunc) {
174 followUpFunc();
175 } else {
176 this.router.navigate(['']);
177 }
178 });
179 }
180
181 next() {
182 if (this.configForm.pristine) {
183 this.getReport();
184 } else {
185 this.updateConfig();
186 }
187 }
188
189 back() {
190 this.step--;
191 }
192
193 onSubmit() {
194 this.telemetryService.enable().subscribe(() => {
195 this.telemetryNotificationService.setVisibility(false);
196 this.notificationService.show(
197 NotificationType.success,
198 $localize`The Telemetry module has been configured and activated successfully.`
199 );
200 this.router.navigate(['']);
201 });
202 }
203 }