anchor.diff:  %% Internal functions
   %%
   
  +%% {Period, Seconds, Samples}
   archives() ->
  -    [{minute, 1, 60}, % in seconds
  -     {hour, 10, 360}, % 10 second intervals
  -     {day, 24, 360},  % 240 second intervals (4 minutes)
  -     {week, 7, 360}   % 1680 second intervals (28 minutes)
  -    ].
  +    [{minute, 1, 60},
  +     {hour, 4, 900},
  +     {day, 60, 1440}, % 24 hours
  +     {week, 600, 1152}, % eight days (computer weeks)
  +     {month, 1800, 1488}, % 31 days
  +     {year, 21600, 1464}]. % 366 days
   
   
   %% @doc Compute the average of a list of entries.
  -%% Timestamp is taken from the first entry.
  -avg(Samples) ->
  +avg(TS, Samples) ->
       [First|Rest] = Samples,
  -    {TS, FirstList} = stat_to_list(First),
  +    {_, FirstList} = stat_to_list(First),
       Sums = lists:foldl(fun (E, Acc) ->
                                  {_, L} = stat_to_list(E),
  -                               lists:zipwith(fun (A, B) -> A + B end, L, Acc)
  +                               lists:zipwith(   % I cleverly duplicate samples
  +                                                % to avoid having to keep
  +                                                % separate counts per field.
  +                                 fun (undefined, undefined) ->
  +                                         0;     % Zero is better than trying to
  +                                                % do math with undefined. This
  +                                                % should be rare anyway.
  +                                     (undefined, B) ->
  +                                         B;
  +                                     (A, undefined) ->
  +                                         A;
  +                                     (A, B) ->
  +                                         A + B
  +                                 end, L, Acc)
                          end, FirstList, Rest),
       Count = length(Samples),
       Avgs = [X / Count || X <- Sums],
       list_to_stat(TS, Avgs).
   
   
  +cascade(Bucket, Prev, Period, Step) ->
  +    Node = node(),
  +    PrevTab = table_name(Node, Bucket, Prev),
  +    NextTab = table_name(Node, Bucket, Period),
  +    {atomic, ok} = mnesia:transaction(
  +                     fun () ->
  +                             case last_chunk(PrevTab, Step) of
  +                                 false -> ok;
  +                                 Avg ->
  +                                     mnesia:write(NextTab, Avg, write)
  +                             end
  +                     end).
  +
  +
   create_tables(Bucket) ->
       Node = node(),
  -    ?log_info("Creating tables for bucket ~p if they don't already exist.",
  -              [Bucket]),
       lists:foreach(
         fun ({Period, _, _}) ->
                 TableName = table_name(Node, Bucket, Period),
  +              ?log_info("Creating table ~p.", [TableName]),
                 case mnesia:create_table(TableName,
                                          [{disc_copies, [Node]},
                                           {record_name, stat_entry},
  
positive.code: 
  init(Bucket) ->
      create_tables(Bucket),
      ns_pubsub:subscribe(ns_stats_event),
      {ok, #state{bucket=Bucket}}.
  
  
negative.code:     {TS, L}.
  
  
  %% @doc Start the timers to cascade samples to the next resolution.
  start_cascade_timers([{Prev, _, _} | [{Next, Step, _} | _] = Rest]) ->
      timer:send_interval(200 * Step, {cascade, Prev, Next, Step}),
      start_cascade_timers(Rest);
  start_cascade_timers([_]) ->
      ok.
  
  
  %% @doc Start timers for various housekeeping tasks.
  start_timers() ->
      Archives = archives(),
      lists:foreach(
        fun ({Period, Step, Samples}) ->
                Interval = 100 * Step * Samples,  % Allow to go over by 10% of the
                                                  % total samples
                timer:send_interval(Interval, {truncate, Period, Samples})
        end, Archives),
      start_cascade_timers(Archives).
  
  
  %% @doc Generate a suitable name for the Mnesia table.
  table_name(Node, Bucket, Period) ->
  