statping/handlers/services.go

320 lines
6.5 KiB
Go
Raw Permalink Normal View History

2018-06-30 00:57:05 +00:00
package handlers
import (
"github.com/gorilla/mux"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/database"
2020-04-17 03:21:17 +00:00
"github.com/statping/statping/types/errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/hits"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
)
type serviceOrder struct {
Id int64 `json:"service"`
Order int `json:"order"`
}
2020-04-16 17:55:47 +00:00
func findService(r *http.Request) (*services.Service, error) {
2020-03-04 10:29:00 +00:00
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
servicer, err := services.Find(id)
if err != nil {
2020-04-17 03:21:17 +00:00
return nil, err
}
2020-05-02 00:19:54 +00:00
if !servicer.Public.Bool && !IsReadAuthenticated(r) {
2020-04-17 03:21:17 +00:00
return nil, errors.NotAuthenticated
2020-03-04 10:29:00 +00:00
}
return servicer, nil
}
func reorderServiceHandler(w http.ResponseWriter, r *http.Request) {
2018-11-29 16:26:42 +00:00
r.ParseForm()
var newOrder []*serviceOrder
2020-03-04 10:29:00 +00:00
if err := DecodeJSON(r, &newOrder); err != nil {
sendErrorJson(err, w, r)
return
}
for _, s := range newOrder {
2020-03-04 10:29:00 +00:00
service, err := services.Find(s.Id)
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
2020-03-04 10:29:00 +00:00
return
}
service.Order = s.Order
2020-03-04 10:29:00 +00:00
service.Update()
}
returnJson(newOrder, w, r)
}
2020-01-20 05:02:15 +00:00
func apiServiceHandler(r *http.Request) interface{} {
2020-04-16 17:55:47 +00:00
srv, err := findService(r)
2020-03-04 10:29:00 +00:00
if err != nil {
return err
}
2020-03-06 22:18:06 +00:00
srv = srv.UpdateStats()
return *srv
2018-06-30 00:57:05 +00:00
}
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
var service *services.Service
if err := DecodeJSON(r, &service); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
if err := service.Create(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-09 15:15:15 +00:00
go services.ServiceCheckQueue(service, true)
2020-03-04 10:29:00 +00:00
2020-02-25 13:18:29 +00:00
sendJsonAction(service, "create", w, r)
}
type servicePatchReq struct {
Online bool `json:"online"`
Issue string `json:"issue,omitempty"`
Latency int64 `json:"latency,omitempty"`
}
func apiServicePatchHandler(w http.ResponseWriter, r *http.Request) {
2020-04-16 17:55:47 +00:00
service, err := findService(r)
2020-01-30 04:34:47 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
2020-01-30 04:34:47 +00:00
return
}
var req servicePatchReq
if err := DecodeJSON(r, &req); err != nil {
sendErrorJson(err, w, r)
return
}
service.Online = req.Online
service.Latency = req.Latency
issueDefault := "Service was triggered to be offline"
if req.Issue != "" {
issueDefault = req.Issue
}
if !req.Online {
services.RecordFailure(service, issueDefault, "trigger")
} else {
services.RecordSuccess(service)
}
if err := service.Update(); err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
2020-01-30 04:34:47 +00:00
return
}
sendJsonAction(service, "update", w, r)
}
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
if err := DecodeJSON(r, &service); err != nil {
sendErrorJson(err, w, r)
return
}
if err := service.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 14:20:47 +00:00
go service.CheckService(true)
sendJsonAction(service, "update", w, r)
2018-10-07 22:38:56 +00:00
}
2018-10-06 03:35:53 +00:00
2020-02-25 07:41:28 +00:00
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
2020-04-17 03:21:17 +00:00
service, err := findService(r)
2020-02-24 05:53:15 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-11 04:37:52 +00:00
groupQuery, err := database.ParseQueries(r, service.AllHits())
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-02-24 05:53:15 +00:00
2020-03-06 09:33:46 +00:00
objs, err := groupQuery.GraphData(database.ByAverage("latency", 1000))
2020-02-26 05:38:03 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(objs, w, r)
2020-02-21 05:36:23 +00:00
}
func apiServiceFailureDataHandler(w http.ResponseWriter, r *http.Request) {
2020-04-17 03:21:17 +00:00
service, err := findService(r)
2020-02-24 05:53:15 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
2020-02-21 05:36:23 +00:00
return
}
2020-02-22 04:40:05 +00:00
2020-03-11 04:37:52 +00:00
groupQuery, err := database.ParseQueries(r, service.AllFailures())
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-02-26 05:38:03 +00:00
objs, err := groupQuery.GraphData(database.ByCount)
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(objs, w, r)
2020-02-22 04:40:05 +00:00
}
func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
2020-04-16 17:55:47 +00:00
service, err := findService(r)
2020-02-24 05:53:15 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
return
}
2020-02-26 05:38:03 +00:00
2020-03-11 04:37:52 +00:00
groupQuery, err := database.ParseQueries(r, service.AllHits())
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-06 09:33:46 +00:00
objs, err := groupQuery.GraphData(database.ByAverage("ping_time", 1000))
2020-02-26 05:38:03 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(objs, w, r)
}
2020-04-13 11:17:09 +00:00
func apiServiceTimeDataHandler(w http.ResponseWriter, r *http.Request) {
2020-04-16 17:55:47 +00:00
service, err := findService(r)
2020-04-13 11:17:09 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
sendErrorJson(err, w, r)
2020-04-13 11:17:09 +00:00
return
}
2020-04-14 16:08:50 +00:00
groupHits, err := database.ParseQueries(r, service.AllHits())
if err != nil {
sendErrorJson(err, w, r)
return
2020-04-13 11:17:09 +00:00
}
2020-04-14 16:08:50 +00:00
groupFailures, err := database.ParseQueries(r, service.AllFailures())
if err != nil {
sendErrorJson(err, w, r)
return
2020-04-13 11:17:09 +00:00
}
2020-04-14 16:08:50 +00:00
var allFailures []*failures.Failure
var allHits []*hits.Hit
2020-04-13 11:17:09 +00:00
2020-04-14 16:08:50 +00:00
if err := groupHits.Find(&allHits); err != nil {
sendErrorJson(err, w, r)
return
2020-04-13 11:17:09 +00:00
}
2020-04-14 16:08:50 +00:00
if err := groupFailures.Find(&allFailures); err != nil {
sendErrorJson(err, w, r)
return
2020-04-13 11:17:09 +00:00
}
2020-04-14 16:08:50 +00:00
uptimeData, err := service.UptimeData(allHits, allFailures)
if err != nil {
sendErrorJson(err, w, r)
return
2020-04-13 11:17:09 +00:00
}
2020-04-14 16:08:50 +00:00
returnJson(uptimeData, w, r)
2020-04-13 11:17:09 +00:00
}
2020-08-25 20:38:47 +00:00
func apiServiceHitsDeleteHandler(w http.ResponseWriter, r *http.Request) {
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
if err := service.AllHits().DeleteAll(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(service, "delete", w, r)
}
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
2020-04-16 17:55:47 +00:00
service, err := findService(r)
2020-03-04 10:29:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
err = service.Delete()
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
sendJsonAction(service, "delete", w, r)
2018-10-03 10:47:32 +00:00
}
2020-01-20 05:02:15 +00:00
func apiAllServicesHandler(r *http.Request) interface{} {
2020-03-29 01:21:32 +00:00
var srvs []services.Service
for _, v := range services.AllInOrder() {
2020-05-02 00:53:35 +00:00
if !v.Public.Bool && !IsUser(r) {
2020-03-29 01:21:32 +00:00
continue
}
srvs = append(srvs, v)
}
return srvs
}
2020-02-25 07:41:28 +00:00
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
2020-04-16 17:55:47 +00:00
service, err := findService(r)
2020-02-25 13:18:29 +00:00
if err != nil {
2020-03-04 10:29:00 +00:00
sendErrorJson(err, w, r)
2019-02-12 19:38:36 +00:00
return
}
if err := service.AllFailures().DeleteAll(); err != nil {
2020-02-25 13:18:29 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
sendJsonAction(service, "delete_failures", w, r)
2018-06-30 00:57:05 +00:00
}
2019-01-10 16:47:01 +00:00
2020-01-20 05:02:15 +00:00
func apiServiceFailuresHandler(r *http.Request) interface{} {
2020-04-17 03:21:17 +00:00
service, err := findService(r)
2020-02-24 05:53:15 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
return err
2019-01-10 16:47:01 +00:00
}
2020-03-04 10:29:00 +00:00
var fails []*failures.Failure
2020-03-11 04:37:52 +00:00
query, err := database.ParseQueries(r, service.AllFailures())
if err != nil {
return err
}
query.Find(&fails)
2020-01-26 21:01:43 +00:00
return fails
2019-01-10 16:47:01 +00:00
}
2020-01-20 05:02:15 +00:00
func apiServiceHitsHandler(r *http.Request) interface{} {
2020-04-17 03:21:17 +00:00
service, err := findService(r)
2019-01-10 16:47:01 +00:00
if err != nil {
2020-04-17 03:21:17 +00:00
return err
2019-01-10 16:47:01 +00:00
}
2020-03-04 10:29:00 +00:00
var hts []*hits.Hit
2020-03-11 04:37:52 +00:00
query, err := database.ParseQueries(r, service.AllHits())
if err != nil {
return err
}
query.Find(&hts)
2020-03-04 10:29:00 +00:00
return hts
2019-12-18 11:36:29 +00:00
}