Skip to content

Commit

Permalink
Implement Elasticsearch/Clickhouse source resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed May 2, 2024
1 parent 3f7549d commit 40da8fd
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions quesma/quesma/source_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package quesma

import (
"context"
"mitmproxy/quesma/clickhouse"
"mitmproxy/quesma/elasticsearch"
"mitmproxy/quesma/quesma/config"
"slices"
"strings"
)

const (
sourceElasticsearch = "elasticsearch"
sourceClickhouse = "clickhouse"
sourceBoth = "both"
sourceNone = "none"
)

func ResolveSources(indexPattern string, cfg config.QuesmaConfiguration, im elasticsearch.IndexManagement, lm *clickhouse.LogManager) string {
if elasticsearch.IsIndexPattern(indexPattern) {
matchesElastic := []string{}
matchesClickhouse := []string{}

for _, pattern := range strings.Split(indexPattern, ",") {
for indexName := range im.GetSourceNamesMatching(pattern) {
matchesElastic = append(matchesElastic, indexName)
}

for _, tableName := range lm.ResolveIndexes(context.Background(), pattern) {
matchesClickhouse = append(matchesClickhouse, tableName)
}
}
slices.Sort(matchesElastic)
slices.Sort(matchesClickhouse)
matchesElastic = slices.Compact(matchesElastic)
matchesClickhouse = slices.Compact(matchesClickhouse)
slices.DeleteFunc(matchesElastic, func(s string) bool {
return slices.Contains(matchesClickhouse, s)
})

switch {
case len(matchesElastic) > 0 && len(matchesClickhouse) > 0:
return sourceBoth
case len(matchesElastic) > 0:
return sourceElasticsearch
case len(matchesClickhouse) > 0:
return sourceClickhouse
default:
return sourceNone
}
} else {
if c, exists := cfg.IndexConfig[indexPattern]; exists {
if c.Enabled {
return sourceClickhouse
} else {
return sourceElasticsearch
}
} else {
return sourceElasticsearch
}
}
}

0 comments on commit 40da8fd

Please sign in to comment.