-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mgr/dashboard: fix variable names and using Math.round() now mgr/dashboard cluster endpoint changes mgr/dashboard: refactoring mgr/dashboard: string concatenation to interpolation and typedict to namedtuple mgr/dashboard: change function order Signed-off-by: Pedro Gonzalez Gomez <pegonzal@redhat.com>
- Loading branch information
Showing
13 changed files
with
356 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
.../dashboard/frontend/src/app/ceph/new-dashboard/dashboard-pie/dashboard-pie.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div class="chart-container"> | ||
<canvas baseChart | ||
#chartCanvas | ||
[datasets]="chartConfig.dataset" | ||
[chartType]="chartConfig.chartType" | ||
[options]="chartConfig.options" | ||
[labels]="chartConfig.labels" | ||
[colors]="chartConfig.colors" | ||
[plugins]="doughnutChartPlugins" | ||
class="chart-canvas"> | ||
</canvas> | ||
<div class="chartjs-tooltip" | ||
#chartTooltip> | ||
<table></table> | ||
</div> | ||
</div> |
22 changes: 22 additions & 0 deletions
22
.../dashboard/frontend/src/app/ceph/new-dashboard/dashboard-pie/dashboard-pie.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
@use './src/styles/chart-tooltip'; | ||
|
||
$canvas-width: 100%; | ||
$canvas-height: 100%; | ||
|
||
.chart-container { | ||
height: $canvas-height; | ||
margin-left: auto; | ||
margin-right: auto; | ||
position: unset; | ||
width: $canvas-width; | ||
} | ||
|
||
.chart-canvas { | ||
height: $canvas-height; | ||
margin-left: auto; | ||
margin-right: auto; | ||
max-height: $canvas-height; | ||
max-width: $canvas-width; | ||
position: unset; | ||
width: $canvas-width; | ||
} |
27 changes: 27 additions & 0 deletions
27
...shboard/frontend/src/app/ceph/new-dashboard/dashboard-pie/dashboard-pie.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NO_ERRORS_SCHEMA } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { CssHelper } from '~/app/shared/classes/css-helper'; | ||
import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe'; | ||
import { configureTestBed } from '~/testing/unit-test-helper'; | ||
import { DashboardPieComponent } from './dashboard-pie.component'; | ||
|
||
describe('DashboardPieComponent', () => { | ||
let component: DashboardPieComponent; | ||
let fixture: ComponentFixture<DashboardPieComponent>; | ||
|
||
configureTestBed({ | ||
schemas: [NO_ERRORS_SCHEMA], | ||
declarations: [DashboardPieComponent], | ||
providers: [CssHelper, DimlessBinaryPipe] | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(DashboardPieComponent); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
185 changes: 185 additions & 0 deletions
185
...gr/dashboard/frontend/src/app/ceph/new-dashboard/dashboard-pie/dashboard-pie.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import { Component, Input, OnChanges, OnInit } from '@angular/core'; | ||
|
||
import * as Chart from 'chart.js'; | ||
import _ from 'lodash'; | ||
import { PluginServiceGlobalRegistrationAndOptions } from 'ng2-charts'; | ||
|
||
import { CssHelper } from '~/app/shared/classes/css-helper'; | ||
import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe'; | ||
|
||
@Component({ | ||
selector: 'cd-dashboard-pie', | ||
templateUrl: './dashboard-pie.component.html', | ||
styleUrls: ['./dashboard-pie.component.scss'] | ||
}) | ||
export class DashboardPieComponent implements OnChanges, OnInit { | ||
@Input() | ||
data: any; | ||
@Input() | ||
highThreshold: number; | ||
@Input() | ||
lowThreshold: number; | ||
|
||
color: string; | ||
|
||
chartConfig: any = { | ||
chartType: 'doughnut', | ||
labels: ['', '', ''], | ||
dataset: [ | ||
{ | ||
label: null, | ||
backgroundColor: [ | ||
this.cssHelper.propertyValue('chart-color-light-gray'), | ||
this.cssHelper.propertyValue('chart-color-slight-dark-gray'), | ||
this.cssHelper.propertyValue('chart-color-dark-gray') | ||
] | ||
}, | ||
{ | ||
label: null, | ||
borderWidth: 0, | ||
backgroundColor: [ | ||
this.cssHelper.propertyValue('chart-color-blue'), | ||
this.cssHelper.propertyValue('chart-color-white') | ||
] | ||
} | ||
], | ||
options: { | ||
cutoutPercentage: 70, | ||
events: ['click', 'mouseout', 'touchstart'], | ||
legend: { | ||
display: true, | ||
position: 'right', | ||
labels: { | ||
boxWidth: 10, | ||
usePointStyle: false, | ||
generateLabels: (chart: any) => { | ||
const labels = { 0: {}, 1: {}, 2: {} }; | ||
labels[0] = { | ||
text: $localize`Capacity: ${chart.data.datasets[1].data[0]}%`, | ||
fillStyle: chart.data.datasets[1].backgroundColor[0], | ||
strokeStyle: chart.data.datasets[1].backgroundColor[0] | ||
}; | ||
labels[1] = { | ||
text: $localize`Warning: ${chart.data.datasets[0].data[0]}%`, | ||
fillStyle: chart.data.datasets[0].backgroundColor[1], | ||
strokeStyle: chart.data.datasets[0].backgroundColor[1] | ||
}; | ||
labels[2] = { | ||
text: $localize`Danger: ${ | ||
chart.data.datasets[0].data[0] + chart.data.datasets[0].data[1] | ||
}%`, | ||
fillStyle: chart.data.datasets[0].backgroundColor[2], | ||
strokeStyle: chart.data.datasets[0].backgroundColor[2] | ||
}; | ||
|
||
return labels; | ||
} | ||
} | ||
}, | ||
plugins: { | ||
center_text: true | ||
}, | ||
tooltips: { | ||
enabled: true, | ||
displayColors: false, | ||
backgroundColor: this.cssHelper.propertyValue('chart-color-tooltip-background'), | ||
cornerRadius: 0, | ||
bodyFontSize: 14, | ||
bodyFontStyle: '600', | ||
position: 'nearest', | ||
xPadding: 12, | ||
yPadding: 12, | ||
filter: (tooltipItem: any) => { | ||
return tooltipItem.datasetIndex === 1; | ||
}, | ||
callbacks: { | ||
label: (item: Record<string, any>, data: Record<string, any>) => { | ||
let text = data.labels[item.index]; | ||
if (!text.includes('%')) { | ||
text = `${text} (${data.datasets[item.datasetIndex].data[item.index]}%)`; | ||
} | ||
return text; | ||
} | ||
} | ||
}, | ||
title: { | ||
display: false | ||
} | ||
} | ||
}; | ||
|
||
public doughnutChartPlugins: PluginServiceGlobalRegistrationAndOptions[] = [ | ||
{ | ||
id: 'center_text', | ||
beforeDraw(chart: Chart) { | ||
const cssHelper = new CssHelper(); | ||
const defaultFontFamily = 'Helvetica Neue, Helvetica, Arial, sans-serif'; | ||
Chart.defaults.global.defaultFontFamily = defaultFontFamily; | ||
const ctx = chart.ctx; | ||
if (!chart.options.plugins.center_text || !chart.data.datasets[0].label) { | ||
return; | ||
} | ||
|
||
ctx.save(); | ||
const label = chart.data.datasets[0].label[0].split('\n'); | ||
|
||
const centerX = (chart.chartArea.left + chart.chartArea.right) / 2; | ||
const centerY = (chart.chartArea.top + chart.chartArea.bottom) / 2; | ||
ctx.textAlign = 'center'; | ||
ctx.textBaseline = 'middle'; | ||
|
||
ctx.font = `24px ${defaultFontFamily}`; | ||
ctx.fillText(label[0], centerX, centerY - 10); | ||
|
||
if (label.length > 1) { | ||
ctx.font = `14px ${defaultFontFamily}`; | ||
ctx.fillStyle = cssHelper.propertyValue('chart-color-center-text-description'); | ||
ctx.fillText(label[1], centerX, centerY + 10); | ||
} | ||
ctx.restore(); | ||
} | ||
} | ||
]; | ||
|
||
constructor(private cssHelper: CssHelper, private dimlessBinary: DimlessBinaryPipe) {} | ||
|
||
ngOnInit() { | ||
this.prepareRawUsage(this.chartConfig, this.data); | ||
} | ||
|
||
ngOnChanges() { | ||
this.prepareRawUsage(this.chartConfig, this.data); | ||
} | ||
|
||
private prepareRawUsage(chart: Record<string, any>, data: Record<string, any>) { | ||
const nearFullRatioPercent = this.lowThreshold * 100; | ||
const fullRatioPercent = this.highThreshold * 100; | ||
const percentAvailable = this.calcPercentage(data.max - data.current, data.max); | ||
const percentUsed = this.calcPercentage(data.current, data.max); | ||
if (percentUsed >= fullRatioPercent) { | ||
this.color = 'chart-color-red'; | ||
} else if (percentUsed >= nearFullRatioPercent) { | ||
this.color = 'chart-color-yellow'; | ||
} else { | ||
this.color = 'chart-color-blue'; | ||
} | ||
|
||
chart.dataset[0].data = [ | ||
Math.round(nearFullRatioPercent), | ||
Math.round(Math.abs(nearFullRatioPercent - fullRatioPercent)), | ||
Math.round(100 - fullRatioPercent) | ||
]; | ||
|
||
chart.dataset[1].data = [percentUsed, percentAvailable]; | ||
chart.dataset[1].backgroundColor[0] = this.cssHelper.propertyValue(this.color); | ||
|
||
chart.dataset[0].label = [`${percentUsed}%\nof ${this.dimlessBinary.transform(data.max)}`]; | ||
} | ||
|
||
private calcPercentage(dividend: number, divisor: number) { | ||
if (!_.isNumber(dividend) || !_.isNumber(divisor) || divisor === 0) { | ||
return 0; | ||
} | ||
return Math.ceil((dividend / divisor) * 100 * 100) / 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 35 additions & 5 deletions
40
...pybind/mgr/dashboard/frontend/src/app/ceph/new-dashboard/dashboard/dashboard.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.