Skip to content

Commit 2f7b1ed

Browse files
committed
refactor: adding value and thresholds to widget model
1 parent bc14e34 commit 2f7b1ed

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed

projects/observability/src/shared/components/gauge/gauge.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class GaugeComponent implements OnChanges {
120120
private calculateInputData(): GaugeInputData | undefined {
121121
if (this.value !== undefined && this.maxValue !== undefined && this.maxValue > 0 && this.thresholds.length > 0) {
122122
const currentThreshold = this.thresholds.find(
123-
threshold => this.value! >= threshold.start && this.value! < threshold.end
123+
threshold => this.value! >= threshold.start && this.value! <= threshold.end
124124
);
125125

126126
if (currentThreshold) {
@@ -138,7 +138,7 @@ export interface GaugeThreshold {
138138
label: string;
139139
start: number;
140140
end: number;
141-
color: Color;
141+
color: Color | string;
142142
}
143143

144144
interface GaugeSvgRendererData {

projects/observability/src/shared/dashboard/widgets/gauge/gauge-widget.model.test.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@ import { createModelFactory } from '@hypertrace/dashboards/testing';
33
import { MODEL_PROPERTY_TYPES } from '@hypertrace/hyperdash-angular';
44
import { runFakeRxjs } from '@hypertrace/test-utils';
55
import { of } from 'rxjs';
6-
import { GaugeWidgetData } from './gauge-widget';
76
import { GaugeWidgetModel } from './gauge-widget.model';
87

98
describe('Gauge widget model', () => {
109
test('uses colors from color map', () => {
1110
const modelFactory = createModelFactory();
1211

13-
const data: GaugeWidgetData = {
14-
value: 5,
15-
maxValue: 10,
16-
thresholds: [
17-
{
18-
start: 0,
19-
end: 6,
20-
label: 'Medium',
21-
color: Color.Brown1
22-
}
23-
]
24-
};
12+
const value = 5;
2513

2614
const spectator = modelFactory(GaugeWidgetModel, {
2715
api: {
28-
getData: () => of(data)
16+
getData: () => of(value)
2917
},
3018
providers: [
3119
{
@@ -34,7 +22,16 @@ describe('Gauge widget model', () => {
3422
}
3523
],
3624
properties: {
37-
title: 'Test Title'
25+
title: 'Test Title',
26+
maxValue: 10,
27+
thresholds: [
28+
{
29+
start: 0,
30+
end: 6,
31+
label: 'Medium',
32+
color: Color.Brown1
33+
}
34+
]
3835
}
3936
});
4037

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { Model, ModelApi, ModelProperty, STRING_PROPERTY } from '@hypertrace/hyperdash';
1+
import {
2+
ARRAY_PROPERTY,
3+
Model,
4+
ModelApi,
5+
ModelProperty,
6+
NUMBER_PROPERTY,
7+
STRING_PROPERTY
8+
} from '@hypertrace/hyperdash';
29
import { ModelInject, MODEL_API } from '@hypertrace/hyperdash-angular';
310
import { Observable } from 'rxjs';
11+
import { map } from 'rxjs/operators';
412
import { GaugeWidgetData } from './gauge-widget';
5-
13+
import { GaugeThresholdModel } from './thresholds/gauge-threshold.model';
614
@Model({
715
type: 'gauge-widget'
816
})
@@ -13,10 +21,30 @@ export class GaugeWidgetModel {
1321
})
1422
public title?: string;
1523

24+
@ModelProperty({
25+
key: 'max-value',
26+
type: NUMBER_PROPERTY.type,
27+
required: true
28+
})
29+
public maxValue!: number;
30+
31+
@ModelProperty({
32+
key: 'thresholds',
33+
type: ARRAY_PROPERTY.type,
34+
required: true
35+
})
36+
public thresholds!: GaugeThresholdModel[];
37+
1638
@ModelInject(MODEL_API)
1739
private readonly api!: ModelApi;
1840

1941
public getData(): Observable<GaugeWidgetData> {
20-
return this.api.getData<GaugeWidgetData>();
42+
return this.api.getData<number>().pipe(
43+
map(value => ({
44+
value: value,
45+
maxValue: this.maxValue,
46+
thresholds: this.thresholds
47+
}))
48+
);
2149
}
2250
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Model, ModelProperty, NUMBER_PROPERTY, STRING_PROPERTY, UNKNOWN_PROPERTY } from '@hypertrace/hyperdash';
2+
3+
@Model({
4+
type: 'gauge-threshold'
5+
})
6+
export class GaugeThresholdModel {
7+
@ModelProperty({
8+
key: 'start',
9+
type: NUMBER_PROPERTY.type,
10+
required: true
11+
})
12+
public start!: number;
13+
14+
@ModelProperty({
15+
key: 'end',
16+
type: NUMBER_PROPERTY.type,
17+
required: true
18+
})
19+
public end!: number;
20+
21+
@ModelProperty({
22+
key: 'label',
23+
type: STRING_PROPERTY.type,
24+
required: true
25+
})
26+
public label!: string;
27+
28+
@ModelProperty({
29+
key: 'color',
30+
type: UNKNOWN_PROPERTY.type,
31+
required: true
32+
})
33+
public color!: string;
34+
}

0 commit comments

Comments
 (0)