<commit_before>%%% -------------------------------------------------------------------
%%% Author		: <NAME>
%%% Description	: 
%%% Version		: 
%%% Created		: 30.09.2011
%%% -------------------------------------------------------------------

-module(sub_info).

-behaviour(gen_server).

%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

-include("../include/sub_info_records.hrl"). 

-define(NODE_DISCOVERY_DELAY, 1000).

%% --------------------------------------------------------------------
%% External exports

-export([start_link/0
		, get_subscriber/1
		, get_payment_info/1
		, cluster/1
		, get_bulk_sleep_time/0
		, get_sync_count/0
		]).

%% gen_server callbacks

-export([init/1
		, handle_call/3
		, handle_cast/2
		, handle_info/2
		, terminate/2
		, code_change/3
		, delete_subscriber/3
		, write_subscriber/4
		, set_sub_counter/5
		, update_sub_counters/4
		, read_sub_counters/4
		]).

-record(state, {session = 0}).

%% ====================================================================
%% External functions
%% ====================================================================

cluster(Node) when is_atom(Node) ->
	cluster([node(), Node]);
cluster(NodeList) when is_list(NodeList) ->
	mnesia:change_config(extra_db_nodes, [node() | NodeList]).
  
start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

get_bulk_sleep_time() ->
	gen_server:call(?MODULE, bulk_sleep_time).
  
get_sync_count() ->
	mnesia:dirty_read(syncinfo, 'change_count').

get_datetime_stamp() ->
    {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
    lists:flatten(io_lib:format("~4.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B",
        [Year, Month, Day, Hour, Min, Sec])).

delete_subscriber(Msisdn, SyncDate, ChangeCount) ->
	Record = mnesia:dirty_read(subscriber, Msisdn),
	case Record of
		[] ->
			{0, 0, 0};
		_ ->
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, -1)) of
				{atomic, _} -> {0, 0, 1};
				{aborted, Reason} -> {error, Reason}
			end
	end.

write_subscriber(Msisdn, Record, SyncDate, ChangeCount) ->
	DbRecord = mnesia:dirty_read(subscriber, Msisdn),
	case DbRecord of
		[] ->
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, 1)) of
				{atomic, _} -> {1, 0, 0};
				{aborted, Reason} -> {error, Reason}
			end;
		[Record] ->
			{0, 0, 0};
		_ ->			
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, 0)) of
				{atomic, _} -> {0, 1, 0};
				{aborted, Reason} -> {error, Reason}
			end
	end.

read_sub_counters(PartitionKey, ShardKey, BaseId, KeysToRead) ->
	case mnesia:sync_transaction(read_sub_counters_fun(PartitionKey, ShardKey, BaseId, KeysToRead)) of
		{atomic, Result} -> Result;
		{aborted, Reason} -> {error, Reason}
	end.

read_sub_counters_fun([], [], BaseId, KeysToRead) -> 
	read_sub_counters_fun(sub_counter, BaseId, KeysToRead);<commit_msg>record 'subscriber' replaced with tuple for the interface<commit_after>%%% -------------------------------------------------------------------
%%% Author		: <NAME>
%%% Description	: 
%%% Version		: 
%%% Created		: 30.09.2011
%%% -------------------------------------------------------------------

-module(sub_info).

-behaviour(gen_server).

%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

-include("../include/sub_info_records.hrl"). 

-define(NODE_DISCOVERY_DELAY, 1000).

%% --------------------------------------------------------------------
%% External exports

-export([start_link/0
		, get_subscriber/1
		, get_payment_info/1
		, cluster/1
		, get_bulk_sleep_time/0
		, get_sync_count/0
		]).

%% gen_server callbacks

-export([init/1
		, handle_call/3
		, handle_cast/2
		, handle_info/2
		, terminate/2
		, code_change/3
		, delete_subscriber/3
		, write_subscriber/4
		, set_sub_counter/5
		, update_sub_counters/4
		, read_sub_counters/4
		]).

-record(state, {session = 0}).

%% ====================================================================
%% External functions
%% ====================================================================

cluster(Node) when is_atom(Node) ->
	cluster([node(), Node]);
cluster(NodeList) when is_list(NodeList) ->
	mnesia:change_config(extra_db_nodes, [node() | NodeList]).
  
start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

get_bulk_sleep_time() ->
	gen_server:call(?MODULE, bulk_sleep_time).
  
get_sync_count() ->
	mnesia:dirty_read(syncinfo, 'change_count').

get_datetime_stamp() ->
    {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
    lists:flatten(io_lib:format("~4.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B",
        [Year, Month, Day, Hour, Min, Sec])).

delete_subscriber(Msisdn, SyncDate, ChangeCount) ->
	Record = mnesia:dirty_read(subscriber, Msisdn),
	case Record of
		[] ->
			{0, 0, 0};
		_ ->
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, -1)) of
				{atomic, _} -> {0, 0, 1};
				{aborted, Reason} -> {error, Reason}
			end
	end.

write_subscriber(Msisdn, {IntMsisdn, PaymentType, PaymentMethod, IntStatus, IntChangeCount}, SyncDate, ChangeCount)
    when is_list(PaymentType), is_list(PaymentMethod)->
    Record = #subscriber{msisdn=IntMsisdn
                ,payment_type=trunc(list_to_integer(PaymentType))
                ,payment_method=trunc(list_to_integer(PaymentMethod))
                ,status=IntStatus
                ,change_count=IntChangeCount
            },
	DbRecord = mnesia:dirty_read(subscriber, Msisdn),
	case DbRecord of
		[] ->
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, 1)) of
				{atomic, _} -> {1, 0, 0};
				{aborted, Reason} -> {error, Reason}
			end;
		[Record] ->
			{0, 0, 0};
		_ ->			
			case mnesia:sync_transaction(trans_fun(Record, SyncDate, ChangeCount, 0)) of
				{atomic, _} -> {0, 1, 0};
				{aborted, Reason} -> {error, Reason}
			end
	end.

read_sub_counters(PartitionKey, ShardKey, BaseId, KeysToRead) ->
	case mnesia:sync_transaction(read_sub_counters_fun(PartitionKey, ShardKey, BaseId, KeysToRead)) of
		{atomic, Result} -> Result;
		{aborted, Reason} -> {error, Reason}
	end.

read_sub_counters_fun([], [], BaseId, KeysToRead) -> 
	read_sub_counters_fun(sub_counter, BaseId, KeysToRead);
{'fraction_non_alphanumeric': 0.2872091207246603, 'fraction_numerical': 0.022489458066531312, 'mean_word_length': 2.6386363636363637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4464897', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2145, 'n_words': 355}