github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/es/reader/resolveIndexHandler.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package reader 18 19 import ( 20 "regexp" 21 "strings" 22 23 esutils "github.com/siglens/siglens/pkg/es/utils" 24 "github.com/siglens/siglens/pkg/utils" 25 "github.com/siglens/siglens/pkg/virtualtable" 26 "github.com/valyala/fasthttp" 27 28 log "github.com/sirupsen/logrus" 29 ) 30 31 var excludedInternalIndices = [...]string{"traces", "red-traces", "service-dependency"} 32 33 func IndicesBody(indexName string) esutils.ResolveIndexEntry { 34 return esutils.ResolveIndexEntry{Name: indexName, Attributes: []string{"open"}} 35 } 36 37 func ExpandAndReturnIndexNames(indexPattern string, allVirtualTableNames map[string]bool, myid uint64) ([]esutils.ResolveIndexEntry, []esutils.ResolveAliasEntry, error) { 38 indicesEntries := []esutils.ResolveIndexEntry{} 39 aliasesEntries := []esutils.ResolveAliasEntry{} 40 41 if strings.Contains(indexPattern, "*") { 42 startLimiter := "^" 43 endLimiter := "$" 44 indexPattern = startLimiter + indexPattern + endLimiter 45 indexRegExp, err := regexp.Compile(strings.ReplaceAll(indexPattern, "*", `.*`)) 46 if err != nil { 47 log.Infof("ExpandAndReturnIndexNames: Error compiling match: %v", err) 48 return indicesEntries, aliasesEntries, err 49 } 50 51 for indexName := range allVirtualTableNames { 52 if indexRegExp.MatchString(indexName) { 53 if isIndexExcluded(indexName) { 54 continue 55 } 56 newEntry := IndicesBody(indexName) 57 currentAliases, err := virtualtable.GetAliasesAsArray(indexName, myid) 58 if err != nil { 59 log.Errorf("ExpandAndReturnIndexNames: GetAliases returned err=%v", err) 60 newEntry.Aliases = []string{} 61 } else { 62 newEntry.Aliases = currentAliases 63 } 64 indicesEntries = append(indicesEntries, newEntry) 65 } 66 } 67 68 aliasInfos, _ := virtualtable.GetAllAliasesAsMapArray(myid) 69 for aliasName, indexes := range aliasInfos { 70 if indexRegExp.MatchString(aliasName) { 71 newEntry := esutils.ResolveAliasEntry{Name: aliasName, Indices: indexes} 72 aliasesEntries = append(aliasesEntries, newEntry) 73 } 74 75 } 76 77 } else { 78 _, exists := allVirtualTableNames[indexPattern] 79 if exists { 80 newEntry := IndicesBody(indexPattern) 81 currentAliases, err := virtualtable.GetAliasesAsArray(indexPattern, myid) 82 if err != nil { 83 log.Errorf("SendResolveIndexResponse: GetAliases returned err=%v", err) 84 newEntry.Aliases = []string{} 85 } else { 86 newEntry.Aliases = currentAliases 87 } 88 indicesEntries = append(indicesEntries, newEntry) 89 } 90 91 aliasInfos, _ := virtualtable.GetAllAliasesAsMapArray(myid) 92 for aliasName, indexes := range aliasInfos { 93 if aliasName == indexPattern { 94 newEntry := esutils.ResolveAliasEntry{Name: aliasName, Indices: indexes} 95 aliasesEntries = append(aliasesEntries, newEntry) 96 } 97 } 98 } 99 return indicesEntries, aliasesEntries, nil 100 } 101 102 func isIndexExcluded(indexName string) bool { 103 for _, value := range excludedInternalIndices { 104 if strings.ReplaceAll(indexName, "*", "") == value { 105 return true 106 } 107 } 108 return false 109 } 110 111 func SendResolveIndexResponse(ctx *fasthttp.RequestCtx, myid uint64) { 112 113 var resResp esutils.ResolveResponse 114 115 indexPattern := utils.ExtractParamAsString(ctx.UserValue("indexPattern")) 116 117 ctx.SetStatusCode(fasthttp.StatusOK) 118 ctx.Response.Header.Set("Content-Type", "application/json") 119 120 allVirtualTableNames, _ := virtualtable.GetVirtualTableNames(myid) 121 122 if len(allVirtualTableNames) == 0 { 123 resResp = esutils.ResolveResponse{} 124 utils.WriteJsonResponse(ctx, resResp) 125 return 126 } 127 128 indicesEntries, aliasesEntries, err := ExpandAndReturnIndexNames(indexPattern, allVirtualTableNames, myid) 129 if err != nil { 130 log.Errorf("SendResolveIndexResponse: Could not resolve index for indexPattern=%v err=%v", indexPattern, err) 131 return 132 } 133 134 resResp = esutils.ResolveResponse{IndicesEntries: indicesEntries, AliasesEntries: aliasesEntries} 135 utils.WriteJsonResponse(ctx, resResp) 136 137 }