1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
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';
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';
21 selector: 'cd-telemetry',
22 templateUrl: './telemetry.component.html',
23 styleUrls: ['./telemetry.component.scss']
25 export class TelemetryComponent implements OnInit {
30 configForm: CdFormGroup;
33 moduleEnabled: boolean;
35 previewForm: CdFormGroup;
46 report: object = undefined;
47 reportId: number = undefined;
53 private formBuilder: CdFormBuilder,
54 private mgrModuleService: MgrModuleService,
55 private notificationService: NotificationService,
56 private router: Router,
57 private telemetryService: TelemetryService,
59 private textToDownloadService: TextToDownloadService,
60 private telemetryNotificationService: TelemetryNotificationService
66 this.mgrModuleService.getOptions('telemetry'),
67 this.mgrModuleService.getConfig('telemetry')
69 observableForkJoin(observables).subscribe(
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);
87 private createConfigForm() {
88 const controlsConfig = {};
89 _.forEach(Object.values(this.options), (option) => {
90 controlsConfig[option.name] = [option.default_value, this.getValidators(option)];
92 this.configForm = this.formBuilder.group(controlsConfig);
95 private createPreviewForm() {
97 report: JSON.stringify(this.report, null, 2),
98 reportId: this.reportId,
99 licenseAgrmt: [this.licenseAgrmt, Validators.requiredTrue]
101 this.previewForm = this.formBuilder.group(controls);
104 private getValidators(option: any): ValidatorFn[] {
106 switch (option.type) {
108 result.push(CdValidators.number());
109 result.push(Validators.required);
110 if (_.isNumber(option.min)) {
111 result.push(Validators.min(option.min));
113 if (_.isNumber(option.max)) {
114 result.push(Validators.max(option.max));
118 if (_.isNumber(option.min)) {
119 result.push(Validators.minLength(option.min));
121 if (_.isNumber(option.max)) {
122 result.push(Validators.maxLength(option.max));
129 private getReport() {
131 this.telemetryService.getReport().subscribe(
134 this.reportId = resp['report']['report_id'];
135 this.createPreviewForm();
136 this.loading = false;
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;
154 this.mgrModuleService.updateConfig('telemetry', config).subscribe(
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.`
168 // Reset the 'Submit' button.
169 this.configForm.setErrors({ cdSubmitButton: true });
174 download(report: object, fileName: string) {
175 this.textToDownloadService.download(JSON.stringify(report, null, 2), fileName);
178 disableModule(message: string = null, followUpFunc: Function = null) {
179 this.telemetryService.enable(false).subscribe(() => {
180 this.telemetryNotificationService.setVisibility(true);
182 this.notificationService.show(NotificationType.success, message);
187 this.router.navigate(['']);
193 if (this.configForm.pristine) {
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.')
211 this.router.navigate(['']);