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

[4.3] acdc - PISTON-1083: acdc stats: allow some wiggle room on query timestamp ranges #6580

Closed
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
6 changes: 4 additions & 2 deletions applications/acdc/src/acdc_agent_stats.erl
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ status_match_builder_fold(<<"Agent-ID">>, AgentId, {StatusStat, Contstraints}) -
status_match_builder_fold(<<"Start-Range">>, Start, {StatusStat, Contstraints}) ->
Now = kz_time:now_s(),
Past = Now - ?CLEANUP_WINDOW,
Start1 = acdc_stats_util:apply_query_window_wiggle_room(Start, Past),

try kz_term:to_integer(Start) of
try kz_term:to_integer(Start1) of
N when N < Past ->
{'error', kz_json:from_list([{<<"Start-Range">>, <<"supplied value is too far in the past">>}
,{<<"Window-Size">>, ?CLEANUP_WINDOW}
Expand All @@ -333,8 +334,9 @@ status_match_builder_fold(<<"Start-Range">>, Start, {StatusStat, Contstraints})
status_match_builder_fold(<<"End-Range">>, End, {StatusStat, Contstraints}) ->
Now = kz_time:now_s(),
Past = Now - ?CLEANUP_WINDOW,
End1 = acdc_stats_util:apply_query_window_wiggle_room(End, Past),

try kz_term:to_integer(End) of
try kz_term:to_integer(End1) of
N when N < Past ->
{'error', kz_json:from_list([{<<"End-Range">>, <<"supplied value is too far in the past">>}
,{<<"Window-Size">>, ?CLEANUP_WINDOW}
Expand Down
6 changes: 4 additions & 2 deletions applications/acdc/src/acdc_stats.erl
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ call_match_builder_fold(<<"Status">>, Status, {CallStat, Contstraints}) ->
call_match_builder_fold(<<"Start-Range">>, Start, {CallStat, Contstraints}) ->
Now = kz_time:now_s(),
Past = Now - ?CLEANUP_WINDOW,
Start1 = acdc_stats_util:apply_query_window_wiggle_room(Start, Past),

try kz_term:to_integer(Start) of
try kz_term:to_integer(Start1) of
N when N < Past ->
{'error', kz_json:from_list([{<<"Start-Range">>, <<"supplied value is too far in the past">>}
,{<<"Window-Size">>, ?CLEANUP_WINDOW}
Expand All @@ -530,8 +531,9 @@ call_match_builder_fold(<<"Start-Range">>, Start, {CallStat, Contstraints}) ->
call_match_builder_fold(<<"End-Range">>, End, {CallStat, Contstraints}) ->
Now = kz_time:now_s(),
Past = Now - ?CLEANUP_WINDOW,
End1 = acdc_stats_util:apply_query_window_wiggle_room(End, Past),

try kz_term:to_integer(End) of
try kz_term:to_integer(End1) of
N when N < Past ->
{'error', kz_json:from_list([{<<"End-Range">>, <<"supplied value is too far in the past">>}
,{<<"Window-Size">>, ?CLEANUP_WINDOW}
Expand Down
3 changes: 3 additions & 0 deletions applications/acdc/src/acdc_stats.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
-define(STATS_QUERY_LIMITS_ENABLED, kapps_config:get_is_true(?CONFIG_CAT, <<"stats_query_limits_enabled">>, 'true')).
-define(MAX_RESULT_SET, kapps_config:get_integer(?CONFIG_CAT, <<"max_result_set">>, 25)).

%% Wiggle room for queries in case the AMQP message is delayed a little
-define(QUERY_WINDOW_WIGGLE_ROOM_S, 5).

-record(agent_miss, {agent_id :: kz_term:api_binary()
,miss_reason :: kz_term:api_binary()
,miss_timestamp = kz_time:now_s() :: pos_integer()
Expand Down
17 changes: 17 additions & 0 deletions applications/acdc/src/acdc_stats_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
,queue_id/2

,get_query_limit/1
,apply_query_window_wiggle_room/2
,db_name/1
]).

Expand Down Expand Up @@ -63,6 +64,22 @@ get_query_limit(JObj, 'false') ->
N -> N
end.

%%------------------------------------------------------------------------------
%% @doc If a query timestamp value is less than the minimum permitted by
%% validation, allow a little wiggle room in case the request just took a little
%% while to be processed.
%% @end
%%------------------------------------------------------------------------------
-spec apply_query_window_wiggle_room(pos_integer(), pos_integer()) -> pos_integer().
apply_query_window_wiggle_room(Timestamp, Minimum) ->
Offset = Minimum - Timestamp,
WithinWiggleRoom = Offset < ?QUERY_WINDOW_WIGGLE_ROOM_S,
case Offset =< 0 of
'true' -> Timestamp;
'false' when WithinWiggleRoom -> Minimum;
'false' -> Timestamp
end.

-spec db_name(kz_term:ne_binary()) -> kz_term:ne_binary().
db_name(Account) ->
kz_util:format_account_mod_id(Account).
8 changes: 4 additions & 4 deletions applications/acdc/src/cb_agents.erl
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ fetch_all_current_agent_stats(Context) ->
-spec fetch_all_current_stats(cb_context:context(), kz_term:api_binary()) -> cb_context:context().
fetch_all_current_stats(Context, AgentId) ->
Now = kz_time:now_s(),
Yday = Now - ?SECONDS_IN_DAY,
From = Now - ?ACDC_CLEANUP_WINDOW,

Req = props:filter_undefined(
[{<<"Account-ID">>, cb_context:account_id(Context)}
,{<<"Agent-ID">>, AgentId}
,{<<"Start-Range">>, Yday}
,{<<"Start-Range">>, From}
,{<<"End-Range">>, Now}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]),
Expand Down Expand Up @@ -378,12 +378,12 @@ fetch_current_status(Context, AgentId, 'true') ->
cb_context:context().
fetch_all_current_statuses(Context, AgentId, Status) ->
Now = kz_time:now_s(),
Yday = Now - ?SECONDS_IN_DAY,
From = Now - ?ACDC_CLEANUP_WINDOW,

Opts = props:filter_undefined(
[{<<"Status">>, Status}
,{<<"Agent-ID">>, AgentId}
,{<<"Start-Range">>, Yday}
,{<<"Start-Range">>, From}
,{<<"End-Range">>, Now}
,{<<"Limit">>, cb_context:req_value(Context, <<"limit">>)}
]),
Expand Down
5 changes: 5 additions & 0 deletions applications/acdc/src/cb_queues.erl
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,15 @@ fetch_all_queue_stats(Context) ->
-spec fetch_all_current_queue_stats(cb_context:context()) -> cb_context:context().
fetch_all_current_queue_stats(Context) ->
lager:debug("querying for all recent stats"),
Now = kz_time:now_s(),
From = Now - ?ACDC_CLEANUP_WINDOW,

Req = props:filter_undefined(
[{<<"Account-ID">>, cb_context:account_id(Context)}
,{<<"Status">>, cb_context:req_value(Context, <<"status">>)}
,{<<"Agent-ID">>, cb_context:req_value(Context, <<"agent_id">>)}
,{<<"Start-Range">>, From}
,{<<"End-Range">>, Now}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]),
fetch_from_amqp(Context, Req).
Expand Down