Skip to content

Commit fefa1de

Browse files
committed
fix: removed bar grouping feature
1 parent 7da4c27 commit fefa1de

9 files changed

Lines changed: 62 additions & 191 deletions

File tree

giraffe/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -715,15 +715,9 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
715715

716716
All values (excluding **type**) are Optional and their defaults is defined by theme `GAUGE_MINI_THEME_BULLET_DARK`
717717

718-
- **type**: _'gauge mini'. **Required**._ Specifies that this LayerConfig is a gauge mini layer.
718+
gauge mini creates one bar per unique __field_ value
719719

720-
- **barsDefinitions** _{groupByColumns; bars;}_ _(types of properties based on selected style)_
721-
- Object style:
722-
- **groupByColumns** _{ [key: string]: true }_ bar for each unique combination of given columns values. _(example: `{cpu: true, _field: true}`)_
723-
- **bars** _{ barDef: { [key in keyof T]: string }, label?: string }[]_ where _barDef_ contains values for specific bar columns and label for this bar.
724-
- _or_ Array style:
725-
- **groupByColumns** _string[]_ bar for each unique combination of given columns values. _(example: `['cpu', '_field', ]`)_
726-
- **bars** _{ barDef: string[], label?: string }[]_ where _barDef_ contains values for specific bar columns and label for this bar. Each barDef value belongs to key grom groupByColumns with same index.
720+
- **type**: _'gauge mini'. **Required**._ Specifies that this LayerConfig is a gauge mini layer.
727721

728722
- **mode** _'progress' | 'bullet'._
729723
- `'bullet'` backgroud bar is colored and value bar has always secondary color
@@ -775,6 +769,8 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
775769
- **labelMainFontColor** _string_ Main label color.
776770

777771
Bar labels
772+
- **labelBarsEnabled** _boolean_ Bar labels shown if true
773+
778774
- **labelBarsFontSize** _number_ Bar labels font size
779775

780776
- **labelBarsFontColor** _string_ Bar labels font color
@@ -810,7 +806,7 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
810806

811807
- **isEnforced**: _boolean. Optional. Defaults to false when not included._ Indicates whether the number of decimal places ("**digits**") will be enforced. When **isEnforced** is falsy or omitted, **digits** will be locked to 2 for stat values with a decimal and 0 for stat values that are integers, and the **digits** option will be ignored.
812808
- **digits**: _number. Optional. Defaults to 0 when not included. Maximum 10._ When **digits** is a non-integer number, the decimal portion is ignored. Represents the number of decimal places to display in the stat value. Displayed stat value is subject to rounding.
813-
- example ```valueFormater: (num: number) => `${num.toFixed(0)}%` ``` for _value=23.213_ will show text value _23%_.
809+
- example ```valueFormater: (num: number) => `${((num || 0) * 100).toFixed(0)}%` ``` for _value=0.23213_ will show text value _23%_.
814810

815811
**Precreated themes**
816812
- `GAUGE_MINI_THEME_BULLET_DARK`
@@ -819,7 +815,6 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
819815
type: 'gauge mini',
820816
mode: 'bullet',
821817
textMode: 'follow',
822-
barsDefinitions: {groupByColumns: ["_field"]},
823818

824819
valueHeight: 18,
825820
gaugeHeight: 25,
@@ -841,6 +836,7 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
841836
labelMainFontSize: 13,
842837
labelMainFontColor: InfluxColors.Ghost,
843838

839+
labelBarsEnabled: false,
844840
labelBarsFontSize: 11,
845841
labelBarsFontColor: InfluxColors.Forge,
846842

@@ -862,7 +858,6 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
862858
type: 'gauge mini',
863859
mode: 'progress',
864860
textMode: 'follow',
865-
barsDefinitions: {groupByColumns: ['_field']},
866861

867862
valueHeight: 20,
868863
gaugeHeight: 20,
@@ -882,6 +877,7 @@ TableGraphLayerConfig uses the `fluxResponse` property from `config` as the data
882877
labelMainFontSize: 13,
883878
labelMainFontColor: InfluxColors.Ghost,
884879

