summaryrefslogtreecommitdiff
path: root/src/couch_stats/src/couch_stats_httpd.erl
blob: b40ba6094f6b580f908da49abd297b71cc97b2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_stats_httpd).
-include_lib("couch/include/couch_db.hrl").

-export([handle_stats_req/1]).

%% exported for use by chttpd_misc
-export([transform_stats/1, nest/1, to_ejson/1, extract_path/2]).

handle_stats_req(#httpd{method = 'GET', path_parts = [_ | Path]} = Req) ->
    flush(Req),
    Stats0 = couch_stats:fetch(),
    Stats = transform_stats(Stats0),
    Nested = nest(Stats),
    EJSON0 = to_ejson(Nested),
    EJSON1 = extract_path(Path, EJSON0),
    couch_httpd:send_json(Req, EJSON1).

transform_stats(Stats) ->
    transform_stats(Stats, []).

transform_stats([], Acc) ->
    Acc;
transform_stats([{Key, Props} | Rest], Acc) ->
    {_, Type} = proplists:lookup(type, Props),
    transform_stats(Rest, [{Key, transform_stat(Type, Props)} | Acc]).

transform_stat(counter, Props) ->
    Props;
transform_stat(gauge, Props) ->
    Props;
transform_stat(histogram, Props) ->
    lists:map(
        fun
            ({value, Value}) ->
                {value,
                    lists:map(
                        fun
                            ({Key, List}) when Key == percentile; Key == histogram ->
                                {Key, [tuple_to_list(Item) || Item <- List]};
                            (Else) ->
                                Else
                        end,
                        Value
                    )};
            (Else) ->
                Else
        end,
        Props
    ).

nest(Proplist) ->
    nest(Proplist, []).

nest([], Acc) ->
    Acc;
nest([{[Key | Keys], Value} | Rest], Acc) ->
    Acc1 =
        case proplists:lookup(Key, Acc) of
            {Key, Old} ->
                [{Key, nest([{Keys, Value}], Old)} | proplists:delete(Key, Acc)];
            none ->
                Term = lists:foldr(fun(K, A) -> [{K, A}] end, Value, Keys),
                [{Key, Term} | Acc]
        end,
    nest(Rest, Acc1).

to_ejson([{_, _} | _] = Proplist) ->
    EJSONProps = lists:map(
        fun({Key, Value}) -> {maybe_format_key(Key), to_ejson(Value)} end,
        Proplist
    ),
    {EJSONProps};
to_ejson(NotAProplist) ->
    NotAProplist.

extract_path([], EJSON) ->
    EJSON;
extract_path([Key | Rest], {Props}) ->
    case proplists:lookup(Key, Props) of
        {Key, SubEJSON} ->
            extract_path(Rest, SubEJSON);
        none ->
            null
    end;
extract_path([_ | _], _NotAnObject) ->
    null.

maybe_format_key(Key) when is_list(Key) ->
    list_to_binary(Key);
maybe_format_key(Key) when is_atom(Key) ->
    list_to_binary(atom_to_list(Key));
maybe_format_key(Key) when is_integer(Key) ->
    list_to_binary(integer_to_list(Key));
maybe_format_key(Key) when is_binary(Key) ->
    Key.

flush(Req) ->
    case couch_util:get_value("flush", chttpd:qs(Req)) of
        "true" ->
            couch_stats_aggregator:flush();
        _Else ->
            ok
    end.