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
4f6e87bb
Commit
4f6e87bb
authored
Jan 17, 2019
by
Hugo Häggmark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small refactor of Gauge and tests
parent
a6e2be86
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
29 deletions
+99
-29
packages/grafana-ui/src/components/Gauge/Gauge.test.tsx
+78
-0
packages/grafana-ui/src/components/Gauge/Gauge.tsx
+21
-29
No files found.
packages/grafana-ui/src/components/Gauge/Gauge.test.tsx
View file @
4f6e87bb
...
...
@@ -3,6 +3,7 @@ import { shallow } from 'enzyme';
import
{
Gauge
,
Props
}
from
'./Gauge'
;
import
{
TimeSeriesVMs
}
from
'../../types/series'
;
import
{
ValueMapping
,
MappingType
}
from
'../../types'
;
jest
.
mock
(
'jquery'
,
()
=>
({
plot
:
jest
.
fn
(),
...
...
@@ -68,3 +69,80 @@ describe('Get font color', () => {
expect
(
instance
.
getFontColor
(
6.5
)).
toEqual
(
'#EAB839'
);
});
});
describe
(
'Format value with value mappings'
,
()
=>
{
it
(
'should return undefined with no valuemappings'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
).
toBeUndefined
();
});
it
(
'should return undefined with no matching valuemappings'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[
{
id
:
0
,
operator
:
''
,
text
:
'elva'
,
type
:
MappingType
.
ValueToText
,
value
:
'11'
},
{
id
:
1
,
operator
:
''
,
text
:
'1-9'
,
type
:
MappingType
.
RangeToText
,
from
:
'1'
,
to
:
'9'
},
];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
).
toBeUndefined
();
});
it
(
'should return first matching mapping with lowest id'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[
{
id
:
0
,
operator
:
''
,
text
:
'1-20'
,
type
:
MappingType
.
RangeToText
,
from
:
'1'
,
to
:
'20'
},
{
id
:
1
,
operator
:
''
,
text
:
'tio'
,
type
:
MappingType
.
ValueToText
,
value
:
'10'
},
];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
.
text
).
toEqual
(
'1-20'
);
});
it
(
'should return rangeToText mapping where value equals to'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[
{
id
:
0
,
operator
:
''
,
text
:
'1-10'
,
type
:
MappingType
.
RangeToText
,
from
:
'1'
,
to
:
'10'
},
{
id
:
1
,
operator
:
''
,
text
:
'elva'
,
type
:
MappingType
.
ValueToText
,
value
:
'11'
},
];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
.
text
).
toEqual
(
'1-10'
);
});
it
(
'should return rangeToText mapping where value equals from'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[
{
id
:
0
,
operator
:
''
,
text
:
'10-20'
,
type
:
MappingType
.
RangeToText
,
from
:
'10'
,
to
:
'20'
},
{
id
:
1
,
operator
:
''
,
text
:
'elva'
,
type
:
MappingType
.
ValueToText
,
value
:
'11'
},
];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
.
text
).
toEqual
(
'10-20'
);
});
it
(
'should return rangeToText mapping where value is between from and to'
,
()
=>
{
const
valueMappings
:
ValueMapping
[]
=
[
{
id
:
0
,
operator
:
''
,
text
:
'1-20'
,
type
:
MappingType
.
RangeToText
,
from
:
'1'
,
to
:
'20'
},
{
id
:
1
,
operator
:
''
,
text
:
'elva'
,
type
:
MappingType
.
ValueToText
,
value
:
'11'
},
];
const
value
=
10
;
const
{
instance
}
=
setup
({
valueMappings
});
const
result
=
instance
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
expect
(
result
.
text
).
toEqual
(
'1-20'
);
});
});
packages/grafana-ui/src/components/Gauge/Gauge.tsx
View file @
4f6e87bb
...
...
@@ -58,34 +58,26 @@ export class Gauge extends PureComponent<Props> {
this
.
draw
();
}
addValueToTextMappingText
(
allTexts
:
Array
<
{
text
:
string
;
type
:
MappingType
}
>
,
valueToTextMapping
:
ValueMap
,
value
:
TimeSeriesValue
)
{
addValueToTextMappingText
(
allValueMappings
:
ValueMapping
[],
valueToTextMapping
:
ValueMap
,
value
:
TimeSeriesValue
)
{
if
(
!
valueToTextMapping
.
value
)
{
return
all
Text
s
;
return
all
ValueMapping
s
;
}
const
valueAsNumber
=
parseFloat
(
value
as
string
);
const
valueToTextMappingAsNumber
=
parseFloat
(
valueToTextMapping
.
value
as
string
);
if
(
isNaN
(
valueAsNumber
)
||
isNaN
(
valueToTextMappingAsNumber
))
{
return
all
Text
s
;
return
all
ValueMapping
s
;
}
if
(
valueAsNumber
!==
valueToTextMappingAsNumber
)
{
return
all
Text
s
;
return
all
ValueMapping
s
;
}
return
all
Texts
.
concat
({
text
:
valueToTextMapping
.
text
,
type
:
MappingType
.
ValueToText
}
);
return
all
ValueMappings
.
concat
(
valueToTextMapping
);
}
addRangeToTextMappingText
(
allTexts
:
Array
<
{
text
:
string
;
type
:
MappingType
}
>
,
rangeToTextMapping
:
RangeMap
,
value
:
TimeSeriesValue
)
{
addRangeToTextMappingText
(
allValueMappings
:
ValueMapping
[],
rangeToTextMapping
:
RangeMap
,
value
:
TimeSeriesValue
)
{
if
(
rangeToTextMapping
.
from
&&
rangeToTextMapping
.
to
&&
...
...
@@ -93,35 +85,35 @@ export class Gauge extends PureComponent<Props> {
value
>=
rangeToTextMapping
.
from
&&
value
<=
rangeToTextMapping
.
to
)
{
return
all
Texts
.
concat
({
text
:
rangeToTextMapping
.
text
,
type
:
MappingType
.
RangeToText
}
);
return
all
ValueMappings
.
concat
(
rangeToTextMapping
);
}
return
all
Text
s
;
return
all
ValueMapping
s
;
}
getAll
MappingText
s
(
valueMappings
:
ValueMapping
[],
value
:
TimeSeriesValue
)
{
const
all
MappingText
s
=
valueMappings
.
reduce
(
(
all
Text
s
,
valueMapping
)
=>
{
getAll
FormattedValueMapping
s
(
valueMappings
:
ValueMapping
[],
value
:
TimeSeriesValue
)
{
const
all
FormattedValueMapping
s
=
valueMappings
.
reduce
(
(
all
ValueMapping
s
,
valueMapping
)
=>
{
if
(
valueMapping
.
type
===
MappingType
.
ValueToText
)
{
all
Texts
=
this
.
addValueToTextMappingText
(
allText
s
,
valueMapping
as
ValueMap
,
value
);
all
ValueMappings
=
this
.
addValueToTextMappingText
(
allValueMapping
s
,
valueMapping
as
ValueMap
,
value
);
}
else
if
(
valueMapping
.
type
===
MappingType
.
RangeToText
)
{
all
Texts
=
this
.
addRangeToTextMappingText
(
allText
s
,
valueMapping
as
RangeMap
,
value
);
all
ValueMappings
=
this
.
addRangeToTextMappingText
(
allValueMapping
s
,
valueMapping
as
RangeMap
,
value
);
}
return
all
Text
s
;
return
all
ValueMapping
s
;
},
[]
as
Array
<
{
text
:
string
;
type
:
MappingType
}
>
[]
as
ValueMapping
[]
);
all
MappingText
s
.
sort
((
t1
,
t2
)
=>
{
return
t1
.
type
-
t2
.
type
;
all
FormattedValueMapping
s
.
sort
((
t1
,
t2
)
=>
{
return
t1
.
id
-
t2
.
id
;
});
return
all
MappingText
s
;
return
all
FormattedValueMapping
s
;
}
formatWithValueMappings
(
valueMappings
:
ValueMapping
[],
value
:
TimeSeriesValue
)
{
return
this
.
getAll
MappingText
s
(
valueMappings
,
value
)[
0
];
getFirstFormattedValueMapping
(
valueMappings
:
ValueMapping
[],
value
:
TimeSeriesValue
)
{
return
this
.
getAll
FormattedValueMapping
s
(
valueMappings
,
value
)[
0
];
}
formatValue
(
value
:
TimeSeriesValue
)
{
...
...
@@ -132,7 +124,7 @@ export class Gauge extends PureComponent<Props> {
}
if
(
valueMappings
.
length
>
0
)
{
const
valueMappedValue
=
this
.
formatWithValueMappings
(
valueMappings
,
value
);
const
valueMappedValue
=
this
.
getFirstFormattedValueMapping
(
valueMappings
,
value
);
if
(
valueMappedValue
)
{
return
`
${
prefix
}
${
valueMappedValue
.
text
}
${
suffix
}
`
;
}
...
...
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