Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
a431efa0
Commit
a431efa0
authored
Jan 14, 2019
by
Hugo Häggmark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored logic in ThresholdEditor
parent
030d20d9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
36 deletions
+68
-36
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.test.tsx
+46
-13
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.tsx
+22
-23
No files found.
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.test.tsx
View file @
a431efa0
...
...
@@ -15,25 +15,56 @@ const setup = (propOverrides?: object) => {
return
shallow
(<
ThresholdsEditor
{
...
props
}
/>).
instance
()
as
ThresholdsEditor
;
};
describe
(
'Initialization'
,
()
=>
{
it
(
'should add a base threshold if missing'
,
()
=>
{
const
instance
=
setup
();
expect
(
instance
.
state
.
thresholds
).
toEqual
([{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
}]);
});
});
describe
(
'Add threshold'
,
()
=>
{
it
(
'should add threshold'
,
()
=>
{
const
instance
=
setup
();
instance
.
onAddThreshold
(
0
);
instance
.
onAddThreshold
(
1
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)'
}]);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
});
it
(
'should add another threshold above a first'
,
()
=>
{
const
instance
=
setup
({
thresholds
:
[{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)
'
}],
thresholds
:
[{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839
'
}],
});
instance
.
onAddThreshold
(
1
);
instance
.
onAddThreshold
(
2
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
});
it
(
'should add another threshold between first and second index'
,
()
=>
{
const
instance
=
setup
({
thresholds
:
[
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
],
});
instance
.
onAddThreshold
(
2
);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
1
,
value
:
75
,
color
:
'rgb(170, 95, 61)'
},
{
index
:
0
,
value
:
50
,
color
:
'rgb(127, 115, 64)'
},
{
index
:
3
,
value
:
75
,
color
:
'#EF843C'
},
{
index
:
2
,
value
:
62.5
,
color
:
'#6ED0E0'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
]);
});
});
...
...
@@ -41,23 +72,25 @@ describe('Add threshold', () => {
describe
(
'change threshold value'
,
()
=>
{
it
(
'should update value and resort rows'
,
()
=>
{
const
instance
=
setup
();
const
mockThresholds
=
[
{
index
:
0
,
value
:
50
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
1
,
value
:
75
,
color
:
'rgba(237, 129, 40, 0.89)'
},
const
thresholds
=
[
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
50
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
];
instance
.
state
=
{
baseColor
:
BasicGaugeColor
.
Green
,
thresholds
:
mockThresholds
,
thresholds
,
};
const
mockEvent
=
{
target
:
{
value
:
78
}
};
instance
.
onChangeThresholdValue
(
mockEvent
,
mockThresholds
[
0
]);
instance
.
onChangeThresholdValue
(
mockEvent
,
thresholds
[
1
]);
expect
(
instance
.
state
.
thresholds
).
toEqual
([
{
index
:
0
,
value
:
78
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
1
,
value
:
75
,
color
:
'rgba(237, 129, 40, 0.89)'
},
{
index
:
0
,
value
:
-
Infinity
,
color
:
'#7EB26D'
},
{
index
:
1
,
value
:
78
,
color
:
'#EAB839'
},
{
index
:
2
,
value
:
75
,
color
:
'#6ED0E0'
},
]);
});
});
packages/grafana-ui/src/components/ThresholdsEditor/ThresholdsEditor.tsx
View file @
a431efa0
import
React
,
{
PureComponent
}
from
'react'
;
import
tinycolor
,
{
ColorInput
}
from
'tinycolor2'
;
//
import tinycolor, { ColorInput } from 'tinycolor2';
import
{
Threshold
,
BasicGaugeColor
}
from
'../../types'
;
import
{
ColorPicker
}
from
'../ColorPicker/ColorPicker'
;
import
{
PanelOptionsGroup
}
from
'../PanelOptionsGroup/PanelOptionsGroup'
;
import
{
colors
}
from
'../../utils'
;
export
interface
Props
{
thresholds
:
Threshold
[];
...
...
@@ -19,43 +20,41 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
constructor
(
props
:
Props
)
{
super
(
props
);
this
.
state
=
{
thresholds
:
props
.
thresholds
,
baseColor
:
BasicGaugeColor
.
Green
};
const
thresholds
:
Threshold
[]
=
props
.
thresholds
.
length
>
0
?
props
.
thresholds
:
[{
index
:
0
,
value
:
-
Infinity
,
color
:
colors
[
0
]
}];
this
.
state
=
{
thresholds
,
baseColor
:
BasicGaugeColor
.
Green
};
}
onAddThreshold
=
(
index
:
number
)
=>
{
const
maxValue
=
100
;
// hardcoded for now before we add the base threshold
const
minValue
=
0
;
// hardcoded for now before we add the base threshold
const
{
thresholds
}
=
this
.
state
;
const
maxValue
=
100
;
const
minValue
=
0
;
if
(
index
===
0
)
{
return
;
}
const
newThresholds
=
thresholds
.
map
(
threshold
=>
{
if
(
threshold
.
index
>=
index
)
{
const
index
=
threshold
.
index
+
1
;
threshold
=
{
...
threshold
,
index
:
threshold
.
index
+
1
,
index
,
color
:
colors
[
index
],
};
}
return
threshold
;
});
// Setting value to a value between the previous thresholds
let
value
;
if
(
index
===
0
&&
thresholds
.
length
===
0
)
{
value
=
maxValue
-
(
maxValue
-
minValue
)
/
2
;
}
else
if
(
index
===
0
&&
thresholds
.
length
>
0
)
{
value
=
newThresholds
[
index
+
1
].
value
-
(
newThresholds
[
index
+
1
].
value
-
minValue
)
/
2
;
}
else
if
(
index
>
newThresholds
[
newThresholds
.
length
-
1
].
index
)
{
value
=
maxValue
-
(
maxValue
-
newThresholds
[
index
-
1
].
value
)
/
2
;
}
// Set a color that lies between the previous thresholds
let
color
;
if
(
index
===
0
&&
thresholds
.
length
===
0
)
{
color
=
tinycolor
.
mix
(
BasicGaugeColor
.
Green
,
BasicGaugeColor
.
Red
,
50
).
toRgbString
();
}
else
{
color
=
tinycolor
.
mix
(
thresholds
[
index
-
1
].
color
as
ColorInput
,
BasicGaugeColor
.
Red
,
50
).
toRgbString
();
}
const
beforeThreshold
=
newThresholds
.
filter
(
threshold
=>
threshold
.
index
===
index
-
1
)[
0
];
const
afterThreshold
=
newThresholds
.
filter
(
threshold
=>
threshold
.
index
===
index
+
1
)[
0
];
const
beforeThresholdValue
=
beforeThreshold
!==
undefined
?
Math
.
max
(
beforeThreshold
.
value
,
minValue
)
:
minValue
;
const
afterThresholdValue
=
afterThreshold
!==
undefined
?
Math
.
min
(
afterThreshold
.
value
,
maxValue
)
:
maxValue
;
const
value
=
afterThresholdValue
-
(
afterThresholdValue
-
beforeThresholdValue
)
/
2
;
// Set a color
const
color
=
colors
[
index
];
this
.
setState
(
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment