From 1548b2e0b724e810c521e6613b1e1150b4007baf Mon Sep 17 00:00:00 2001 From: Simon MacMullen Date: Tue, 5 Feb 2013 15:56:03 +0000 Subject: [PATCH] Calculate average as well as average rate. --- deps/rabbitmq_management/src/rabbit_mgmt_db.erl | 7 +++++-- .../test/src/rabbit_mgmt_test_db.erl | 8 +++++--- .../test/src/rabbit_mgmt_test_db_unit.erl | 13 +++++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_db.erl b/deps/rabbitmq_management/src/rabbit_mgmt_db.erl index c9f609246d..a7c0080723 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_db.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_db.erl @@ -790,10 +790,13 @@ format_sample_details(Range, #stats{diffs = Diffs, base = Base}, Interval) -> Part1 = [{rate, Rate}, {interval, Interval}, {samples, Samples}], - Part2 = case length(Samples) > 1 of + Length = length(Samples), + Part2 = case Length > 1 of true -> [{sample, S2}, {timestamp, T2}] = hd(Samples), [{sample, S1}, {timestamp, T1}] = lists:last(Samples), - [{avg_rate, (S2 - S1) * 1000 / (T2 - T1)}]; + Total = lists:sum([pget(sample, I) || I <- Samples]), + [{avg_rate, (S2 - S1) * 1000 / (T2 - T1)}, + {avg, Total / Length}]; false -> [] end, {Part1 ++ Part2, Count}. diff --git a/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db.erl b/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db.erl index 73ade23d15..0f931eafff 100644 --- a/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db.erl +++ b/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db.erl @@ -216,15 +216,17 @@ get_ch(Name, Range) -> rabbit_mgmt_db:get_channel(a2b(Name), Range). get_overview(Range) -> rabbit_mgmt_db:get_overview(Range). get_overview_q(Range) -> pget(queue_totals, get_overview(Range)). -details(R, AR, L) -> +details(R, AR, A, L) -> [{rate, R}, {interval, 5000}, {samples, [[{sample, S}, {timestamp, T * 1000}] || {T, S} <- L]}, - {avg_rate, AR}]. + {avg_rate, AR}, + {avg, A}]. simple_details(Thing, N) -> [{Thing, N}, - {atom_suffix(Thing, "_details"), details(0, 0.0, [{1, N}, {0, N}])}]. + {atom_suffix(Thing, "_details"), + details(0, 0.0, N * 1.0, [{1, N}, {0, N}])}]. atom_suffix(Atom, Suffix) -> list_to_atom(atom_to_list(Atom) ++ Suffix). diff --git a/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db_unit.erl b/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db_unit.erl index a653cd634a..d066dcb276 100644 --- a/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db_unit.erl +++ b/deps/rabbitmq_management/test/src/rabbit_mgmt_test_db_unit.erl @@ -59,20 +59,20 @@ format_sample_details_test() -> %% Just three samples, all of which we format. Note the %% instantaneous rate is taken from the penultimate sample. T({10, 30, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, - {[{30, 61}, {20, 31}, {10, 11}], 2.0, 10, 2.5, 61}), + {[{30, 61}, {20, 31}, {10, 11}], 2.0, 10, 2.5, 103/3, 61}), %% Skip over the second (and ditto). T({10, 30, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1}, - {[{30, 61}, {10, 11}], 2.0, 10, 2.5, 61}), + {[{30, 61}, {10, 11}], 2.0, 10, 2.5, 36.0, 61}), %% Skip over some and invent some. Note that the instantaneous %% rate drops to 0 since the last event is now in the past. T({0, 40, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1}, - {[{40, 61}, {20, 31}, {0, 1}], 0, 10, 1.5, 61}), + {[{40, 61}, {20, 31}, {0, 1}], 0, 10, 1.5, 31.0, 61}), %% And a case where the range starts after the samples T({20, 40, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, - {[{40, 61}, {30, 61}, {20, 31}], 0, 10, 1.5, 61}), + {[{40, 61}, {30, 61}, {20, 31}], 0, 10, 1.5, 51.0, 61}), %% A single sample - which should lead to some bits not getting generated T({10, 10, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, @@ -106,11 +106,12 @@ format({Samples, Rate, Interval, Count}) -> {samples, format_samples(Samples)}], Count}; -format({Samples, Rate, Interval, AvgRate, Count}) -> +format({Samples, Rate, Interval, AvgRate, Avg, Count}) -> {[{rate, Rate}, {interval, Interval * 1000}, {samples, format_samples(Samples)}, - {avg_rate, AvgRate}], + {avg_rate, AvgRate}, + {avg, Avg}], Count}. format_samples(Samples) ->