]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/components/grafana/grafana.component.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / components / grafana / grafana.component.ts
1 import { Component, Input, OnChanges, OnInit } from '@angular/core';
2 import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5
6 import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
7 import { SummaryService } from '../../../shared/services/summary.service';
8 import { SettingsService } from '../../api/settings.service';
9
10 @Component({
11 selector: 'cd-grafana',
12 templateUrl: './grafana.component.html',
13 styleUrls: ['./grafana.component.scss']
14 })
15 export class GrafanaComponent implements OnInit, OnChanges {
16 grafanaSrc: SafeUrl;
17 url: string;
18 protocol: string;
19 host: string;
20 port: number;
21 baseUrl: any;
22 panelStyle: any;
23 grafanaExist = false;
24 mode = '&kiosk';
25 loading = true;
26 styles = {};
27 dashboardExist = true;
28 time: string;
29 grafanaTimes: any;
30 readonly DEFAULT_TIME: string = 'from=now-1h&to=now';
31
32 @Input()
33 grafanaPath: string;
34 @Input()
35 grafanaStyle: string;
36 @Input()
37 uid: string;
38
39 docsUrl: string;
40
41 constructor(
42 private summaryService: SummaryService,
43 private sanitizer: DomSanitizer,
44 private settingsService: SettingsService,
45 private cephReleaseNamePipe: CephReleaseNamePipe,
46 private i18n: I18n
47 ) {
48 this.grafanaTimes = [
49 {
50 name: this.i18n('Last 5 minutes'),
51 value: 'from=now-5m&to=now'
52 },
53 {
54 name: this.i18n('Last 15 minutes'),
55 value: 'from=now-15m&to=now'
56 },
57 {
58 name: this.i18n('Last 30 minutes'),
59 value: 'from=now-30m&to=now'
60 },
61 {
62 name: this.i18n('Last 1 hour (Default)'),
63 value: 'from=now-1h&to=now'
64 },
65 {
66 name: this.i18n('Last 3 hours'),
67 value: 'from=now-3h&to=now'
68 },
69 {
70 name: this.i18n('Last 6 hours'),
71 value: 'from=now-6h&to=now'
72 },
73 {
74 name: this.i18n('Last 12 hours'),
75 value: 'from=now-12h&to=now'
76 },
77 {
78 name: this.i18n('Last 24 hours'),
79 value: 'from=now-24h&to=now'
80 },
81 {
82 name: this.i18n('Yesterday'),
83 value: 'from=now-1d%2Fd&to=now-1d%2Fd'
84 },
85 {
86 name: this.i18n('Today'),
87 value: 'from=now%2Fd&to=now%2Fd'
88 },
89 {
90 name: this.i18n('Today so far'),
91 value: 'from=now%2Fd&to=now'
92 },
93 {
94 name: this.i18n('Day before yesterday'),
95 value: 'from=now-2d%2Fd&to=now-2d%2Fd'
96 },
97 {
98 name: this.i18n('Last 2 days'),
99 value: 'from=now-2d&to=now'
100 },
101 {
102 name: this.i18n('This day last week'),
103 value: 'from=now-7d%2Fd&to=now-7d%2Fd'
104 },
105 {
106 name: this.i18n('Previous week'),
107 value: 'from=now-1w%2Fw&to=now-1w%2Fw'
108 },
109 {
110 name: this.i18n('This week'),
111 value: 'from=now%2Fw&to=now%2Fw'
112 },
113 {
114 name: this.i18n('This week so far'),
115 value: 'from=now%2Fw&to=now'
116 },
117 {
118 name: this.i18n('Last 7 days'),
119 value: 'from=now-7d&to=now'
120 },
121 {
122 name: this.i18n('Previous month'),
123 value: 'from=now-1M%2FM&to=now-1M%2FM'
124 },
125 {
126 name: this.i18n('This month'),
127 value: 'from=now%2FM&to=now%2FM'
128 },
129 {
130 name: this.i18n('This month so far'),
131 value: 'from=now%2FM&to=now'
132 },
133 {
134 name: this.i18n('Last 30 days'),
135 value: 'from=now-30d&to=now'
136 },
137 {
138 name: this.i18n('Last 90 days'),
139 value: 'from=now-90d&to=now'
140 },
141 {
142 name: this.i18n('Last 6 months'),
143 value: 'from=now-6M&to=now'
144 },
145 {
146 name: this.i18n('Last 1 year'),
147 value: 'from=now-1y&to=now'
148 },
149 {
150 name: this.i18n('Previous year'),
151 value: 'from=now-1y%2Fy&to=now-1y%2Fy'
152 },
153 {
154 name: this.i18n('This year'),
155 value: 'from=now%2Fy&to=now%2Fy'
156 },
157 {
158 name: this.i18n('This year so far'),
159 value: 'from=now%2Fy&to=now'
160 },
161 {
162 name: this.i18n('Last 2 years'),
163 value: 'from=now-2y&to=now'
164 },
165 {
166 name: this.i18n('Last 5 years'),
167 value: 'from=now-5y&to=now'
168 }
169 ];
170 }
171
172 ngOnInit() {
173 this.time = this.DEFAULT_TIME;
174 this.styles = {
175 one: 'grafana_one',
176 two: 'grafana_two',
177 three: 'grafana_three'
178 };
179
180 const subs = this.summaryService.subscribe((summary: any) => {
181 if (!summary) {
182 return;
183 }
184
185 const releaseName = this.cephReleaseNamePipe.transform(summary.version);
186 this.docsUrl =
187 `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/` +
188 `#enabling-the-embedding-of-grafana-dashboards`;
189
190 setTimeout(() => {
191 subs.unsubscribe();
192 }, 0);
193 });
194 this.settingsService.ifSettingConfigured('api/grafana/url', (url) => {
195 this.grafanaExist = true;
196 this.loading = false;
197 this.baseUrl = url + '/d/';
198 this.getFrame();
199 });
200 this.panelStyle = this.styles[this.grafanaStyle];
201 }
202
203 getFrame() {
204 this.settingsService
205 .validateGrafanaDashboardUrl(this.uid)
206 .subscribe((data: any) => (this.dashboardExist = data === 200));
207 this.url =
208 this.baseUrl +
209 this.uid +
210 '/' +
211 this.grafanaPath +
212 '&refresh=2s' +
213 this.mode +
214 '&' +
215 this.time;
216 this.grafanaSrc = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
217 }
218
219 onTimepickerChange() {
220 if (this.grafanaExist) {
221 this.getFrame();
222 }
223 }
224
225 reset() {
226 this.time = this.DEFAULT_TIME;
227 if (this.grafanaExist) {
228 this.getFrame();
229 }
230 }
231
232 ngOnChanges() {
233 if (this.grafanaExist) {
234 this.getFrame();
235 }
236 }
237 }