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-08-20 07:20:05 +00:00
|
|
|
type Hit struct {
|
|
|
|
*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-08-20 07:20:05 +00:00
|
|
|
func (s *Service) CreateHit(h *types.Hit) (int64, error) {
|
|
|
|
h.CreatedAt = time.Now()
|
|
|
|
h.Service = s.Id
|
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-08-20 07:20:05 +00:00
|
|
|
func (s *Service) Hits() ([]*Hit, error) {
|
|
|
|
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-08-19 00:37:00 +00:00
|
|
|
func (s *Service) LimitedHits() ([]*Hit, error) {
|
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-08-20 07:20:05 +00:00
|
|
|
func (s *Service) SelectHitsGroupBy(group string) ([]*Hit, error) {
|
|
|
|
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-08-19 00:37:00 +00:00
|
|
|
func (s *Service) TotalHits() (uint64, error) {
|
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
|
|
|
}
|