Skip to content

Instantly share code, notes, and snippets.

@anowak
Last active November 18, 2016 10:37
Show Gist options
  • Save anowak/7c29111215c1ceefbad7e597ba7edefc to your computer and use it in GitHub Desktop.
Save anowak/7c29111215c1ceefbad7e597ba7edefc to your computer and use it in GitHub Desktop.
Get information about which users have open continuous change feeds to CouchDB
% To be executed in Erlang shell
% E.g. connect with erl -sname shell -setcookie $(cat /usr/local/var/lib/couchdb/.erlang.cookie) -remsh couch@$(hostname)
Get_user_for_changes_process = fun(ProcessData) ->
AuthSession = proplists:get_value("AuthSession", proplists:get_value(mochiweb_request_cookie, ProcessData)),
case AuthSession of
undefined -> "not_authenticated_with_cookie";
Cookie ->
SessionInfo = couch_util:decodeBase64Url(Cookie),
[User, _, _] = re:split(SessionInfo, ":", [{return, list}, {parts, 3}]),
User
end
end,
AllProcessesData = [proplists:get_value(dictionary, erlang:process_info(P)) || P <- erlang:processes(), erlang:is_process_alive(P)],
FeedProcessesData = [Data || Data <- AllProcessesData, proplists:is_defined(last_changes_heartbeat, Data)],
rp(io:format("Number of change feeds: ~B~n", [length(FeedProcessesData)])),
ClientIds = [Get_user_for_changes_process(Data) || Data <- FeedProcessesData],
Freqs = dict:to_list(lists:foldl(fun(ClientId, Acc) -> dict:update_counter(ClientId, 1, Acc) end, dict:new(), ClientIds)),
rp(io:format("Number of unique clients: ~B~n", [length(Freqs)])),
rp(lists:sort(fun({_, X}, {_, Y}) -> X =< Y end, Freqs)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment