diff options
Diffstat (limited to 'src/couchdb/couch_httpd_stats_handlers.erl')
-rw-r--r-- | src/couchdb/couch_httpd_stats_handlers.erl | 67 |
1 files changed, 30 insertions, 37 deletions
diff --git a/src/couchdb/couch_httpd_stats_handlers.erl b/src/couchdb/couch_httpd_stats_handlers.erl index 81ff2621..40444bf8 100644 --- a/src/couchdb/couch_httpd_stats_handlers.erl +++ b/src/couchdb/couch_httpd_stats_handlers.erl @@ -12,51 +12,44 @@ -module(couch_httpd_stats_handlers). -include("couch_db.hrl"). --include("couch_stats.hrl"). -export([handle_stats_req/1]). --import(couch_httpd, - [send_json/2,send_json/3,send_json/4,send_method_not_allowed/2, - start_json_response/2,send_chunk/2,end_json_response/1, - start_chunked_response/3, send_error/4]). - --define(b2a(V), list_to_atom(binary_to_list(V))). - --record(stats_query_args, { - range='0', - flush=false -}). +-import(couch_httpd, [ + send_json/2, send_json/3, send_json/4, send_method_not_allowed/2, + start_json_response/2, send_chunk/2, end_json_response/1, + start_chunked_response/3, send_error/4 +]). handle_stats_req(#httpd{method='GET', path_parts=[_]}=Req) -> - send_json(Req, couch_stats_aggregator:all()); + flush(Req), + send_json(Req, couch_stats_aggregator:all(range(Req))); -handle_stats_req(#httpd{method='GET', path_parts=[_Stats, Module, Key]}=Req) -> - #stats_query_args{ - range=Range, - flush=Flush - } = parse_stats_query(Req), +handle_stats_req(#httpd{method='GET', path_parts=[_, _Mod]}) -> + throw({bad_request, <<"Stat names must have exactly to parts.">>}); - case Flush of - true -> - couch_stats_aggregator:time_passed(); - _ -> ok - end, +handle_stats_req(#httpd{method='GET', path_parts=[_, Mod, Key]}=Req) -> + flush(Req), + Stats = couch_stats_aggregator:get_json({?b2a(Mod), ?b2a(Key)}, range(Req)), + send_json(Req, {[{Mod, {[{Key, Stats}]}}]}); - Stats = couch_stats_aggregator:get_json({?b2a(Module), ?b2a(Key)}, Range), - Response = {[{Module, {[{Key, Stats}]}}]}, - send_json(Req, Response); +handle_stats_req(#httpd{method='GET', path_parts=[_, _Mod, _Key | _Extra]}) -> + throw({bad_request, <<"Stat names must have exactly two parts.">>}); handle_stats_req(Req) -> send_method_not_allowed(Req, "GET"). -parse_stats_query(Req) -> - lists:foldl(fun({Key,Value}, Args) -> - case {Key, Value} of - {"range", Range} -> - Args#stats_query_args{range=list_to_atom(Range)}; - {"flush", "true"} -> - Args#stats_query_args{flush=true}; - _Else -> % unknown key value pair, ignore. - Args - end - end, #stats_query_args{}, couch_httpd:qs(Req)). +range(Req) -> + case proplists:get_value("range", couch_httpd:qs(Req)) of + undefined -> + 0; + Value -> + list_to_integer(Value) + end. + +flush(Req) -> + case proplists:get_value("flush", couch_httpd:qs(Req)) of + "true" -> + couch_stats_aggregator:collect_sample(); + _Else -> + ok + end. |