Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add temporal_route end-date #6664

Open
wants to merge 6 commits into
base: 4.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions applications/callflow/src/module/cf_temporal_route.erl
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,36 @@ get_temporal_rules([{Route, Id}|Routes], LSec, AccountDb, TZ, Now, Rules) ->
maybe_build_rule(Routes, LSec, AccountDb, TZ, Now, Rules, Id, JObj)
end.

-spec check_end_date(non_neg_integer(), kz_term:ne_binary(), kz_time:datetime(), integer(), kzd_temporal_rules:doc()) -> 'future' | 'equal' | 'past'.
check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) ->
% TODO, this is an workaround since the from_gregorian_seconds function are crashing on low values such as zero
case EndDateSeconds of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the value for 'undefined' instead of 0, and I would do it in a function clause:

check_end_date(_LSec, _TZ, _Now, 'undefined', _RulesDoc) -> 'future';
check_end_date(...) ->

0 -> 'future';
_ ->
EndDate = kz_date:from_gregorian_seconds(kzd_temporal_rules:end_date(RulesDoc, LSec), TZ),
case kz_date:relative_difference(Now, {EndDate, {0,0,0}}) of
'past' -> 'past';
_ -> 'future'
end
end.

-spec maybe_build_rule(routes(), non_neg_integer(), kz_term:ne_binary(), kz_term:ne_binary(), kz_time:datetime(), rules(), kz_term:ne_binary(), kzd_temporal_rules:doc()) -> rules().
maybe_build_rule(Routes, LSec, AccountDb, TZ, Now, Rules, Id, RulesDoc) ->
StartDate = kz_date:from_gregorian_seconds(kzd_temporal_rules:start_date(RulesDoc, LSec), TZ),
RuleName = kzd_temporal_rules:name(RulesDoc, ?RULE_DEFAULT_NAME),

EndDateSeconds = kzd_temporal_rules:end_date(RulesDoc),
case kz_date:relative_difference(Now, {StartDate, {0,0,0}}) of
'future' ->
lager:warning("rule ~p is in the future discarding", [RuleName]),
get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules);
_ ->
get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules])
case check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) of
'future' ->
get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules]);
'past' ->
lager:warning("rule ~p end_date is pass discarding", [RuleName]),
get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules)
end
end.

-spec build_rule(kz_term:ne_binary(), kzd_temporal_rules:doc(), kz_time:date(), kz_term:ne_binary()) -> rule().
Expand All @@ -203,6 +222,7 @@ build_rule(Id, RulesDoc, StartDate, RuleName) ->
,name = RuleName
,ordinal = kzd_temporal_rules:ordinal(RulesDoc, ?RULE_DEFAULT_ORDINAL)
,start_date = StartDate
,end_date = kzd_temporal_rules:end_date(RulesDoc)
,wdays = sort_wdays(kzd_temporal_rules:wdays(RulesDoc, ?RULE_DEFAULT_WDAYS))
,wtime_start = kzd_temporal_rules:time_window_start(RulesDoc, ?RULE_DEFAULT_WTIME_START)
,wtime_stop = kzd_temporal_rules:time_window_stop(RulesDoc, ?RULE_DEFAULT_WTIME_STOP)
Expand Down
2 changes: 2 additions & 0 deletions applications/callflow/src/module/cf_temporal_route.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
-define(RULE_DEFAULT_ORDINAL, <<"first">>).
-define(RULE_DEFAULT_MONTH, 1).
-define(RULE_DEFAULT_START_DATE, {2011,1,1}).
-define(RULE_DEFAULT_END_DATE, {0,0,0}).
-define(RULE_DEFAULT_WTIME_START, 0).
-define(RULE_DEFAULT_WTIME_STOP, ?SECONDS_IN_DAY).

Expand All @@ -51,6 +52,7 @@
,ordinal = ?RULE_DEFAULT_ORDINAL :: ordinal()
,month = ?RULE_DEFAULT_MONTH :: kz_time:month()
,start_date = ?RULE_DEFAULT_START_DATE :: kz_time:date()
,end_date = ?RULE_DEFAULT_END_DATE :: kz_time:date()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should default to 'undefined' instead of the tuple. Type would be kz_time:date() | 'undefined'