880+
labelBarsEnabled: false,
885881
labelBarsFontSize: 11,
886882
labelBarsFontColor: InfluxColors.Forge,
887883

giraffe/src/components/GaugeMini.tsx

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {scaleLinear} from 'd3-scale'
55

66
// Types
77
import {GaugeMiniColors, GaugeMiniLayerConfig} from '../types'
8+
import {GroupedData} from './LatestMultipleValueTransform'
9+
810
import {
911
gaugeMiniNormalizeThemeMemoized,
1012
GaugeMiniThemeNormalized,
@@ -13,28 +15,10 @@ import {
1315
interface Props {
1416
width: number
1517
height: number
16-
values: {colsMString: string; value: number}[]
18+
values: GroupedData
1719
theme: Required<GaugeMiniLayerConfig>
1820
}
1921

20-
// todo: move into gauge utils
21-
/** create merged string for given column string values. String is same for all columns with same values and unique for different ones */
22-
export const createColsMString = <T extends {[key: string]: true}>(
23-
groupedBy: T,
24-
col: {[key in keyof T]: string}
25-
): string => {
26-
const columns = Object.keys(groupedBy)
27-
.filter(x => groupedBy[x])
28-
.sort()
29-
const columnValues = columns.map(x => col[x])
30-
/**
31-
* replacing - with -- will ensures that rows
32-
* { a: '0-1', b: '2' } and { a: '0', b: '1-2' }
33-
* will not have same string (0-1-2 instead they will be 0--1-2 and 0-1--2)
34-
*/
35-
return columnValues.map(x => x.split('-').join('--')).join('-')
36-
}
37-
3822
const barCssClass = 'gauge-mini-bar'
3923

4024
//#region svg helpers
@@ -473,6 +457,7 @@ export const GaugeMini: FunctionComponent<Props> = ({
473457
labelMain,
474458
labelMainFontSize,
475459
labelMainFontColor,
460+
labelBarsEnabled,
476461
labelBarsFontColor,
477462
labelBarsFontSize,
478463
colors,
@@ -485,29 +470,18 @@ export const GaugeMini: FunctionComponent<Props> = ({
485470
const barLabelWidth = Math.max(...barLabelsWidth) || 0
486471
const barWidth = width - sidePaddings * 2 - barLabelWidth
487472
const maxBarHeight = Math.max(gaugeHeight, valueHeight)
488-
const allBarsHeight = values.length * (maxBarHeight + barPaddings)
489-
490-
const barsDefinitions = theme.barsDefinitions
473+
const allBarsHeight =
474+
Object.keys(values).length * (maxBarHeight + barPaddings)
491475

492476
// create unified barsDefinition
493477

494-
const labelMapping: any = {}
495-
barsDefinitions?.bars?.forEach(x => {
496-
if (!x.label) {
497-
return
498-
}
499-
const mstring = createColsMString(barsDefinitions.groupByColumns, x.barDef)
500-
labelMapping[mstring] = x.label
501-
})
502-
503478
const [autocenterToken, setAutocenterToken] = useState(Date.now())
504479
useEffect(() => {
505480
setAutocenterToken(Date.now())
506481
}, [
507482
width,
508483
height,
509484
barLabelWidth,
510-
barsDefinitions,
511485
valueHeight,
512486
gaugeHeight,
513487
barPaddings,
@@ -536,11 +510,10 @@ export const GaugeMini: FunctionComponent<Props> = ({
536510
{labelMain}
537511
</text>
538512
)}
539-
{values.map(({colsMString, value}, i) => {
513+
{Object.entries(values).map(([group, value], i) => {
540514
const y = 0 + i * (maxBarHeight + barPaddings)
541-
const label = labelMapping?.[colsMString]
542-
543515
const textCenter = y + maxBarHeight / 2
516+
const label = labelBarsEnabled ? group : ''
544517

545518
// todo: no rerender ?
546519
const onRectChanged = (r: DOMRect) => {

giraffe/src/components/GaugeMiniLayer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {GaugeMiniLayerConfig} from '../types'
77
import {GaugeMini} from './GaugeMini'
88

99
import {GAUGE_MINI_THEME_BULLET_DARK} from '../constants/gaugeMiniStyles'
10+
import {GroupedData} from './LatestMultipleValueTransform'
1011

1112
interface Props {
12-
values: {colsMString: string; value: number}[]
13+
values: GroupedData
1314
theme: GaugeMiniLayerConfig
1415
}
1516

17+
// todo: move gauge mini here
1618
export const GaugeMiniLayer: FunctionComponent<Props> = (props: Props) => {
1719
const {theme, values} = props
1820
const themeOrDefault: Required<GaugeMiniLayerConfig> = {
Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
// Libraries
22
import React, {useMemo, FunctionComponent} from 'react'
33
import {Table} from '../types'
4-
import {createColsMString} from './GaugeMini'
54

6-
interface SelectedColumns {
7-
[key: string]: true
8-
}
9-
10-
export const getLatestValuesGrouped = (
11-
table: Table,
12-
columnsObj: SelectedColumns
13-
) => {
14-
const columns = Object.keys(columnsObj).sort()
15-
16-
columns.forEach(x => {
17-
if (table.getColumnType(x) !== 'string') {
18-
throw new Error(
19-
`Data can be grouped only by string columns. But column ${x} is typeof ${table.getColumnType(
20-
x
21-
)}`
22-
)
23-
}
24-
})
25-
26-
const valueColumn = table.getColumn('_value', 'number') as number[]
27-
28-
if (!valueColumn.length) {
29-
return []
30-
}
5+
export type GroupedData = {[key: string]: number}
316

7+
const getTimeColumn = (table: Table) => {
328
// Fallback to `_stop` column if `_time` column missing otherwise return empty array.
339
let timeColData: number[] = []
3410

@@ -41,73 +17,61 @@ export const getLatestValuesGrouped = (
4117
return []
4218
}
4319

44-
const groupColsData = columns.map(k =>
45-
table.getColumn(k, 'string')
46-
) as string[][]
20+
return timeColData
21+
}
4722

48-
const result: {[key: string]: number} = {}
23+
export const getLatestValuesGrouped = (
24+
table: Table,
25+
groupColumnKey: string
26+
): GroupedData => {
27+
const valueCol = table.getColumn('_value', 'number')
28+
const groupCol = table.getColumn(groupColumnKey)
4929

50-
timeColData
51-
// merge time with it's index
52-
.map((time, index) => ({time, index}))
53-
// remove entries without time
54-
.filter(({time}) => time)
55-
// todo: sort time complexity too high ... replace with another solution
56-
// from low time to high time (last is last)
57-
.sort(({time: t1}, {time: t2}) => t1 - t2)
58-
// get relevant data from index (we don't need time anymore)
59-
.map(({index}) => ({
60-
value: valueColumn[index],
61-
groupRow: groupColsData.map(x => x[index]),
62-
}))
63-
// remove invalid data
64-
.filter(({value}) => Number.isFinite(value) && typeof value === 'number')
65-
// create result
66-
.forEach(({value, groupRow}) => {
67-
const grupObj = {}
68-
groupRow.forEach((x, i) => (grupObj[columns[i]] = x))
69-
const strKey = createColsMString(columnsObj, grupObj)
70-
// data is inserted from last to first so latest data is first
71-
result[strKey] = value
72-
})
30+
if (!valueCol.length) {
31+
return {}
32+
}
7333

74-
return result
34+
return Object.fromEntries(
35+
getTimeColumn(table)
36+
// merge time with it's index
37+
.map((time, index) => ({time, index}))
38+
// remove entries without time
39+
.filter(({time}) => time)
40+
// todo: sort time complexity too high ... replace with linear solution
41+
// from low time to high time (last is last)
42+
.sort(({time: t1}, {time: t2}) => t1 - t2)
43+
// get relevant data from index (we don't need time anymore)
44+
.map(({index}) => [groupCol?.[index] ?? '', valueCol[index]] as const)
45+
// remove invalid data
46+
.filter(
47+
([_, value]) => typeof value === 'number' && Number.isFinite(value)
48+
)
49+
)
7550
}
7651

7752
interface Props {
7853
table: Table
79-
columns: SelectedColumns
80-
children: (latestValue: {colsMString: string; value: number}[]) => JSX.Element
54+
children: (latestValue: GroupedData) => JSX.Element
8155
quiet?: boolean
8256
}
8357

8458
// todo: can return string ?
8559
export const LatestMultipleValueTransform: FunctionComponent<Props> = ({
8660
table,
87-
columns,
8861
quiet = false,
8962
children,
9063
}) => {
91-
const latestValues = useMemo(() => getLatestValuesGrouped(table, columns), [
64+
const latestValues = useMemo(() => getLatestValuesGrouped(table, '_field'), [
9265
table,
9366
])
9467

95-
if (latestValues.length === 0 && quiet) {
96-
return null
97-
}
98-
99-
if (latestValues.length === 0) {
100-
return (
68+
if (Object.keys(latestValues).length === 0) {
69+
return quiet ? null : (
10170
<div>
10271
<h4>No latest value found</h4>
10372
</div>
10473
)
10574
}
10675

107-
const entries = Object.keys(latestValues).map(x => ({
108-
colsMString: x,
109-
value: latestValues[x],
110-
}))
111-
112-
return children(entries)
76+
return children(latestValues)
11377
}

giraffe/src/components/SizedTable.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {TableGraphLayer} from './TableGraphLayer'
1717

1818
import {usePlotEnv} from '../utils/usePlotEnv'
1919
import {LatestMultipleValueTransform} from './LatestMultipleValueTransform'
20-
import {getGaugeMiniBarsDefinitions} from '../utils/gaugeMiniThemeNormalize'
2120

2221
interface Props {
2322
config: SizedConfig
@@ -87,10 +86,6 @@ export const SizedTable: FunctionComponent<Props> = ({
8786
<LatestMultipleValueTransform
8887
key={layerIndex}
8988
table={newTableFromConfig(config)}
90-
columns={
91-
getGaugeMiniBarsDefinitions(layerConfig.barsDefinitions)
92-
.groupByColumns
93-
}
9489
>
9590
{latestValues => (
9691
<GaugeMiniLayer

giraffe/src/constants/gaugeMiniStyles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const GAUGE_MINI_THEME_BULLET_DARK: Required<GaugeMiniLayerConfig> = {
55
type: 'gauge mini',
66
mode: 'bullet',
77
textMode: 'follow',
8-
barsDefinitions: {groupByColumns: ['_field']},
98

109
valueHeight: 18,
1110
gaugeHeight: 25,
@@ -27,6 +26,7 @@ export const GAUGE_MINI_THEME_BULLET_DARK: Required<GaugeMiniLayerConfig> = {
2726
labelMainFontSize: 13,
2827
labelMainFontColor: InfluxColors.Ghost,
2928

29+
labelBarsEnabled: false,
3030
labelBarsFontSize: 11,
3131
labelBarsFontColor: InfluxColors.Forge,
3232

@@ -46,7 +46,6 @@ export const GAUGE_MINI_THEME_PROGRESS_DARK: Required<GaugeMiniLayerConfig> = {
4646
type: 'gauge mini',
4747
mode: 'progress',
4848
textMode: 'follow',
49-
barsDefinitions: {groupByColumns: ['_field']},
5049

5150
valueHeight: 20,
5251
gaugeHeight: 20,
@@ -66,6 +65,7 @@ export const GAUGE_MINI_THEME_PROGRESS_DARK: Required<GaugeMiniLayerConfig> = {
6665
labelMainFontSize: 13,
6766
labelMainFontColor: InfluxColors.Ghost,
6867

68+
labelBarsEnabled: false,
6969
labelBarsFontSize: 11,
7070
labelBarsFontColor: InfluxColors.Forge,
7171

0 commit comments

Comments
 (0)