From 19a9bab3a45271960fa67c725e3ee1ae8d0e6421 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Tue, 20 Apr 2021 12:04:48 +0800 Subject: [PATCH] feat(rule_engine): add rule sql functions for datetime rfc3339 --- apps/emqx_rule_engine/src/emqx_rule_funcs.erl | 19 ++++++++++++++++++- .../test/emqx_rule_engine_SUITE.erl | 2 +- .../test/emqx_rule_funcs_SUITE.erl | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_funcs.erl b/apps/emqx_rule_engine/src/emqx_rule_funcs.erl index 4d91b2fe1..1d6f7d30e 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_funcs.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_funcs.erl @@ -180,6 +180,10 @@ %% Date functions -export([ now_rfc3339/0 , 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/1 ]). @@ -834,9 +838,22 @@ now_rfc3339() -> now_rfc3339(<<"second">>). 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( 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() -> erlang:system_time(second). diff --git a/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl b/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl index 29c1ee6b6..c24c4d025 100644 --- a/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl +++ b/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl @@ -706,7 +706,7 @@ t_disable_rule(_Config) -> types=[], params_spec = #{}, title = #{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">>, rawsql => <<"select * from \"t/#\"">>, actions => [#{name => 'simple_action_2', args => #{}}] diff --git a/apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl b/apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl index 4060fb49d..96172a196 100644 --- a/apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl +++ b/apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl @@ -647,6 +647,23 @@ t_now_timestamp_1(_) -> apply_func(now_timestamp, [atom_to_binary(Unit, utf8)]))) || 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 %%------------------------------------------------------------------------------