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
|
|
|
|
//
|
2020-03-09 18:17:55 +00:00
|
|
|
// https://github.com/statping/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-03-04 10:29:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2018-08-22 05:41:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func reorderServiceHandler(w http.ResponseWriter, r *http.Request) {
|
2018-11-29 16:26:42 +00:00
|
|
|
r.ParseForm()
|
2018-08-22 05:41:15 +00:00
|
|
|
var newOrder []*serviceOrder
|
2020-03-04 10:29:00 +00:00
|
|
|
if err := DecodeJSON(r, &newOrder); err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-22 05:41:15 +00:00
|
|
|
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
|
|
|
|
}
|
2018-09-06 05:28:35 +00:00
|
|
|
service.Order = s.Order
|
2020-03-04 10:29:00 +00:00
|
|
|
service.Update()
|
2018-08-22 05:41:15 +00:00
|
|
|
}
|
2019-03-02 01:33:41 +00:00
|
|
|
returnJson(newOrder, w, r)
|
2018-08-22 05:41:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
2020-03-06 22:18:06 +00:00
|
|
|
srv = srv.UpdateStats()
|
|
|
|
return *srv
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 03:39:40 +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 {
|
2018-11-21 03:39:40 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
if err := service.Create(); err != nil {
|
2018-11-21 03:39:40 +00:00
|
|
|
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)
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-03-10 05:24:35 +00:00
|
|
|
sendErrorJson(err, w, r, http.StatusNotFound)
|
2020-01-30 04:34:47 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
if err := DecodeJSON(r, &service); err != nil {
|
2020-03-10 05:24:35 +00:00
|
|
|
sendErrorJson(err, w, r, http.StatusBadRequest)
|
2020-01-30 04:34:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
err = service.Update()
|
2018-11-21 03:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 14:20:47 +00:00
|
|
|
go service.CheckService(true)
|
2018-11-21 03:39:40 +00:00
|
|
|
sendJsonAction(service, "update", w, r)
|
2018-10-07 22:38:56 +00:00
|
|
|
}
|
2018-10-06 03:35:53 +00:00
|
|
|
|
2019-04-30 20:57:29 +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)
|
2019-04-30 20:57:29 +00:00
|
|
|
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) {
|
2018-11-21 03:39:40 +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 {
|
2018-11-21 03:39:40 +00:00
|
|
|
sendErrorJson(errors.New("service data not found"), 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) {
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2018-11-21 03:39:40 +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)
|
2018-11-21 03:39:40 +00:00
|
|
|
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
|
|
|
|
}
|
2019-02-05 00:12:35 +00:00
|
|
|
|
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)
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-11-21 03:39:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
err = service.Delete()
|
2018-11-21 03:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2018-11-21 03:39:40 +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()
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
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{} {
|
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
|
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
|
|
|
}
|