TP-55555 | latest push

This commit is contained in:
varnit-goyal_navi
2024-08-21 13:00:34 +05:30
parent 8e2104bc9b
commit 96ea78fb46
6 changed files with 110 additions and 10 deletions

51
.air.toml Normal file
View File

@@ -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

2
.gitignore vendored
View File

@@ -25,3 +25,5 @@ out/
vendor/
go.sum
tmp/

View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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,

View File

@@ -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)
}