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