Page 1

-module(click_handler). %% th 2011-08-11: converting to a flat module to get the basics to work. %%-behaviour(gen_event). %% -export([start/1, stop/0]). %% -export([init/1, handle_event/2, code_change/3, handle_call/2, handle_info/2, terminate/2]). -include("backend_log.hrl"). -export([process_click/1]). process_click({MessageId, {struct, [_|_] = ClickData}}) -> try ClickDict = dict:from_list(ClickData), Timestamp = dict:fetch(<<"timestamp">>, ClickDict), AdId = dict:fetch(<<"ad_id">>, ClickDict), PriceOrig = dict:fetch(<<"price">>, ClickDict), IP = dict:fetch(<<"ip_number">>, ClickDict), ?log_info("Click ~p received at ~b for ~s from ~s with price of ~b", [MessageId, Timestamp, AdId, IP, PriceOrig]), Q = "select _user, _account, campaign_id from AdGroup where adgroup_id='~s'", Query = lists:flatten(io_lib:format(Q, [AdId])), case backend_sdb:select(Query) of {ok, _, [{_, CampaignData}], _} -> Dict = dict:from_list(CampaignData), CampaignId = dict:fetch(<<"campaign_id">>, Dict), ?log_info("Click arrived for campaign: ~p", [CampaignData]), case campaign:process_action(CampaignId,{click,PriceOrig}) of {ok, PriceCampaign} -> UserData = {dict:fetch(<<"_user">>, Dict), dict:fetch(<<"_account">>, Dict)}, case ad_user:process_action(UserData, {click, PriceCampaign}) of {ok, OverCharged} when is_integer(OverCharged) -> ?log_info("Overcharged happened: ~p ~b", [{UserData,CampaignId, AdId}, OverCharged]), RealPrice = PriceCampaign-OverCharged, send_click(AdId, RealPrice), case OverCharged > 0 of true -> case campaign:process_action(CampaignId, {overcharged, OverCharged}) of ok -> ?log_info("Overcharged processed: ~p ~b", [MessageId, OverCharged]); {error, Error} -> ?log_error("Failed to process overcharge: ~p ~b ~p", [MessageId, OverCharged, Error]) end; false -> no_action


end; {error, Error} -> ?log_error("Failed to charge ~p ~b -> ~p", [MessageId, PriceCampaign, Error]) end; {error, Error} -> ?log_error("Failed to process click: ~p. ~p", [MessageId, Error]) end; Error -> ?log_info("Error processing click for ~s: ~p", [AdId, Error]) end catch E11:E12 -> ?log_error("~p:~p ~p", [E11, E12, ClickData]) end, ok.

%% process_click({MessageId, {struct, [_|_] = ClickData}}) -> %% try %% ClickDict = dict:from_list(ClickData), %% Timestamp = dict:fetch(<<"timestamp">>, ClickDict), %% AdId = dict:fetch(<<"ad_id">>, ClickDict), %% PriceOrig = dict:fetch(<<"price">>, ClickDict), %% IP = dict:fetch(<<"ip_number">>, ClickDict), %% ?log_info("Click ~p received at ~b for ~s from ~s with price of ~b", [MessageId, Timestamp, AdId, IP, PriceOrig]), %% Q = "select _user, _account, campaign_id from AdGroup where adgroup_id='~s'", %% Query = lists:flatten(io_lib:format(Q, [AdId])), %% case backend_sdb:select(Query) of %% {ok, _, [{_, CampaignData}], _} -> %% Dict = dict:from_list(CampaignData), %% CampaignId = dict:fetch(<<"campaign_id">>, Dict), %% ?log_info("Click arrived for campaign: ~p", [CampaignData]), %% MainProc = self(), %% {ok, Super} = supervisor:start_link(fsm_sup, []), %% try %% succeed_or_retry(Super, campaign_price, %% fun() -> %% {ok, CSM} = campaign_fsm:start_link(CampaignId), %% Result1 = gen_fsm:sync_send_event(CSM, {click, PriceOrig}), %% %gen_fsm:send_all_state_event(CSM, stop), %% case Result1 of %% {ok, PriceCampaign} -> MainProc ! {ok, PriceCampaign} %% end %% end %% ), %% receive


%% {ok, PriceCampaign} -> %% succeed_or_retry(Super, account_price, %% fun() -> %% {ok, USM} = user_fsm:start_link({dict:fetch(<<"_user">>, Dict), dict:fetch(<<"_account">>, Dict)}), %% Result2 = gen_fsm:sync_send_event(USM, {click, PriceCampaign}), %% %gen_fsm:send_all_state_event(USM, stop), %% case Result2 of %% {ok, OverCharged} -> MainProc ! {ok, OverCharged} %% end %% end %% ), %% receive %% {ok, OverCharged} when is_integer(OverCharged) -> %% RealPrice = PriceCampaign-OverCharged, %% send_click(AdId, RealPrice), %% case OverCharged > 0 of %% true -> %% succeed_or_retry(Super, overcharged_campaign, %% fun() -> %% case OverCharged > 0 of %% false -> %% MainProc ! ok; %% true -> %% {ok, CSM} = campaign_fsm:start_link(CampaignId), %% Result3 = gen_fsm:sync_send_event(CSM, {overcharged, OverCharged}), %% %gen_fsm:send_all_state_event(CSM, stop), %% case Result3 of %% ok -> MainProc ! ok %% end %% end %% end %% ), %% receive %% ok -> ?log_info("Overcharged processed: ~p ~b", [MessageId, OverCharged]) %% after 30000 -> %% ?log_error("Failed to process overcharge: ~p ~b", [MessageId, OverCharged]) %% end; %% _ -> %% no_action %% end;


%% {error, Error} -> %% ?log_error("Failed to charge ~p ~b -> ~p", [MessageId, PriceCampaign, Error]) %% end %% after 30000 -> %% ?log_error("Failed to process click: ~p", [MessageId]), %% error %% end %% catch %% E1:E2 -> %% ?log_error("~p:~p", [E1, E2]) %% end, %% ?log_info("supervisor:count_children -> ~p", [supervisor:count_children(Super)]), %% exit(Super, kill); %% Error -> %% ?log_info("Error processing click for ~s: ~p", [AdId, Error]) %% end %% catch %% E11:E12 -> %% ?log_error("~p:~p ~p", [E11, E12, ClickData]) %% end, %% ok.

%% succeed_or_retry(Supervisor, Name, Function) -> %% {ok, _} = supervisor:start_child(Supervisor, {Name, {fsm_transaction, start_link, [Function]}, transient, 15000, worker, [fsm_transaction]}). send_click(AdId, Price) -> Stat = util:config(backend_admgr, stat_url), Timeout = util:config(backend_admgr, timeout, 10000), Req = lists:flatten(io_lib:format(Stat, [AdId, Price])), case ibrowse:send_req(Req, [], get, [], [], Timeout) of {error, Reason} -> ?log_error("Failed to send click to statistics: ~s ~p", [Req, Reason]); Response-> ?log_info("Click sent to statistics with ok ~s~n~p", [Req, Response]) end.


Test Document  

Just used for testing

Advertisement
Read more
Read more
Similar to
Popular now
Just for you