Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v1.47' into v1.47
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Jan 27, 2012
2 parents 2d58cda + 19c7b64 commit f1df32b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 57 deletions.
46 changes: 23 additions & 23 deletions whistle_apps/apps/dth/src/cdr_handler.erl
Expand Up @@ -26,37 +26,37 @@
init() ->
ok.

handle_req2(JObj, Props) ->
true = wapi_call:cdr_v(JObj),
%% handle_req2(JObj, Props) ->
%% true = wapi_call:cdr_v(JObj),

CallID = wh_json:get_value(<<"Call-ID">>, JObj),
CallDirection = wh_json:get_value(<<"Call-Direction">>, JObj),
?LOG_SYS(CallID, "Valid call cdr", []),
?LOG_SYS(CallID, "Call Direction: ~s", [CallDirection]),
%% CallID = wh_json:get_value(<<"Call-ID">>, JObj),
%% CallDirection = wh_json:get_value(<<"Call-Direction">>, JObj),
%% ?LOG_SYS(CallID, "Valid call cdr", []),
%% ?LOG_SYS(CallID, "Call Direction: ~s", [CallDirection]),

<<"outbound">> = CallDirection, %% b-leg only, though not the greatest way of determining this
%% <<"outbound">> = CallDirection, %% b-leg only, though not the greatest way of determining this

Timestamp = wh_util:to_integer(wh_json:get_value(<<"Timestamp">>, JObj, wh_util:current_tstamp())),
BillingSec = wh_util:to_integer(wh_json:get_value(<<"Billing-Seconds">>, JObj, 0)),
%% Timestamp = wh_util:to_integer(wh_json:get_value(<<"Timestamp">>, JObj, wh_util:current_tstamp())),
%% BillingSec = wh_util:to_integer(wh_json:get_value(<<"Billing-Seconds">>, JObj, 0)),

DateTime = now_to_datetime(Timestamp - BillingSec),
%% DateTime = now_to_datetime(Timestamp - BillingSec),

ToE164 = wh_util:to_e164(get_to_user(JObj)),
FromE164 = wh_util:to_e164(get_from_user(JObj)),
%% ToE164 = wh_util:to_e164(get_to_user(JObj)),
%% FromE164 = wh_util:to_e164(get_from_user(JObj)),

AccountCode = get_account_code(JObj),
%% AccountCode = get_account_code(JObj),

?LOG(CallID, "CDR from ~s to ~s with account code ~s", [FromE164, ToE164, AccountCode]),
%% ?LOG(CallID, "CDR from ~s to ~s with account code ~s", [FromE164, ToE164, AccountCode]),

CallRecord = #'p:CallRecord'{'CustomerID'=AccountCode
,'OriginatingNumber'=FromE164
,'DestinationNumber'=ToE164
,'StartTime'=DateTime
,'Duration'=wh_util:to_binary(BillingSec)
,'UniqueID'=CallID
,'CallType'=?DTH_CALL_TYPE_OTHER},
WsdlModel = props:get_value(wsdl, Props),
detergent:call(WsdlModel, "SubmitCallRecord", [CallRecord]).
%% CallRecord = #'p:CallRecord'{'CustomerID'=AccountCode
%% ,'OriginatingNumber'=FromE164
%% ,'DestinationNumber'=ToE164
%% ,'StartTime'=DateTime
%% ,'Duration'=wh_util:to_binary(BillingSec)
%% ,'UniqueID'=CallID
%% ,'CallType'=?DTH_CALL_TYPE_OTHER},
%% WsdlModel = props:get_value(wsdl, Props),
%% detergent:call(WsdlModel, "SubmitCallRecord", [CallRecord]).

handle_req(JObj, Props) ->
true = wapi_call:cdr_v(JObj),
Expand Down
64 changes: 30 additions & 34 deletions whistle_apps/apps/dth/src/dth_listener.erl
Expand Up @@ -15,26 +15,25 @@

%% gen_listener callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, handle_event/2
,terminate/2, code_change/3]).
,terminate/2, code_change/3]).

-include("dth.hrl").

-define(RESPONDERS, [
{cdr_handler, [{<<"call_detail">>, <<"cdr">>}]}
,{blacklist_req, [{<<"dth">>, <<"blacklist_req">>}]}
]).
{cdr_handler, [{<<"call_detail">>, <<"cdr">>}]}
,{blacklist_req, [{<<"dth">>, <<"blacklist_req">>}]}
]).
-define(BINDINGS, [
{cdrs, [{callid, <<"*">>}]}
,{dth, []}
]).
{call, [{restrict_to, [cdr]}]}
]).

