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
493bf0c7
Unverified
Commit
493bf0c7
authored
May 03, 2019
by
Ryan McKinley
Committed by
GitHub
May 03, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor: rename statsCalculator to fieldReducer (#16867)
parent
bb64a067
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
143 additions
and
144 deletions
+143
-144
packages/grafana-ui/src/components/SingleStatShared/SingleStatValueEditor.tsx
+3
-3
packages/grafana-ui/src/components/SingleStatShared/shared.ts
+4
-4
packages/grafana-ui/src/components/StatsPicker/StatsPicker.tsx
+3
-3
packages/grafana-ui/src/utils/fieldReducer.test.ts
+26
-26
packages/grafana-ui/src/utils/fieldReducer.ts
+88
-93
packages/grafana-ui/src/utils/index.ts
+1
-1
public/app/plugins/panel/bargauge/types.ts
+2
-2
public/app/plugins/panel/gauge/types.ts
+2
-2
public/app/plugins/panel/graph2/getGraphSeriesModel.ts
+7
-3
public/app/plugins/panel/piechart/types.ts
+2
-2
public/app/plugins/panel/singlestat2/SingleStatPanel.tsx
+3
-3
public/app/plugins/panel/singlestat2/types.ts
+2
-2
No files found.
packages/grafana-ui/src/components/SingleStatShared/SingleStatValueEditor.tsx
View file @
493bf0c7
...
...
@@ -8,7 +8,7 @@ import {
PanelOptionsGroup
,
StatsPicker
,
UnitPicker
,
Stat
ID
,
Reducer
ID
,
SelectOptionItem
,
}
from
'@grafana/ui'
;
...
...
@@ -27,7 +27,7 @@ export class SingleStatValueEditor extends PureComponent<Props> {
onUnitChange
=
(
unit
:
SelectOptionItem
<
string
>
)
=>
this
.
props
.
onChange
({
...
this
.
props
.
value
,
unit
:
unit
.
value
});
onStatsChange
=
(
stats
:
string
[])
=>
{
const
stat
=
stats
[
0
]
||
Stat
ID
.
mean
;
const
stat
=
stats
[
0
]
||
Reducer
ID
.
mean
;
this
.
props
.
onChange
({
...
this
.
props
.
value
,
stat
});
};
...
...
@@ -65,7 +65,7 @@ export class SingleStatValueEditor extends PureComponent<Props> {
<
StatsPicker
width=
{
12
}
placeholder=
"Choose Stat"
defaultStat=
{
Stat
ID
.
mean
}
defaultStat=
{
Reducer
ID
.
mean
}
allowMultiple=
{
false
}
stats=
{
[
stat
]
}
onChange=
{
this
.
onStatsChange
}
...
...
packages/grafana-ui/src/components/SingleStatShared/shared.ts
View file @
493bf0c7
...
...
@@ -11,7 +11,7 @@ import {
SeriesData
,
InterpolateFunction
,
}
from
'../../types'
;
import
{
get
StatsCalculators
,
calculateStats
}
from
'../../utils/statsCalculato
r'
;
import
{
get
FieldReducers
,
reduceField
}
from
'../../utils/fieldReduce
r'
;
import
{
getDisplayProcessor
}
from
'../../utils/displayValue'
;
export
{
SingleStatValueEditor
}
from
'./SingleStatValueEditor'
;
...
...
@@ -66,10 +66,10 @@ export const getSingleStatDisplayValues = (options: GetSingleStatDisplayValueOpt
// Show all fields that are not 'time'
if
(
column
.
type
===
FieldType
.
number
)
{
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
,
fieldIndex
:
i
,
stat
s
:
[
stat
],
// The stats to calculate
reducer
s
:
[
stat
],
// The stats to calculate
nullValueMode
:
NullValueMode
.
Null
,
});
...
...
@@ -124,7 +124,7 @@ export const sharedSingleStatMigrationCheck = (panel: PanelModel<SingleStatBaseO
// avg -> mean, current -> last, total -> sum
const
{
valueOptions
}
=
options
;
if
(
valueOptions
&&
valueOptions
.
stat
)
{
valueOptions
.
stat
=
get
StatsCalculato
rs
([
valueOptions
.
stat
]).
map
(
s
=>
s
.
id
)[
0
];
valueOptions
.
stat
=
get
FieldReduce
rs
([
valueOptions
.
stat
]).
map
(
s
=>
s
.
id
)[
0
];
}
}
return
options
;
...
...
packages/grafana-ui/src/components/StatsPicker/StatsPicker.tsx
View file @
493bf0c7
...
...
@@ -5,7 +5,7 @@ import difference from 'lodash/difference';
import
{
Select
}
from
'../index'
;
import
{
get
StatsCalculators
}
from
'../../utils/statsCalculato
r'
;
import
{
get
FieldReducers
}
from
'../../utils/fieldReduce
r'
;
import
{
SelectOptionItem
}
from
'../Select/Select'
;
interface
Props
{
...
...
@@ -34,7 +34,7 @@ export class StatsPicker extends PureComponent<Props> {
checkInput
=
()
=>
{
const
{
stats
,
allowMultiple
,
defaultStat
,
onChange
}
=
this
.
props
;
const
current
=
get
StatsCalculato
rs
(
stats
);
const
current
=
get
FieldReduce
rs
(
stats
);
if
(
current
.
length
!==
stats
.
length
)
{
const
found
=
current
.
map
(
v
=>
v
.
id
);
const
notFound
=
difference
(
stats
,
found
);
...
...
@@ -65,7 +65,7 @@ export class StatsPicker extends PureComponent<Props> {
render
()
{
const
{
width
,
stats
,
allowMultiple
,
defaultStat
,
placeholder
}
=
this
.
props
;
const
options
=
get
StatsCalculato
rs
().
map
(
s
=>
{
const
options
=
get
FieldReduce
rs
().
map
(
s
=>
{
return
{
value
:
s
.
id
,
label
:
s
.
name
,
...
...
packages/grafana-ui/src/utils/
statsCalculato
r.test.ts
→
packages/grafana-ui/src/utils/
fieldReduce
r.test.ts
View file @
493bf0c7
import
{
get
StatsCalculators
,
StatID
,
calculateStats
}
from
'./statsCalculato
r'
;
import
{
get
FieldReducers
,
ReducerID
,
reduceField
}
from
'./fieldReduce
r'
;
import
_
from
'lodash'
;
...
...
@@ -10,28 +10,28 @@ describe('Stats Calculators', () => {
it
(
'should load all standard stats'
,
()
=>
{
const
names
=
[
Stat
ID
.
sum
,
Stat
ID
.
max
,
Stat
ID
.
min
,
Stat
ID
.
logmin
,
Stat
ID
.
mean
,
Stat
ID
.
last
,
Stat
ID
.
first
,
Stat
ID
.
count
,
Stat
ID
.
range
,
Stat
ID
.
diff
,
Stat
ID
.
step
,
Stat
ID
.
delta
,
//
Stat
ID.allIsZero,
//
Stat
ID.allIsNull,
Reducer
ID
.
sum
,
Reducer
ID
.
max
,
Reducer
ID
.
min
,
Reducer
ID
.
logmin
,
Reducer
ID
.
mean
,
Reducer
ID
.
last
,
Reducer
ID
.
first
,
Reducer
ID
.
count
,
Reducer
ID
.
range
,
Reducer
ID
.
diff
,
Reducer
ID
.
step
,
Reducer
ID
.
delta
,
//
Reducer
ID.allIsZero,
//
Reducer
ID.allIsNull,
];
const
stats
=
get
StatsCalculato
rs
(
names
);
const
stats
=
get
FieldReduce
rs
(
names
);
expect
(
stats
.
length
).
toBe
(
names
.
length
);
});
it
(
'should fail to load unknown stats'
,
()
=>
{
const
names
=
[
'not a stat'
,
StatID
.
max
,
Stat
ID
.
min
,
'also not a stat'
];
const
stats
=
get
StatsCalculato
rs
(
names
);
const
names
=
[
'not a stat'
,
ReducerID
.
max
,
Reducer
ID
.
min
,
'also not a stat'
];
const
stats
=
get
FieldReduce
rs
(
names
);
expect
(
stats
.
length
).
toBe
(
2
);
const
found
=
stats
.
map
(
v
=>
v
.
id
);
...
...
@@ -42,10 +42,10 @@ describe('Stats Calculators', () => {
});
it
(
'should calculate basic stats'
,
()
=>
{
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
:
basicTable
,
fieldIndex
:
0
,
stat
s
:
[
'first'
,
'last'
,
'mean'
],
reducer
s
:
[
'first'
,
'last'
,
'mean'
],
});
// First
...
...
@@ -59,10 +59,10 @@ describe('Stats Calculators', () => {
});
it
(
'should support a single stat also'
,
()
=>
{
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
:
basicTable
,
fieldIndex
:
0
,
stat
s
:
[
'first'
],
reducer
s
:
[
'first'
],
});
// Should do the simple version that just looks up value
...
...
@@ -71,10 +71,10 @@ describe('Stats Calculators', () => {
});
it
(
'should get non standard stats'
,
()
=>
{
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
:
basicTable
,
fieldIndex
:
0
,
stats
:
[
StatID
.
distinctCount
,
Stat
ID
.
changeCount
],
reducers
:
[
ReducerID
.
distinctCount
,
Reducer
ID
.
changeCount
],
});
expect
(
stats
.
distinctCount
).
toEqual
(
2
);
...
...
@@ -82,10 +82,10 @@ describe('Stats Calculators', () => {
});
it
(
'should calculate step'
,
()
=>
{
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
:
{
fields
:
[{
name
:
'A'
}],
rows
:
[[
100
],
[
200
],
[
300
],
[
400
]]
},
fieldIndex
:
0
,
stats
:
[
StatID
.
step
,
Stat
ID
.
delta
],
reducers
:
[
ReducerID
.
step
,
Reducer
ID
.
delta
],
});
expect
(
stats
.
step
).
toEqual
(
100
);
...
...
packages/grafana-ui/src/utils/
statsCalculato
r.ts
→
packages/grafana-ui/src/utils/
fieldReduce
r.ts
View file @
493bf0c7
...
...
@@ -3,7 +3,7 @@ import isNumber from 'lodash/isNumber';
import
{
SeriesData
,
NullValueMode
}
from
'../types/index'
;
export
enum
Stat
ID
{
export
enum
Reducer
ID
{
sum
=
'sum'
,
max
=
'max'
,
min
=
'min'
,
...
...
@@ -24,14 +24,14 @@ export enum StatID {
allIsNull
=
'allIsNull'
,
}
export
interface
Field
Stat
s
{
export
interface
Field
Calc
s
{
[
key
:
string
]:
any
;
}
// Internal function
type
StatCalculator
=
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
)
=>
FieldStat
s
;
type
FieldReducer
=
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
)
=>
FieldCalc
s
;
export
interface
StatCalculato
rInfo
{
export
interface
FieldReduce
rInfo
{
id
:
string
;
name
:
string
;
description
:
string
;
...
...
@@ -40,16 +40,16 @@ export interface StatCalculatorInfo {
// Internal details
emptyInputResult
?:
any
;
// typically null, but some things like 'count' & 'sum' should be zero
standard
:
boolean
;
// The most common stats can all be calculated in a single pass
calculator
?:
StatCalculato
r
;
reduce
?:
FieldReduce
r
;
}
/**
* @param ids list of stat names or null to get all of them
*/
export
function
get
StatsCalculators
(
ids
?:
string
[]):
StatCalculato
rInfo
[]
{
export
function
get
FieldReducers
(
ids
?:
string
[]):
FieldReduce
rInfo
[]
{
if
(
ids
===
null
||
ids
===
undefined
)
{
if
(
!
hasBuiltIndex
)
{
getById
(
Stat
ID
.
mean
);
getById
(
Reducer
ID
.
mean
);
}
return
listOfStats
;
}
...
...
@@ -60,53 +60,53 @@ export function getStatsCalculators(ids?: string[]): StatCalculatorInfo[] {
list
.
push
(
stat
);
}
return
list
;
},
new
Array
<
StatCalculato
rInfo
>
());
},
new
Array
<
FieldReduce
rInfo
>
());
}
export
interface
CalculateStats
Options
{
interface
ReduceField
Options
{
series
:
SeriesData
;
fieldIndex
:
number
;
stat
s
:
string
[];
// The stats to calculate
reducer
s
:
string
[];
// The stats to calculate
nullValueMode
?:
NullValueMode
;
}
/**
* @returns an object with a key for each selected stat
*/
export
function
calculateStats
(
options
:
CalculateStatsOptions
):
FieldStat
s
{
const
{
series
,
fieldIndex
,
stat
s
,
nullValueMode
}
=
options
;
export
function
reduceField
(
options
:
ReduceFieldOptions
):
FieldCalc
s
{
const
{
series
,
fieldIndex
,
reducer
s
,
nullValueMode
}
=
options
;
if
(
!
stats
||
stat
s
.
length
<
1
)
{
if
(
!
reducers
||
reducer
s
.
length
<
1
)
{
return
{};
}
const
queue
=
get
StatsCalculators
(
stat
s
);
const
queue
=
get
FieldReducers
(
reducer
s
);
// Return early for empty series
// This lets the concrete implementations assume at least one row
if
(
!
series
.
rows
||
series
.
rows
.
length
<
1
)
{
const
stats
=
{}
as
FieldStat
s
;
for
(
const
stat
of
queue
)
{
stats
[
stat
.
id
]
=
stat
.
emptyInputResult
!==
null
?
stat
.
emptyInputResult
:
null
;
const
calcs
=
{}
as
FieldCalc
s
;
for
(
const
reducer
of
queue
)
{
calcs
[
reducer
.
id
]
=
reducer
.
emptyInputResult
!==
null
?
reducer
.
emptyInputResult
:
null
;
}
return
stat
s
;
return
calc
s
;
}
const
ignoreNulls
=
nullValueMode
===
NullValueMode
.
Ignore
;
const
nullAsZero
=
nullValueMode
===
NullValueMode
.
AsZero
;
// Avoid calculating all the standard stats if possible
if
(
queue
.
length
===
1
&&
queue
[
0
].
calculator
)
{
return
queue
[
0
].
calculator
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
);
if
(
queue
.
length
===
1
&&
queue
[
0
].
reduce
)
{
return
queue
[
0
].
reduce
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
);
}
// For now everything can use the standard stats
let
values
=
standardStatsStat
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
);
for
(
const
calc
of
queue
)
{
if
(
!
values
.
hasOwnProperty
(
calc
.
id
)
&&
calc
.
calculator
)
{
let
values
=
doStandardCalcs
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
);
for
(
const
reducer
of
queue
)
{
if
(
!
values
.
hasOwnProperty
(
reducer
.
id
)
&&
reducer
.
reduce
)
{
values
=
{
...
values
,
...
calc
.
calculator
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
),
...
reducer
.
reduce
(
series
,
fieldIndex
,
ignoreNulls
,
nullAsZero
),
};
}
}
...
...
@@ -121,30 +121,30 @@ export function calculateStats(options: CalculateStatsOptions): FieldStats {
// private registry of all stats
interface
TableStatIndex
{
[
id
:
string
]:
StatCalculato
rInfo
;
[
id
:
string
]:
FieldReduce
rInfo
;
}
const
listOfStats
:
StatCalculato
rInfo
[]
=
[];
const
listOfStats
:
FieldReduce
rInfo
[]
=
[];
const
index
:
TableStatIndex
=
{};
let
hasBuiltIndex
=
false
;
function
getById
(
id
:
string
):
StatCalculato
rInfo
|
undefined
{
function
getById
(
id
:
string
):
FieldReduce
rInfo
|
undefined
{
if
(
!
hasBuiltIndex
)
{
[
{
id
:
Stat
ID
.
last
,
id
:
Reducer
ID
.
last
,
name
:
'Last'
,
description
:
'Last Value (current)'
,
standard
:
true
,
alias
:
'current'
,
calculator
:
calculateLast
,
reduce
:
calculateLast
,
},
{
id
:
StatID
.
first
,
name
:
'First'
,
description
:
'First Value'
,
standard
:
true
,
calculator
:
calculateFirst
},
{
id
:
Stat
ID
.
min
,
name
:
'Min'
,
description
:
'Minimum Value'
,
standard
:
true
},
{
id
:
Stat
ID
.
max
,
name
:
'Max'
,
description
:
'Maximum Value'
,
standard
:
true
},
{
id
:
Stat
ID
.
mean
,
name
:
'Mean'
,
description
:
'Average Value'
,
standard
:
true
,
alias
:
'avg'
},
{
id
:
ReducerID
.
first
,
name
:
'First'
,
description
:
'First Value'
,
standard
:
true
,
reduce
:
calculateFirst
},
{
id
:
Reducer
ID
.
min
,
name
:
'Min'
,
description
:
'Minimum Value'
,
standard
:
true
},
{
id
:
Reducer
ID
.
max
,
name
:
'Max'
,
description
:
'Maximum Value'
,
standard
:
true
},
{
id
:
Reducer
ID
.
mean
,
name
:
'Mean'
,
description
:
'Average Value'
,
standard
:
true
,
alias
:
'avg'
},
{
id
:
Stat
ID
.
sum
,
id
:
Reducer
ID
.
sum
,
name
:
'Total'
,
description
:
'The sum of all values'
,
emptyInputResult
:
0
,
...
...
@@ -152,55 +152,55 @@ function getById(id: string): StatCalculatorInfo | undefined {
alias
:
'total'
,
},
{
id
:
Stat
ID
.
count
,
id
:
Reducer
ID
.
count
,
name
:
'Count'
,
description
:
'Number of values in response'
,
emptyInputResult
:
0
,
standard
:
true
,
},
{
id
:
Stat
ID
.
range
,
id
:
Reducer
ID
.
range
,
name
:
'Range'
,
description
:
'Difference between minimum and maximum values'
,
standard
:
true
,
},
{
id
:
Stat
ID
.
delta
,
id
:
Reducer
ID
.
delta
,
name
:
'Delta'
,
description
:
'Cumulative change in value'
,
standard
:
true
,
},
{
id
:
Stat
ID
.
step
,
id
:
Reducer
ID
.
step
,
name
:
'Step'
,
description
:
'Minimum interval between values'
,
standard
:
true
,
},
{
id
:
Stat
ID
.
diff
,
id
:
Reducer
ID
.
diff
,
name
:
'Difference'
,
description
:
'Difference between first and last values'
,
standard
:
true
,
},
{
id
:
Stat
ID
.
logmin
,
id
:
Reducer
ID
.
logmin
,
name
:
'Min (above zero)'
,
description
:
'Used for log min scale'
,
standard
:
true
,
},
{
id
:
Stat
ID
.
changeCount
,
id
:
Reducer
ID
.
changeCount
,
name
:
'Change Count'
,
description
:
'Number of times the value changes'
,
standard
:
false
,
calculator
:
calculateChangeCount
,
reduce
:
calculateChangeCount
,
},
{
id
:
Stat
ID
.
distinctCount
,
id
:
Reducer
ID
.
distinctCount
,
name
:
'Distinct Count'
,
description
:
'Number of distinct values'
,
standard
:
false
,
calculator
:
calculateDistinctCount
,
reduce
:
calculateDistinctCount
,
},
].
forEach
(
info
=>
{
const
{
id
,
alias
}
=
info
;
...
...
@@ -222,13 +222,8 @@ function getById(id: string): StatCalculatorInfo | undefined {
return
index
[
id
];
}
function
standardStatsStat
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
FieldStats
{
const
stats
=
{
function
doStandardCalcs
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
FieldCalcs
{
const
calcs
=
{
sum
:
0
,
max
:
-
Number
.
MAX_VALUE
,
min
:
Number
.
MAX_VALUE
,
...
...
@@ -247,7 +242,7 @@ function standardStatsStat(
// Just used for calcutations -- not exposed as a stat
previousDeltaUp
:
true
,
}
as
Field
Stat
s
;
}
as
Field
Calc
s
;
for
(
let
i
=
0
;
i
<
data
.
rows
.
length
;
i
++
)
{
let
currentValue
=
data
.
rows
[
i
][
fieldIndex
];
...
...
@@ -262,94 +257,94 @@ function standardStatsStat(
}
if
(
currentValue
!==
null
)
{
const
isFirst
=
stat
s
.
first
===
null
;
const
isFirst
=
calc
s
.
first
===
null
;
if
(
isFirst
)
{
stat
s
.
first
=
currentValue
;
calc
s
.
first
=
currentValue
;
}
if
(
isNumber
(
currentValue
))
{
stat
s
.
sum
+=
currentValue
;
stat
s
.
allIsNull
=
false
;
stat
s
.
nonNullCount
++
;
calc
s
.
sum
+=
currentValue
;
calc
s
.
allIsNull
=
false
;
calc
s
.
nonNullCount
++
;
if
(
!
isFirst
)
{
const
step
=
currentValue
-
stat
s
.
last
!
;
if
(
stat
s
.
step
>
step
)
{
stat
s
.
step
=
step
;
// the minimum interval
const
step
=
currentValue
-
calc
s
.
last
!
;
if
(
calc
s
.
step
>
step
)
{
calc
s
.
step
=
step
;
// the minimum interval
}
if
(
stat
s
.
last
!
>
currentValue
)
{
if
(
calc
s
.
last
!
>
currentValue
)
{
// counter reset
stat
s
.
previousDeltaUp
=
false
;
calc
s
.
previousDeltaUp
=
false
;
if
(
i
===
data
.
rows
.
length
-
1
)
{
// reset on last
stat
s
.
delta
+=
currentValue
;
calc
s
.
delta
+=
currentValue
;
}
}
else
{
if
(
stat
s
.
previousDeltaUp
)
{
stat
s
.
delta
+=
step
;
// normal increment
if
(
calc
s
.
previousDeltaUp
)
{
calc
s
.
delta
+=
step
;
// normal increment
}
else
{
stat
s
.
delta
+=
currentValue
;
// account for counter reset
calc
s
.
delta
+=
currentValue
;
// account for counter reset
}
stat
s
.
previousDeltaUp
=
true
;
calc
s
.
previousDeltaUp
=
true
;
}
}
if
(
currentValue
>
stat
s
.
max
)
{
stat
s
.
max
=
currentValue
;
if
(
currentValue
>
calc
s
.
max
)
{
calc
s
.
max
=
currentValue
;
}
if
(
currentValue
<
stat
s
.
min
)
{
stat
s
.
min
=
currentValue
;
if
(
currentValue
<
calc
s
.
min
)
{
calc
s
.
min
=
currentValue
;
}
if
(
currentValue
<
stat
s
.
logmin
&&
currentValue
>
0
)
{
stat
s
.
logmin
=
currentValue
;
if
(
currentValue
<
calc
s
.
logmin
&&
currentValue
>
0
)
{
calc
s
.
logmin
=
currentValue
;
}
}
if
(
currentValue
!==
0
)
{
stat
s
.
allIsZero
=
false
;
calc
s
.
allIsZero
=
false
;
}
stat
s
.
last
=
currentValue
;
calc
s
.
last
=
currentValue
;
}
}
if
(
stat
s
.
max
===
-
Number
.
MAX_VALUE
)
{
stat
s
.
max
=
null
;
if
(
calc
s
.
max
===
-
Number
.
MAX_VALUE
)
{
calc
s
.
max
=
null
;
}
if
(
stat
s
.
min
===
Number
.
MAX_VALUE
)
{
stat
s
.
min
=
null
;
if
(
calc
s
.
min
===
Number
.
MAX_VALUE
)
{
calc
s
.
min
=
null
;
}
if
(
stat
s
.
step
===
Number
.
MAX_VALUE
)
{
stat
s
.
step
=
null
;
if
(
calc
s
.
step
===
Number
.
MAX_VALUE
)
{
calc
s
.
step
=
null
;
}
if
(
stat
s
.
nonNullCount
>
0
)
{
stats
.
mean
=
stats
.
sum
!
/ stat
s.nonNullCount
;
if
(
calc
s
.
nonNullCount
>
0
)
{
calcs
.
mean
=
calcs
.
sum
!
/ calc
s.nonNullCount
;
}
if
(
stats
.
max
!==
null
&&
stat
s
.
min
!==
null
)
{
stats
.
range
=
stats
.
max
-
stat
s
.
min
;
if
(
calcs
.
max
!==
null
&&
calc
s
.
min
!==
null
)
{
calcs
.
range
=
calcs
.
max
-
calc
s
.
min
;
}
if
(
stats
.
first
!==
null
&&
stat
s
.
last
!==
null
)
{
if
(
isNumber
(
stats
.
first
)
&&
isNumber
(
stat
s
.
last
))
{
stats
.
diff
=
stats
.
last
-
stat
s
.
first
;
if
(
calcs
.
first
!==
null
&&
calc
s
.
last
!==
null
)
{
if
(
isNumber
(
calcs
.
first
)
&&
isNumber
(
calc
s
.
last
))
{
calcs
.
diff
=
calcs
.
last
-
calc
s
.
first
;
}
}
return
stat
s
;
return
calc
s
;
}
function
calculateFirst
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Stat
s
{
function
calculateFirst
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Calc
s
{
return
{
first
:
data
.
rows
[
0
][
fieldIndex
]
};
}
function
calculateLast
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Stat
s
{
function
calculateLast
(
data
:
SeriesData
,
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Calc
s
{
return
{
last
:
data
.
rows
[
data
.
rows
.
length
-
1
][
fieldIndex
]
};
}
...
...
@@ -358,7 +353,7 @@ function calculateChangeCount(
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Stat
s
{
):
Field
Calc
s
{
let
count
=
0
;
let
first
=
true
;
let
last
:
any
=
null
;
...
...
@@ -387,7 +382,7 @@ function calculateDistinctCount(
fieldIndex
:
number
,
ignoreNulls
:
boolean
,
nullAsZero
:
boolean
):
Field
Stat
s
{
):
Field
Calc
s
{
const
distinct
=
new
Set
<
any
>
();
for
(
let
i
=
0
;
i
<
data
.
rows
.
length
;
i
++
)
{
let
currentValue
=
data
.
rows
[
i
][
fieldIndex
];
...
...
packages/grafana-ui/src/utils/index.ts
View file @
493bf0c7
...
...
@@ -5,7 +5,7 @@ export * from './namedColorsPalette';
export
*
from
'./thresholds'
;
export
*
from
'./string'
;
export
*
from
'./csv'
;
export
*
from
'./
statsCalculato
r'
;
export
*
from
'./
fieldReduce
r'
;
export
*
from
'./displayValue'
;
export
*
from
'./deprecationWarning'
;
export
*
from
'./logs'
;
...
...
public/app/plugins/panel/bargauge/types.ts
View file @
493bf0c7
import
{
VizOrientation
,
SelectOptionItem
,
Stat
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
import
{
VizOrientation
,
SelectOptionItem
,
Reducer
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
export
interface
BarGaugeOptions
extends
SingleStatBaseOptions
{
minValue
:
number
;
...
...
@@ -24,7 +24,7 @@ export const defaults: BarGaugeOptions = {
orientation
:
VizOrientation
.
Horizontal
,
valueOptions
:
{
unit
:
'none'
,
stat
:
Stat
ID
.
mean
,
stat
:
Reducer
ID
.
mean
,
prefix
:
''
,
suffix
:
''
,
decimals
:
null
,
...
...
public/app/plugins/panel/gauge/types.ts
View file @
493bf0c7
import
{
VizOrientation
,
Stat
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
import
{
VizOrientation
,
Reducer
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
export
interface
GaugeOptions
extends
SingleStatBaseOptions
{
maxValue
:
number
;
...
...
@@ -16,7 +16,7 @@ export const defaults: GaugeOptions = {
prefix
:
''
,
suffix
:
''
,
decimals
:
null
,
stat
:
Stat
ID
.
mean
,
stat
:
Reducer
ID
.
mean
,
unit
:
'none'
,
},
valueMappings
:
[],
...
...
public/app/plugins/panel/graph2/getGraphSeriesModel.ts
View file @
493bf0c7
import
{
GraphSeriesXY
,
NullValueMode
,
calculateStats
,
reduceField
,
colors
,
getFlotPairs
,
getColorFromHexRgbOrName
,
...
...
@@ -45,8 +45,12 @@ export const getGraphSeriesModel = (
});
if
(
points
.
length
>
0
)
{
const
seriesStats
=
calculateStats
({
series
,
stats
:
legendOptions
.
stats
,
fieldIndex
:
field
.
index
});
let
statsDisplayValues
;
const
seriesStats
=
reduceField
({
series
,
reducers
:
legendOptions
.
stats
,
fieldIndex
:
field
.
index
,
});
let
statsDisplayValues
:
DisplayValue
[];
if
(
legendOptions
.
stats
)
{
statsDisplayValues
=
legendOptions
.
stats
.
map
<
DisplayValue
>
(
stat
=>
{
...
...
public/app/plugins/panel/piechart/types.ts
View file @
493bf0c7
import
{
PieChartType
,
Stat
ID
,
VizOrientation
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
import
{
PieChartType
,
Reducer
ID
,
VizOrientation
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
export
interface
PieChartOptions
extends
SingleStatBaseOptions
{
pieType
:
PieChartType
;
...
...
@@ -10,7 +10,7 @@ export const defaults: PieChartOptions = {
strokeWidth
:
1
,
valueOptions
:
{
unit
:
'short'
,
stat
:
Stat
ID
.
last
,
stat
:
Reducer
ID
.
last
,
suffix
:
''
,
prefix
:
''
,
},
...
...
public/app/plugins/panel/singlestat2/SingleStatPanel.tsx
View file @
493bf0c7
...
...
@@ -16,7 +16,7 @@ import {
PanelProps
,
getDisplayProcessor
,
NullValueMode
,
calculateStats
,
reduceField
,
FieldCache
,
FieldType
,
}
from
'@grafana/ui'
;
...
...
@@ -57,10 +57,10 @@ export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>
for
(
let
i
=
0
;
i
<
numberFields
.
length
;
i
++
)
{
const
field
=
numberFields
[
i
];
const
stats
=
calculateStats
({
const
stats
=
reduceField
({
series
,
fieldIndex
:
field
.
index
,
stat
s
:
[
stat
],
// The stats to calculate
reducer
s
:
[
stat
],
// The stats to calculate
nullValueMode
:
NullValueMode
.
Null
,
});
...
...
public/app/plugins/panel/singlestat2/types.ts
View file @
493bf0c7
import
{
VizOrientation
,
Stat
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
import
{
VizOrientation
,
Reducer
ID
,
SingleStatBaseOptions
}
from
'@grafana/ui'
;
export
interface
SparklineOptions
{
show
:
boolean
;
...
...
@@ -33,7 +33,7 @@ export const defaults: SingleStatOptions = {
prefix
:
''
,
suffix
:
''
,
decimals
:
null
,
stat
:
Stat
ID
.
mean
,
stat
:
Reducer
ID
.
mean
,
unit
:
'none'
,
},
valueMappings
:
[],
...
...
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