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)
|
selector := Dbtimestamp(group, column)
|
||||||
if CoreApp.DbConnection == "postgres" {
|
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))
|
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 {
|
} 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.
|
// Sum returns the added value Latency for all of the services successful hits.
|
||||||
func (s *Service) Sum() (float64, error) {
|
func (s *Service) Sum() float64 {
|
||||||
var amount float64
|
var sum float64
|
||||||
hits, err := s.Hits()
|
rows, _ := hitsDB().Where("service = ?", s.Id).Select("sum(latency) as total").Rows()
|
||||||
if err != nil {
|
for rows.Next() {
|
||||||
utils.Log(2, err)
|
rows.Scan(&sum)
|
||||||
}
|
}
|
||||||
for _, h := range hits {
|
return sum
|
||||||
amount += h.Latency
|
|
||||||
}
|
|
||||||
return amount, err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ func (s *Service) AvgTime() float64 {
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
return float64(0)
|
return float64(0)
|
||||||
}
|
}
|
||||||
sum, _ := s.Sum()
|
sum := s.Sum()
|
||||||
avg := sum / float64(total) * 100
|
avg := sum / float64(total) * 100
|
||||||
amount := fmt.Sprintf("%0.0f", avg*10)
|
amount := fmt.Sprintf("%0.0f", avg*10)
|
||||||
val, _ := strconv.ParseFloat(amount, 10)
|
val, _ := strconv.ParseFloat(amount, 10)
|
||||||
|
|
Loading…
Reference in New Issue