From 0b7687aa855c8975ad2e3c8113144437d1de1f2d Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Thu, 3 Jan 2019 11:13:48 -0800 Subject: [PATCH] grouping --- core/groups.go | 21 ++- core/sample.go | 12 +- core/services_test.go | 3 + handlers/groups.go | 21 --- handlers/handlers.go | 5 +- handlers/routes.go | 1 - source/tmpl/form_service.gohtml | 7 +- source/tmpl/index.gohtml | 46 ++++-- source/tmpl/postman.json | 250 +------------------------------- source/tmpl/services.gohtml | 4 +- source/wiki.go | 2 +- 11 files changed, 77 insertions(+), 295 deletions(-) diff --git a/core/groups.go b/core/groups.go index 5e01c4ae..fcd4d9f7 100644 --- a/core/groups.go +++ b/core/groups.go @@ -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 diff --git a/core/sample.go b/core/sample.go index 7de53968..f4a72f2f 100644 --- a/core/sample.go +++ b/core/sample.go @@ -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 diff --git a/core/services_test.go b/core/services_test.go index c7e0c5ab..d08f3d9d 100644 --- a/core/services_test.go +++ b/core/services_test.go @@ -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) diff --git a/handlers/groups.go b/handlers/groups.go index 9ee68852..bffff987 100644 --- a/handlers/groups.go +++ b/handlers/groups.go @@ -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) diff --git a/handlers/handlers.go b/handlers/handlers.go index 342242d6..2c2cd9f8 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -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 diff --git a/handlers/routes.go b/handlers/routes.go index 6e4e4fe6..1f1386cc 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -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 diff --git a/source/tmpl/form_service.gohtml b/source/tmpl/form_service.gohtml index 6ca2bbaf..eada6fbd 100644 --- a/source/tmpl/form_service.gohtml +++ b/source/tmpl/form_service.gohtml @@ -1,6 +1,7 @@ {{define "form_service"}}
+{{$s := .}} {{if ne .Id 0}}
{{else}} @@ -104,10 +105,10 @@
- + {{range Groups}} - + {{end}} Attach this service to a group diff --git a/source/tmpl/index.gohtml b/source/tmpl/index.gohtml index b587887b..b9edaae1 100644 --- a/source/tmpl/index.gohtml +++ b/source/tmpl/index.gohtml @@ -8,21 +8,39 @@
{{ .Description }}
{{ end }} - + {{else}} + +
+ {{end}} {{ if .Messages }}
diff --git a/source/tmpl/postman.json b/source/tmpl/postman.json index 3407dbf9..c60ce2d7 100644 --- a/source/tmpl/postman.json +++ b/source/tmpl/postman.json @@ -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": { diff --git a/source/tmpl/services.gohtml b/source/tmpl/services.gohtml index 958c342b..6d71009d 100644 --- a/source/tmpl/services.gohtml +++ b/source/tmpl/services.gohtml @@ -42,13 +42,15 @@ Name + Services - + {{range .Groups}} {{.Name}} + {{len .Services}}
{{if Auth}}{{end}} diff --git a/source/wiki.go b/source/wiki.go index a3200182..4f790729 100644 --- a/source/wiki.go +++ b/source/wiki.go @@ -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