Skip to content

Commit

Permalink
Merging changed from KAZOO-213
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbw committed Aug 8, 2013
2 parents 5152c61 + 0efb78c commit 073f35c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion applications/cdr/src/cdr_v3_migrate_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ get_next_n_months(Year, Month, NumMonths) when Month =< 12, Month > 0 ->
get_next_n_months(Year, Month, NumMonths, 'ASC').

-spec get_next_n_months(wh_year(), wh_month(), pos_integer(), atom()) -> wh_proplist().
get_next_n_months(Year, Month, NumMonths, 'ASC') when Month =< 12, Month > 0 ->
get_next_n_months(Year, Month, NumMonths, 'ASC') when Month =< 11, Month > 0 ->
next_n_months(Year, Month, NumMonths, []);
get_next_n_months(Year, Month, NumMonths, 'DESC') when Month =< 12, Month > 0 ->
lists:reverse(next_n_months(Year, Month, NumMonths, [])).
Expand Down
74 changes: 74 additions & 0 deletions applications/cdr/src/csv_util.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%%%-------------------------------------------------------------------
%%% @copyright (c) 2010-2013, 2600Hz
%%% @doc
%%% Utility module for V3 Kazoo Migration
%%% @end
%%% @contributors
%%% Ben Wann
%%% James Aimonetti
%%% Karl Anderson
%%%-------------------------------------------------------------------
-module(csv_util).

-include("cdr.hrl").

%% API
-export([json_objs_to_csv/1,
json_objs_to_csv/2
,test_convert/1
]).

-define(INCLUDE_HEADERS, 'true').

%%%===================================================================
%%% API
%%%===================================================================
%% TODO change name...
-spec json_objs_to_csv(wh_json:objects()) -> iolist().
json_objs_to_csv(JObjs) ->
json_objs_to_csv(JObjs, ?INCLUDE_HEADERS).

-spec json_objs_to_csv(wh_json:objects(), boolean()) -> iolist().
json_objs_to_csv([], _) -> [];
json_objs_to_csv(JObjs, _) ->
wh_json:encode(JObjs).

test_convert(AccountDb) ->
ViewOptions = ['include_docs'],
case couch_mgr:get_results(AccountDb, <<"cdrs/crossbar_listing">>, ViewOptions) of
{'ok', []} -> 'ok';
{'error', _E} ->
lager:error("failed view ~s: ~p", [AccountDb, _E]), [];
{'ok', JObjs} ->
CdrDocs = lists:foldr(fun(JObj, Acc) ->
Doc = wh_json:get_value([<<"doc">>], JObj),
CdrDoc = wh_json:delete_key(<<"custom_channel_vars">>, Doc),
[CdrDoc | Acc]
end, [], JObjs),
CsvData = json_objs_to_csv(CdrDocs),
maybe_save_csv(<<"test.csv">>, CsvData)
end.

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------

%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec maybe_save_csv(ne_binary(), iolist()) -> 'ok'.
maybe_save_csv(FileName, CsvData) ->
TestPath = list_to_binary([code:priv_dir('cdr')
,"/test_data/"
]),
case filelib:ensure_dir(TestPath) of
'ok' ->
FilePath = list_to_binary([TestPath, FileName]),
case file:write_file(FilePath, CsvData) of
{'error', _E} -> lager:error("Error writing file: ~p", [_E]);
'ok' -> 'ok'
end;
{'error', _E} -> lager:error("Error creating directory: ~p", [_E])
end.

0 comments on commit 073f35c

Please sign in to comment.