func (ts *timeSeries) mergeValue(observation Observable, t time.Time) {
	for _, level := range ts.levels {
		index := (ts.numBuckets - 1) - int(level.end.Sub(t)/level.size)
		if 0 <= index && index < ts.numBuckets {
			bucketNumber := (level.oldest + index) % ts.numBuckets
			if level.buckets[bucketNumber] == nil {
				level.buckets[bucketNumber] = level.provider()
			}
			level.buckets[bucketNumber].Add(observation)
		}
	}
	ts.total.Add(observation)
}
mergeValue inserts the observation at the specified time in the past into all levels.
package timeseries  
import (
	"fmt"
	"log"
	"time"
)
const (
	timeSeriesNumBuckets       = 64
	minuteHourSeriesNumBuckets = 60
)
var timeSeriesResolutions = []time.Duration{
	1 * time.Second,
	10 * time.Second,
	1 * time.Minute,
	10 * time.Minute,
	1 * time.Hour,
	6 * time.Hour,
	24 * time.Hour,           
	7 * 24 * time.Hour,       
	4 * 7 * 24 * time.Hour,   
	16 * 7 * 24 * time.Hour,  
}
var minuteHourSeriesResolutions = []time.Duration{
	1 * time.Second,
	1 * time.Minute,
}
type Observable interface {
	Multiply(ratio float64)     
	Add(other Observable)       
	Clear()                     
	CopyFrom(other Observable)  
}
type Float float64
func NewFloat() Observable {
	f := Float(0)
	return &f
}
func (f *Float) String() string { return fmt.Sprintf("%g", f.Value()) }
func (f *Float) Value() float64 { return float64(*f) }
func (f *Float) Multiply(ratio float64) { *f *= Float(ratio) }
func (f *Float) Add(other Observable) {
	o := other.(*Float)
	*f += *o
}
func (f *Float) Clear() { *f = 0 }
func (f *Float) CopyFrom(other Observable) {
	o := other.(*Float)
	*f = *o
}
type Clock interface {
	Time() time.Time
}
type defaultClock int
var defaultClockInstance defaultClock
func (defaultClock) Time() time.Time { return time.Now() }
type tsLevel struct {
	oldest   int                
	newest   int                
	end      time.Time          
	size     time.Duration      
	buckets  []Observable       
	provider func() Observable  
}
func (l *tsLevel) Clear() {
	l.oldest = 0
	l.newest = len(l.buckets) - 1
	l.end = time.Time{}
	for i := range l.buckets {
		if l.buckets[i] != nil {
			l.buckets[i].Clear()
			l.buckets[i] = nil
		}
	}
}
func (l *tsLevel) InitLevel(size time.Duration, numBuckets int, f func() Observable) {
	l.size = size
	l.provider = f
	l.buckets = make([]Observable, numBuckets)
}
type timeSeries struct {
	provider    func() Observable  
	numBuckets  int                
	levels      []*tsLevel         
	lastAdd     time.Time          
	total       Observable         
	clock       Clock              
	pending     Observable         
	pendingTime time.Time          
	dirty       bool               
}
func (ts *timeSeries) init(resolutions []time.Duration, f func() Observable, numBuckets int, clock Clock) {
	ts.provider = f
	ts.numBuckets = numBuckets
	ts.clock = clock
	ts.levels = make([]*tsLevel, len(resolutions))
	for i := range resolutions {
		if i > 0 && resolutions[i-1] >= resolutions[i] {
			log.Print("timeseries: resolutions must be monotonically increasing")
			break
		}
		newLevel := new(tsLevel)
		newLevel.InitLevel(resolutions[i], ts.numBuckets, ts.provider)
		ts.levels[i] = newLevel
	}
	ts.Clear()
}
func (ts *timeSeries) Clear() {
	ts.lastAdd = time.Time{}
	ts.total = ts.resetObservation(ts.total)
	ts.pending = ts.resetObservation(ts.pending)
	ts.pendingTime = time.Time{}
	ts.dirty = false
	for i := range ts.levels {
		ts.levels[i].Clear()
	}
}
func (ts *timeSeries) Add(observation Observable) {
	ts.AddWithTime(observation, ts.clock.Time())
}
func (ts *timeSeries) AddWithTime(observation Observable, t time.Time) {
	smallBucketDuration := ts.levels[0].size
	if t.After(ts.lastAdd) {
		ts.lastAdd = t
	}
	if t.After(ts.pendingTime) {
		ts.advance(t)
		ts.mergePendingUpdates()
		ts.pendingTime = ts.levels[0].end
		ts.pending.CopyFrom(observation)
		ts.dirty = true
	} else if t.After(ts.pendingTime.Add(-1 * smallBucketDuration)) {
		ts.pending.Add(observation)
		ts.dirty = true
	} else {
		ts.mergeValue(observation, t)
	}
}