170 lines
4.4 KiB
Go
170 lines
4.4 KiB
Go
package service
|
|
|
|
import (
|
|
"cybertron/internal/client/elastic"
|
|
"cybertron/pkg/log"
|
|
"cybertron/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type SearchService struct {
|
|
logger *log.Logger
|
|
elasticSearchClient *elastic.ElasticSearchClient
|
|
}
|
|
|
|
func NewSearchService(logger *log.Logger, elasticSearchClient *elastic.ElasticSearchClient) *SearchService {
|
|
return &SearchService{
|
|
logger: logger,
|
|
elasticSearchClient: elasticSearchClient,
|
|
}
|
|
}
|
|
|
|
func (s *SearchService) Search(c *gin.Context) {
|
|
|
|
}
|
|
|
|
func (s *SearchService) GetErrorDetails(c *gin.Context) {
|
|
error_hash := c.Query("error_hash")
|
|
from := c.DefaultQuery("from", "0")
|
|
|
|
fromInNumber, err := strconv.ParseInt(from, 10, 64)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, "from should be a number")
|
|
return
|
|
}
|
|
|
|
term_query := utils.CreateTermSubQuery("error_hash", error_hash)
|
|
search_query := utils.CreateSearchQuery(term_query)
|
|
size_query := utils.CreateSizeQuery(1)
|
|
sort_query := utils.CreateSortQuery("created_at", "desc", "")
|
|
after_query := utils.CreateFromQuery(fromInNumber)
|
|
es_query := utils.CreateEsQuery(search_query, size_query, sort_query, after_query)
|
|
println(es_query)
|
|
|
|
// searchRequest := `
|
|
//{
|
|
// "size": 1,
|
|
// "query": {
|
|
// "term": {
|
|
// "error_hash": {
|
|
// "value": "%s"
|
|
// }
|
|
// }
|
|
// },
|
|
// "sort": [
|
|
// { "created_at": { "order": "asc" } }
|
|
// ],
|
|
// "search_after": ["1724732743"]
|
|
//}
|
|
//
|
|
// `
|
|
|
|
searchRequestformatted := es_query
|
|
fields := []string{"error", "significant_stack", "title"}
|
|
|
|
response, _, total, err := s.elasticSearchClient.SearchDocuments(searchRequestformatted, fields)
|
|
|
|
if err != nil {
|
|
utils.ErrorResponse(c, "Failed to search please try again later")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"results": response,
|
|
"total": total,
|
|
})
|
|
}
|
|
|
|
func (s *SearchService) GetErrorList(c *gin.Context) {
|
|
//todo pagination and aggregation of errors
|
|
projectId := c.Query("project_id")
|
|
size := c.DefaultQuery("size", "10")
|
|
afterKey := c.DefaultQuery("after_key", "")
|
|
|
|
sizeInNumber, sizeParseError := strconv.Atoi(size)
|
|
|
|
if sizeParseError != nil {
|
|
c.JSON(http.StatusBadRequest, "size should be a number")
|
|
return
|
|
}
|
|
|
|
term_query := utils.CreateTermSubQuery("project_id", projectId)
|
|
search_query := utils.CreateSearchQuery(term_query)
|
|
size_query := utils.CreateSizeQuery(0)
|
|
top_hits_aggs_name_query := utils.BuildAggregationQuery("unique_errors", utils.CreateTopHitsAggsQuery(1, []string{"error", "significant_stack", "created_at", "error_hash"}))
|
|
top_hits_aggs_query := utils.CreateAggregationQuery(top_hits_aggs_name_query)
|
|
|
|
//building composite aggregation
|
|
composite_term_query := utils.CreateTermsAggregationQueryWithoutSize("error_hash")
|
|
composite_aggregation_query_without_name := utils.CreateCompositeAggsQuery(sizeInNumber, "error_hash", "error_hash", afterKey, composite_term_query)
|
|
composite_aggs_query := utils.BuildAggregationQuery("errors_by_hash", composite_aggregation_query_without_name, top_hits_aggs_query)
|
|
|
|
compositeAggsQuery := utils.CreateAggregationQuery(composite_aggs_query)
|
|
|
|
//final_aggs_query := utils.CreateAggregationQuery(top_hits_value_count, compositeAggsQuery)
|
|
|
|
final_query := utils.CreateEsQuery(search_query, size_query, compositeAggsQuery)
|
|
|
|
println("%s", final_query)
|
|
// searchRequest := `
|
|
//{
|
|
// "size": 0,
|
|
// "query": {
|
|
// "term": {
|
|
// "project_id": {
|
|
// "value": "%s"
|
|
// }
|
|
// }
|
|
// },
|
|
// "aggs": {
|
|
// "errors_by_hash": {
|
|
// "composite": {
|
|
// "size": 3,
|
|
// "sources": [
|
|
// {
|
|
// "error_hash": {
|
|
// "terms": {
|
|
// "field": "error_hash.keyword"
|
|
// }
|
|
// }
|
|
// }
|
|
// ]
|
|
// },
|
|
// "aggs": {
|
|
// "unique_errors": {
|
|
// "top_hits": {
|
|
// "_source": {
|
|
// "includes": ["error", "error_hash"]
|
|
// }
|
|
// }
|
|
// },
|
|
// "error_count": {
|
|
// "value_count": {
|
|
// "field": "error.keyword"
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
//}
|
|
//
|
|
// `
|
|
|
|
fields := []string{"error", "significant_stack", "title"}
|
|
var _, aggs, total, err = s.elasticSearchClient.SearchDocuments(final_query, fields)
|
|
|
|
if err != nil {
|
|
utils.ErrorResponse(c, "search failed please try again")
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"results": aggs,
|
|
"total": total,
|
|
})
|
|
|
|
}
|