1%%%
2%%% Copyright 2012, Basho Technologies, Inc.  All Rights Reserved.
3%%%
4%%% Licensed under the Apache License, Version 2.0 (the "License");
5%%% you may not use this file except in compliance with the License.
6%%% You may obtain a copy of the License at
7%%%
8%%%     http://www.apache.org/licenses/LICENSE-2.0
9%%%
10%%% Unless required by applicable law or agreed to in writing, software
11%%% distributed under the License is distributed on an "AS IS" BASIS,
12%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13%%% See the License for the specific language governing permissions and
14%%% limitations under the License.
15%%%
16%%%-------------------------------------------------------------------
17%%% File:      folsom_sample_slide.erl
18%%% @author    Russell Brown <russelldb@basho.com>
19%%% @doc
20%%% Sliding window sample. Last Window seconds readings are recorded.
21%%% @end
22%%%-----------------------------------------------------------------
23
24-module(folsom_sample_slide).
25
26-export([
27         new/1,
28         update/2,
29         get_values/1,
30         moment/0,
31         trim/2,
32         resize/2
33        ]).
34
35-include("folsom.hrl").
36
37-define(WIDTH, 16). %% Keep this a power of two
38
39new(Size) ->
40    Sample = #slide{window = Size},
41    Pid = folsom_sample_slide_sup:start_slide_server(?MODULE, Sample#slide.reservoir, Sample#slide.window),
42    Sample#slide{server=Pid}.
43
44update(#slide{reservoir = Reservoir} = Sample, Value) ->
45    Moment = moment(),
46    X = erlang:system_info(scheduler_id),
47    Rnd = X band (?WIDTH-1),
48    ets:insert(Reservoir, {{Moment, Rnd}, Value}),
49    Sample.
50
51resize(Sample, NewSize) ->
52    folsom_sample_slide_server:resize(Sample#slide.server, NewSize),
53    Sample#slide{window = NewSize}.
54
55get_values(#slide{window = Window, reservoir = Reservoir}) ->
56    Oldest = moment() - Window,
57    ets:select(Reservoir, [{{{'$1','_'},'$2'},[{'>=', '$1', Oldest}],['$2']}]).
58
59moment() ->
60    folsom_utils:now_epoch().
61
62trim(Reservoir, Window) ->
63    Oldest = moment() - Window,
64    ets:select_delete(Reservoir, [{{{'$1','_'},'_'},[{'<', '$1', Oldest}],['true']}]).
65