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 (
|
2018-08-22 05:41:15 +00:00
|
|
|
"encoding/json"
|
2018-11-21 03:39:40 +00:00
|
|
|
"errors"
|
2018-06-30 00:57:05 +00:00
|
|
|
"github.com/gorilla/mux"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
2020-02-24 05:53:15 +00:00
|
|
|
"github.com/hunterlong/statping/database"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
|
|
|
"github.com/hunterlong/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"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
2018-11-29 16:26:42 +00:00
|
|
|
decoder.Decode(&newOrder)
|
2018-08-22 05:41:15 +00:00
|
|
|
for _, s := range newOrder {
|
2020-02-25 13:18:29 +00:00
|
|
|
service := core.SelectService(s.Id).Model()
|
2018-09-06 05:28:35 +00:00
|
|
|
service.Order = s.Order
|
2020-02-25 13:18:29 +00:00
|
|
|
database.Update(service)
|
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{} {
|
2018-06-30 00:57:05 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-02-26 05:38:03 +00:00
|
|
|
servicer := core.SelectService(utils.ToInt(vars["id"]))
|
2018-11-21 03:39:40 +00:00
|
|
|
if servicer == nil {
|
2020-01-20 05:02:15 +00:00
|
|
|
return errors.New("service not found")
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
return servicer.Service
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 03:39:40 +00:00
|
|
|
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var service *types.Service
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&service)
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
_, err = database.Create(service)
|
2018-11-21 03:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
sendJsonAction(service, "create", w, r)
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 04:34:47 +00:00
|
|
|
func apiTestServiceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var service *types.Service
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&service)
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:18:29 +00:00
|
|
|
_, err = database.Create(service)
|
2020-01-30 04:34:47 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
sendJsonAction(service, "create", w, r)
|
2020-01-30 04:34:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 03:39:40 +00:00
|
|
|
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-30 00:57:05 +00:00
|
|
|
vars := mux.Vars(r)
|
2018-12-06 19:03:55 +00:00
|
|
|
service := core.SelectService(utils.ToInt(vars["id"]))
|
|
|
|
if service == nil {
|
2018-11-21 03:39:40 +00:00
|
|
|
sendErrorJson(errors.New("service not found"), w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
decoder.Decode(&service)
|
2020-02-25 13:18:29 +00:00
|
|
|
err := database.Update(service)
|
2018-11-21 03:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
go core.CheckService(service, 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) {
|
|
|
|
vars := mux.Vars(r)
|
2020-02-25 13:18:29 +00:00
|
|
|
srv := core.SelectService(utils.ToInt(vars["id"]))
|
|
|
|
service := srv.Model()
|
2019-04-30 20:57:29 +00:00
|
|
|
if service == nil {
|
|
|
|
sendErrorJson(errors.New("service not found"), 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) {
|
2018-11-21 03:39:40 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-02-24 05:53:15 +00:00
|
|
|
service, err := database.Service(utils.ToInt(vars["id"]))
|
|
|
|
if err != nil {
|
2018-11-21 03:39:40 +00:00
|
|
|
sendErrorJson(errors.New("service data not found"), w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
groupQuery := database.ParseQueries(r, service.Hits())
|
2020-02-24 05:53:15 +00:00
|
|
|
|
2020-02-26 05:38:03 +00:00
|
|
|
objs, err := groupQuery.GraphData(database.ByAverage("latency"))
|
|
|
|
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-02-24 05:53:15 +00:00
|
|
|
service, err := database.Service(utils.ToInt(vars["id"]))
|
|
|
|
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-02-26 05:38:03 +00:00
|
|
|
groupQuery := database.ParseQueries(r, service.Failures())
|
|
|
|
|
|
|
|
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) {
|
|
|
|
vars := mux.Vars(r)
|
2020-02-24 05:53:15 +00:00
|
|
|
service, err := database.Service(utils.ToInt(vars["id"]))
|
|
|
|
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-02-25 07:41:28 +00:00
|
|
|
groupQuery := database.ParseQueries(r, service.Hits())
|
2019-02-05 00:12:35 +00:00
|
|
|
|
2020-02-26 05:38:03 +00:00
|
|
|
objs, err := groupQuery.GraphData(database.ByAverage("ping_time"))
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
returnJson(objs, w, r)
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 12:02:13 +00:00
|
|
|
type dataXy struct {
|
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type dataXyMonth struct {
|
2019-02-01 04:48:18 +00:00
|
|
|
Date string `json:"date"`
|
2019-01-29 12:02:13 +00:00
|
|
|
Data []*dataXy `json:"data"`
|
|
|
|
}
|
|
|
|
|
2018-11-21 03:39:40 +00:00
|
|
|
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-07 22:38:56 +00:00
|
|
|
vars := mux.Vars(r)
|
2018-11-21 08:45:55 +00:00
|
|
|
service := core.SelectService(utils.ToInt(vars["id"]))
|
2018-11-21 03:39:40 +00:00
|
|
|
if service == nil {
|
|
|
|
sendErrorJson(errors.New("service not found"), w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
err := database.Delete(service)
|
2018-11-21 03:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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{} {
|
2018-11-21 03:39:40 +00:00
|
|
|
services := core.Services()
|
2020-01-20 05:02:15 +00:00
|
|
|
return joinServices(services)
|
2018-11-21 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 21:12:35 +00:00
|
|
|
func joinServices(srvs map[int64]*core.Service) []*types.Service {
|
2020-01-20 05:02:15 +00:00
|
|
|
var services []*types.Service
|
2020-01-15 08:10:59 +00:00
|
|
|
for _, v := range srvs {
|
2020-02-26 05:38:03 +00:00
|
|
|
v.UpdateStats()
|
|
|
|
services = append(services, v.Service)
|
2020-01-15 08:10:59 +00:00
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-03 10:47:32 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-02-25 13:18:29 +00:00
|
|
|
srv, err := database.Service(utils.ToInt(vars["id"]))
|
|
|
|
if err != nil {
|
2019-02-12 19:38:36 +00:00
|
|
|
sendErrorJson(errors.New("service not found"), w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-25 13:18:29 +00:00
|
|
|
err = srv.Failures().DeleteAll()
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJsonAction(srv.Model(), "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-02-24 05:53:15 +00:00
|
|
|
|
|
|
|
service, err := database.Service(utils.ToInt(vars["id"]))
|
|
|
|
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-02-26 16:56:38 +00:00
|
|
|
var fails []*types.Failure
|
2020-02-25 07:41:28 +00:00
|
|
|
database.ParseQueries(r, service.Failures()).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-02-24 05:53:15 +00:00
|
|
|
service, err := database.Service(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
|
|
|
|
|
|
|
var hits []types.Hit
|
2020-02-25 07:41:28 +00:00
|
|
|
database.ParseQueries(r, service.Hits()).Find(&hits)
|
2020-01-20 05:02:15 +00:00
|
|
|
return hits
|
2019-01-10 16:47:01 +00:00
|
|
|
}
|
2019-12-18 11:36:29 +00:00
|
|
|
|
|
|
|
func createServiceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ExecuteResponse(w, r, "service_create.gohtml", core.CoreApp, nil)
|
|
|
|
}
|