package store import ( "encoding/json" "errors" "fmt" "strconv" "time" ) // MaxSpan is the longest window a request may ask for. const MaxSpan = 30 * 24 * time.Hour // DefaultRetention is the default and the maximum --retention. const DefaultRetention = 30 * 24 * time.Hour // minuteKeep is how far back minute buckets exist. const minuteKeep = 48 * time.Hour // RangeOrder lists the named windows. var RangeOrder = []string{"1h", "2h", "3h", "6h", "12h", "24h", "2d", "3d", "5d", "7d", "15d", "30d"} // BackgroundRanges are the windows a hub keeps fresh for every peer; any other // window is fetched on demand. var BackgroundRanges = []string{"1h", "24h", "7d", "30d"} // Range describes a time window: a named one relative to now, or a custom one // (From/To in unix seconds). type Range struct { Name string Dur time.Duration Step int64 // timeline bucket size in seconds From int64 // custom windows only To int64 Clipped bool } // stepFor picks the timeline granularity from the window length. func stepFor(d time.Duration) int64 { switch { case d <= 3*time.Hour: return 60 case d <= 6*time.Hour: return 120 case d <= 12*time.Hour: return 300 case d <= 48*time.Hour: return 900 } return 3600 } // Ranges are the named windows. var Ranges = func() map[string]Range { m := map[string]Range{} for _, n := range RangeOrder { d, _ := time.ParseDuration(n) if n[len(n)-1] == 'd' { k, _ := strconv.Atoi(n[:len(n)-1]) d = time.Duration(k) * 24 * time.Hour } m[n] = Range{Name: n, Dur: d, Step: stepFor(d)} } return m }() // ParseRange returns the named range, defaulting to 24h. func ParseRange(name string) Range { if r, ok := Ranges[name]; ok { return r } return Ranges["24h"] } // ParseRangeStrict returns the named range or false. func ParseRangeStrict(name string) (Range, bool) { r, ok := Ranges[name] return r, ok } // Custom reports whether the window is an absolute one. func (r Range) Custom() bool { return r.To != 0 } // Key identifies the window (cache keys, peer requests). func (r Range) Key() string { if r.Custom() { return fmt.Sprintf("custom:%d-%d", r.From, r.To) } return r.Name } func (r Range) bounds(now int64) (int64, int64) { if r.Custom() { return r.From, r.To } return now - int64(r.Dur/time.Second), now } // Bounds returns the window as unix seconds. func (r Range) Bounds(now int64) (from, to int64) { return r.bounds(now) } type grid struct { Start, Step int64 N int } func (g grid) minute() bool { return g.Step < 3600 } func (r Range) grid(now int64) grid { from, to := r.bounds(now) if !r.Custom() { step := r.Step nb := int(int64(r.Dur/time.Second) / step) return grid{Start: (to/step - int64(nb) + 1) * step, Step: step, N: nb} } step := stepFor(time.Duration(to-from) * time.Second) if step < 3600 && from < now-int64(minuteKeep/time.Second) { step = 3600 // minute buckets no longer exist that far back } start := from / step * step nb := int((to - start + step - 1) / step) if nb < 1 { nb = 1 } return grid{Start: start, Step: step, N: nb} } // RangeInfo is the "range" object of the responses. type RangeInfo struct { Name string `json:"name"` From int64 `json:"from"` To int64 `json:"to"` Step int64 `json:"step"` Clipped bool `json:"clipped"` } // UnmarshalJSON accepts the object and the legacy string form of old peers. func (ri *RangeInfo) UnmarshalJSON(b []byte) error { var s string if json.Unmarshal(b, &s) == nil { *ri = RangeInfo{Name: s} return nil } type plain RangeInfo var p plain if err := json.Unmarshal(b, &p); err != nil { return err } *ri = RangeInfo(p) return nil } // Info describes the window at time now. func (r Range) Info(now int64) RangeInfo { from, to := r.bounds(now) return RangeInfo{Name: r.Name, From: from, To: to, Step: r.grid(now).Step, Clipped: r.Clipped} } // ErrRange is a bad range request (HTTP 400). var ErrRange = errors.New("invalid range") // ResolveRange validates the range parameters of a request: a named window, or // from/to (unix seconds; to defaults to now; from/to win over a name). The // result is clipped to the retention and never longer than MaxSpan. func (s *Store) ResolveRange(name, fromS, toS string) (Range, error) { if fromS == "" && toS == "" { if name == "" { return Ranges["24h"], nil } r, ok := Ranges[name] if !ok { return Range{}, fmt.Errorf("%w: unknown range %q", ErrRange, name) } return r, nil } if fromS == "" { return Range{}, fmt.Errorf("%w: to needs from", ErrRange) } now := s.now().Unix() from, err := strconv.ParseInt(fromS, 10, 64) if err != nil || from < 0 || from > 1<<33 { return Range{}, fmt.Errorf("%w: bad from", ErrRange) } to := now if toS != "" { if to, err = strconv.ParseInt(toS, 10, 64); err != nil || to < 0 || to > 1<<33 { return Range{}, fmt.Errorf("%w: bad to", ErrRange) } } if to <= from { return Range{}, fmt.Errorf("%w: to must be after from", ErrRange) } if to-from > int64(MaxSpan/time.Second) { return Range{}, fmt.Errorf("%w: at most 30 days", ErrRange) } if to > now { to = now if to <= from { return Range{}, fmt.Errorf("%w: from is in the future", ErrRange) } } rg := Range{Name: "custom", From: from, To: to} if cut := now - int64(s.cfg.Retention/time.Second); from < cut { rg.From, rg.Clipped = cut, true if rg.To <= rg.From { rg.To = rg.From + 60 } } rg.Dur = time.Duration(rg.To-rg.From) * time.Second rg.Step = stepFor(rg.Dur) return rg, nil } // Retention is the configured retention. func (s *Store) Retention() time.Duration { return s.cfg.Retention } // Now is the store clock. func (s *Store) Now() time.Time { return s.now() }