anchor.diff:      add_counters(Rest, Counters#{Key => List}).
   
   
  -%% @private
  -get_counters(TimeKeys, Counters) ->
  -    TimeList = get_time(TimeKeys),
  -    get_counters(TimeList, Counters, []).
  -
  -
  -%% @private
  -get_counters([], _Counters, Acc) ->
  +%% @doc Get the current counter for a series of time slots
  +-spec get_last_counter([time_key()], counters()) ->
  +    #{time_key() => integer()}.
  +
  +get_last_counter(TimeKeys, Counters) ->
  +    get_last_counter(TimeKeys, Counters, timestamp()).
  +
  +
  +%% @doc Get the current counter for a series of time slots
  +-spec get_last_counter([time_key()], counters(), time()) ->
  +    #{time_key() => integer()}.
  +
  +get_last_counter(TimeKeys, Counters, Now) ->
  +    KeySlots = get_time_slots(TimeKeys, Now),
  +    %lager:error("NKLOG SLOTS ~p", [KeySlots]),
  +    do_get_counter(KeySlots, Counters, #{}).
  +
  +
  +%% @private
  +do_get_counter([], _Counters, Acc) ->
       Acc;
   
  -get_counters([{Key, Time}|Rest], Counters, Acc) ->
  -    Count = case maps:get(Key, Counters, []) of
  -        [{Time, KCount}|Rest] ->
  -            KCount;
  +do_get_counter([{Key, Slot}|Rest], Counters, Acc) ->
  +    KeyCount = case maps:get(Key, Counters, []) of
  +        [{Slot, Count}|_] ->
  +            Count;
           _ ->
               0
       end,
  -    get_counters(Rest, Counters, Acc#{Key => Count}).
  -
  +    do_get_counter(Rest, Counters, Acc#{Key => KeyCount}).
  +
  +
  +%% @doc Get the current counter for a series of time slots
  +-spec get_last_counters([time_key()], counters()) ->
  +    #{time_key() => [integer()]}.
  +
  +get_last_counters(TimeKeys, Counters) ->
  +    get_last_counters(TimeKeys, Counters, timestamp()).
  +
  +
  +%% @doc Get the current counter for a series of time slots
  +-spec get_last_counters([time_key()], counters(), time()) ->
  +    #{time_key() => [integer()]}.
  +
  +get_last_counters(TimeKeys, Counters, Now) ->
  +    KeySlots = get_time_slots(TimeKeys, Now),
  +    %lager:error("NKLOG SLOTS2 ~p", [KeySlots]),
  +    do_get_counters(KeySlots, Counters, #{}).
  +
  +
  +%% @private
  +do_get_counters([], _Counters, Acc) ->
  +    Acc;
  +
  +do_get_counters([{Key, Slot}|Rest], Counters, Acc) ->
  +    List = maps:get(Key, Counters, []),
  +    KeyCount = do_get_counters2(Slot, List, 5, []),
  +    do_get_counters(Rest, Counters, Acc#{Key => KeyCount}).
  +
  +
  +do_get_counters2(_Slot, _List, 0, Acc) ->
  +    lists:reverse(Acc);
  +
  +do_get_counters2(Slot, List, Len, Acc) ->
  +   Count = case lists:keyfind(Slot, 1, List) of
  +       {Slot, Counter} ->
  +           Counter;
  +       false ->
  +           0
  +    end,
  +    do_get_counters2(Slot-1, List, Len-1, [Count|Acc]).
   
   
   
  
positive.code: -module(nklib_throttle).
  -author('Carlos Gonzalez <carlosj.gf@gmail.com>').
  
  -export([get_time/1, incr/2, get_counters/2]).
  -export([test/0]).
  -export_type([time_key/0, counters/0]).
  
  
  
negative.code: %% Types
  %% ===================================================================
  
  -type time_key() ::
      {msecs, integer()}  |
      {secs, integer()}  |
      {mins, integer()} |
      {hours, integer()} |
      {days, integer()} |
      {months, integer()} |
      {years, integer()}.
  
  -type time() :: nklib_util:m_timestamp().
  
  -type time_slot() :: integer().
  
  -type counters() :: #{time_key() => [{time_slot(), Counter::integer()}]}.
  
  
  %% ===================================================================
  %% Public
  %% ===================================================================
  
  %% @doc Create a new counter
  -spec new() ->
      counters().
  
  new() ->
      #{}.
  
  
  %% @doc Get a time series (number of units of the time_key() from unixtime 0)
  -spec get_time_slots([time_key()]) ->
      [{time_key(), time_slot()}].
  
  get_time_slots(TimeKeys) ->
      get_time_slots(TimeKeys, timestamp()).
  
  
  %% @doc Get a time series (number of units of the time_key() from unixtime 0)
  -spec get_time_slots([time_key()], time()) ->
      [{time_key(), time_slot()}].
  
  get_time_slots(TimeKeys, Now) ->
      get_time(TimeKeys, Now, []).
  
  
  %% @doc Increment counters for a series of time slots
  -spec incr([time_key()], counters()) ->
      counters().
  
  incr(TimeKeys, Counters) ->
      incr(TimeKeys, Counters, timestamp()).
  
  
  %% @doc
  -spec incr([time_key()], counters(), time()) ->
      counters().
  
  incr(TimeKeys, Counters, Now) ->
      TimeList = get_time_slots(TimeKeys, Now),
      add_counters(TimeList, Counters).
  
  
  