From 4c2fc3924aa31d46e9a2903efd2fe1d148bbdcb4 Mon Sep 17 00:00:00 2001 From: Bartosz Zurkowski Date: Mon, 6 Mar 2017 03:10:47 +0100 Subject: [PATCH] Reset behaviours counter after each metrics write --- src/mas_counter.erl | 19 ++++++++++++++++++- src/mas_population.erl | 10 ++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/mas_counter.erl b/src/mas_counter.erl index 716a44d..4adfa78 100644 --- a/src/mas_counter.erl +++ b/src/mas_counter.erl @@ -8,7 +8,9 @@ %%% API -export([new/1, new/2, - update/2]). + update/2, + reset/1, + reset/2]). %%%============================================================================= %%% API functions @@ -36,3 +38,18 @@ new(Metrics, InitValue) -> update(DataPoints, Counter) -> Fun = fun ({K, V}, DictAcc) -> dict:update_counter(K, V, DictAcc) end, lists:foldl(Fun, Counter, DataPoints). + +%%------------------------------------------------------------------------------ +%% @doc Resets all metric values to zero. +%% @end +%%------------------------------------------------------------------------------ +reset(Counter) -> + reset(Counter, 0). + +%%------------------------------------------------------------------------------ +%% @doc Resets all metric values to provided value. +%% @end +%%------------------------------------------------------------------------------ +reset(Counter, Value) -> + Fun = fun(K, DictAcc) -> dict:store(K, Value, DictAcc) end, + lists:foldl(Fun, Counter, dict:fetch_keys(Counter)). diff --git a/src/mas_population.erl b/src/mas_population.erl index 59220a8..bda3b87 100644 --- a/src/mas_population.erl +++ b/src/mas_population.erl @@ -101,9 +101,8 @@ handle_info(process_population, State) -> self() ! process_population, {noreply, NewState}; handle_info(update_metrics, State = #state{config = Config}) -> - update_metrics(State), schedule_metrics_update(Config), - {noreply, State}; + {noreply, update_metrics(State)}; handle_info(_Info, State) -> {noreply, State}. @@ -240,7 +239,10 @@ schedule_metrics_update(#config{write_interval = WriteInterval}) -> %%------------------------------------------------------------------------------ %% @private %%------------------------------------------------------------------------------ -update_metrics(#state{metrics = Metrics, behaviours_counter = Counter}) -> +update_metrics(State) -> + #state{metrics = Metrics, behaviours_counter = Counter} = State, lists:foreach(fun(Metric = [_Pid, Behaviour]) -> exometer:update(Metric, dict:fetch(Behaviour, Counter)) - end, Metrics). + end, Metrics), + NewCounter = mas_counter:reset(Counter), + State#state{behaviours_counter = NewCounter}.