From e50f1cf9848bc5545852fbc1552b29a83041496a Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Fri, 11 Sep 2015 17:22:01 +0100 Subject: [PATCH] Allow filtering of active tasks by property COUCHDB-2807 --- src/couch_httpd_misc_handlers.erl | 4 ++++ src/couch_task_status.erl | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/couch_httpd_misc_handlers.erl b/src/couch_httpd_misc_handlers.erl index 10d6d9ed..e2c58c98 100644 --- a/src/couch_httpd_misc_handlers.erl +++ b/src/couch_httpd_misc_handlers.erl @@ -96,6 +96,10 @@ handle_all_dbs_req(Req) -> send_method_not_allowed(Req, "GET,HEAD"). +handle_task_status_req(#httpd{method='GET', path_parts=[_, K, V]}=Req) -> + ok = couch_httpd:verify_is_server_admin(Req), + % convert the list of prop lists to a list of json objects + send_json(Req, [{Props} || Props <- couch_task_status:filter(K, V)]); handle_task_status_req(#httpd{method='GET'}=Req) -> ok = couch_httpd:verify_is_server_admin(Req), % convert the list of prop lists to a list of json objects diff --git a/src/couch_task_status.erl b/src/couch_task_status.erl index 4083c3f8..7cf54263 100644 --- a/src/couch_task_status.erl +++ b/src/couch_task_status.erl @@ -28,6 +28,7 @@ -export([start_link/0, stop/0]). -export([all/0, add_task/1, update/1, get/1, set_update_frequency/1]). -export([is_task_added/0]). +-export([filter/2]). -export([init/1, terminate/2, code_change/3]). -export([handle_call/3, handle_cast/2, handle_info/2]). @@ -49,6 +50,10 @@ all() -> gen_server:call(?MODULE, all). +filter(Key, Value) -> + gen_server:call(?MODULE, {filter, Key, Value}). + + add_task(Props) -> put(task_status_update, {{0, 0, 0}, 0}), Ts = timestamp(), @@ -122,6 +127,14 @@ handle_call({add_task, TaskProps}, {From, _}, Server) -> [_] -> {reply, {add_task_error, already_registered}, Server} end; +handle_call({filter, Key, Value}, _, Server) -> + All = [ + [{pid, ?l2b(pid_to_list(Pid))} | TaskProps] + || + {Pid, TaskProps} <- ets:tab2list(?MODULE), + filter_task(Key, Value, TaskProps) + ], + {reply, All, Server}; handle_call(all, _, Server) -> All = [ [{pid, ?l2b(pid_to_list(Pid))} | TaskProps] @@ -130,6 +143,12 @@ handle_call(all, _, Server) -> ], {reply, All, Server}. +filter_task(Key, Value, TaskProps) -> + Key1 = couch_util:to_list(Key), + Value1 = couch_util:to_list(Value), + TaskProps1 = [{couch_util:to_list(K), couch_util:to_list(V)} + || {K, V} <- TaskProps], + couch_util:get_value(Key1, TaskProps1) =:= Value1. handle_cast({update_status, Pid, NewProps}, Server) -> case ets:lookup(?MODULE, Pid) of