2020-03-04 10:29:00 +00:00
|
|
|
package hits
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
2020-03-04 10:29:00 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ColumnIDInterfacer interface {
|
|
|
|
HitsColumnID() (string, int64)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Hitters struct {
|
|
|
|
db database.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Hitters) Db() database.Database {
|
|
|
|
return h.db
|
|
|
|
}
|
|
|
|
|
2020-03-06 09:33:46 +00:00
|
|
|
func (h Hitters) First() *Hit {
|
|
|
|
var hit Hit
|
|
|
|
h.db.Order("id ASC").Limit(1).Find(&hit)
|
|
|
|
return &hit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Hitters) Last() *Hit {
|
|
|
|
var hit Hit
|
|
|
|
h.db.Order("id DESC").Limit(1).Find(&hit)
|
|
|
|
return &hit
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
func (h Hitters) Since(t time.Time) []*Hit {
|
|
|
|
var hits []*Hit
|
|
|
|
h.db.Since(t).Find(&hits)
|
|
|
|
return hits
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Hitters) List() []*Hit {
|
|
|
|
var hits []*Hit
|
|
|
|
h.db.Find(&hits)
|
|
|
|
return hits
|
|
|
|
}
|
|
|
|
|
2020-03-06 09:33:46 +00:00
|
|
|
func (h Hitters) LastCount(amounts int) []*Hit {
|
2020-03-04 10:29:00 +00:00
|
|
|
var hits []*Hit
|
2020-03-06 09:33:46 +00:00
|
|
|
h.db.Order("id asc").Limit(amounts).Find(&hits)
|
2020-03-04 10:29:00 +00:00
|
|
|
return hits
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Hitters) Count() int {
|
|
|
|
var count int
|
|
|
|
h.db.Count(&count)
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Hitters) Sum() float64 {
|
|
|
|
result := struct {
|
|
|
|
amount float64
|
|
|
|
}{0}
|
|
|
|
|
|
|
|
h.db.Select("AVG(latency) as amount").Scan(&result).Debug()
|
|
|
|
return result.amount
|
|
|
|
}
|
|
|
|
|
2020-03-06 22:18:06 +00:00
|
|
|
func (h Hitters) Avg() int64 {
|
|
|
|
result := struct {
|
|
|
|
amount int64
|
|
|
|
}{0}
|
|
|
|
|
|
|
|
h.db.Select("AVG(latency) as amount").Scan(&result)
|
|
|
|
return result.amount
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
func AllHits(obj ColumnIDInterfacer) Hitters {
|
|
|
|
column, id := obj.HitsColumnID()
|
|
|
|
return Hitters{DB().Where(fmt.Sprintf("%s = ?", column), id)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HitsSince(t time.Time, obj ColumnIDInterfacer) Hitters {
|
|
|
|
column, id := obj.HitsColumnID()
|
|
|
|
timestamp := DB().FormatTime(t)
|
|
|
|
return Hitters{DB().Where(fmt.Sprintf("%s = ? AND created_at > ?", column), id, timestamp)}
|
|
|
|
}
|