If there's nothing in the querystring about a range, don't return any samples or averages or anything.

This commit is contained in:
Simon MacMullen 2013-02-05 16:50:59 +00:00
parent 908cff84b9
commit 04aa8b311a
3 changed files with 59 additions and 16 deletions

View File

@ -775,6 +775,14 @@ pick_range(K, {RangeL, RangeM, RangeD}) ->
{false, false, true} -> RangeD
end.
format_sample_details(no_range, #stats{diffs = Diffs, base = Base}, Interval) ->
Rate = case nth_largest(Diffs, 2) of
false -> 0;
{_TS, S} -> S * 1000 / Interval
end,
Count = sum_entire_tree(gb_trees:iterator(Diffs), Base),
{[{rate, Rate}], Count};
format_sample_details(Range, #stats{diffs = Diffs, base = Base}, Interval) ->
RangePoint = Range#range.last - Interval,
{Samples, Count} = extract_samples(
@ -817,6 +825,12 @@ nth_largest(Tree, N) ->
end
end.
sum_entire_tree(Iter, Acc) ->
case gb_trees:next(Iter) of
none -> Acc;
{_TS, S, Iter2} -> sum_entire_tree(Iter2, Acc + S)
end.
%% What we want to do here is: given the #range{}, provide a set of
%% samples such that we definitely provide a set of samples which
%% covers the exact range requested, despite the fact that we might

View File

@ -492,16 +492,27 @@ range(ReqData) -> {range("lengths", ReqData),
range("data_rates", ReqData)}.
range(Prefix, ReqData) ->
Age = int(wrq:get_qs_value(Prefix ++ "_age", ReqData), 5) * 1000,
Incr = int(wrq:get_qs_value(Prefix ++ "_incr", ReqData), 5) * 1000,
%% Take floor on queries so we make sure we only return samples
%% for which we've finished receiving events. Fixes the "drop at
%% the end" problem.
Last = (rabbit_mgmt_format:timestamp_ms(erlang:now()) div Incr) * Incr,
#range{first = Last - Age, last = Last, incr = Incr}.
int(Str, Default) ->
case catch list_to_integer(Str) of
{'EXIT', _} -> Default;
Integer -> Integer
Age = int(Prefix ++ "_age", ReqData),
Incr = int(Prefix ++ "_incr", ReqData),
if
is_integer(Age) andalso is_integer(Incr) ->
%% Take floor on queries so we make sure we only return samples
%% for which we've finished receiving events. Fixes the "drop at
%% the end" problem.
Now = rabbit_mgmt_format:timestamp_ms(erlang:now()),
Last = (Now div Incr) * Incr,
#range{first = (Last - Age) * 1000,
last = Last * 1000,
incr = Incr * 1000};
true ->
no_range
end.
int(Name, ReqData) ->
case wrq:get_qs_value(Name, ReqData) of
undefined -> undefined;
Str -> case catch list_to_integer(Str) of
{'EXIT', _} -> undefined;
Integer -> Integer
end
end.

View File

@ -49,10 +49,10 @@ format_sample_details_test() ->
T = fun ({First, Last, Incr}, Stats, Results) ->
?assertEqual(format(Results),
rabbit_mgmt_db:format_sample_details(
#range{first = First * 1000,
last = Last * 1000,
incr = Incr * 1000},
stats(Stats),
#range{first = First * 1000,
last = Last * 1000,
incr = Incr * 1000},
stats(Stats),
Interval * 1000))
end,
@ -85,6 +85,20 @@ format_sample_details_test() ->
%% TODO more?
ok.
format_sample_details_no_range_test() ->
Interval = 10,
T = fun (Stats, Results) ->
?assertEqual(format(Results),
rabbit_mgmt_db:format_sample_details(
no_range, stats(Stats), Interval * 1000))
end,
%% Just three samples
T({[{10, 10}, {20, 20}, {30, 30}], 1},
{2.0, 61}),
ok.
%%--------------------------------------------------------------------
cutoff() ->
@ -100,6 +114,10 @@ unstats(#stats{diffs = Diffs, base = Base}) ->
secs_to_millis(L) -> [{TS * 1000, S} || {TS, S} <- L].
millis_to_secs(L) -> [{TS div 1000, S} || {TS, S} <- L].
format({Rate, Count}) ->
{[{rate, Rate}],
Count};
format({Samples, Rate, Count}) ->
{[{rate, Rate},
{samples, format_samples(Samples)}],