问题
I have two groups of Graphite series, both in this format. The second group is identical, except that instead of "a.b", it has "x.y" prefix.
a.b.ccc.a1.hr
a.b.ccc.a2.hr
a.b.ccc.a3.hr
a.b.ddd.a1.hr
a.b.ddd.a4.hr
To group by 3rd node I use groupByNode(a.b.*.*.hr,2,"sumSeries")
, which gets me two series: ccc
and ddd
. I would like to divide ccc
and ddd
series from the first group by corresponding series in the second group. How do I use the result of groupByNode
in the map/reduce function?
回答1:
This is possible but tricky, or at least I don't know of an easier way to do it in an extensible way.
Note that the approach below uses mapSeries
/ reduceSeries
functions which are only available in graphite-web master
(not 0.9.x, see below for a manual approach that will work on 0.9.x)
We start with 2 seriesLists, that each contain ccc
and ddd
:
groupByNode(a.b.*.*.hr,2,"sumSeries")
groupByNode(x.y.*.*.hr,2,"sumSeries")
Now we need to get them into a single seriesList that contains all the items, so first we're going to need to make them distinguishable again:
aliasSub(groupByNode(a.b.*.*.hr,2,"sumSeries"), "$", ".a_b")
aliasSub(groupByNode(x.y.*.*.hr,2,"sumSeries"), "$", ".x_y")
Now we have ccc.a_b
, ddd.a_b
, ccc.x_y
, and ddd.x_y
, and we can get them into a single list with group
:
group(
aliasSub(groupByNode(a.b.*.*.hr,2,"sumSeries"), "$", ".a_b"),
aliasSub(groupByNode(x.y.*.*.hr,2,"sumSeries"), "$", ".x_y")
)
Now we can start on the map/reduce:
reduceSeries(
mapSeries(
group(
aliasSub(groupByNode(a.b.*.*.hr,2,"sumSeries"), "$", ".a_b"),
aliasSub(groupByNode(x.y.*.*.hr,2,"sumSeries"), "$", ".x_y")
),
0
),
"asPercent", 1, "a_b", "x_y"
)
At this point we'll have ccc.reduce.asPercent
and ddd.reduce.asPercent
, you can then wrap the enire thing in another aliasByNode(<query>, 0)
if you want just ccc
and ddd
.
What this is doing is essentially the same as calling:
group(
alias(asPercent(
groupByNode(a.b.ccc.*.hr,2,"sumSeries"),
groupByNode(x.y.ccc.*.hr,2,"sumSeries"),
), "ccc"),
alias(asPercent(
groupByNode(a.b.ddd.*.hr,2,"sumSeries"),
groupByNode(x.y.ddd.*.hr,2,"sumSeries"),
), "ddd")
)
except of course that it'll continue to work if you add eee
etc.
来源:https://stackoverflow.com/questions/40926039/calculate-percentage-in-graphite-for-groupbynode-results