,wtime_start = ?RULE_DEFAULT_WTIME_START :: non_neg_integer()
,wtime_stop = ?RULE_DEFAULT_WTIME_STOP :: non_neg_integer()
}).
Expand Down
1 change: 1 addition & 0 deletions applications/crossbar/doc/ref/temporal_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Key | Description | Type | Default | Required | Support Level
`cycle` | The recurrence cycle for this rule | `string('date' | 'daily' | 'weekly' | 'monthly' | 'yearly')` | | `true` | `supported`
`days` | The recurrence days for this rule | `array(integer())` | | `false` | `supported`
`enabled` | Whether the rule is enabled | `boolean()` | | `false` |
`end_date` | The date that the rule ends. Zero means no end date | `integer()` | `0` | `false` | `supported`
`flags.[]` | | `string()` | | `false` | `supported`
`flags` | Flags set by external applications | `array(string())` | | `false` | `supported`
`interval` | The recurrence interval for this rule | `integer()` | `1` | `false` | `supported`
Expand Down
1 change: 1 addition & 0 deletions applications/crossbar/doc/temporal_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Key | Description | Type | Default | Required | Support Level
`cycle` | The recurrence cycle for this rule | `string('date' | 'daily' | 'weekly' | 'monthly' | 'yearly')` | | `true` | `supported`
`days` | The recurrence days for this rule | `array(integer())` | | `false` | `supported`
`enabled` | Whether the rule is enabled | `boolean()` | | `false` |
`end_date` | The date that the rule ends. Zero means no end date | `integer()` | `0` | `false` | `supported`
`flags.[]` | | `string()` | | `false` | `supported`
`flags` | Flags set by external applications | `array(string())` | | `false` | `supported`
`interval` | The recurrence interval for this rule | `integer()` | `1` | `false` | `supported`
Expand Down
5 changes: 5 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -36181,6 +36181,11 @@
"description": "Whether the rule is enabled",
"type": "boolean"
},
"end_date": {
"default": 0,
"description": "The date that the rule ends. Zero means no end date",
"type": "integer"
},
"flags": {
"description": "Flags set by external applications",
"items": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"description": "Whether the rule is enabled",
"type": "boolean"
},
"end_date": {
"default": 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we prefer that the absence of the value would mean no end date. Remove the default; end_date should only have a value if it is a proper, meaningful timestamp.

"description": "The date that the rule ends. Zero means no end date",
"support_level": "supported",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support_level is a 2600Hz-only field to include, please remove.

"type": "integer"
},
"flags": {
"description": "Flags set by external applications",
"items": {
Expand Down
13 changes: 13 additions & 0 deletions core/kazoo_documents/src/kzd_temporal_rules.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-export([name/1, name/2, set_name/2]).
-export([ordinal/1, ordinal/2, set_ordinal/2]).
-export([start_date/1, start_date/2, set_start_date/2]).
-export([end_date/1, end_date/2, set_end_date/2]).
-export([time_window_start/1, time_window_start/2, set_time_window_start/2]).
-export([time_window_stop/1, time_window_stop/2, set_time_window_stop/2]).
-export([wdays/1, wdays/2, set_wdays/2]).
Expand Down Expand Up @@ -145,6 +146,18 @@ start_date(Doc, Default) ->
set_start_date(Doc, StartDate) ->
kz_json:set_value([<<"start_date">>], StartDate, Doc).

-spec end_date(doc()) -> integer().
end_date(Doc) ->
end_date(Doc, 0).

-spec end_date(doc(), Default) -> integer() | Default.
end_date(Doc, Default) ->
kz_json:get_integer_value([<<"end_date">>], Doc, Default).

-spec set_end_date(doc(), integer()) -> doc().
set_end_date(Doc, EndDate) ->
kz_json:set_value([<<"end_date">>], EndDate, Doc).

-spec time_window_start(doc()) -> integer().
time_window_start(Doc) ->
time_window_start(Doc, 'undefined').
Expand Down
13 changes: 13 additions & 0 deletions core/kazoo_documents/src/kzd_temporal_rules.erl.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-export([cycle/1, cycle/2, set_cycle/2]).
-export([days/1, days/2, set_days/2]).
-export([enabled/1, enabled/2, set_enabled/2]).
-export([end_date/1, end_date/2, set_end_date/2]).
-export([flags/1, flags/2, set_flags/2]).
-export([interval/1, interval/2, set_interval/2]).
-export([month/1, month/2, set_month/2]).
Expand Down Expand Up @@ -67,6 +68,18 @@ enabled(Doc, Default) ->
set_enabled(Doc, Enabled) ->
kz_json:set_value([<<"enabled">>], Enabled, Doc).

-spec end_date(doc()) -> integer().
end_date(Doc) ->
end_date(Doc, 0).

-spec end_date(doc(), Default) -> integer() | Default.
end_date(Doc, Default) ->
kz_json:get_integer_value([<<"end_date">>], Doc, Default).

-spec set_end_date(doc(), integer()) -> doc().
set_end_date(Doc, EndDate) ->
kz_json:set_value([<<"end_date">>], EndDate, Doc).

-spec flags(doc()) -> kz_term:api_ne_binaries().
flags(Doc) ->
flags(Doc, 'undefined').
Expand Down