Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Allow filtering of active tasks by property #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/couch_httpd_misc_handlers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/couch_task_status.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand All @@ -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(),
Expand Down Expand Up @@ -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 = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use following to avoid doing the same thing in a tight loop

handle_call({filter, Key0, Value0}, _, Server) ->
Key = couch_util:to_list(Key0),
Value = couch_util:to_list(Value0),

[{pid, ?l2b(pid_to_list(Pid))} | TaskProps]
||
{Pid, TaskProps} <- ets:tab2list(?MODULE),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use ets:foldl(Function, [], ?MODULE) here?

filter_task(Key, Value, TaskProps)
],
{reply, All, Server};
handle_call(all, _, Server) ->
All = [
[{pid, ?l2b(pid_to_list(Pid))} | TaskProps]
Expand All @@ -130,6 +143,12 @@ handle_call(all, _, Server) ->
],
{reply, All, Server}.

filter_task(Key, Value, TaskProps) ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a huge fan of this function but I don't see much option

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
Expand Down