Skip to content

Commit

Permalink
WHISTLE-1441: update queue endpoint with stat query/resp
Browse files Browse the repository at this point in the history
  • Loading branch information
James Aimonetti committed Sep 20, 2012
1 parent 1274523 commit f593032
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
8 changes: 5 additions & 3 deletions whistle_apps/apps/crossbar/src/modules/cb_agents.erl
Expand Up @@ -160,12 +160,15 @@ fetch_all_agent_stats(#cb_context{account_id=AcctId}=Context, ?REALTIME_PATH_TOK
,2000
) of
{ok, Resp} ->
lager:debug("stats req responded"),
Resp1 = strip_api_fields(wh_json:normalize(Resp)),
Context#cb_context{resp_status=success
,resp_data=Resp1
,doc=Resp1
};
{error, _} -> Context
{error, _E} ->
lager:debug("stats req failed: ~p", [_E]),
Context
end.

fetch_agent_stats(Id, Context) ->
Expand Down Expand Up @@ -245,11 +248,10 @@ normalize_agent_results(JObj, Acc) ->

agent_key([AID, TStamp]) when is_integer(TStamp) -> AID;
agent_key([TStamp, AID]) when is_integer(TStamp) -> AID.

strip_api_fields(JObj) ->
Strip = [<<"event_name">>, <<"event_category">>
,<<"app_name">>, <<"app_version">>
,<<"node">>, <<"msg_id">>, <<"server_id">>
],
wh_json:filter(fun({K,_}) -> not lists:member(K, Strip) end, JObj).

53 changes: 46 additions & 7 deletions whistle_apps/apps/crossbar/src/modules/cb_queues.erl
Expand Up @@ -255,7 +255,7 @@ add_queue_to_agents(Id, #cb_context{req_data=[]}=Context) ->
lager:debug("no agents listed, removing all agents from ~s", [Id]),
#cb_context{resp_data=CurrAgentIds} = load_agent_roster(Id, Context),
rm_queue_from_agents(Id, Context#cb_context{req_data=CurrAgentIds});

add_queue_to_agents(Id, #cb_context{req_data=[_|_]=AgentIds}=Context) ->
%% We need to figure out what agents are on the queue already, and remove those not
%% in the AgentIds list
Expand Down Expand Up @@ -347,9 +347,26 @@ fetch_all_queue_stats(Context) ->
fetch_all_queue_stats(Context, history).
fetch_all_queue_stats(Context, history) ->
crossbar_doc:load_view(<<"acdc_stats/stats_per_queue">>, [], Context, fun normalize_queue_results/2);
fetch_all_queue_stats(Context, realtime) ->
%% TODO: send AMQP query to queues for stats
Context.
fetch_all_queue_stats(#cb_context{account_id=AcctId}=Context, realtime) ->
Req = [{<<"Account-ID">>, AcctId}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
case whapps_util:amqp_pool_request(Req
,fun wapi_acdc_queue:publish_stats_req/1
,fun wapi_acdc_queue:stats_resp_v/1
,2000
) of
{ok, Resp} ->
lager:debug("fetched stats successfully"),
Resp1 = strip_api_fields(wh_json:normalize(Resp)),
Context#cb_context{resp_status=success
,resp_data=Resp1
,doc=Resp1
};
{error, _E} ->
lager:debug("failed to fetch stats: ~p", [_E]),
Context
end.

fetch_queue_stats(Id, Context) ->
fetch_queue_stats(Id, Context, history).
Expand All @@ -363,9 +380,24 @@ fetch_queue_stats(Id, Context, history) ->
,Context
,fun normalize_queue_results/2
);
fetch_queue_stats(Id, Context, realtime) ->
%% TODO: send AMQP request for stats about queue
Context.
fetch_queue_stats(Id, #cb_context{account_id=AcctId}=Context, realtime) ->
Req = [{<<"Account-ID">>, AcctId}
,{<<"Queue-ID">>, Id}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
case whapps_util:amqp_pool_request(Req
,fun wapi_acdc_queue:publish_stats_req/1
,fun wapi_acdc_queue:stats_resp_v/1
,2000
) of
{ok, Resp} ->
Resp1 = strip_api_fields(wh_json:normalize(Resp)),
Context#cb_context{resp_status=success
,resp_data=Resp1
,doc=Resp1
};
{error, _} -> Context
end.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -409,3 +441,10 @@ normalize_agents_results(JObj, Acc) ->
-spec add_pvt_type/2 :: (wh_json:json_object(), #cb_context{}) -> wh_json:json_object().
add_pvt_type(JObj, _) ->
wh_json:set_value(<<"pvt_type">>, ?PVT_TYPE, JObj).

strip_api_fields(JObj) ->
Strip = [<<"event_name">>, <<"event_category">>
,<<"app_name">>, <<"app_version">>
,<<"node">>, <<"msg_id">>, <<"server_id">>
],
wh_json:filter(fun({K,_}) -> not lists:member(K, Strip) end, JObj).

0 comments on commit f593032

Please sign in to comment.