1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
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';
20 selector: 'cd-telemetry',
21 templateUrl: './telemetry.component.html',
22 styleUrls: ['./telemetry.component.scss']
24 export class TelemetryComponent extends CdForm implements OnInit {
25 configForm: CdFormGroup;
27 moduleEnabled: boolean;
29 previewForm: CdFormGroup;
40 report: object = undefined;
41 reportId: number = undefined;
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
60 this.mgrModuleService.getOptions('telemetry'),
61 this.mgrModuleService.getConfig('telemetry')
63 observableForkJoin(observables).subscribe(
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);
81 private createConfigForm() {
82 const controlsConfig = {};
83 _.forEach(Object.values(this.options), (option) => {
84 controlsConfig[option.name] = [option.default_value, this.getValidators(option)];
86 this.configForm = this.formBuilder.group(controlsConfig);
89 private createPreviewForm() {
91 report: JSON.stringify(this.report, null, 2),
92 reportId: this.reportId,
93 licenseAgrmt: [this.licenseAgrmt, Validators.requiredTrue]
95 this.previewForm = this.formBuilder.group(controls);
98 private getValidators(option: any): ValidatorFn[] {
100 switch (option.type) {
102 result.push(CdValidators.number());
103 result.push(Validators.required);
104 if (_.isNumber(option.min)) {
105 result.push(Validators.min(option.min));
107 if (_.isNumber(option.max)) {
108 result.push(Validators.max(option.max));
112 if (_.isNumber(option.min)) {
113 result.push(Validators.minLength(option.min));
115 if (_.isNumber(option.max)) {
116 result.push(Validators.maxLength(option.max));
123 private getReport() {
126 this.telemetryService.getReport().subscribe(
129 this.reportId = resp['report']['report_id'];
130 this.createPreviewForm();
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;
149 this.mgrModuleService.updateConfig('telemetry', config).subscribe(
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.`,
161 // Reset the 'Submit' button.
162 this.configForm.setErrors({ cdSubmitButton: true });
167 disableModule(message: string = null, followUpFunc: Function = null) {
168 this.telemetryService.enable(false).subscribe(() => {
169 this.telemetryNotificationService.setVisibility(true);
171 this.notificationService.show(NotificationType.success, message);
176 this.router.navigate(['']);
182 if (this.configForm.pristine) {
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.`
200 this.router.navigate(['']);