Added Grafana url setting in config panel
[src/xds/xds-agent.git] / webapp / src / app / pages / supervision / supervision.component.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, OnInit, Input } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { Subject } from 'rxjs/Subject';
22 import { NbThemeService } from '@nebular/theme';
23 import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
24
25 import { ConfigService, IConfig } from '../../@core-xds/services/config.service';
26 import { SupervisionService } from '../../@core-xds/services/supervision.service';
27 import { AlertService } from '../../@core-xds/services/alert.service';
28
29 export interface GrafanaDashboard {
30   name: string;
31   shortname: string;
32   url?: string;
33   safeUrl?: SafeResourceUrl;
34 }
35
36 export interface GrafanaPanel {
37   name: string;
38   index: string;
39   url?: string;
40   safeUrl?: SafeResourceUrl;
41 }
42
43 @Component({
44   selector: 'xds-supervision',
45   styleUrls: ['./supervision.component.scss'],
46   templateUrl: './supervision.component.html',
47 })
48
49 export class SupervisionComponent implements OnInit {
50
51   @Input() theme = 'light';
52   @Input() tm_from = 1528988550450;
53   @Input() tm_to = 1528988842496;
54   @Input() scroll_factor = 10000;
55   @Input() zoom_factor = 100000;
56
57   displayMode = 'dashboard';
58
59   private dashboards: Map<string, GrafanaDashboard> = new Map<string, GrafanaDashboard>([
60     ['xds_supervisor', { name: 'AGL XDS Supervisor', shortname: 'agl-xds-supervisor' }],
61   ]);
62
63   private panels: Map<string, GrafanaPanel> = new Map<string, GrafanaPanel>([
64     ['table', { name: 'Supervisor traces table', index: '2' }],
65     ['evt_data_bytes', { name: 'Requests & Events per second', index: '5' }],
66     ['req_evts_per_sec', { name: 'Events Data bytes', index: '12' }],
67   ]);
68
69   constructor(
70     private supervisionSvr: SupervisionService,
71     private alert: AlertService,
72     private themeService: NbThemeService,
73     private sanitizer: DomSanitizer,
74     private configSvr: ConfigService,
75   ) {
76   }
77
78   Config: IConfig = <IConfig>{};
79
80   ngOnInit() {
81     this.configSvr.Conf$.subscribe(cfg => this.Config = cfg);
82
83     this._initDashboard();
84     this._initPanels();
85
86     this.themeService.onThemeChange().subscribe(tm => {
87       this.theme = (tm.name === 'cosmic') ? 'dark' : 'light';
88       this.themeUpdate();
89     });
90   }
91
92   getDashboard(name: string): SafeResourceUrl {
93     return this.dashboards.get(name).safeUrl;
94   }
95
96   getPanel(name: string): SafeResourceUrl {
97     return this.panels.get(name).safeUrl;
98   }
99
100   displayModeChange() {
101     if (this.displayMode === 'dashboard') {
102       this.displayMode = 'panels';
103     } else {
104       this.displayMode = 'dashboard';
105     }
106   }
107
108   themeUpdate() {
109     this._initDashboard();
110     this._initPanels();
111   }
112
113   timeChange(val: number) {
114     this.tm_from += val * this.scroll_factor;
115     this.tm_to += val * this.scroll_factor;
116     this._initPanels();
117   }
118
119   zoomOut() {
120     this.tm_from -= this.zoom_factor;
121     this.tm_to += this.zoom_factor;
122     this._initPanels();
123   }
124
125
126   private _initDashboard() {
127     this.dashboards.forEach(dd => {
128       dd.url = this._buildDashboardUrl(dd.shortname, this.tm_from, this.tm_to, this.theme);
129       dd.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(dd.url);
130     });
131   }
132   private _initPanels() {
133     this.panels.forEach(gg => {
134       gg.url = this._buildPanelUrl(gg.index, this.tm_from, this.tm_to, this.theme);
135       gg.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(gg.url);
136     });
137   }
138
139   private _buildDashboardUrl(sname: string, from: number, to: number, theme: string) {
140     // FIXME get sname from config to support several dashboards
141     let url = 'http://localhost:3000/d/Lbpwc6Iiz/' + sname;
142     if (this.Config.grafanaDashboardUrl !== '') {
143       url = this.Config.grafanaDashboardUrl;
144     }
145     url += '?orgId=1';
146     url += '&from=' + from;
147     url += '&to=' + to;
148     url += '&theme=' + theme;
149     url += '&sidemenu=close';
150     return url;
151   }
152
153   private _buildPanelUrl(idx: string, from: number, to: number, theme: string) {
154     let url = 'http://localhost:3000/d-solo/Lbpwc6Iiz/agl-xds-supervisor';
155     if (this.Config.grafanaDashboardUrl !== '') {
156       url = this.Config.grafanaDashboardUrl;
157     }
158     url += '?panelId=' + idx;
159     url += '&orgId=1';
160     url += '&from=' + from;
161     url += '&to=' + to;
162     url += '&theme=' + theme;
163     url += '&sidemenu=close';
164     return url;
165   }
166 }