-define(SERVER, ?MODULE).
-define(BLACKLIST_REFRESH, 60000).

-record(state, {
wsdl_model = undefined :: 'undefined' | term()
,cache = undefined :: 'undefined' | pid()
,dth_cdr_url = <<>> :: binary()
wsdl_model = undefined :: 'undefined' | term()
,cache = undefined :: 'undefined' | pid()
,dth_cdr_url = <<>> :: binary()
}).

%%%===================================================================
Expand All @@ -50,10 +49,10 @@
%%--------------------------------------------------------------------
start_link() ->
gen_listener:start_link(?MODULE, [{responders, ?RESPONDERS}
,{bindings, ?BINDINGS}
], []).
-spec stop/1 :: (Srv) -> ok when
Srv :: atom() | pid().
,{bindings, ?BINDINGS}
], []).

-spec stop/1 :: (atom() | pid()) -> 'ok'.
stop(Srv) ->
gen_listener:stop(Srv).

Expand Down Expand Up @@ -85,18 +84,18 @@ init([]) ->

case filelib:is_regular(WSDLHrlFile) of
true -> ok;
false ->
true = filelib:is_regular(WSDLFile),
ok = detergent:write_hrl(WSDLFile, WSDLHrlFile)
false ->
true = filelib:is_regular(WSDLFile),
ok = detergent:write_hrl(WSDLFile, WSDLHrlFile)
end,

Cache = whereis(dth_cache),
erlang:monitor(process, Cache),

{ok, #state{wsdl_model=detergent:initModel(WSDLFile)
,dth_cdr_url=URL
,cache=Cache
}}.
,dth_cdr_url=URL
,cache=Cache
}}.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -171,7 +170,7 @@ handle_event(_JObj, #state{dth_cdr_url=Url, cache=Cache, wsdl_model=WSDL}) ->
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
-spec terminate/2 :: (term(), #state{}) -> ok.
-spec terminate/2 :: (term(), #state{}) -> 'ok'.
terminate(_Reason, _) ->
?LOG_SYS("dth: ~p termination", [_Reason]).

Expand All @@ -189,25 +188,22 @@ code_change(_OldVsn, State, _Extra) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec refresh_blacklist/2 :: (Cache, WSDL) -> ok when
Cache :: pid(),
WSDL :: #wsdl{}.
-spec refresh_blacklist/2 :: (pid(), #wsdl{}) -> 'ok'.
refresh_blacklist(Cache, WSDL) ->
{ok, _, [Response]} = detergent:call(WSDL, "GetBlockList", []),
BlockListEntries = get_blocklist_entries(Response),
?LOG_SYS("Entries: ~p", [BlockListEntries]),
wh_cache:store_local(Cache, dth_util:blacklist_cache_key(), BlockListEntries).

-spec get_blocklist_entries/1 :: (Response) -> [{binary(),binary()},...] | [] when
Response :: #'p:GetBlockListResponse'{}.
-spec get_blocklist_entries/1 :: (#'p:GetBlockListResponse'{}) -> wh_json:json_object().
get_blocklist_entries(#'p:GetBlockListResponse'{
'GetBlockListResult'=#'p:ArrayOfBlockListEntry'{
'BlockListEntry'=undefined
}}) ->
{struct, []};
'GetBlockListResult'=#'p:ArrayOfBlockListEntry'{
'BlockListEntry'=undefined
}}) ->
wh_json:new();
get_blocklist_entries(#'p:GetBlockListResponse'{
'GetBlockListResult'=#'p:ArrayOfBlockListEntry'{
'BlockListEntry'=Entries
}}) when is_list(Entries) ->
'GetBlockListResult'=#'p:ArrayOfBlockListEntry'{
'BlockListEntry'=Entries
}}) when is_list(Entries) ->
%% do some formatting of the entries to be [{ID, Reason}]
{struct, [{wh_util:to_binary(ID), wh_util:to_binary(Reason)} || #'p:BlockListEntry'{'CustomerID'=ID, 'BlockReason'=Reason} <- Entries]}.
wh_json:from_list([{wh_util:to_binary(ID), wh_util:to_binary(Reason)} || #'p:BlockListEntry'{'CustomerID'=ID, 'BlockReason'=Reason} <- Entries]).

0 comments on commit f1df32b

Please sign in to comment.