Skip to content

Instantly share code, notes, and snippets.

@moonpolysoft
Created January 15, 2009 19:30
Show Gist options
  • Save moonpolysoft/47547 to your computer and use it in GitHub Desktop.
Save moonpolysoft/47547 to your computer and use it in GitHub Desktop.
diff --git a/elibs/rate.erl b/elibs/rate.erl
index 5f2d357..dd30e7c 100644
--- a/elibs/rate.erl
+++ b/elibs/rate.erl
@@ -13,6 +13,8 @@
-behaviour(gen_server).
+-define(MAX_POINTS, 300).
+
%% API
-export([start_link/1, get_rate/2, add_datapoint/3, close/1]).
@@ -84,7 +86,7 @@ handle_call(datapoints, _From, State = #rate{datapoints=DataPoints}) ->
%% @end
%%--------------------------------------------------------------------
handle_cast({datapoint, {Value, Time}}, State = #rate{datapoints=DataPoints,period=Period}) ->
- ModifiedDP = lists:keysort(2, [{Value, lib_misc:time_to_epoch_int(Time)} | trim_datapoints(Period,DataPoints)]),
+ ModifiedDP = [{Value, lib_misc:time_to_epoch_int(Time)} | trim_datapoints(Period, DataPoints)],
{noreply, State#rate{datapoints=ModifiedDP}};
handle_cast(close, State) ->
@@ -129,7 +131,9 @@ calculate_rate(DataPoints, Period, OverPeriod) ->
trim_datapoints(Period, DataPoints) ->
Limit = epoch() - Period,
- lists:dropwhile(fun({_,Time}) -> Time < Limit end, DataPoints).
+ Reversed = lists:reverse(DataPoints),
+ N = ?MAX_POINTS - length(Reversed),
+ lists:reverse(lists:dropwhile(fun({_,Time}) -> Time < Limit end, lists:nthtail(N, Reversed))).
epoch() ->
lib_misc:time_to_epoch_int(now()).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment