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

ref: 67b7a6f4e83990d7d5591d5cd8c3f01486a4efa4 utos-usr-local-hcs-exam-ecg/src/utos_usr_local_hcs_exam_ecg_wb_create.erl -rw-r--r-- 2.2 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
-module(utos_usr_local_hcs_exam_ecg_wb_create).

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

-export([create/3]).

create(#wb{ecg = null, exam = Exam, uid = null} = WB, Influx, TSK) ->
    {ok, ECG} = utos_usr_local_hcs_exam_ecg:uid(Exam),
    create(WB#wb{ecg = ECG}, Influx, TSK);
create(#wb{ecg = ECG, phy = Phy, uid = null} = WB, Influx, TSK) ->
    {ok, UID} = utos_usr_local_hcs_exam_ecg:wb_uid(ECG, Phy),
    create(WB#wb{uid = UID}, Influx, TSK);
create(WB, Influx, TSK) ->
    {ok, update} = utos_usr_local_tsk:update_obj(TSK, WB#wb.uid),
    {ok, drop} = utos_usr_local_hcs_exam_ecg:wb_drop(WB),
    {ok, update} = utos_usr_local_tsk:update(TSK, <<"drop done">>),
    {ok, seek} = seek(WB, Influx, 0),
    {ok, done} = utos_usr_local_tsk:done(TSK),
    {ok, WB#wb.uid}.

data_to_lp(_, [], Swap) ->
    {ok, Swap};
data_to_lp(#wb{uid = UID} = WB, [[Time, Label, Lead, _, C1, C2, C3] | T], Swap) ->
    LP = #lp{
            f = [{"value", 0}],
            m = erlang:binary_to_list(UID),
            t = [
                 {"cluster_l1", C1},
                 {"cluster_l2", C2},
                 {"cluster_l3", C3},
                 {"label", Label},
                 {"lead_origin", Lead}
                ],
            ts = Time
           },
    data_to_lp(WB, T, lists:append(Swap, [LP])).

seek(#wb{ecg = ECG} = WB, {Influx, DB}, TS) ->
    Query = "SELECT time, label, lead_origin, value, cluster_l1, cluster_l2, cluster_l3 FROM \""
        ++ erlang:binary_to_list(ECG) ++ "_golden_l0\" WHERE time > "
        ++ erlang:integer_to_list(TS) ++ " LIMIT 3000",
    case catch utos_usr_db_influx:query(Influx, DB, Query) of
        {ok, {_, #{<<"results">> := [#{<<"series">> := [#{<<"values">> := R}]}]}}} ->
            {ok, LP} = data_to_lp(WB, R, []),
            {ok, Data} = utos_usr_db_influx:lp_encode(LP),
            {ok, write} = write(Influx, DB, Data),
            [TF | _ ] = lists:last(R),
            seek(WB, {Influx, DB}, TF);
        _ ->
            {ok, seek}
    end.

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