-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Elasticsearch/Clickhouse source resolver
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |