~umgeher/utos-usr-local-hcs-exam-ecg

ref: 67b7a6f4e83990d7d5591d5cd8c3f01486a4efa4 utos-usr-local-hcs-exam-ecg/src/utos_usr_local_hcs_exam_ecg_corelead_etu.erl -rw-r--r-- 3.1 KiB
67b7a6f4Umgeher Torgersen qt-ai upgraded to 0.2.0 1 year, 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-module(utos_usr_local_hcs_exam_ecg_corelead_etu).

-include_lib("utos_usr_db_influx/src/utos_usr_db_influx.hrl").

-export([
         process/3,
         uid/1
        ]).

cl_to_etu_data([], Swap) ->
    {ok, Swap};
cl_to_etu_data([[Time, Label | _] | T], Swap) ->
    cl_to_etu_data(T, lists:append(Swap, [[Time, Label]])).

ect_to_influx(_, [], Swap) ->
    {ok, Swap};
ect_to_influx(UID, [#{<<"timestamp">> := TS, <<"label">> := L, <<"count">> := C, <<"max-bpm">> := MX, <<"min-bpm">> := MN, <<"average-bpm">> := AV} | T], Swap) ->
    LP = #lp{
            f = [{"count", C}, {"maxbpm", MX}, {"minbpm", MN}, {"avbpm", AV}],
            m = UID,
            t = [{"label", L}],
            ts = erlang:integer_to_list(TS)
           },
    ect_to_influx(UID, T, lists:append(Swap, [LP])).

pause_to_influx(_, [], Swap) ->
    {ok, Swap};
pause_to_influx(UID, [#{<<"timestamp">> := TS, <<"duration">> := V} | T], Swap) ->
    LP = #lp{
            f = [{"value", V}],
            m = UID,
            ts = erlang:integer_to_list(TS)
           },
    pause_to_influx(UID, T, lists:append(Swap, [LP])).

process(Exam, {T0, T1}, Influx) ->
    case catch utos_usr_local_hcs_exam_ecg:corelead_get(Exam, {T0, T1}) of
        {ok, CLData} ->
            {ok, Data} = cl_to_etu_data(CLData, []),
            {ok, MinMax} = utos_usr_local_hcs_exam_ecg:settings_ecg_pause(),
            {ok, service} = service_call(Exam, MinMax, Data, Influx);
        {'EXIT', {{badmatch, {ok, {200, #{<<"results">> := [#{<<"statement_id">> := 0}]}}}}, _}} ->
            {error, nodata};
        {error, generic} ->
            {error, nodata}
    end.

rr_to_influx(_, [], Swap) ->
    {ok, Swap};
rr_to_influx(UID, [[TS, Label, Interval] | T], Swap) ->
    LP = #lp{
            f = [{"value", Interval}],
            m = UID,
            t = [{"label", Label}],
            ts = erlang:integer_to_list(TS)
           },
    rr_to_influx(UID, T, lists:append(Swap, [LP])).

service_call(Exam, MinMax, Data, Influx) ->
    case catch utos_usr_local_hcs_exam_w_qt_etu:count_ectopies(MinMax, Data) of
        {ok, {RR, ECT, P}} ->
            {ok, {URR, UECT, UP}} = uid(Exam),
            {ok, LP0} = rr_to_influx(erlang:binary_to_list(URR), RR, []),
            {ok, LP1} = ect_to_influx(erlang:binary_to_list(UECT), ECT, []),
            {ok, LP2} = pause_to_influx(erlang:binary_to_list(UP), P, []),
            {ok, Payload} = utos_usr_db_influx:lp_encode(lists:append(LP0, lists:append(LP1, LP2))),
            {ok, write} = write(Influx, Payload),
            {ok, service};
        {error, Error} ->
            {error, Error}
    end.

uid(Exam) ->
    {ok, RR} = utos_crypto:sha256({hex, ["HCS-EXAM-ECG-CORELEAD-ETU-RR:", Exam]}),
    {ok, ECT} = utos_crypto:sha256({hex, ["HCS-EXAM-ECG-CORELEAD-ETU-ECTOPIES:", Exam]}),
    {ok, P} = utos_crypto:sha256({hex, ["HCS-EXAM-ECG-CORELEAD-ETU-PAUSE:", Exam]}),
    {ok, {RR, ECT, P}}.

write({Influx, DB}, Data) ->
    case catch utos_usr_db_influx:write(Influx, DB, Data) of
        {ok, {204, _}} ->
            {ok, write};
        {error, _} ->
            write({Influx, DB}, Data)
    end.