feat(rule_engine): add rule sql functions for datetime rfc3339

This commit is contained in:
Shawn 2021-04-20 12:04:48 +08:00 committed by wwhai
parent 73e563c883
commit 19a9bab3a4
3 changed files with 36 additions and 2 deletions

View File

@ -180,6 +180,10 @@
%% Date functions %% Date functions
-export([ now_rfc3339/0 -export([ now_rfc3339/0
, now_rfc3339/1 , now_rfc3339/1
, unix_ts_to_rfc3339/1
, unix_ts_to_rfc3339/2
, rfc3339_to_unix_ts/1
, rfc3339_to_unix_ts/2
, now_timestamp/0 , now_timestamp/0
, now_timestamp/1 , now_timestamp/1
]). ]).
@ -834,9 +838,22 @@ now_rfc3339() ->
now_rfc3339(<<"second">>). now_rfc3339(<<"second">>).
now_rfc3339(Unit) -> now_rfc3339(Unit) ->
unix_ts_to_rfc3339(now_timestamp(Unit), Unit).
unix_ts_to_rfc3339(Epoch) ->
unix_ts_to_rfc3339(Epoch, <<"second">>).
unix_ts_to_rfc3339(Epoch, Unit) when is_integer(Epoch) ->
emqx_rule_utils:bin( emqx_rule_utils:bin(
calendar:system_time_to_rfc3339( calendar:system_time_to_rfc3339(
now_timestamp(Unit), [{unit, time_unit(Unit)}])). Epoch, [{unit, time_unit(Unit)}])).
rfc3339_to_unix_ts(DateTime) ->
rfc3339_to_unix_ts(DateTime, <<"second">>).
rfc3339_to_unix_ts(DateTime, Unit) when is_binary(DateTime) ->
calendar:rfc3339_to_system_time(binary_to_list(DateTime),
[{unit, time_unit(Unit)}]).
now_timestamp() -> now_timestamp() ->
erlang:system_time(second). erlang:system_time(second).

View File

@ -706,7 +706,7 @@ t_disable_rule(_Config) ->
types=[], params_spec = #{}, types=[], params_spec = #{},
title = #{en => <<"Simple Action">>}, title = #{en => <<"Simple Action">>},
description = #{en => <<"Simple Action">>}}), description = #{en => <<"Simple Action">>}}),
{ok, #rule{actions = [#action_instance{id = ActInsId0}]}} = emqx_rule_engine:create_rule( {ok, #rule{actions = [#action_instance{}]}} = emqx_rule_engine:create_rule(
#{id => <<"simple_rule_2">>, #{id => <<"simple_rule_2">>,
rawsql => <<"select * from \"t/#\"">>, rawsql => <<"select * from \"t/#\"">>,
actions => [#{name => 'simple_action_2', args => #{}}] actions => [#{name => 'simple_action_2', args => #{}}]

View File

@ -647,6 +647,23 @@ t_now_timestamp_1(_) ->
apply_func(now_timestamp, [atom_to_binary(Unit, utf8)]))) apply_func(now_timestamp, [atom_to_binary(Unit, utf8)])))
|| Unit <- [second,millisecond,microsecond,nanosecond]]. || Unit <- [second,millisecond,microsecond,nanosecond]].
t_unix_ts_to_rfc3339(_) ->
[begin
BUnit = atom_to_binary(Unit, utf8),
Epoch = apply_func(now_timestamp, [BUnit]),
DateTime = apply_func(unix_ts_to_rfc3339, [Epoch, BUnit]),
?assertEqual(Epoch,
calendar:rfc3339_to_system_time(binary_to_list(DateTime), [{unit, Unit}]))
end || Unit <- [second,millisecond,microsecond,nanosecond]].
t_rfc3339_to_unix_ts(_) ->
[begin
BUnit = atom_to_binary(Unit, utf8),
Epoch = apply_func(now_timestamp, [BUnit]),
DateTime = apply_func(unix_ts_to_rfc3339, [Epoch, BUnit]),
?assertEqual(Epoch, emqx_rule_funcs:rfc3339_to_unix_ts(DateTime, BUnit))
end || Unit <- [second,millisecond,microsecond,nanosecond]].
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Utility functions %% Utility functions
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------