mirror of https://github.com/statping/statping
fast latency calculation
parent
7aac686c84
commit
61ed0aeb11
|
@ -93,9 +93,9 @@ func (s *Service) HitsBetween(t1, t2 time.Time, group string, column string) *go
|
|||
selector := Dbtimestamp(group, column)
|
||||
if CoreApp.DbConnection == "postgres" {
|
||||
timeQuery := fmt.Sprintf("service = %v AND created_at BETWEEN '%v.000000' AND '%v.000000'", s.Id, t1.UTC().Format(types.POSTGRES_TIME), t2.UTC().Format(types.POSTGRES_TIME))
|
||||
return DbSession.Model(&types.Hit{}).Select(selector).Where(timeQuery)
|
||||
return hitsDB().Select(selector).Where(timeQuery)
|
||||
} else {
|
||||
return DbSession.Model(&types.Hit{}).Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME_DAY), t2.UTC().Format(types.TIME_DAY))
|
||||
return hitsDB().Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME_DAY), t2.UTC().Format(types.TIME_DAY))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
15
core/hits.go
15
core/hits.go
|
@ -84,14 +84,11 @@ func (s *Service) TotalHitsSince(ago time.Time) (uint64, error) {
|
|||
}
|
||||
|
||||
// Sum returns the added value Latency for all of the services successful hits.
|
||||
func (s *Service) Sum() (float64, error) {
|
||||
var amount float64
|
||||
hits, err := s.Hits()
|
||||
if err != nil {
|
||||
utils.Log(2, err)
|
||||
func (s *Service) Sum() float64 {
|
||||
var sum float64
|
||||
rows, _ := hitsDB().Where("service = ?", s.Id).Select("sum(latency) as total").Rows()
|
||||
for rows.Next() {
|
||||
rows.Scan(&sum)
|
||||
}
|
||||
for _, h := range hits {
|
||||
amount += h.Latency
|
||||
}
|
||||
return amount, err
|
||||
return sum
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ func (s *Service) AvgTime() float64 {
|
|||
if total == 0 {
|
||||
return float64(0)
|
||||
}
|
||||
sum, _ := s.Sum()
|
||||
sum := s.Sum()
|
||||
avg := sum / float64(total) * 100
|
||||
amount := fmt.Sprintf("%0.0f", avg*10)
|
||||
val, _ := strconv.ParseFloat(amount, 10)
|
||||
|
|
Loading…
Reference in New Issue