<commit_before>-module(rpgb_session).

-behavior(gen_server).

-include("log.hrl").
-include_lib("stdlib/include/qlc.hrl").

% gen_server
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
	code_change/3]).
% api
-export([start_link/0, start_link/1, get_or_create/1, get/1, create/0,
	destroy/1, get_id/1, get_user/1, get_value/2, get_value/3]).

%% =================================================================
%% Api
%% =================================================================

start_link() -> start_link([]).

start_link(Opts) ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, Opts, []).

get_or_create(Id) ->
	case ?MODULE:get(Id) of
		{error, notfound} -> create();
		E -> E
	end.

get(Id) ->
	QH = qlc:q([X || {SessId, _, _, _} = X <- ets:table(?MODULE),
		SessId =:= Id]),
	case qlc:e(QH) of
		[] -> {error, notfound};
		[Session] ->
			ets:update_element(?MODULE, Id, {4, calendar:local_time()}),
			{ok, Session}
	end.

create() ->
	Uuid = make_uuid(),
	Session = {Uuid, undefined, dict:new(), calendar:local_time()},
	case ets:insert_new(?MODULE, Session) of
		false ->
			create();
		true ->
			{ok, Session}
	end.

destroy(Id) ->
	ets:delete(?MODULE, Id).

get_id({Id, _, _, _}) -> Id.

get_user({_, User, _, _}) -> User.

get_value(Key, {_, _, Dict, _}) ->
	dict:find(Key, Dict).

get_value(Key, {_, _, Dict, _}, Default) ->
	case dict:find(Key, Dict) of
		error -> {ok, Default};
		E -> E
	end.

%% =================================================================
%% Init
%% =================================================================

init(_Opts) ->
	Ets = ets:new(?MODULE, [named_table, public]),
	Self = self(),
	Timer = erlang:send_after(1000 * 60 * 60, Self, clear_dead_sessions),<commit_msg>Added a set_user for the session.<commit_after>-module(rpgb_session).

-behavior(gen_server).

-include("log.hrl").
-include_lib("stdlib/include/qlc.hrl").

% gen_server
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
	code_change/3]).
% api
-export([start_link/0, start_link/1, get_or_create/1, get/1, create/0,
	destroy/1, get_id/1, get_user/1, get_value/2, get_value/3, set_user/2]).

%% =================================================================
%% Api
%% =================================================================

start_link() -> start_link([]).

start_link(Opts) ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, Opts, []).

get_or_create(Id) ->
	case ?MODULE:get(Id) of
		{error, notfound} -> create();
		E -> E
	end.

get(Id) ->
	QH = qlc:q([X || {SessId, _, _, _} = X <- ets:table(?MODULE),
		SessId =:= Id]),
	case qlc:e(QH) of
		[] -> {error, notfound};
		[Session] ->
			ets:update_element(?MODULE, Id, {4, calendar:local_time()}),
			{ok, Session}
	end.

create() ->
	Uuid = make_uuid(),
	Session = {Uuid, undefined, dict:new(), calendar:local_time()},
	case ets:insert_new(?MODULE, Session) of
		false ->
			create();
		true ->
			{ok, Session}
	end.

destroy(Id) ->
	ets:delete(?MODULE, Id).

get_id({Id, _, _, _}) -> Id.

get_user({_, User, _, _}) -> User.

set_user(User, {Id, undefined, Values, _}) ->
	Session = {Id, User, Values, calendar:local_time()},
	ets:insert(?MODULE, Session),
	{ok, Session};

set_user(_, _) ->
	{error, user_already_set}.

get_value(Key, {_, _, Dict, _}) ->
	dict:find(Key, Dict).

get_value(Key, {_, _, Dict, _}, Default) ->
	case dict:find(Key, Dict) of
		error -> {ok, Default};
		E -> E
	end.

%% =================================================================
%% Init
%% =================================================================

init(_Opts) ->
	Ets = ets:new(?MODULE, [named_table, public]),
	Self = self(),
	Timer = erlang:send_after(1000 * 60 * 60, Self, clear_dead_sessions),
{'fraction_non_alphanumeric': 0.3227357734437617, 'fraction_numerical': 0.013625434143734972, 'mean_word_length': 4.804651162790698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '2931330', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1361, 'n_words': 257}