diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..5ff19f9 --- /dev/null +++ b/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./out/cybertron" + cmd = "go mod tidy && CGO_ENABLED=1 go build -ldflags=\"-s -w\" -o out/cybertron cmd/cybertron/main.go" + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/.gitignore b/.gitignore index f8d4a9c..c49a829 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ out/ vendor/ go.sum + +tmp/ diff --git a/internal/client/elastic/elastic.go b/internal/client/elastic/elastic.go index cd08495..23442d7 100644 --- a/internal/client/elastic/elastic.go +++ b/internal/client/elastic/elastic.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "cybertron/configs" + "encoding/json" elasticsearch8 "github.com/elastic/go-elasticsearch/v8" "github.com/elastic/go-elasticsearch/v8/typedapi/core/search" "github.com/elastic/go-elasticsearch/v8/typedapi/types" @@ -44,15 +45,53 @@ func (el *ElasticSearchClient) IndexDocument(document interface{}) { } } -func (el *ElasticSearchClient) SearchDocuments(query *types.Query) *search.Response { +func (el *ElasticSearchClient) SearchDocuments(query *types.Query, fields []string) ([]map[string]interface{}, error) { res, err := el.client.Search(). Index(el.Config.Index). Request(&search.Request{ Query: query, - }). + }).Source_(fields). Do(context.TODO()) if err != nil { log.Println("Error getting response: %s", err) } - return res + var results []map[string]interface{} + for _, hit := range res.Hits.Hits { + var doc map[string]interface{} + + err := json.Unmarshal(hit.Source_, &doc) + doc["id"] = *hit.Id_ + if err != nil { + log.Printf("Error unmarshalling document: %s", err) + continue + } + results = append(results, doc) + } + + return results, nil +} + +func (el *ElasticSearchClient) GetDocument(documentID string) (interface{}, error) { + // Retrieve the document by its ID + res, err := el.client.Get(el.Config.Index, documentID).Do(context.Background()) + var document interface{} + if err != nil { + log.Printf("Error getting response: %s", err) + return nil, err + } + + // Check if the document exists + if !res.Found { + log.Printf("Document with ID %s not found", documentID) + return document, nil + } + + // Unmarshal the JSON response into the provided document interface + err = json.Unmarshal(res.Source_, &document) + if err != nil { + log.Printf("Error unmarshalling document: %s", err) + return nil, err + } + + return document, nil } diff --git a/internal/transport/handler/search.go b/internal/transport/handler/search.go index 58b794c..6286c6a 100644 --- a/internal/transport/handler/search.go +++ b/internal/transport/handler/search.go @@ -10,10 +10,10 @@ type SearchHandler struct { } func (h *SearchHandler) SearchErrors(c *gin.Context) { - h.searchService.Search(c) + h.searchService.GetErrorDetails(c) } func (h *SearchHandler) GetErrorDetails(c *gin.Context) { - h.searchService.Search(c) + h.searchService.GetErrorDetails(c) } func (h *SearchHandler) GetErrorList(c *gin.Context) { diff --git a/internal/transport/server.go b/internal/transport/server.go index 7e86be7..84c80f9 100644 --- a/internal/transport/server.go +++ b/internal/transport/server.go @@ -43,7 +43,7 @@ func (s *Server) Start() { s.gin.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, AllowMethods: []string{"PUT", "PATCH", "GET", "POST", "OPTIONS"}, - AllowHeaders: []string{"*"}, + AllowHeaders: []string{"*", "x-session-token"}, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: true, MaxAge: 24 * time.Hour, diff --git a/service/searchService.go b/service/searchService.go index e2e3866..0c72508 100644 --- a/service/searchService.go +++ b/service/searchService.go @@ -3,9 +3,9 @@ package service import ( "cybertron/internal/client/elastic" "cybertron/pkg/log" - "github.com/elastic/go-elasticsearch/v8/typedapi/core/search" "github.com/elastic/go-elasticsearch/v8/typedapi/types" "github.com/gin-gonic/gin" + "net/http" ) type SearchService struct { @@ -24,11 +24,16 @@ func (s *SearchService) Search(c *gin.Context) { } -func (s *SearchService) GetErrorDetails(searchTerm string) { +func (s *SearchService) GetErrorDetails(c *gin.Context) { + documentId := c.Query("document_id") + response, _ := s.elasticSearchClient.GetDocument(documentId) + c.JSON(http.StatusOK, response) } -func (s *SearchService) GetErrorList(c *gin.Context) *search.Response { +func (s *SearchService) GetErrorList(c *gin.Context) { + //todo pagination and aggregation of errors projectId := c.Query("project_id") + println(projectId) query := &types.Query{ Term: map[string]types.TermQuery{ "project_id": { @@ -36,6 +41,9 @@ func (s *SearchService) GetErrorList(c *gin.Context) *search.Response { }, }, } - return s.elasticSearchClient.SearchDocuments(query) + fields := []string{"error", "significant_stack", "title"} + var response, _ = s.elasticSearchClient.SearchDocuments(query, fields) + + c.JSON(http.StatusOK, response) }