From bb23673547b979e3405a315cfe69fa59360682af Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 28 Jul 2024 13:16:04 -0400 Subject: [PATCH] default values for system / update collections snapshot --- hub/main.go | 9 +++ hub/migrations/1722180627_updated_users.go | 78 ------------------- ....go => 1722186612_collections_snapshot.go} | 16 ++-- hub/site/src/components/add-system.tsx | 13 ---- 4 files changed, 17 insertions(+), 99 deletions(-) delete mode 100644 hub/migrations/1722180627_updated_users.go rename hub/migrations/{1720568457_collections_snapshot.go => 1722186612_collections_snapshot.go} (96%) diff --git a/hub/main.go b/hub/main.go index b048797..1288a38 100644 --- a/hub/main.go +++ b/hub/main.go @@ -161,6 +161,15 @@ func main() { return nil }) + // system creation defaults + app.OnModelBeforeCreate("systems").Add(func(e *core.ModelEvent) error { + record := e.Model.(*models.Record) + var info = SystemInfo{} + record.Set("info", info) + record.Set("status", "pending") + return nil + }) + // immediately create connection for new servers app.OnModelAfterCreate("systems").Add(func(e *core.ModelEvent) error { go updateSystem(e.Model.(*models.Record)) diff --git a/hub/migrations/1722180627_updated_users.go b/hub/migrations/1722180627_updated_users.go deleted file mode 100644 index e1d5176..0000000 --- a/hub/migrations/1722180627_updated_users.go +++ /dev/null @@ -1,78 +0,0 @@ -package migrations - -import ( - "encoding/json" - - "github.com/pocketbase/dbx" - "github.com/pocketbase/pocketbase/daos" - m "github.com/pocketbase/pocketbase/migrations" - "github.com/pocketbase/pocketbase/models/schema" -) - -func init() { - m.Register(func(db dbx.Builder) error { - dao := daos.New(db); - - collection, err := dao.FindCollectionByNameOrId("_pb_users_auth_") - if err != nil { - return err - } - - // update - edit_role := &schema.SchemaField{} - if err := json.Unmarshal([]byte(`{ - "system": false, - "id": "qkbp58ae", - "name": "role", - "type": "select", - "required": false, - "presentable": false, - "unique": false, - "options": { - "maxSelect": 1, - "values": [ - "user", - "admin", - "readonly" - ] - } - }`), edit_role); err != nil { - return err - } - collection.Schema.AddField(edit_role) - - return dao.SaveCollection(collection) - }, func(db dbx.Builder) error { - dao := daos.New(db); - - collection, err := dao.FindCollectionByNameOrId("_pb_users_auth_") - if err != nil { - return err - } - - // update - edit_role := &schema.SchemaField{} - if err := json.Unmarshal([]byte(`{ - "system": false, - "id": "qkbp58ae", - "name": "role", - "type": "select", - "required": true, - "presentable": false, - "unique": false, - "options": { - "maxSelect": 1, - "values": [ - "user", - "admin", - "readonly" - ] - } - }`), edit_role); err != nil { - return err - } - collection.Schema.AddField(edit_role) - - return dao.SaveCollection(collection) - }) -} diff --git a/hub/migrations/1720568457_collections_snapshot.go b/hub/migrations/1722186612_collections_snapshot.go similarity index 96% rename from hub/migrations/1720568457_collections_snapshot.go rename to hub/migrations/1722186612_collections_snapshot.go index 047c16c..6cf10b5 100644 --- a/hub/migrations/1720568457_collections_snapshot.go +++ b/hub/migrations/1722186612_collections_snapshot.go @@ -15,7 +15,7 @@ func init() { { "id": "2hz5ncl8tizk5nx", "created": "2024-07-07 16:08:20.979Z", - "updated": "2024-07-22 19:39:17.434Z", + "updated": "2024-07-28 17:00:47.996Z", "name": "systems", "type": "base", "system": false, @@ -39,7 +39,7 @@ func init() { "id": "waj7seaf", "name": "status", "type": "select", - "required": true, + "required": false, "presentable": false, "unique": false, "options": { @@ -85,7 +85,7 @@ func init() { "id": "qoq64ntl", "name": "info", "type": "json", - "required": true, + "required": false, "presentable": false, "unique": false, "options": { @@ -120,7 +120,7 @@ func init() { { "id": "ej9oowivz8b2mht", "created": "2024-07-07 16:09:09.179Z", - "updated": "2024-07-18 15:56:45.302Z", + "updated": "2024-07-22 20:13:31.324Z", "name": "system_stats", "type": "base", "system": false, @@ -186,7 +186,7 @@ func init() { { "id": "juohu4jipgc13v7", "created": "2024-07-07 16:09:57.976Z", - "updated": "2024-07-18 15:57:50.933Z", + "updated": "2024-07-22 20:13:31.324Z", "name": "container_stats", "type": "base", "system": false, @@ -250,7 +250,7 @@ func init() { { "id": "_pb_users_auth_", "created": "2024-07-14 16:25:18.226Z", - "updated": "2024-07-22 20:10:20.670Z", + "updated": "2024-07-28 17:02:08.311Z", "name": "users", "type": "auth", "system": false, @@ -260,7 +260,7 @@ func init() { "id": "qkbp58ae", "name": "role", "type": "select", - "required": true, + "required": false, "presentable": false, "unique": false, "options": { @@ -316,7 +316,7 @@ func init() { { "id": "elngm8x1l60zi2v", "created": "2024-07-15 01:16:04.044Z", - "updated": "2024-07-22 19:13:16.498Z", + "updated": "2024-07-22 20:13:31.324Z", "name": "alerts", "type": "base", "system": false, diff --git a/hub/site/src/components/add-system.tsx b/hub/site/src/components/add-system.tsx index de27f6c..00f0973 100644 --- a/hub/site/src/components/add-system.tsx +++ b/hub/site/src/components/add-system.tsx @@ -53,20 +53,7 @@ export function AddSystemButton() { e.preventDefault() const formData = new FormData(e.target as HTMLFormElement) const data = Object.fromEntries(formData) as Record - data.status = 'pending' data.users = pb.authStore.model!.id - data.info = { - cpu: 0, - m: 0, - mu: 0, - mp: 0, - mb: 0, - d: 0, - du: 0, - dp: 0, - dr: 0, - dw: 0, - } as SystemStats try { setOpen(false) await pb.collection('systems').create(data)