~glhrmfrts/utos-usr-local-hcs-exam-ecg-storage-pgsql

66b8513e8de449d3f47d9ba110ffe147868ee11d — Guilherme Nemeth 8 months ago f4df780
start writing read funcs
M src/utos_usr_local_hcs_exam_ecg_storage_pgsql.erl => src/utos_usr_local_hcs_exam_ecg_storage_pgsql.erl +8 -0
@@ 7,6 7,8 @@
         settings_utfsc_host/0,
         settings_utfsc_port/0,
         si_set/4,
         read_first_and_last/1,
         read_raw_with_sample/3,
         table_name_ecg/1,
         utfsc/0
        ]).


@@ 29,6 31,12 @@ settings_utfsc_port() ->
si_set(UID, Key, Index, Value) ->
    utos_usr_local_hcs_exam_ecg_storage_pgsql_db_set:si(UID, Key, Index, Value).

read_first_and_last(<<UID/binary>>) ->
    utos_usr_local_hcs_exam_ecg_storage_pgsql_read:first_and_last(UID).

read_raw_with_sample(<<UID/binary>>, Start, End) ->
    utos_usr_local_hcs_exam_ecg_storage_pgsql_read:raw_with_sample(UID, Start, End).

table_name_ecg(<<UID/binary>>) ->
    utos_crypto:sha256({hex, ["exam-ecg:", UID]}).


A src/utos_usr_local_hcs_exam_ecg_storage_pgsql_read.erl => src/utos_usr_local_hcs_exam_ecg_storage_pgsql_read.erl +31 -0
@@ 0,0 1,31 @@
-module(utos_usr_local_hcs_exam_ecg_storage_pgsql_read).

-export([
         first_and_last/3,
         raw_with_sample/3,
        ]).

-define(QUERY_ECG_FIRST_AND_LAST, "SELECT MIN(time), MAX(time) FROM \"${measurement}\"").
-define(QUERY_ECG_RAW_WITH_SAMPLE, "SELECT * FROM \"${measurement}\" WHERE time >= $1 AND time < $2 ORDER BY time ASC").

first_and_last(<<UID/binary>>) ->
    {ok, Table} = utos_usr_local_hcs_exam_ecg_storage_pgsql:table_name_ecg(UID),
    Q = string:replace(?QUERY_ECG_FIRST_AND_LAST, "${measurement}", Table),
    case catch utos_usr_db_postgresql:equery(utos_usr_local_hcs_exam_ecg_storage_pgsql_pool, Q, []) of
        {ok, R} ->
            io:format("~p", [R]),
            {reply, {ok, R}, S};
        {error, E} ->
            {reply, {error, E}, S}
    end.

raw_with_sample(<<UID/binary>>, Start, End) when is_integer(Start), is_integer(End) ->
    {ok, Table} = utos_usr_local_hcs_exam_ecg_storage_pgsql:table_name_ecg(UID),
    Q = string:replace(?QUERY_ECG_RAW_WITH_SAMPLE, "${measurement}", Table),
    case catch utos_usr_db_postgresql:equery(utos_usr_local_hcs_exam_ecg_storage_pgsql_pool, Q, [Start, End]) of
        {ok, R} ->
            io:format("~p", [R]),
            {reply, {ok, R}, S};
        {error, E} ->
            {reply, {error, E}, S}
    end.

M test/main_test.erl => test/main_test.erl +4 -0
@@ 10,3 10,7 @@ settings_utfsc_host_test() ->

settings_utfsc_port_test() ->
    ?assertNotEqual({ok, none}, utos_usr_local_hcs_exam_ecg_storage_pgsql:settings_utfsc_port()).

read_first_and_last_test() ->
    io:format("~p~n", [utos_usr_local_hcs_exam_ecg_storage_pgsql:read_first_and_last(<<"TEST_EXAM">>)]),
    ok.
\ No newline at end of file