statping/handlers/services.go

369 lines
9.0 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 (
"encoding/json"
"errors"
"fmt"
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"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
2019-02-06 18:51:30 +00:00
"strconv"
2018-09-18 22:02:27 +00:00
"time"
2018-06-30 00:57:05 +00:00
)
func renderServiceChartsHandler(w http.ResponseWriter, r *http.Request) {
services := core.CoreApp.Services
2018-07-04 09:00:16 +00:00
w.Header().Set("Content-Type", "text/javascript")
w.Header().Set("Cache-Control", "max-age=60")
2018-09-18 22:02:27 +00:00
2018-10-03 15:20:45 +00:00
end := time.Now().UTC()
start := time.Now().Add((-24 * 7) * time.Hour).UTC()
var srvs []*core.Service
2018-09-18 22:02:27 +00:00
for _, s := range services {
srvs = append(srvs, s.(*core.Service))
2018-09-18 22:02:27 +00:00
}
out := struct {
Services []*core.Service
2018-10-03 15:20:45 +00:00
Start int64
End int64
}{srvs, start.Unix(), end.Unix()}
2018-09-18 22:02:27 +00:00
executeJSResponse(w, r, "charts.js", out)
2018-07-04 01:19:18 +00:00
}
func servicesHandler(w http.ResponseWriter, r *http.Request) {
if !IsUser(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
2018-12-31 11:41:19 +00:00
data := map[string]interface{}{
"Services": core.CoreApp.Services,
}
ExecuteResponse(w, r, "services.gohtml", data, nil)
2018-06-30 00:57:05 +00:00
}
type serviceOrder struct {
Id int64 `json:"service"`
Order int `json:"order"`
}
func reorderServiceHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
2018-11-29 16:26:42 +00:00
r.ParseForm()
var newOrder []*serviceOrder
decoder := json.NewDecoder(r.Body)
2018-11-29 16:26:42 +00:00
decoder.Decode(&newOrder)
for _, s := range newOrder {
service := core.SelectService(s.Id)
service.Order = s.Order
2018-11-29 16:26:42 +00:00
service.Update(false)
}
2018-11-29 16:26:42 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(newOrder)
}
func servicesViewHandler(w http.ResponseWriter, r *http.Request) {
2018-06-30 00:57:05 +00:00
vars := mux.Vars(r)
2018-09-18 22:02:27 +00:00
fields := parseGet(r)
r.ParseForm()
2019-02-06 18:51:30 +00:00
var serv *core.Service
id := vars["id"]
if _, err := strconv.Atoi(id); err == nil {
serv = core.SelectService(utils.ToInt(id))
} else {
serv = core.SelectServiceLink(id)
}
2018-08-10 04:38:54 +00:00
if serv == nil {
w.WriteHeader(http.StatusNotFound)
return
}
2018-09-18 22:02:27 +00:00
2019-02-06 18:51:30 +00:00
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))
group := r.Form.Get("group")
end := time.Now().UTC()
start := end.Add((-24 * 7) * time.Hour).UTC()
if startField != 0 {
start = time.Unix(startField, 0).UTC()
2018-09-18 22:02:27 +00:00
}
if endField != 0 {
end = time.Unix(endField, 0).UTC()
}
if group == "" {
group = "hour"
}
data := core.GraphDataRaw(serv, start, end, group, "latency")
2018-09-18 22:02:27 +00:00
out := struct {
Service *core.Service
Start string
End string
StartUnix int64
EndUnix int64
Data string
}{serv, start.Format(utils.FlatpickrReadable), end.Format(utils.FlatpickrReadable), start.Unix(), end.Unix(), data.ToString()}
2018-09-18 22:02:27 +00:00
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "service.gohtml", out, nil)
2018-06-30 00:57:05 +00:00
}
func apiServiceHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
2018-06-30 00:57:05 +00:00
return
}
vars := mux.Vars(r)
2018-12-06 19:03:55 +00:00
servicer := core.SelectService(utils.ToInt(vars["id"]))
if servicer == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
w.Header().Set("Content-Type", "application/json")
2018-12-06 19:03:55 +00:00
json.NewEncoder(w).Encode(servicer)
2018-06-30 00:57:05 +00:00
}
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
var service *types.Service
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&service)
if err != nil {
sendErrorJson(err, w, r)
return
}
newService := core.ReturnService(service)
_, err = newService.Create(true)
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(newService, "create", w, r)
}
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)
2018-06-30 00:57:05 +00:00
return
}
vars := mux.Vars(r)
2018-12-06 19:03:55 +00:00
service := core.SelectService(utils.ToInt(vars["id"]))
if service == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
decoder := json.NewDecoder(r.Body)
decoder.Decode(&service)
err := service.Update(true)
if err != nil {
sendErrorJson(err, w, r)
return
}
go service.Check(true)
sendJsonAction(service, "update", w, r)
2018-10-07 22:38:56 +00:00
}
2018-10-06 03:35:53 +00:00
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := core.SelectService(utils.ToInt(vars["id"]))
if service == nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
if grouping == "" {
grouping = "hour"
}
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))
start := time.Unix(startField, 0)
end := time.Unix(endField, 0)
obj := core.GraphDataRaw(service, start, end, grouping, "latency")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := core.SelectService(utils.ToInt(vars["id"]))
if service == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))
start := time.Unix(startField, 0)
end := time.Unix(endField, 0)
obj := core.GraphDataRaw(service, start, end, grouping, "ping_time")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
2019-01-29 12:02:13 +00:00
type dataXy struct {
X int `json:"x"`
Y int `json:"y"`
}
type dataXyMonth struct {
Date string `json:"date"`
2019-01-29 12:02:13 +00:00
Data []*dataXy `json:"data"`
}
func apiServiceHeatmapHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := core.SelectService(utils.ToInt(vars["id"]))
if service == nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
var monthOutput []*dataXyMonth
start := service.CreatedAt
//now := time.Now()
2019-01-29 12:02:13 +00:00
sY, sM, _ := start.Date()
var date time.Time
month := int(sM)
maxMonth := 12
for year := int(sY); year <= time.Now().Year(); year++ {
2019-01-29 12:02:13 +00:00
if year == time.Now().Year() {
maxMonth = int(time.Now().Month())
}
2019-01-29 12:02:13 +00:00
for m := month; m <= maxMonth; m++ {
2019-01-29 12:02:13 +00:00
var output []*dataXy
for day := 1; day <= 31; day++ {
date = time.Date(year, time.Month(m), day, 0, 0, 0, 0, time.UTC)
2019-01-29 12:02:13 +00:00
failures, _ := service.TotalFailuresOnDate(date)
output = append(output, &dataXy{day, int(failures)})
}
thisDate := fmt.Sprintf("%v-%v-01 00:00:00", year, m)
monthOutput = append(monthOutput, &dataXyMonth{thisDate, output})
2019-01-29 12:02:13 +00:00
}
month = 1
2019-01-29 12:02:13 +00:00
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(monthOutput)
}
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)
2018-10-07 22:38:56 +00:00
return
}
vars := mux.Vars(r)
service := core.SelectService(utils.ToInt(vars["id"]))
if service == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
err := service.Delete()
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(service, "delete", w, r)
2018-10-03 10:47:32 +00:00
}
func apiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
services := core.Services()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(services)
}
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
2018-10-03 10:47:32 +00:00
vars := mux.Vars(r)
service := core.SelectService(utils.ToInt(vars["id"]))
service.DeleteFailures()
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "services.gohtml", core.CoreApp.Services, "/services")
2018-06-30 00:57:05 +00:00
}
2019-01-10 16:47:01 +00:00
func apiServiceFailuresHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
servicer := core.SelectService(utils.ToInt(vars["id"]))
if servicer == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(servicer.AllFailures())
}
func apiServiceHitsHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
servicer := core.SelectService(utils.ToInt(vars["id"]))
if servicer == nil {
sendErrorJson(errors.New("service not found"), w, r)
return
}
hits, err := servicer.Hits()
if err != nil {
sendErrorJson(err, w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(hits)
}