statping/handlers/services.go

233 lines
5.4 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// 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 handlers
import (
"github.com/gorilla/mux"
2020-02-24 05:53:15 +00:00
"github.com/hunterlong/statping/database"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/failures"
"github.com/hunterlong/statping/types/hits"
"github.com/hunterlong/statping/types/services"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/utils"
2020-03-04 10:29:00 +00:00
"github.com/pkg/errors"
2018-06-30 00:57:05 +00:00
"net/http"
)
type serviceOrder struct {
Id int64 `json:"service"`
Order int `json:"order"`
}
2020-03-04 10:29:00 +00:00
func serviceByID(r *http.Request) (*services.Service, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
servicer, err := services.Find(id)
if err != nil {
return nil, errors.Errorf("service %d not found", id)
}
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 {
sendErrorJson(errors.Errorf("service %d not found", s.Id), w, r)
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-03-06 22:18:06 +00:00
srv, err := serviceByID(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-04 10:29:00 +00:00
2020-02-25 13:18:29 +00:00
sendJsonAction(service, "create", w, r)
}
2020-03-04 10:29:00 +00:00
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
2020-01-30 04:34:47 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
if err := DecodeJSON(r, &service); err != nil {
2020-01-30 04:34:47 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
err = service.Update()
if 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
func apiServiceRunningHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
service, err := serviceByID(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
if service.IsRunning() {
service.Close()
} else {
service.Start()
}
sendJsonAction(service, "running", w, r)
}
2020-02-25 07:41:28 +00:00
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
service, err := services.Find(utils.ToInt(vars["id"]))
2020-02-24 05:53:15 +00:00
if err != nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
2020-03-04 10:29:00 +00:00
groupQuery := database.ParseQueries(r, service.AllHits())
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) {
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
service, err := services.Find(utils.ToInt(vars["id"]))
2020-02-24 05:53:15 +00:00
if err != nil {
2020-02-21 05:36:23 +00:00
sendErrorJson(errors.New("service data not found"), w, r)
return
}
2020-02-22 04:40:05 +00:00
2020-03-04 10:29:00 +00:00
groupQuery := database.ParseQueries(r, service.AllFailures())
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-03-04 10:29:00 +00:00
service, err := serviceByID(r)
2020-02-24 05:53:15 +00:00
if err != nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
2020-02-26 05:38:03 +00:00
2020-03-04 10:29:00 +00:00
groupQuery := database.ParseQueries(r, service.AllHits())
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)
}
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
service, err := serviceByID(r)
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-06 03:03:18 +00:00
return services.AllInOrder()
}
2020-02-25 07:41:28 +00:00
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
service, err := serviceByID(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
}
2020-03-04 10:29:00 +00:00
err = service.DeleteFailures()
2020-02-25 13:18:29 +00:00
if err != nil {
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{} {
2019-01-10 16:47:01 +00:00
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
service, err := services.Find(utils.ToInt(vars["id"]))
2020-02-24 05:53:15 +00:00
if err != nil {
2020-01-20 05:02:15 +00:00
return errors.New("service not found")
2019-01-10 16:47:01 +00:00
}
2020-02-24 05:53:15 +00:00
2020-03-04 10:29:00 +00:00
var fails []*failures.Failure
database.ParseQueries(r, service.AllFailures()).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{} {
2019-01-10 16:47:01 +00:00
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
service, err := services.Find(utils.ToInt(vars["id"]))
2019-01-10 16:47:01 +00:00
if err != nil {
2020-02-24 05:53:15 +00:00
return errors.New("service not found")
2019-01-10 16:47:01 +00:00
}
2020-02-24 05:53:15 +00:00
2020-03-04 10:29:00 +00:00
var hts []*hits.Hit
database.ParseQueries(r, service.AllHits()).Find(&hts)
return hts
2019-12-18 11:36:29 +00:00
}