statping/core/hits.go

94 lines
2.2 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package core
2018-06-10 01:31:13 +00:00
2018-06-22 04:02:57 +00:00
import (
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
2018-06-22 04:02:57 +00:00
"time"
"upper.io/db.v3"
)
2018-06-10 01:31:13 +00:00
2018-06-30 00:57:05 +00:00
type Hit types.Hit
2018-06-10 01:31:13 +00:00
2018-06-22 04:02:57 +00:00
func hitCol() db.Collection {
2018-06-30 00:57:05 +00:00
return DbSession.Collection("hits")
2018-06-22 04:02:57 +00:00
}
2018-07-14 02:37:39 +00:00
func CreateServiceHit(s *types.Service, d HitData) (int64, error) {
2018-06-15 04:30:10 +00:00
h := Hit{
Service: s.Id,
Latency: d.Latency,
CreatedAt: time.Now(),
2018-06-11 03:41:02 +00:00
}
2018-06-22 04:02:57 +00:00
uuid, err := hitCol().Insert(h)
2018-06-15 04:30:10 +00:00
if uuid == nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, err)
2018-06-15 04:30:10 +00:00
return 0, err
2018-06-11 03:41:02 +00:00
}
2018-06-15 04:30:10 +00:00
return uuid.(int64), err
2018-06-11 03:41:02 +00:00
}
2018-07-14 02:37:39 +00:00
func (ser *Service) Hits() ([]Hit, error) {
s := ser.ToService()
2018-06-15 04:30:10 +00:00
var hits []Hit
2018-06-22 04:02:57 +00:00
col := hitCol().Find("service", s.Id).OrderBy("-id")
err := col.All(&hits)
return hits, err
}
2018-07-14 02:37:39 +00:00
func (ser *Service) LimitedHits() ([]*Hit, error) {
s := ser.ToService()
2018-06-24 11:51:07 +00:00
var hits []*Hit
col := hitCol().Find("service", s.Id).OrderBy("-id").Limit(1024)
2018-06-15 04:30:10 +00:00
err := col.All(&hits)
2018-06-24 11:51:07 +00:00
return reverseHits(hits), err
}
func reverseHits(input []*Hit) []*Hit {
if len(input) == 0 {
return input
}
return append(reverseHits(input[1:]), input[0])
2018-06-10 01:31:13 +00:00
}
2018-07-14 02:37:39 +00:00
func (ser *Service) SelectHitsGroupBy(group string) ([]Hit, error) {
s := ser.ToService()
2018-06-15 04:30:10 +00:00
var hits []Hit
2018-06-22 04:02:57 +00:00
col := hitCol().Find("service", s.Id)
2018-06-15 04:30:10 +00:00
err := col.All(&hits)
return hits, err
2018-06-10 01:31:13 +00:00
}
2018-07-14 02:37:39 +00:00
func (ser *Service) TotalHits() (uint64, error) {
s := ser.ToService()
2018-06-22 04:02:57 +00:00
col := hitCol().Find("service", s.Id)
2018-06-15 04:30:10 +00:00
amount, err := col.Count()
return amount, err
}
func (s *Service) Sum() (float64, error) {
2018-06-10 01:31:13 +00:00
var amount float64
2018-06-15 04:30:10 +00:00
hits, err := s.Hits()
2018-06-30 00:57:05 +00:00
if err != nil {
utils.Log(2, err)
}
2018-06-15 04:30:10 +00:00
for _, h := range hits {
amount += h.Latency
}
return amount, err
2018-06-10 01:31:13 +00:00
}