pull/119/head
Hunter Long 2019-01-03 11:13:48 -08:00
parent bcedc75c8e
commit 0b7687aa85
11 changed files with 77 additions and 295 deletions

View File

@ -11,6 +11,10 @@ type Group struct {
// Delete will remove a group
func (g *Group) Delete() error {
for _, s := range g.Services() {
s.GroupId = 0
s.Update(false)
}
err := messagesDb().Delete(g)
if err.Error != nil {
return err.Error
@ -18,12 +22,6 @@ func (g *Group) Delete() error {
return err.Error
}
// Update will update a group in the database
func (g *Group) Update() error {
err := servicesDB().Update(&g)
return err.Error
}
// Create will create a group and insert it into the database
func (g *Group) Create() (int64, error) {
g.CreatedAt = time.Now()
@ -31,6 +29,17 @@ func (g *Group) Create() (int64, error) {
return g.Id, db.Error
}
// Services returns all services belonging to a group
func (g *Group) Services() []*Service {
var services []*Service
for _, s := range Services() {
if s.Select().GroupId == int(g.Id) {
services = append(services, s.(*Service))
}
}
return services
}
// SelectGroups returns all groups
func SelectGroups() []*Group {
var groups []*Group

View File

@ -26,6 +26,9 @@ import (
// InsertSampleData will create the example/dummy services for a brand new Statping installation
func InsertSampleData() error {
utils.Log(1, "Inserting Sample Data...")
insertSampleGroups()
s1 := ReturnService(&types.Service{
Name: "Google",
Domain: "https://google.com",
@ -35,6 +38,7 @@ func InsertSampleData() error {
Method: "GET",
Timeout: 10,
Order: 1,
GroupId: 1,
})
s2 := ReturnService(&types.Service{
Name: "Statping Github",
@ -55,6 +59,8 @@ func InsertSampleData() error {
Method: "GET",
Timeout: 30,
Order: 3,
Public: types.NewNullBool(true),
GroupId: 2,
})
s4 := ReturnService(&types.Service{
Name: "JSON API Tester",
@ -67,6 +73,8 @@ func InsertSampleData() error {
PostData: types.NewNullString(`{ "title": "statup", "body": "bar", "userId": 19999 }`),
Timeout: 30,
Order: 4,
Public: types.NewNullBool(true),
GroupId: 2,
})
s5 := ReturnService(&types.Service{
Name: "Google DNS",
@ -76,6 +84,8 @@ func InsertSampleData() error {
Port: 53,
Timeout: 120,
Order: 5,
Public: types.NewNullBool(true),
GroupId: 1,
})
s1.Create(false)
@ -86,8 +96,6 @@ func InsertSampleData() error {
insertMessages()
insertSampleGroups()
utils.Log(1, "Sample data has finished importing")
return nil

View File

@ -189,6 +189,7 @@ func TestCreateService(t *testing.T) {
Type: "http",
Method: "GET",
Timeout: 20,
GroupId: 1,
})
var err error
newServiceId, err = s.Create(false)
@ -212,6 +213,7 @@ func TestCreateFailingHTTPService(t *testing.T) {
Type: "http",
Method: "GET",
Timeout: 5,
GroupId: 1,
})
var err error
newServiceId, err = s.Create(false)
@ -238,6 +240,7 @@ func TestCreateFailingTCPService(t *testing.T) {
Interval: 30,
Type: "tcp",
Timeout: 5,
GroupId: 1,
})
var err error
newServiceId, err = s.Create(false)

View File

@ -69,27 +69,6 @@ func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) {
sendJsonAction(group, "create", w, r)
}
func apiGroupUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
group := core.SelectGroup(utils.ToInt(vars["id"]))
if group == nil {
sendErrorJson(errors.New("group not found"), w, r)
return
}
decoder := json.NewDecoder(r.Body)
decoder.Decode(&group)
err := group.Update()
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(group, "update", w, r)
}
func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)

View File

@ -195,8 +195,9 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap
"Groups": func() []*core.Group {
return core.SelectGroups()
},
"len": func(g []types.ServiceInterface) int {
return len(g)
"len": func(g interface{}) int {
val := reflect.ValueOf(g)
return val.Len()
},
"IsNil": func(g interface{}) bool {
return g == nil

View File

@ -89,7 +89,6 @@ func Router() *mux.Router {
r.Handle("/api/groups", http.HandlerFunc(apiAllGroupHandler)).Methods("GET")
r.Handle("/api/groups", http.HandlerFunc(apiCreateGroupHandler)).Methods("POST")
r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupHandler)).Methods("GET")
r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupUpdateHandler)).Methods("POST")
r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupDeleteHandler)).Methods("DELETE")
// API Routes

View File

@ -1,6 +1,7 @@
{{define "form_service"}}
<div class="card">
<div class="card-body">
{{$s := .}}
{{if ne .Id 0}}
<form class="ajax_form" action="/api/services/{{.Id}}" data-redirect="/services" method="POST">
{{else}}
@ -104,10 +105,10 @@
<div class="form-group row">
<label for="service_type" class="col-sm-4 col-form-label">Group</label>
<div class="col-sm-8">
<select name="group_id" class="form-control" id="group_id" value="{{.GroupId}}">
<option value="0" {{if eq .GroupId 0}}selected{{end}}>None</option>
<select name="group_id" class="form-control" id="group_id">
<option value="0" {{if eq $s.GroupId 0}}selected{{end}}>None</option>
{{range Groups}}
<option value="{{.Id}}">{{.Name}}</option>
<option value="{{.Id}}" {{if eq $s.GroupId .Id}}selected{{end}}>{{.Name}}</option>
{{end}}
</select>
<small class="form-text text-muted">Attach this service to a group</small>

View File

@ -8,21 +8,39 @@
<h5 class="col-12 text-center mb-5 header-desc">{{ .Description }}</h5>
{{ end }}
<div class="col-12 full-col-12">
<div class="list-group online_list mb-3">
{{ range Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{if ne (len Groups) 0}}
{{ range Groups }}
<div class="col-12 full-col-12">
<h4>{{.Name}}</h4>
<div class="list-group online_list mb-3">
{{ range .Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{end}}
</a>
{{ end }}
</div>
</div>
{{end}}
</a>
{{ end }}
</div>
</div>
{{else}}
<div class="list-group online_list mb-3">
{{ range Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{end}}
</a>
{{ end }}
</div>
</div>
{{end}}
{{ if .Messages }}
<div class="col-12">

View File

@ -816,7 +816,7 @@
"groups"
]
},
"description": "View an array of all Services added to your Statping instance."
"description": "View an array of all Groups added to your Statping instance."
},
"response": []
},
@ -872,58 +872,9 @@
"1"
]
},
"description": "View a specific service, this will include the service's failures and checkins."
"description": "View a specific group"
},
"response": [
{
"name": "View Service",
"originalRequest": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "{{endpoint}}/api/services/1",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"services",
"1"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Date",
"value": "Mon, 10 Dec 2018 19:31:19 GMT"
},
{
"key": "Content-Length",
"value": "482"
}
],
"cookie": [],
"body": "{\n \"id\": 1,\n \"name\": \"Google\",\n \"domain\": \"https://google.com\",\n \"expected\": null,\n \"expected_status\": 200,\n \"check_interval\": 10,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": null,\n \"port\": 0,\n \"timeout\": 10,\n \"order_id\": 1,\n \"allow_notifications\": false,\n \"created_at\": \"2018-12-10T11:15:42.24769-08:00\",\n \"updated_at\": \"2018-12-10T11:15:42.247837-08:00\",\n \"online\": true,\n \"latency\": 0.190599816,\n \"ping_time\": 0.00476598,\n \"online_24_hours\": 0,\n \"avg_response\": \"\",\n \"status_code\": 200,\n \"last_success\": \"2018-12-10T11:31:13.511139-08:00\"\n}"
}
]
"response": []
},
{
"name": "Create Group",
@ -975,156 +926,9 @@
"groups"
]
},
"description": "Create a new service and begin monitoring."
"description": "Create a new Group to organize services."
},
"response": [
{
"name": "Create Service",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"New Service\",\n \"domain\": \"https://statping.com\",\n \"expected\": \"\",\n \"expected_status\": 200,\n \"check_interval\": 30,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": \"\",\n \"port\": 0,\n \"timeout\": 30,\n \"order_id\": 0\n}"
},
"url": {
"raw": "{{endpoint}}/api/services",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"services"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Date",
"value": "Mon, 10 Dec 2018 19:31:47 GMT"
},
{
"key": "Content-Length",
"value": "528"
}
],
"cookie": [],
"body": "{\n \"status\": \"success\",\n \"type\": \"service\",\n \"method\": \"create\",\n \"id\": 10,\n \"output\": {\n \"id\": 10,\n \"name\": \"New Service\",\n \"domain\": \"https://statping.com\",\n \"expected\": \"\",\n \"expected_status\": 200,\n \"check_interval\": 30,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": \"\",\n \"port\": 0,\n \"timeout\": 30,\n \"order_id\": 0,\n \"allow_notifications\": false,\n \"created_at\": \"2018-12-10T11:31:47.535086-08:00\",\n \"updated_at\": \"2018-12-10T11:31:47.535184-08:00\",\n \"online\": false,\n \"latency\": 0,\n \"ping_time\": 0,\n \"online_24_hours\": 0,\n \"avg_response\": \"\",\n \"status_code\": 0,\n \"last_success\": \"0001-01-01T00:00:00Z\"\n }\n}"
}
]
},
{
"name": "Update Group",
"event": [
{
"listen": "test",
"script": {
"id": "b5a67a19-fd08-40b0-a961-3e9474ab78c6",
"exec": [
"pm.test(\"Update Service\", function () {",
" var jsonData = pm.response.json();",
" pm.expect(jsonData.output.name).to.eql(\"Updated Group\");",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{api_key}}",
"type": "string"
}
]
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Updated Group\"\n}"
},
"url": {
"raw": "{{endpoint}}/api/groups/{{group_id}}",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"groups",
"{{group_id}}"
]
},
"description": "Update a service with new values and begin monitoring."
},
"response": [
{
"name": "Update Service",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Updated New Service\",\n \"domain\": \"https://google.com\",\n \"expected\": \"\",\n \"expected_status\": 200,\n \"check_interval\": 60,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": \"\",\n \"port\": 0,\n \"timeout\": 10,\n \"order_id\": 0\n}"
},
"url": {
"raw": "{{endpoint}}/api/services/{{service_id}}",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"services",
"{{service_id}}"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Date",
"value": "Mon, 10 Dec 2018 19:31:54 GMT"
},
{
"key": "Content-Length",
"value": "567"
}
],
"cookie": [],
"body": "{\n \"status\": \"success\",\n \"type\": \"service\",\n \"method\": \"update\",\n \"id\": 10,\n \"output\": {\n \"id\": 10,\n \"name\": \"Updated New Service\",\n \"domain\": \"https://google.com\",\n \"expected\": \"\",\n \"expected_status\": 200,\n \"check_interval\": 60,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": \"\",\n \"port\": 0,\n \"timeout\": 10,\n \"order_id\": 0,\n \"allow_notifications\": false,\n \"created_at\": \"2018-12-10T11:31:47.535086-08:00\",\n \"updated_at\": \"2018-12-10T11:31:47.535184-08:00\",\n \"online\": true,\n \"latency\": 0.550636193,\n \"ping_time\": 0.073339805,\n \"online_24_hours\": 0,\n \"avg_response\": \"\",\n \"status_code\": 200,\n \"last_success\": \"2018-12-10T11:31:49.161389-08:00\"\n }\n}"
}
]
"response": []
},
{
"name": "Delete Group",
@ -1175,49 +979,7 @@
},
"description": "Delete a group"
},
"response": [
{
"name": "Delete Service",
"originalRequest": {
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "{{endpoint}}/api/services/{{service_id}}",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"services",
"{{service_id}}"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Date",
"value": "Mon, 10 Dec 2018 19:32:06 GMT"
},
{
"key": "Content-Length",
"value": "567"
}
],
"cookie": [],
"body": "{\n \"status\": \"success\",\n \"type\": \"service\",\n \"method\": \"delete\",\n \"id\": 10,\n \"output\": {\n \"id\": 10,\n \"name\": \"Updated New Service\",\n \"domain\": \"https://google.com\",\n \"expected\": \"\",\n \"expected_status\": 200,\n \"check_interval\": 60,\n \"type\": \"http\",\n \"method\": \"GET\",\n \"post_data\": \"\",\n \"port\": 0,\n \"timeout\": 10,\n \"order_id\": 0,\n \"allow_notifications\": false,\n \"created_at\": \"2018-12-10T11:31:47.535086-08:00\",\n \"updated_at\": \"2018-12-10T11:31:47.535184-08:00\",\n \"online\": true,\n \"latency\": 0.203382878,\n \"ping_time\": 0.001664491,\n \"online_24_hours\": 0,\n \"avg_response\": \"\",\n \"status_code\": 200,\n \"last_success\": \"2018-12-10T11:31:55.455091-08:00\"\n }\n}"
}
]
"response": []
}
],
"auth": {

View File

@ -42,13 +42,15 @@
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Services</th>
<th scope="col"></th>
</tr>
</thead>
<tbody class="sortable" id="groups_table">
<tbody id="groups_table">
{{range .Groups}}
<tr id="group_{{.Id}}" data-id="{{.Id}}">
<td>{{.Name}}</td>
<td>{{len .Services}}</td>
<td class="text-right">
<div class="btn-group">
{{if Auth}}<a href="/api/groups/{{.Id}}" class="ajax_delete btn btn-danger" data-method="DELETE" data-obj="group_{{.Id}}" data-id="{{.Id}}"><i class="fas fa-times"></i></a>{{end}}

View File

@ -1,6 +1,6 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2019-01-03 07:34:25.019934 -0800 PST m=+0.528929648
// 2019-01-03 11:12:03.7014 -0800 PST m=+0.913709962
//
// This contains the most recently Markdown source for the Statping Wiki.
package source