src/app/shared/components/grafana/grafana.component.ts
selector | cd-grafana |
styleUrls | ./grafana.component.scss |
templateUrl | ./grafana.component.html |
Properties |
Methods |
Inputs |
constructor(summaryService: SummaryService, sanitizer: DomSanitizer, settingsService: SettingsService, cephReleaseNamePipe: CephReleaseNamePipe, i18n: I18n)
|
||||||||||||||||||
Parameters :
|
grafanaPath | |
Type : string
|
|
grafanaStyle | |
Type : string
|
|
uid | |
Type : string
|
|
getFrame |
getFrame()
|
Returns :
void
|
ngOnChanges |
ngOnChanges()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onTimepickerChange |
onTimepickerChange()
|
Returns :
void
|
reset |
reset()
|
Returns :
void
|
baseUrl |
Type : any
|
dashboardExist |
Default value : true
|
Readonly DEFAULT_TIME |
Type : string
|
Default value : 'from=now-1h&to=now'
|
docsUrl |
Type : string
|
grafanaExist |
Default value : false
|
grafanaSrc |
Type : SafeUrl
|
grafanaTimes |
Type : any
|
host |
Type : string
|
loading |
Default value : true
|
mode |
Type : string
|
Default value : '&kiosk'
|
panelStyle |
Type : any
|
port |
Type : number
|
protocol |
Type : string
|
styles |
Type : object
|
Default value : {}
|
time |
Type : string
|
url |
Type : string
|
import { Component, Input, OnChanges, OnInit } from '@angular/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { I18n } from '@ngx-translate/i18n-polyfill';
import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
import { SummaryService } from '../../../shared/services/summary.service';
import { SettingsService } from '../../api/settings.service';
@Component({
selector: 'cd-grafana',
templateUrl: './grafana.component.html',
styleUrls: ['./grafana.component.scss']
})
export class GrafanaComponent implements OnInit, OnChanges {
grafanaSrc: SafeUrl;
url: string;
protocol: string;
host: string;
port: number;
baseUrl: any;
panelStyle: any;
grafanaExist = false;
mode = '&kiosk';
loading = true;
styles = {};
dashboardExist = true;
time: string;
grafanaTimes: any;
readonly DEFAULT_TIME: string = 'from=now-1h&to=now';
@Input()
grafanaPath: string;
@Input()
grafanaStyle: string;
@Input()
uid: string;
docsUrl: string;
constructor(
private summaryService: SummaryService,
private sanitizer: DomSanitizer,
private settingsService: SettingsService,
private cephReleaseNamePipe: CephReleaseNamePipe,
private i18n: I18n
) {
this.grafanaTimes = [
{
name: this.i18n('Last 5 minutes'),
value: 'from=now-5m&to=now'
},
{
name: this.i18n('Last 15 minutes'),
value: 'from=now-15m&to=now'
},
{
name: this.i18n('Last 30 minutes'),
value: 'from=now-30m&to=now'
},
{
name: this.i18n('Last 1 hour (Default)'),
value: 'from=now-1h&to=now'
},
{
name: this.i18n('Last 3 hours'),
value: 'from=now-3h&to=now'
},
{
name: this.i18n('Last 6 hours'),
value: 'from=now-6h&to=now'
},
{
name: this.i18n('Last 12 hours'),
value: 'from=now-12h&to=now'
},
{
name: this.i18n('Last 24 hours'),
value: 'from=now-24h&to=now'
},
{
name: this.i18n('Yesterday'),
value: 'from=now-1d%2Fd&to=now-1d%2Fd'
},
{
name: this.i18n('Today'),
value: 'from=now%2Fd&to=now%2Fd'
},
{
name: this.i18n('Today so far'),
value: 'from=now%2Fd&to=now'
},
{
name: this.i18n('Day before yesterday'),
value: 'from=now-2d%2Fd&to=now-2d%2Fd'
},
{
name: this.i18n('Last 2 days'),
value: 'from=now-2d&to=now'
},
{
name: this.i18n('This day last week'),
value: 'from=now-7d%2Fd&to=now-7d%2Fd'
},
{
name: this.i18n('Previous week'),
value: 'from=now-1w%2Fw&to=now-1w%2Fw'
},
{
name: this.i18n('This week'),
value: 'from=now%2Fw&to=now%2Fw'
},
{
name: this.i18n('This week so far'),
value: 'from=now%2Fw&to=now'
},
{
name: this.i18n('Last 7 days'),
value: 'from=now-7d&to=now'
},
{
name: this.i18n('Previous month'),
value: 'from=now-1M%2FM&to=now-1M%2FM'
},
{
name: this.i18n('This month'),
value: 'from=now%2FM&to=now%2FM'
},
{
name: this.i18n('This month so far'),
value: 'from=now%2FM&to=now'
},
{
name: this.i18n('Last 30 days'),
value: 'from=now-30d&to=now'
},
{
name: this.i18n('Last 90 days'),
value: 'from=now-90d&to=now'
},
{
name: this.i18n('Last 6 months'),
value: 'from=now-6M&to=now'
},
{
name: this.i18n('Last 1 year'),
value: 'from=now-1y&to=now'
},
{
name: this.i18n('Previous year'),
value: 'from=now-1y%2Fy&to=now-1y%2Fy'
},
{
name: this.i18n('This year'),
value: 'from=now%2Fy&to=now%2Fy'
},
{
name: this.i18n('This year so far'),
value: 'from=now%2Fy&to=now'
},
{
name: this.i18n('Last 2 years'),
value: 'from=now-2y&to=now'
},
{
name: this.i18n('Last 5 years'),
value: 'from=now-5y&to=now'
}
];
}
ngOnInit() {
this.time = this.DEFAULT_TIME;
this.styles = {
one: 'grafana_one',
two: 'grafana_two',
three: 'grafana_three'
};
const subs = this.summaryService.subscribe((summary: any) => {
if (!summary) {
return;
}
const releaseName = this.cephReleaseNamePipe.transform(summary.version);
this.docsUrl =
`http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/` +
`#enabling-the-embedding-of-grafana-dashboards`;
setTimeout(() => {
subs.unsubscribe();
}, 0);
});
this.settingsService.ifSettingConfigured('api/grafana/url', (url) => {
this.grafanaExist = true;
this.loading = false;
this.baseUrl = url + '/d/';
this.getFrame();
});
this.panelStyle = this.styles[this.grafanaStyle];
}
getFrame() {
this.settingsService
.validateGrafanaDashboardUrl(this.uid)
.subscribe((data: any) => (this.dashboardExist = data === 200));
this.url =
this.baseUrl +
this.uid +
'/' +
this.grafanaPath +
'&refresh=2s' +
this.mode +
'&' +
this.time;
this.grafanaSrc = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
}
onTimepickerChange() {
if (this.grafanaExist) {
this.getFrame();
}
}
reset() {
this.time = this.DEFAULT_TIME;
if (this.grafanaExist) {
this.getFrame();
}
}
ngOnChanges() {
if (this.grafanaExist) {
this.getFrame();
}
}
}
<!-- Embed dashboard -->
<cd-loading-panel *ngIf="loading && grafanaExist"
i18n>Loading panel data...</cd-loading-panel>
<cd-info-panel *ngIf="!grafanaExist"
i18n>Please consult the
<a href="{{ docsUrl }}" target="_blank">documentation</a> on how to
configure and enable the monitoring functionality.</cd-info-panel>
<cd-info-panel *ngIf="!dashboardExist"
i18n>Grafana Dashboard doesn't exist. Please refer to
<a href="{{ docsUrl }}" target="_blank">documentation</a> on how to
add dashboards to Grafana.</cd-info-panel>
<div class="row"
*ngIf="grafanaExist && dashboardExist">
<div class="col-md-12 timepicker">
<label class="control-label col-sm-1"
for="timepicker-control">
<ng-container i18n>Grafana Time Picker</ng-container>
</label>
<div class="col-sm-9">
<select id="timepicker"
name="timepicker-control"
class="form-control "
[(ngModel)]="time"
(ngModelChange)="onTimepickerChange($event)">
<option *ngFor="let key of grafanaTimes"
[ngValue]="key.value">{{ key.name }}
</option>
</select>
</div>
<button class="btn btn-sm"
i18n-title
title="Reset Settings"
(click)="reset()">
<i class="fa fa-undo"></i>
</button>
<br>
</div>
<div class="col-md-12">
<div class="grafana-container">
<iframe #iframe
id="iframe"
[src]="grafanaSrc"
class="grafana"
[ngClass]="panelStyle"
frameborder="0"
scrolling="no">
</iframe>
</div>
</div>
</div>
./grafana.component.scss
.grafana {
width: 100%;
height: 600px;
z-index: 0;
}
.grafana_one {
height: 400px;
}
.grafana_two {
height: 750px;
}
.grafana_three {
height: 900px;
}
.timepicker {
select {
display: inline-block;
}
label {
font-size: 1.05em;
padding: 5px !important;
}
}
button {
margin-bottom: 10px;
margin-left: 10px;
float: right;
i {
font-size: 14px;
padding: 2px;
}
}
.dropdown-menu {
top: 20px;
right: 20px;
left: auto;
}