mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-12-04 05:09:09 +08:00
为历史数据增加单位
This commit is contained in:
parent
296e627544
commit
6bb850961c
@ -36,7 +36,9 @@ get_echart_data(Channel, ProductId, DeviceId, Args) ->
|
|||||||
<<"db">> => ProductId
|
<<"db">> => ProductId
|
||||||
}) of
|
}) of
|
||||||
{Names, {ok, #{<<"results">> := Results}}} ->
|
{Names, {ok, #{<<"results">> := Results}}} ->
|
||||||
|
io:format("~s ~p Results = ~p ~n",[?FILE,?LINE,Results]),
|
||||||
Chartdata = get_echart(ProductId, Results, Names, Interval),
|
Chartdata = get_echart(ProductId, Results, Names, Interval),
|
||||||
|
io:format("~s ~p Chartdata = ~p~n",[?FILE,?LINE,Chartdata]),
|
||||||
{ok, #{<<"chartData">> => Chartdata}};
|
{ok, #{<<"chartData">> => Chartdata}};
|
||||||
_ ->
|
_ ->
|
||||||
{ok, #{<<"code">> => 400, <<"msg">> => <<"no data">>}}
|
{ok, #{<<"code">> => 400, <<"msg">> => <<"no data">>}}
|
||||||
@ -44,6 +46,7 @@ get_echart_data(Channel, ProductId, DeviceId, Args) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
get_echart(ProductId, Results, Names, Interval) ->
|
get_echart(ProductId, Results, Names, Interval) ->
|
||||||
|
io:format("~s ~p ProductId = ~p, Results = ~p, Names = ~p, Interval = ~p ~n",[?FILE,?LINE,ProductId, Results, Names, Interval]),
|
||||||
Maps = dgiot_product:get_prop(ProductId),
|
Maps = dgiot_product:get_prop(ProductId),
|
||||||
Units = dgiot_product:get_unit(ProductId),
|
Units = dgiot_product:get_unit(ProductId),
|
||||||
NewMaps = maps:merge(#{<<"createdat">> => <<"日期"/utf8>>}, Maps),
|
NewMaps = maps:merge(#{<<"createdat">> => <<"日期"/utf8>>}, Maps),
|
||||||
@ -122,7 +125,7 @@ get_data_by_month(Channel, ProductId, DeviceId, Args) ->
|
|||||||
end,
|
end,
|
||||||
%% io:format("~s ~p Res = ~p~n",[?FILE,?LINE,Res]),
|
%% io:format("~s ~p Res = ~p~n",[?FILE,?LINE,Res]),
|
||||||
{ok,Sql} = maps:find(<<"sql">>,Res),
|
{ok,Sql} = maps:find(<<"sql">>,Res),
|
||||||
{ok,Names} = maps:find(<<"names">>,Res),
|
{ok,Name_and_nuit} = maps:find(<<"name_and_unit">>,Res),
|
||||||
%% 配置参数
|
%% 配置参数
|
||||||
TableName = ?Table(DeviceId),
|
TableName = ?Table(DeviceId),
|
||||||
Interval = <<"1d">>,
|
Interval = <<"1d">>,
|
||||||
@ -130,8 +133,8 @@ get_data_by_month(Channel, ProductId, DeviceId, Args) ->
|
|||||||
case dgiot_device_tdengine:get_history_data2(Sql, Channel, TableName, Interval, ProductId, StartTime, EndTime) of
|
case dgiot_device_tdengine:get_history_data2(Sql, Channel, TableName, Interval, ProductId, StartTime, EndTime) of
|
||||||
%% 判断结果并转换格式
|
%% 判断结果并转换格式
|
||||||
{ok, #{<<"results">> := Results}} ->
|
{ok, #{<<"results">> := Results}} ->
|
||||||
%% io:format("~s ~p Results = ~p ~n",[?FILE,?LINE,Results]),
|
%% io:format("~s ~p Results = ~p,Name_and_nuit = ~p ~n",[?FILE,?LINE,Results,Name_and_nuit]),
|
||||||
Tabledata = get_table(Results,Names),
|
Tabledata = get_table(Results,Name_and_nuit),
|
||||||
%% io:format("~s ~p Tabledata = ~p ~n",[?FILE,?LINE,Tabledata]),
|
%% io:format("~s ~p Tabledata = ~p ~n",[?FILE,?LINE,Tabledata]),
|
||||||
{ok,Tabledata};
|
{ok,Tabledata};
|
||||||
_ ->
|
_ ->
|
||||||
@ -142,71 +145,73 @@ get_data_by_month(Channel, ProductId, DeviceId, Args) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
get_keys(#{<<"thing">> := #{<<"properties">> := Properties}}, [<<"*">>]) ->
|
get_keys(#{<<"thing">> := #{<<"properties">> := Properties}}, [<<"*">>]) ->
|
||||||
lists:foldl(fun(X, Acc) ->
|
lists:foldl(
|
||||||
|
fun(X, Acc) ->
|
||||||
%% io:format("~s ~p Acc = ~p ~n",[?FILE,?LINE,Acc]),
|
%% io:format("~s ~p Acc = ~p ~n",[?FILE,?LINE,Acc]),
|
||||||
case X of
|
case X of
|
||||||
#{<<"identifier">> := Identifier, <<"isaccumulate">> := true,<<"name">> := Name,<<"identifier">> := Key} ->
|
#{<<"identifier">> := Identifier, <<"isaccumulate">> := true, <<"name">> := Name, <<"identifier">> := Key,<<"dataType">> := #{<<"specs">> := #{<<"unit">> := Unit}} } ->
|
||||||
case Acc of
|
case Acc of
|
||||||
#{<<"sql">> := Sql,<<"names">> :=Names } ->
|
#{<<"sql">> := Sql, <<"name_and_unit">> := Map} ->
|
||||||
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
#{<<"sql">> => <<Sql/binary, ", sum(", Identifier/binary, ")">>,<<"names">> => Names#{<<Key/binary>> => <<Name/binary>>}};
|
#{<<"sql">> => <<Sql/binary, ", sum(", Identifier/binary, ")">>, <<"name_and_unit">> =>Map#{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}};
|
||||||
_ ->
|
_ ->
|
||||||
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
#{<<"sql">> => <<" sum(", Identifier/binary, ")">>,<<"names">> => #{<<Key/binary>> => Name/binary}}
|
#{<<"sql">> => <<" sum(", Identifier/binary, ")">>, <<"name_and_unit">> => #{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}}
|
||||||
end;
|
end;
|
||||||
#{<<"identifier">> := Identifier, <<"isaccumulate">> := false,<<"name">> := Name,<<"identifier">> := Key} ->
|
#{<<"identifier">> := Identifier, <<"isaccumulate">> := false, <<"name">> := Name, <<"identifier">> := Key,<<"dataType">> := #{<<"specs">> := #{<<"unit">> := Unit}}} ->
|
||||||
case Acc of
|
case Acc of
|
||||||
#{<<"sql">> := Sql,<<"names">> :=Names } ->
|
#{<<"sql">> := Sql, <<"name_and_unit">> := Map} ->
|
||||||
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
#{<<"sql">> => <<Sql/binary, ", last(", Identifier/binary, ")">>,<<"names">> => Names#{<<Key/binary>> => <<Name/binary>>}};
|
#{<<"sql">> => <<Sql/binary, ", last(", Identifier/binary, ")">>, <<"name_and_unit">> =>Map#{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}};
|
||||||
_ ->
|
_ ->
|
||||||
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
%% io:format("~s ~p Name = ~p ~n",[?FILE,?LINE,Name]),
|
#{<<"sql">> => <<" last(", Identifier/binary, ")">>, <<"name_and_unit">> => #{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}}
|
||||||
#{<<"sql">> => <<" last(", Identifier/binary, ")">>,<<"names">> => #{<<Key/binary>> => <<Name/binary>>}}
|
end;
|
||||||
end
|
_ ->
|
||||||
end
|
%% io:format("~s ~p Property = ~p ~n",[?FILE,?LINE,X]),
|
||||||
end, #{}, Properties);
|
Acc
|
||||||
|
end
|
||||||
|
end, #{}, Properties);
|
||||||
|
|
||||||
get_keys(#{<<"thing">> := #{<<"properties">> := Properties}}, Keys) ->
|
get_keys(#{<<"thing">> := #{<<"properties">> := Properties}}, Keys) ->
|
||||||
lists:foldl(fun(X, Acc) ->
|
lists:foldl(fun(X, Acc) ->
|
||||||
case X of
|
case X of
|
||||||
#{<<"identifier">> := Identifier, <<"isaccumulate">> := true, <<"name">> := Name, <<"identifier">> := Key} ->
|
#{<<"identifier">> := Identifier, <<"isaccumulate">> := true, <<"name">> := Name, <<"identifier">> := Key,<<"dataType">> := #{<<"specs">> := #{<<"unit">> := Unit}}} ->
|
||||||
case lists:member(Identifier, Keys) of
|
case lists:member(Identifier, Keys) of
|
||||||
true ->
|
true ->
|
||||||
case Acc of
|
case Acc of
|
||||||
#{<<"sql">> := Sql,<<"names">> :=Names } ->
|
#{<<"sql">> := Sql, <<"name_and_unit">> := Map} ->
|
||||||
{ok, Sql} = maps:find(<<"sql">>, Acc),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
{ok, Names} = maps:find(<<"names">>, Acc),
|
#{<<"sql">> => <<Sql/binary, ", sum(", Identifier/binary, ")">>, <<"name_and_unit">> =>Map#{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}};
|
||||||
#{<<"sql">> => <<Sql/binary, ", sum(", Identifier/binary, ")">>, <<"names">> => Names#{<<Key/binary>> => <<Name/binary>>}};
|
|
||||||
_ ->
|
_ ->
|
||||||
#{<<"sql">> => <<" sum(", Identifier/binary, ")">>, <<"names">> => #{<<Key/binary>> => <<Name/binary>>}}
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
|
#{<<"sql">> => <<" sum(", Identifier/binary, ")">>, <<"name_and_unit">> => #{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}}
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
Acc
|
Acc
|
||||||
end;
|
end;
|
||||||
|
|
||||||
#{<<"identifier">> := Identifier, <<"isaccumulate">> := false, <<"name">> := Name, <<"identifier">> := Key} ->
|
#{<<"identifier">> := Identifier, <<"isaccumulate">> := false, <<"name">> := Name, <<"identifier">> := Key,<<"dataType">> := #{<<"specs">> := #{<<"unit">> := Unit}} } ->
|
||||||
|
|
||||||
case lists:member(Identifier, Keys) of
|
case lists:member(Identifier, Keys) of
|
||||||
true ->
|
true ->
|
||||||
case Acc of
|
case Acc of
|
||||||
#{<<"sql">> := Sql,<<"names">> :=Names } ->
|
#{<<"sql">> := Sql, <<"name_and_unit">> := Map} ->
|
||||||
{ok, Sql} = maps:find(<<"sql">>, Acc),
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
{ok, Names} = maps:find(<<"names">>, Acc),
|
#{<<"sql">> => <<Sql/binary, ", last(", Identifier/binary, ")">>, <<"name_and_unit">> =>Map#{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}};
|
||||||
#{<<"sql">> => <<Sql/binary, " last(", Identifier/binary, ")">>, <<"names">> => Names#{<<Key/binary>> => <<Name/binary>>}};
|
|
||||||
_ ->
|
_ ->
|
||||||
|
%% io:format("~s ~p Key = ~p ~n",[?FILE,?LINE,Key]),
|
||||||
#{<<"sql">> => <<" last(", Identifier/binary, ")">>, <<"names">> => #{<<Key/binary>> => <<Name/binary>>}}
|
#{<<"sql">> => <<" last(", Identifier/binary, ")">>, <<"name_and_unit">> => #{<<Key/binary>> => #{<<"name">> => <<Name/binary>>,<<"unit">> => <<Unit/binary>>}}}
|
||||||
end;
|
end;
|
||||||
false -> Acc
|
false -> Acc
|
||||||
end
|
end;
|
||||||
|
_ ->
|
||||||
|
Acc
|
||||||
|
|
||||||
end
|
end
|
||||||
end, #{}, Properties).
|
end, #{}, Properties).
|
||||||
|
|
||||||
get_table(Results,Names) ->
|
get_table(Results,Name_and_nuit) ->
|
||||||
Count = string:len(Results),
|
Count = string:len(Results),
|
||||||
|
|
||||||
TableData = lists:foldl(fun(X, Acc) ->
|
TableData = lists:foldl(fun(X, Acc) ->
|
||||||
Res = maps:fold(fun(K, V, Init) ->
|
Res = maps:fold(fun(K, V, Init) ->
|
||||||
case K of
|
case K of
|
||||||
@ -216,9 +221,17 @@ get_table(Results,Names) ->
|
|||||||
case binary:match(K, <<"last">>) of
|
case binary:match(K, <<"last">>) of
|
||||||
{0, 4} ->
|
{0, 4} ->
|
||||||
Last_Key = binary:part(K, 5, byte_size(K) - 6),
|
Last_Key = binary:part(K, 5, byte_size(K) - 6),
|
||||||
case maps:find(<<Last_Key/binary>>, Names) of
|
case maps:find(<<Last_Key/binary>>, Name_and_nuit) of
|
||||||
{ok, Name} ->
|
{ok, Map} ->
|
||||||
Init#{<<Name/binary>> => V};
|
Name = maps:get(<<"name">>,Map,K),
|
||||||
|
Unit = maps:get(<<"unit">>,Map,<<"">>),
|
||||||
|
K_with_unit = case V of
|
||||||
|
null -> <<"-">>;
|
||||||
|
_ ->
|
||||||
|
NewK = dgiot_utils:to_binary(V),
|
||||||
|
<<NewK/binary,Unit/binary>>
|
||||||
|
end,
|
||||||
|
Init#{<<Name/binary>> => <<K_with_unit/binary>>};
|
||||||
error ->
|
error ->
|
||||||
Init#{<<K/binary>> => V}
|
Init#{<<K/binary>> => V}
|
||||||
end;
|
end;
|
||||||
@ -226,10 +239,18 @@ get_table(Results,Names) ->
|
|||||||
case binary:match(K, <<"sum">>) of
|
case binary:match(K, <<"sum">>) of
|
||||||
{0, 3} ->
|
{0, 3} ->
|
||||||
Sum_Key = binary:part(K, 4, byte_size(K) - 5),
|
Sum_Key = binary:part(K, 4, byte_size(K) - 5),
|
||||||
case maps:find(<<Sum_Key/binary>>, Names) of
|
case maps:find(<<Sum_Key/binary>>, Name_and_nuit) of
|
||||||
{ok, Name1} ->
|
{ok, Map} ->
|
||||||
Init#{<<Name1/binary>> => V};
|
Name = maps:get(<<"name">>,Map,K),
|
||||||
error ->
|
Unit = maps:get(<<"unit">>,Map,<<"">>),
|
||||||
|
K_with_unit = case V of
|
||||||
|
null -> <<"-">>;
|
||||||
|
_ ->
|
||||||
|
NewK = dgiot_utils:to_binary(V),
|
||||||
|
<<NewK/binary,Unit/binary>>
|
||||||
|
end,
|
||||||
|
Init#{<<Name/binary>> => <<K_with_unit/binary>>};
|
||||||
|
errgor ->
|
||||||
Init#{<<K/binary>> => V}
|
Init#{<<K/binary>> => V}
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
|
Loading…
Reference in New Issue
Block a user