anchor.diff:        end, archives()).
   
   
  +%% @doc Return the last N records starting with the given key from Tab.
  +fetch_latest(Bucket, Period, N) ->
  +    case lists:keyfind(Period, 1, archives()) of
  +        false ->
  +            {error, bad_period, Period};
  +        {_, Interval, _} ->
  +            Seconds = N * Interval,
  +            Tab = table(Bucket, Period),
  +            case mnesia:dirty_last(Tab) of
  +                '$end_of_table' ->
  +                    {ok, []};
  +                Key ->
  +                    Oldest = Key - Seconds * 1000 + 500,
  +                    Handle = qlc:q([Sample || #stat_entry{timestamp=TS} = Sample
  +                                                  <- mnesia:table(Tab), TS > Oldest]),
  +                    case mnesia:activity(async_dirty, fun qlc:eval/1, [Handle]) of
  +                        {error, _, _} = Error ->
  +                            Error;
  +                        Results ->
  +                            {ok, Results}
  +                    end
  +            end
  +    end.
  +
  +
   last_chunk(Tab, Step) ->
       case mnesia:last(Tab) of
           '$end_of_table' ->
  
positive.code: -module(stats_archiver).
  
  -include_lib("eunit/include/eunit.hrl").
  
  -include("ns_common.hrl").
  -include("ns_stats.hrl").
  
negative.code: 
  
  %% @doc Resample the stats in a table. Only reads the necessary number of rows.
  resample(Bucket, Period, Step, N) ->
      case lists:keyfind(Period, 1, archives()) of
          false ->
              {error, bad_period, Period};
          {_, Interval, _} ->
              Seconds = N * Interval * Step,
              Tab = table(Bucket, Period),
              case mnesia:dirty_last(Tab) of
                  '$end_of_table' ->
                      {ok, []};
                  Key ->
                      Oldest = Key - Seconds * 1000 - 500,
                      Handle = qlc:q([Sample || #stat_entry{timestamp=TS} = Sample
                                                    <- mnesia:table(Tab), TS > Oldest]),
                      F = fun (#stat_entry{timestamp = T} = Sample,
                               {T1, Acc, Chunk}) ->
                                  case T1 of
                                      undefined ->
                                          {T, Acc, [Sample]};
                                      T ->
                                          {T1, Acc, [Sample|Chunk]};
                                      _ ->
                                          {T1, [avg(T, Chunk)|Acc], [Sample]}
                                  end
                          end,
                      case mnesia:activity(async_dirty, fun qlc:fold/3,
                                           [F, {undefined, [], []},
                                            Handle]) of
                          {error, _, _} = Error ->
                              Error;
                          {undefined, [], []} ->
                              {ok, []};
                          {T, Acc, LastChunk} ->
                              {ok, lists:reverse([avg(T, LastChunk)|Acc])}
                      end
              end
      end.
  
  
  