Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Merge a9b6f4e into e92cd3e
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Feb 10, 2018
2 parents e92cd3e + a9b6f4e commit 607dad6
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 145 deletions.
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: false
dist: trusty
language: erlang
sudo: false
otp_release:
- 20.2
services:
Expand All @@ -11,11 +11,19 @@ install:
- make xref
before_script:
- epmd -daemon
- sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
script:
- make test
- make bench
- make test-multiple-releases
- rebar3 as test coveralls send
# - make bench
after_failure:
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/ct.latest.log
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/*/log/console.log
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/*/log/crash.log
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/*/log/error.log
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/*/log/error.log
- cat /home/travis/build/goncalotomas/FMKe/_build/test/logs/*/lib.fmke.logs/suite.log
cache:
directories:
- "$HOME/.cache/rebar3/hex/default"
35 changes: 32 additions & 3 deletions src/fmke.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get_staff_by_id/1,
get_staff_prescriptions/1,
get_staff_treatments/1,
get_status/0,
process_prescription/2,
update_patient_details/3,
update_pharmacy_details/3,
Expand Down Expand Up @@ -60,9 +61,6 @@ init([]) ->
Driver:start([]),
{ok, Driver}.

get_driver_setup(Database) when is_list(Database) ->
get_driver_setup(list_to_atom(Database));

get_driver_setup(Database) when is_atom(Database) ->
%% TODO maybe I should find a better way to do this... later.
DriverSetups = #{
Expand All @@ -79,6 +77,30 @@ get_driver_setup(Database) when is_atom(Database) ->
error -> {error, not_supported, Database}
end.

get_status() ->
Status = is_alive(fmke),
ConnManagerStatus = is_alive(fmke_db_conn_manager),
WebServerStatus = is_alive(cowboy_sup),
{ok, Pools} = application:get_env(?APP, pools),
PoolStatuses = lists:map(
fun(Pool) ->
PoolUp = is_alive(Pool),
{PoolStatus, CurrPoolSize, CurrOverflow, _Monitors} = gen_server:call(Pool, status),
[
{pool_is_up, PoolUp}, {pool_status, PoolStatus},
{worker_pool_size, CurrPoolSize}, {current_overflow, CurrOverflow}
]
end, Pools),
PoolDetails = lists:zip(Pools, PoolStatuses),
{ok, TargetDatabase} = application:get_env(?APP, target_database),
{ok, HttpPort} = application:get_env(?APP, http_port),
{ok, ConnPoolSize} = application:get_env(?APP, connection_pool_size),
{ok, Addresses} = application:get_env(?APP, database_addresses),
{ok, Ports} = application:get_env(?APP, database_ports),
[{fmke_up, Status}, {connection_manager_up, ConnManagerStatus}, {web_server_up, WebServerStatus},
{target_database, TargetDatabase}, {connection_pool_size, ConnPoolSize}, {http_port, HttpPort},
{database_addresses, lists:map(fun list_to_binary/1, Addresses)}, {database_ports, Ports}, {pools, PoolDetails}].

handle_cast(_Msg, State) ->
{noreply, State}.

Expand Down Expand Up @@ -259,3 +281,10 @@ update_prescription_medication(Id, Operation, Drugs) ->

process_prescription(Id, Date) ->
gen_server:call(?MODULE, {process_prescription, Id, Date}).

%%-----------------------------------------------------------------------------
%% Helper functions
%%-----------------------------------------------------------------------------

is_alive(Proc) ->
undefined =/= whereis(Proc).
23 changes: 16 additions & 7 deletions src/fmke_db_conn_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-export([start_link/0]).

%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3]).
-export([init/1, handle_cast/2, handle_info/2, handle_call/3]).

%% conn_manager API
-export([checkout/0, checkin/1]).
Expand Down Expand Up @@ -40,17 +40,26 @@ handle_call(checkout, _From, State) ->
queue = Queue} = State,
Pool = queue:head(Queue),
Pid = poolboy:checkout(Pool),
true = link(Pid),
MapState = maps:put(Pid, Pool, PidOwners),
QueueState = queue:snoc(queue:tail(Queue), Pool),
{reply, Pid, #state{queue = QueueState, pid_owners = MapState}};

handle_call({checkin, Pid}, _From, State) ->
#state{pid_owners = PidOwners,
queue = Queue} = State,
Owner = maps:get(Pid, PidOwners, queue:head(Queue)),
MapState = maps:remove(Pid, PidOwners),
Result = poolboy:checkin(Owner, Pid),
{reply, Result, State#state{pid_owners = MapState}};
#state{pid_owners = PidOwners} = State,
case maps:get(Pid, PidOwners, no_such_pid) of
no_such_pid ->
{reply, no_such_pid, State};
Owner ->
MapState = maps:remove(Pid, PidOwners),
Result = poolboy:checkin(Owner, Pid),
{reply, Result, State#state{pid_owners = MapState}}
end;

handle_call(_Msg, _From, State) ->
{noreply, State}.

handle_info({'EXIT', Pid, _Reason}, State) ->
#state{pid_owners = PidOwners} = State,
MapState = maps:remove(Pid, PidOwners),
{noreply, State#state{pid_owners = MapState}}.
42 changes: 22 additions & 20 deletions src/fmke_http_handler_app.erl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
-module (fmke_http_handler_app).
-include ("fmk_http.hrl").
-include ("fmke.hrl").

-export([init/2]).
-behaviour(fmke_gen_http_handler).

init(Req0, Opts) ->
-export([init/2, handle_req/3, perform_operation/4]).

init(Req, Opts) ->
fmke_gen_http_handler:init(?MODULE, Req, Opts).

handle_req(<<"GET">>, _, Req) ->
fmke_gen_http_handler:handle_req(?MODULE, <<"GET">>, Req, [], []);

handle_req(<<"POST">>, _, Req) ->
fmke_gen_http_handler:handle_req(?MODULE, <<"GET">>, Req, [], []);

handle_req(<<"PUT">>, _, Req) ->
fmke_gen_http_handler:handle_req(?MODULE, <<"GET">>, Req, [], []).

perform_operation(<<"GET">>, Req, [], []) ->
try
Method = cowboy_req:method(Req0),
HasBody = cowboy_req:has_body(Req0),
Req = handle_req(Method, HasBody, Req0),
{ok, Req, Opts}
catch
Err:Reason ->
ErrorMessage = io_lib:format("Error ~p:~n~p~n~p~n", [Err, Reason, erlang:get_stacktrace()]),
lager:error(ErrorMessage),
Req2 = cowboy_req:reply(500, #{}, ErrorMessage, Req0),
{ok, Req2, Opts}
StatusPropList = fmke:get_status(),
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, true, proplists:delete(http_port, StatusPropList) )
catch error:ErrReason ->
lager:debug("Error getting status:~n~p~n", [erlang:get_stacktrace()]),
fmke_gen_http_handler:handle_reply(?MODULE, Req, {error, bad_req}, false, ErrReason)
end.

handle_req(<<"GET">>, true, Req) ->
cowboy_req:reply(400, #{}, ?ERR_BODY_IN_A_GET_REQUEST, Req);
handle_req(<<"GET">>, false, Req) ->
JsonReply = "FMKe is running!",
cowboy_req:reply(200, #{
<<"content-type">> => <<"application/json">>
}, JsonReply, Req).
6 changes: 3 additions & 3 deletions src/fmke_http_handler_events.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
% IntegerStaffId = binary_to_integer(StaffMemberId),
% StringTimestamp = binary_to_list(Timestamp),
% StringDescription = binary_to_list(Description),
% Resp = fmke:create_event(IntegerId, IntegerTreatmentId,
% IntegerStaffId, StringTimestamp, StringDescription),
% Resp = fmke:create_event(IntegerId, IntegerTreatmentId, IntegerStaffId,
% StringTimestamp, StringDescription),
% Success = Resp =:= ok,
% JsonReply = lists:flatten(io_lib:format(
% "{\"success\": \"~p\", \"result\": \"~p\"}",
Expand All @@ -68,7 +68,7 @@
% JsonReply = case Success of
% true ->
% lists:flatten(io_lib:format(
% ("{\"success\": \"~p\", \"result\": " ++ fmke_proplists:encode(event, ServerResponse) ++ "}"),
% ("{\"success\": \"~p\", \"result\": " ++ fmke_json:encode(event, ServerResponse) ++ "}"),
% [Success]
% ));
% false ->
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_http_handler_facilities.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ perform_operation(<<"GET">>, Req, [{id, BinaryId}], []) ->
Id = fmke_http_utils:parse_id(BinaryId),
{Success, ServerResponse} = case fmke:get_facility_by_id(Id) of
{error, Reason} -> {false, Reason};
FacilityRecord -> {true, fmke_proplists:encode_object(FacilityRecord)}
FacilityRecord -> {true, fmke_json:encode(FacilityRecord)}
end,
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, Success, ServerResponse)
catch error:ErrReason ->
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_http_handler_patients.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ perform_operation(<<"GET">>, Req, [{id, BinaryId}], []) ->
Id = fmke_http_utils:parse_id(BinaryId),
{Success, ServerResponse} = case fmke:get_patient_by_id(Id) of
{error, Reason} -> {false, Reason};
PatientRecord -> {true, fmke_proplists:encode_object(PatientRecord)}
PatientRecord -> {true, fmke_json:encode(PatientRecord)}
end,
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, Success, ServerResponse)
catch error:ErrReason ->
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_http_handler_pharmacies.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ perform_operation(<<"GET">>, Req, [{id, BinaryId}], []) ->
Id = fmke_http_utils:parse_id(BinaryId),
{Success, ServerResponse} = case fmke:get_pharmacy_by_id(Id) of
{error, Reason} -> {false, Reason};
PharmacyRecord -> {true, fmke_proplists:encode_object(PharmacyRecord)}
PharmacyRecord -> {true, fmke_json:encode(PharmacyRecord)}
end,
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, Success, ServerResponse)
catch error:ErrReason ->
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_http_handler_prescriptions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ perform_operation(<<"GET">>, Req, [{id, BinaryId}], []) ->
Id = fmke_http_utils:parse_id(BinaryId),
{Success, ServerResponse} = case fmke:get_prescription_by_id(Id) of
{error, Reason} -> {false, Reason};
PrescriptionRecord -> {true, fmke_proplists:encode_object(PrescriptionRecord)}
PrescriptionRecord -> {true, fmke_json:encode(PrescriptionRecord)}
end,
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, Success, ServerResponse)
catch error:ErrReason ->
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_http_handler_staff.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ perform_operation(<<"GET">>, Req, [{id, BinaryId}], []) ->
Id = fmke_http_utils:parse_id(BinaryId),
{Success, ServerResponse} = case fmke:get_staff_by_id(Id) of
{error, Reason} -> {false, Reason};
StaffRecord -> {true, fmke_proplists:encode_object(StaffRecord)}
StaffRecord -> {true, fmke_json:encode(StaffRecord)}
end,
fmke_gen_http_handler:handle_reply(?MODULE, Req, ok, Success, ServerResponse)
catch error:ErrReason ->
Expand Down
3 changes: 1 addition & 2 deletions src/fmke_http_handler_treatments.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
% JsonReply = case Success of
% true ->
% lists:flatten(io_lib:format(
% "{\"success\": \"~p\", \"result\": " ++
% fmke_proplists:encode(treatment, ServerResponse) ++ "}",
% "{\"success\": \"~p\", \"result\": " ++ fmke_json:encode(treatment, ServerResponse) ++ "}",
% [Success]
% ));
% false ->
Expand Down
Loading

0 comments on commit 607dad6

Please sign in to comment.