1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2018-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_mgmt_data_compat).
9
10-export([fill_get_empty_queue_metric/1,
11         drop_get_empty_queue_metric/1,
12         fill_consumer_active_fields/1,
13         fill_drop_unroutable_metric/1,
14         drop_drop_unroutable_metric/1]).
15
16fill_get_empty_queue_metric(Slide) ->
17    exometer_slide:map(
18      fun
19          (Value) when is_tuple(Value) andalso size(Value) =:= 8 ->
20              Value;
21          (Value) when is_tuple(Value) andalso size(Value) =:= 7 ->
22              %% Inject a 0 for the new metric
23              list_to_tuple(
24                tuple_to_list(Value) ++ [0]);
25          (Value) ->
26              Value
27      end, Slide).
28
29drop_get_empty_queue_metric(Slide) ->
30    exometer_slide:map(
31      fun
32          (Value) when is_tuple(Value) andalso size(Value) =:= 8 ->
33              %% We want to remove the last element, which is
34              %% the count of basic.get on empty queues.
35              list_to_tuple(
36                lists:sublist(
37                  tuple_to_list(Value), size(Value) - 1));
38          (Value) when is_tuple(Value) andalso size(Value) =:= 7 ->
39              Value;
40          (Value) ->
41              Value
42      end, Slide).
43
44fill_drop_unroutable_metric(Slide) ->
45    exometer_slide:map(
46      fun
47          (Value) when is_tuple(Value) andalso size(Value) =:= 4 ->
48              Value;
49          (Value) when is_tuple(Value) andalso size(Value) =:= 3 ->
50              %% Inject a 0
51              list_to_tuple(
52                tuple_to_list(Value) ++ [0]);
53          (Value) ->
54              Value
55      end, Slide).
56
57drop_drop_unroutable_metric(Slide) ->
58    exometer_slide:map(
59      fun
60          (Value) when is_tuple(Value) andalso size(Value) =:= 4 ->
61              %% Remove the last element.
62              list_to_tuple(
63                lists:sublist(
64                  tuple_to_list(Value), size(Value) - 1));
65          (Value) when is_tuple(Value) andalso size(Value) =:= 3 ->
66              Value;
67          (Value) ->
68              Value
69      end, Slide).
70
71fill_consumer_active_fields(ConsumersStats) ->
72    [case proplists:get_value(active, ConsumerStats) of
73         undefined ->
74             [{active, true},
75              {activity_status, up}
76              | ConsumerStats];
77         _ ->
78             ConsumerStats
79     end
80     || ConsumerStats <- ConsumersStats].
81