Files
cybertron/service/searchService.go
2024-11-29 16:20:01 +05:30

187 lines
5.3 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")
project_id := c.Query("project_id")
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)
project_id_term_query := utils.CreateTermSubQuery("project_id", project_id)
mustQuery := utils.CreateMustQuery(term_query, project_id_term_query)
boolQuery := utils.CreateBoolQuery(mustQuery)
search_query := utils.CreateSearchQuery(boolQuery)
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)
// 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", "")
sortKey := c.DefaultQuery("sort_key", "lastSeen")
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"}))
last_seen_aggs_query := utils.BuildAggregationQuery("last_seen", utils.CreateMaxAggregationQuery("created_at"))
first_seen_aggs_query := utils.BuildAggregationQuery("first_seen", utils.CreateMinAggregationQuery("created_at"))
sortQuery := utils.CreateSortQuery("last_seen", "desc", "")
if sortKey == "lastSeen" {
sortQuery = utils.CreateSortQuery("last_seen", "desc", "")
}
if sortKey == "firstSeen" {
sortQuery = utils.CreateSortQuery("first_seen", "desc", "")
}
if sortKey == "count" {
sortQuery = utils.CreateSortQuery("_count", "desc", "")
}
customSortQuery := utils.CreateCustomQueryWithBraces("bucket_sort", sortQuery)
customFinalSortQuery := utils.CreateCustomQueryWithBraces("sorting", customSortQuery)
top_hits_aggs_query := utils.CreateAggregationQuery(top_hits_aggs_name_query, last_seen_aggs_query, first_seen_aggs_query, customFinalSortQuery)
//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_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,
})
}