Skip to content

Instantly share code, notes, and snippets.

Created June 9, 2012 13:47
Show Gist options
  • Save anonymous/2901032 to your computer and use it in GitHub Desktop.
Save anonymous/2901032 to your computer and use it in GitHub Desktop.
-module(collector).
-behaviour(gen_server).
-export([start_link/1, do/2, ended/0]).
-export([handle_call/3, handle_cast/2]).
-export([init/1, terminate/2, handle_info/2, code_change/3]).
%% Client interface
start_link(Num) ->
gen_server:start_link({local, collector}, collector, Num, []).
do(Id, Ans) ->
gen_server:cast(collector, {do, Id, Ans}).
ended() ->
gen_server:cast(collector, ended).
%% Implementation
init(Arg) ->
{ok, {Arg, dict:new()}}.
%
handle_cast({do, Id, Ans}, {Num, Dict}) ->
NewDict = case catch proccess_build(Id, Ans, Dict) of
{error, _} -> Dict;
Out -> Out
end,
{noreply, {Num, NewDict}};
handle_cast(ended, {I, Dict}) ->
if
I == 1 ->
out(Dict),
{stop, 'EXIT', []};
true ->
io:format("I=~.10b~n", [I]),
{noreply, {I - 1, Dict}}
end;
handle_cast(_, Dict) ->
io:format("Ban cast~n"),
{noreply, Dict}.
proccess_build(Id, Ans, Stats) ->
Body = orddict:from_list(Ans),
Suits = orddict:fetch(<<"suites">>, Body),
lists:foldr(fun(El, Acc) -> proccess_suite(El, Acc, Id) end, Stats, Suits).
proccess_suite({Suite}, Stats, BuildId) ->
Dict = orddict:from_list(Suite),
Cases = orddict:fetch(<<"cases">>, Dict),
NotFail = lists:foldl(fun ({El}, Acc) ->
Acc and (<<"FAILED">> /= orddict:fetch(<<"status">>, orddict:from_list(El)))
end, true, Cases),
if NotFail == true -> Sinary_to_list(tatus = ok;
true -> Status = fail
end,
SuiteName = orddict:fetch(<<"name">>, Dict),
SuiteDuration = orddict:fetch(<<"duration">>, Dict),
SuiteStatus = Status,
ToStore = [{duration, SuiteDuration}, {status, SuiteStatus}],
ChangedBuilds = case dict:find(SuiteName, Stats) of
{ok, Builds} -> orddict:store(BuildId, ToStore, Builds);
_ -> orddict:store(BuildId, ToStore, orddict:new())
end,
dict:store(SuiteName, ChangedBuilds, Stats).
out(DStats) ->
Stats = dict:to_list(DStats),
{ok, File} = file:open("out.erlf", [write]),
io:write(File, Stats),
file:close(File),
JStats = lists:map(fun ({Name, List}) ->
El = lists:map(fun({Id, Arr}) ->
{[{id, Id}, {info, {Arr}}]}
end, List),
{[{name, Name}, {builds, El}]}
end, Stats),
Encoded = jiffy:encode(JStats),
file:write_file("out.json", binary_to_list(Encoded)).
%
handle_call(_, _Form, {_, Dict}) ->
{reply, Dict, Dict}.
%
terminate(_, _) ->
ok.
handle_info(_, State) ->
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-module(main).
-export([main/0]).
user() -> "<user here>".
pass() -> "<pass here>".
line() -> "Basic " ++ binary_to_list(base64:encode(user() ++ ":" ++ pass())).
get_url(Url) ->
io:fwrite("~s... ", [Url]),
{ok, {_, _, Result}} = httpc:request(get, {Url, [{"Authorization", line()}]}, [], []),
io:fwrite("ok~n"),
jiffy:decode(Result).
main() ->
inets:start(),
{Master_build} = get_url("http://jenkins.ul.js-kit.com/job/master/api/json"),
Builds = orddict:fetch(<<"builds">>, orddict:from_list(Master_build)),
N = 8,
collector:start_link(N),
Lsts = lists:map(fun (I) ->
lists:filter(fun ({Build}) ->
Id = orddict:fetch(<<"number">>, Build),
(Id rem N == I)
end, Builds)
end, lists:seq(0, N-1)),
lists:map(fun (List) ->
spawn(fun () ->
lists:map(fun ({Build}) ->
catch do_one(Build)
end, List),
io:format("ended~n"),
collector:ended(),
{}
end)
end, Lsts).
do_one(Build) ->
Url = binary_to_list(orddict:fetch(<<"url">>, Build)) ++ "testReport/api/json",
Id = orddict:fetch(<<"number">>, Build),
{Ans} = get_url(Url),
collector:do(Id, Ans).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment