From 40da8fd0198459c6c921e912b8e5199c2bafeb78 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 2 May 2024 13:10:07 +0200 Subject: [PATCH] Implement Elasticsearch/Clickhouse source resolver --- quesma/quesma/source_resolver.go | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 quesma/quesma/source_resolver.go diff --git a/quesma/quesma/source_resolver.go b/quesma/quesma/source_resolver.go new file mode 100644 index 000000000..c3bc43c36 --- /dev/null +++ b/quesma/quesma/source_resolver.go @@ -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 + } + } +}