Skip to content

Commit

Permalink
Merge pull request #138 from SiaFoundation/nate/add-5m-metrics
Browse files Browse the repository at this point in the history
Add 5m interval to metrics endpoint
  • Loading branch information
n8maninger committed Aug 7, 2023
2 parents b51e995 + 09a6612 commit 81c78ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func (a *api) handleGETPeriodMetrics(c jape.Context) {
if periods == 0 {
// if periods is 0 calculate the number of periods between start and now
switch interval {
case metrics.Interval5Minutes:
periods = int(time.Now().Truncate(5*time.Minute).Sub(start)/(5*time.Minute)) + 1
case metrics.Interval15Minutes:
periods = int(time.Now().Truncate(15*time.Minute).Sub(start)/(15*time.Minute)) + 1
case metrics.IntervalHourly:
Expand Down
2 changes: 2 additions & 0 deletions host/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (mm *MetricManager) Metrics(timestamp time.Time) (m Metrics, err error) {
// Normalize returns the normalized timestamp for the given interval.
func Normalize(timestamp time.Time, interval Interval) (time.Time, error) {
switch interval {
case Interval5Minutes:
return timestamp.Truncate(5 * time.Minute), nil
case Interval15Minutes:
return timestamp.Truncate(15 * time.Minute), nil
case IntervalHourly:
Expand Down
6 changes: 5 additions & 1 deletion host/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

// intervals at which metrics should be aggregated.
const (
Interval15Minutes Interval = iota
Interval5Minutes Interval = iota
Interval15Minutes
IntervalHourly
IntervalDaily
IntervalWeekly
Expand Down Expand Up @@ -113,6 +114,8 @@ type (
// String returns the interval as a string
func (i Interval) String() string {
switch i {
case Interval5Minutes:
return "5m"
case Interval15Minutes:
return "15m"
case IntervalHourly:
Expand All @@ -132,6 +135,7 @@ func (i Interval) String() string {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Interval) UnmarshalText(buf []byte) error {
values := map[string]Interval{
Interval5Minutes.String(): Interval5Minutes,
Interval15Minutes.String(): Interval15Minutes,
IntervalHourly.String(): IntervalHourly,
IntervalDaily.String(): IntervalDaily,
Expand Down
6 changes: 6 additions & 0 deletions persist/sqlite/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (s *Store) PeriodMetrics(start time.Time, n int, interval metrics.Interval)

var end time.Time
switch interval {
case metrics.Interval5Minutes:
end = start.Add(5 * time.Minute * time.Duration(n))
case metrics.Interval15Minutes:
end = start.Add(15 * time.Minute * time.Duration(n))
case metrics.IntervalHourly:
Expand Down Expand Up @@ -111,6 +113,8 @@ func (s *Store) PeriodMetrics(start time.Time, n int, interval metrics.Interval)
// normalize the stored timestamp to the locale and interval
timestamp = timestamp.In(start.Location())
switch interval {
case metrics.Interval5Minutes:
timestamp = timestamp.Truncate(5 * time.Minute)
case metrics.Interval15Minutes:
timestamp = timestamp.Truncate(15 * time.Minute)
case metrics.IntervalHourly:
Expand Down Expand Up @@ -155,6 +159,8 @@ func (s *Store) PeriodMetrics(start time.Time, n int, interval metrics.Interval)

// increment the current time by the interval
switch interval {
case metrics.Interval5Minutes:
current = current.Add(5 * time.Minute)
case metrics.Interval15Minutes:
current = current.Add(15 * time.Minute)
case metrics.IntervalHourly:
Expand Down

0 comments on commit 81c78ce

Please sign in to comment.