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

ref: 67b7a6f4e83990d7d5591d5cd8c3f01486a4efa4 utos-usr-local-hcs-exam-ecg/src/utos_usr_local_hcs_exam_ecg.erl -rw-r--r-- 4.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-module(utos_usr_local_hcs_exam_ecg).

-include_lib("utos_usr_local_hcs_exam_include/src/utos_usr_local_hcs_exam_include.hrl").
-include_lib("utos_usr_local_tsk_include/src/utos_usr_local_tsk_include.hrl").

-export([
         corelead_drop/1,
         corelead_etu_uid/1,
         corelead_process/1,
         corelead_uid/1,
         flux_context/1,
         flux_context/2,
         influx/0,
         notify_bus/1,
         raw_set/3,
         raw_ts_first/1,
         raw_ts_last/1,
         raw_ts_period/1,
         settings_ecg_pause/0,
         settings_ecg_pause_min/0,
         settings_ecg_pause_max/0,
         settings_influx_db/0,
         uid/1,
         wb_create/1,
         wb_create/2,
         wb_delete/2,
         wb_delete/3,
         wb_drop/1,
         wb_drop/2,
         wb_uid/1,
         wb_uid/2
        ]).

corelead_drop(Exam) when is_binary(Exam) ->
    {ok, UID} = uid(Exam),
    {ok, DB} = settings_influx_db(),
    {ok, Pid} = influx(),
    _ = utos_usr_db_influx:drop_measurement(Pid, DB, erlang:binary_to_list(UID) ++ "_golden_l0"),
    {ok, drop}.

corelead_etu_uid(Exam) ->
    utos_usr_local_hcs_exam_ecg_corelead_etu:uid(Exam).

corelead_process(<<Exam/binary>>) ->
    {ok, #{<<"golden">> := G, <<"corelead">> := C}} = flux_context(Exam),
    utos_usr_local_hcs_exam_ecg_corelead_generator:run(G, C).

corelead_uid(<<Exam/binary>>) ->
    utos_crypto:sha256({hex, ["HCS-EXAM-ECG-CORELEAD:", Exam]}).

flux_context(<<Exam/binary>>) ->
    {ok, ECG} = uid(Exam),
    {ok, Corelead} = corelead_uid(Exam),
    {ok, {RR, ECT, P}} = corelead_etu_uid(Exam),
    {ok, #{
           <<"bucket">> => <<"ecg">>,
           <<"clusters">> => <<ECG/binary, "_clusters">>,
           <<"corelead">> => Corelead,
           <<"golden">> => <<ECG/binary, "_golden">>,
           <<"ecg">> => ECG,
           <<"etu_ect">> => ECT,
           <<"etu_p">> => P,
           <<"etu_rr">> => RR
          }}.

flux_context(<<Exam/binary>>, Append) ->
    {ok, C} = flux_context(Exam),
    {ok, maps:merge(Append, C)}.

influx() ->
    utos_usr_local_hcs_exam_ecg_influx:connection().

notify_bus(Msg) ->
    utos_usr_local_bus:notify({hcs, {exam, ecg}, Msg}).

raw_set(Exam, TS, Data) ->
    utos_usr_local_hcs_exam_ecg_raw:set(Exam, TS, Data).

raw_ts_first(<<Exam/binary>>) ->
    utos_usr_local_hcs_exam_ecg_raw:ts_first(Exam).

raw_ts_last(<<Exam/binary>>) ->
    utos_usr_local_hcs_exam_ecg_raw:ts_last(Exam).

raw_ts_period(<<Exam/binary>>) ->
    {_, First} = raw_ts_first(Exam),
    {_, Last} = raw_ts_last(Exam),
    {ok, {First, Last}}.

settings_ecg_pause() ->
    {ok, Min} = settings_ecg_pause_min(),
    {ok, Max} = settings_ecg_pause_max(),
    {ok, {Min, Max}}.

settings_ecg_pause_min() ->
    {ok, 1000000000}.

settings_ecg_pause_max() ->
    {ok, 2000000000}.

settings_influx_db() ->
    utos_usr_local_hcs_exam_ecg_settings:influx_db().

uid(Exam) ->
    utos_crypto:sha256({hex, ["HCS-EXAM-ECG:", Exam]}).

wb_create(WB) when is_record(WB, wb) ->
    {ok, DB} = settings_influx_db(),
    {ok, Pid} = influx(),
    {ok, TaskID} = utos_usr_local_tsk:new(#tsk{tag = {wb, create}}),
    spawn(utos_usr_local_hcs_exam_ecg_wb_create, create, [WB, {Pid, DB}, TaskID]),
    {ok, TaskID}.

wb_create(Exam, Phy) ->
    wb_create(#wb{exam = Exam, phy = Phy}).

wb_delete(WB, TS) when is_record(WB, wb) ->
    {ok, DB} = settings_influx_db(),
    {ok, Pid} = influx(),
    {ok, TaskID} = utos_usr_local_tsk:new(#tsk{tag = {wb, delete}}),
    spawn(utos_usr_local_hcs_exam_ecg_wb_delete, delete, [WB, TS, {Pid, DB}]),
    {ok, TaskID}.

wb_delete(Exam, Phy, TS) ->
    wb_delete(#wb{exam = Exam, phy = Phy}, TS).

wb_drop(WB) when is_record(WB, wb) ->
    {ok, DB} = settings_influx_db(),
    {ok, Pid} = influx(),
    {ok, TaskID} = utos_usr_local_tsk:new(#tsk{tag = {wb, drop}}),
    spawn(utos_usr_local_hcs_exam_ecg_wb_drop, drop, [WB, {Pid, DB}]),
    {ok, TaskID}.

wb_drop(Exam, Phy) when is_binary(Exam), is_binary(Phy) ->
    wb_drop(#wb{exam = Exam, phy = Phy}).

wb_uid(#wb{exam = Exam, ecg = null, uid = null} = WB) when is_binary(Exam) ->
    {ok, ECG} = uid(Exam),
    wb_uid(WB#wb{ecg = ECG});
wb_uid(#wb{ecg = ECG, phy = Phy, uid = null}) when is_binary(ECG), is_binary(Phy) ->
    utos_crypto:sha256({hex, ["HCS-EXAM-ECG-WB:", ECG, "|", Phy]}).

wb_uid(ECG, Phy) when is_binary(ECG), is_binary(Phy) ->
    wb_uid(#wb{ecg = ECG, phy = Phy}).