From 1a41cf5b6a2bba7b3d04addd1994cc8b9b9f8133 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 28 Jul 2024 01:08:08 -0400 Subject: [PATCH] Add learned tgs --- pkg/gordio/database/models.go | 13 +++++++------ pkg/gordio/server/calls.go | 21 ++++++++++++++------- pkg/gordio/server/routes.go | 11 ----------- sql/postgres/migrations/001_initial.up.sql | 13 +++++++++---- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index 6f7ea93..900e0f3 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -68,12 +68,12 @@ type System struct { } type Talkgroup struct { - SystemID int `json:"system_id"` - Tgid int `json:"tgid"` - Name *string `json:"name"` - Frequency *int32 `json:"frequency"` - AutoCreated *bool `json:"auto_created"` - Metadata []byte `json:"metadata"` + SystemID int `json:"system_id"` + Tgid int `json:"tgid"` + Name *string `json:"name"` + TgGroup *string `json:"tg_group"` + Frequency *int32 `json:"frequency"` + Metadata []byte `json:"metadata"` } type TalkgroupsLearned struct { @@ -82,6 +82,7 @@ type TalkgroupsLearned struct { Tgid int `json:"tgid"` GroupName string `json:"group_name"` GroupTag *string `json:"group_tag"` + Ignored *bool `json:"ignored"` } type TalkgroupsTag struct { diff --git a/pkg/gordio/server/calls.go b/pkg/gordio/server/calls.go index 4abfa93..68b0bea 100644 --- a/pkg/gordio/server/calls.go +++ b/pkg/gordio/server/calls.go @@ -16,8 +16,8 @@ import ( ) type callUploadRequest struct { - Audio []byte `form:"audio"` - AudioName string `form:"audioName"` + Audio []byte `form:"audio"` + AudioName string AudioType string `form:"audioType"` DateTime time.Time `form:"dateTime"` Frequencies []int `form:"frequencies"` @@ -83,15 +83,16 @@ func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) { return } - if r.Form.Get("test") == "1" { - w.Write([]byte("Test succeeded")) + if strings.Trim(r.Form.Get("test"), "\r\n") == "1" { + // fudge the official response + http.Error(w, "incomplete call data: no talkgroup", http.StatusExpectationFailed) return } call := new(callUploadRequest) err = call.fill(r) if err != nil { - http.Error(w, "cannot bind upload "+err.Error(), 500) + http.Error(w, "cannot bind upload "+err.Error(), http.StatusExpectationFailed) return } @@ -102,6 +103,8 @@ func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) { return } + w.Write([]byte("Call imported successfully.")) + _ = dbCall } @@ -112,14 +115,18 @@ func (car *callUploadRequest) fill(r *http.Request) error { for i := 0; i < rv.NumField(); i++ { f := rv.Field(i) fi := f.Interface() - formField := rt.Field(i).Tag.Get("form") + formField, has := rt.Field(i).Tag.Lookup("form") + if !has { + continue + } switch v := fi.(type) { case []byte: - file, _, err := r.FormFile(formField) + file, hdr, err := r.FormFile(formField) if err != nil { return fmt.Errorf("get form file: %w", err) } + car.AudioName = hdr.Filename audioBytes, err := io.ReadAll(file) if err != nil { return fmt.Errorf("file read: %w", err) diff --git a/pkg/gordio/server/routes.go b/pkg/gordio/server/routes.go index 17fc847..1148652 100644 --- a/pkg/gordio/server/routes.go +++ b/pkg/gordio/server/routes.go @@ -1,8 +1,6 @@ package server import ( - "fmt" - "io" "net/http" "time" @@ -16,7 +14,6 @@ import ( func (s *Server) setupRoutes() { r := s.r - r.Use(middleware.Recoverer) r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db)) r.Group(func(r chi.Router) { @@ -28,7 +25,6 @@ func (s *Server) setupRoutes() { r.Group(func(r chi.Router) { r.Use(rateLimiter()) r.Use(render.SetContentType(render.ContentTypeJSON)) - // r.Use(teeRequest()) // public routes r.Post("/auth", s.routeAuth) r.Post("/api/call-upload", s.routeCallUpload) @@ -44,13 +40,6 @@ func (s *Server) setupRoutes() { }) } -func teeRequest() func(http.Handler) http.Handler { - return middleware.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - by, err := io.ReadAll(r.Body) - fmt.Println(string(by), err) - })) -} - func rateLimiter() func(http.Handler) http.Handler { return httprate.LimitByRealIP(100, 1*time.Minute) } diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 0d38e0d..4ca0c39 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -27,8 +27,8 @@ CREATE TABLE IF NOT EXISTS talkgroups( system_id INTEGER REFERENCES systems(id) NOT NULL, tgid INTEGER, name TEXT, + tg_group TEXT, frequency INTEGER, - auto_created BOOLEAN, metadata JSONB, PRIMARY KEY (system_id, tgid) ); @@ -39,7 +39,8 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned( tgid INTEGER NOT NULL, group_name TEXT NOT NULL, group_tag TEXT, - UNIQUE (system_id, tgid, group_name, group_tag) + ignored BOOLEAN, + UNIQUE (system_id, tgid, group_name) ); CREATE OR REPLACE FUNCTION learn_talkgroup() @@ -47,18 +48,22 @@ RETURNS TRIGGER AS $$ BEGIN IF NOT EXISTS (SELECT * FROM talkgroups - JOIN talkgroups_tags + LEFT JOIN talkgroups_tags ON talkgroups_tags.system_id = talkgroups.system_id AND talkgroups_tags.talkgroup_id = talkgroups.tgid + LEFT JOIN talkgroups_learned + ON talkgroups_learned.system_id = talkgroups.system_id AND talkgroups_learned.tgid = talkgroups.tgid WHERE talkgroups.system_id = NEW.system AND talkgroups.tgid = NEW.talkgroup AND ( talkgroups.name != NEW.tg_label OR NOT (talkgroups_tags.tags @> ARRAY[NEW.tg_tag]) + OR talkgroups.tg_group != NEW.tg_group ) + AND talkgroups_learned.ignored IS NOT TRUE ) THEN INSERT INTO talkgroups_learned(system_id, tgid, group_name, group_tag) VALUES( NEW.system, NEW.talkgroup, NEW.tg_label, NEW.tg_tag - ) ON CONFLICT DO NOTHING; + ) ON CONFLICT (system_id, tgid, group_name) DO UPDATE SET group_tag = EXCLUDED.group_tag; END IF; RETURN NEW; END