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

bdc3001c1cb756ddc66f29df9b5ecc3bd93297b5 — Guilherme Nemeth 8 months ago bb0bda1 develop
use new repos for table name and datetime conversion
5 files changed, 16 insertions(+), 56 deletions(-)

M rebar.config
M src/utos_usr_local_hcs_exam_ecg_storage_pgsql.erl
D src/utos_usr_local_hcs_exam_ecg_storage_pgsql_read.erl
M src/utos_usr_local_hcs_exam_ecg_storage_pgsql_worker.erl
M test/main_test.erl
M rebar.config => rebar.config +2 -1
@@ 6,9 6,10 @@
        {utos_crypto, {git, "https://git.sr.ht/~umgeher/utos-crypto", {tag, "0.2.0"}}},
        {utos_datetime, {git, "https://git.sr.ht/~umgeher/utos-datetime", {tag, "0.3.3"}}},
        {utos_etc, {git, "https://git.sr.ht/~umgeher/utos-etc", {tag, "0.1.0"}}},
        {utos_usr_db_postgresql, {git, "https://git.sr.ht/~umgeher/utos-usr-db-postgresql", {tag, "0.1.1"}}},
        {utos_usr_db_postgresql, {git, "git@gitlab.com:quoretech/utos-usr-db-postgresql.git", {branch, "develop"}}},
        {utos_usr_federation, {git, "https://git.sr.ht/~umgeher/utos-usr-federation", {tag, "0.0.1"}}},
        {utos_usr_local_bus, {git, "https://git.sr.ht/~umgeher/utos-usr-local-bus", {tag, "0.1.1"}}},
        {utos_usr_local_hcs_exam_ecg_pgsql, {git, "git@gitlab.com:quoretech/utos-usr-local-hcs-exam-ecg-pgsql.git", {branch, "master"}}},
        {utos_usr_local_tsk, {git, "https://git.sr.ht/~umgeher/utos-usr-local-tsk", {tag, "0.3.0"}}}
       ]}.


M src/utos_usr_local_hcs_exam_ecg_storage_pgsql.erl => src/utos_usr_local_hcs_exam_ecg_storage_pgsql.erl +0 -12
@@ 7,9 7,6 @@
         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
        ]).



@@ 31,14 28,5 @@ 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]}).

utfsc() ->
    utos_actor:call(utos_usr_local_hcs_exam_ecg_storage_pgsql_utfsc, io).

D src/utos_usr_local_hcs_exam_ecg_storage_pgsql_read.erl => src/utos_usr_local_hcs_exam_ecg_storage_pgsql_read.erl +0 -31
@@ 1,31 0,0 @@
-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 src/utos_usr_local_hcs_exam_ecg_storage_pgsql_worker.erl => src/utos_usr_local_hcs_exam_ecg_storage_pgsql_worker.erl +14 -9
@@ 32,6 32,9 @@
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

flush(UID, Buffer) ->
    write_db(UID, lists:reverse(Buffer)).

handle_call(state, _, S) ->
    {reply, {ok, S}, S};
handle_call(_, _, S) ->


@@ 93,9 96,6 @@ init({Master, UID, BUID}) ->
    utos_actor:cast(self(), init),
    {ok, #state{buid = BUID, master = Master, uid = UID}}.

flush(UID, Buffer) ->
    write_db(UID, lists:reverse(Buffer)).

push(Entry, #state{uid = UID, buffer = Buffer} = S) ->
    case length(Buffer) of
        Size when Size+1 >= ?BUFFER_MAX_SIZE ->


@@ 114,9 114,14 @@ terminate(_Reason, _) ->
write_db(<<UID/binary>>, [{L, V, TS} | Buffer]) ->
    OnlyValue = fun({_, V_, _}) -> V_ end,
    VS = jsx:encode([V] ++ lists:map(OnlyValue, Buffer)),
    {ok, Table} = utos_usr_local_hcs_exam_ecg_storage_pgsql:table_name_ecg(UID),
    Q = <<"INSERT INTO ", Table/binary, " (time, lead, data) VALUES ($1, $2, $3)">>,
    DT = calendar:system_time_to_universal_time(TS, 1000),
    {ok, _} = utos_usr_db_postgresql:equery(utos_usr_local_hcs_exam_ecg_storage_pgsql_pool, Q, [DT, L, VS]),
    io:format("~p: 1 row of ~p values inserted~n", [?MODULE, length(Buffer)+1]),
    ok.
\ No newline at end of file
    {ok, Table} = utos_usr_local_hcs_exam_ecg_pgsql:table_name_ecg(UID),
    Q = <<"INSERT INTO \"", Table/binary, "\" (time, lead, data) VALUES ($1, $2, $3)">>,
    DT = utos_usr_db_postgresql_datetime:millis_to_datetime(TS),
    case catch utos_usr_db_postgresql:equery(utos_usr_local_hcs_exam_ecg_storage_pgsql_pool, Q, [DT, L, VS]) of
        {error, E} ->
            io:format("~p: insert error: ~p~n", [?MODULE, E]),
            ok;
        _ ->
            io:format("~p: 1 row of ~p values inserted~n", [?MODULE, length(Buffer)+1]),
            ok
    end.
\ No newline at end of file

M test/main_test.erl => test/main_test.erl +0 -3
@@ 11,6 11,3 @@ 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