-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from clidey/hk/feature/elasticsearch
[Feature] Add support for Elastic Search
- Loading branch information
Showing
20 changed files
with
530 additions
and
23 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ enum DatabaseType { | |
Sqlite3, | ||
MongoDB, | ||
Redis, | ||
ElasticSearch, | ||
} | ||
|
||
type Column { | ||
|
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
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,42 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/clidey/whodb/core/src/common" | ||
"github.com/clidey/whodb/core/src/engine" | ||
"github.com/elastic/go-elasticsearch/v8" | ||
) | ||
|
||
func DB(config *engine.PluginConfig) (*elasticsearch.Client, error) { | ||
var addresses []string | ||
port := common.GetRecordValueOrDefault(config.Credentials.Advanced, "Port", "9200") | ||
sslMode := common.GetRecordValueOrDefault(config.Credentials.Advanced, "SSL Mode", "disable") | ||
if sslMode == "enable" { | ||
addresses = []string{ | ||
fmt.Sprintf("https://%s:%s", config.Credentials.Hostname, port), | ||
} | ||
} else { | ||
addresses = []string{ | ||
fmt.Sprintf("http://%s:%s", config.Credentials.Hostname, port), | ||
} | ||
} | ||
|
||
cfg := elasticsearch.Config{ | ||
Addresses: addresses, | ||
Username: config.Credentials.Username, | ||
Password: config.Credentials.Password, | ||
} | ||
|
||
client, err := elasticsearch.NewClient(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := client.Info() | ||
if err != nil || res.IsError() { | ||
return nil, fmt.Errorf("error pinging Elasticsearch: %v", err) | ||
} | ||
|
||
return client, nil | ||
} |
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,159 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/clidey/whodb/core/src/engine" | ||
) | ||
|
||
type ElasticSearchPlugin struct{} | ||
|
||
func (p *ElasticSearchPlugin) IsAvailable(config *engine.PluginConfig) bool { | ||
client, err := DB(config) | ||
if err != nil { | ||
return false | ||
} | ||
res, err := client.Info() | ||
if err != nil || res.IsError() { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (p *ElasticSearchPlugin) GetDatabases() ([]string, error) { | ||
return nil, errors.ErrUnsupported | ||
} | ||
|
||
func (p *ElasticSearchPlugin) GetSchema(config *engine.PluginConfig) ([]string, error) { | ||
return nil, errors.ErrUnsupported | ||
} | ||
|
||
func (p *ElasticSearchPlugin) GetStorageUnits(config *engine.PluginConfig, database string) ([]engine.StorageUnit, error) { | ||
client, err := DB(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := client.Indices.Stats() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.IsError() { | ||
return nil, fmt.Errorf("error getting stats for indices: %s", res.String()) | ||
} | ||
|
||
var stats map[string]interface{} | ||
if err := json.NewDecoder(res.Body).Decode(&stats); err != nil { | ||
return nil, err | ||
} | ||
|
||
indicesStats := stats["indices"].(map[string]interface{}) | ||
storageUnits := make([]engine.StorageUnit, 0, len(indicesStats)) | ||
|
||
for indexName, indexStatsInterface := range indicesStats { | ||
indexStats := indexStatsInterface.(map[string]interface{}) | ||
primaries := indexStats["primaries"].(map[string]interface{}) | ||
docs := primaries["docs"].(map[string]interface{}) | ||
store := primaries["store"].(map[string]interface{}) | ||
|
||
storageUnit := engine.StorageUnit{ | ||
Name: indexName, | ||
Attributes: []engine.Record{ | ||
{Key: "Storage Size", Value: fmt.Sprintf("%v", store["size_in_bytes"])}, | ||
{Key: "Count", Value: fmt.Sprintf("%v", docs["count"])}, | ||
}, | ||
} | ||
storageUnits = append(storageUnits, storageUnit) | ||
} | ||
|
||
return storageUnits, nil | ||
} | ||
|
||
func (p *ElasticSearchPlugin) GetRows(config *engine.PluginConfig, database, collection, filter string, pageSize, pageOffset int) (*engine.GetRowsResult, error) { | ||
client, err := DB(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var elasticSearchConditions map[string]interface{} | ||
if len(filter) > 0 { | ||
if err := json.Unmarshal([]byte(filter), &elasticSearchConditions); err != nil { | ||
return nil, fmt.Errorf("invalid filter format: %v", err) | ||
} | ||
} | ||
|
||
query := map[string]interface{}{ | ||
"from": pageOffset, | ||
"size": pageSize, | ||
} | ||
|
||
for key, value := range elasticSearchConditions { | ||
query[key] = value | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := json.NewEncoder(&buf).Encode(query); err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := client.Search( | ||
client.Search.WithContext(context.Background()), | ||
client.Search.WithIndex(collection), | ||
client.Search.WithBody(&buf), | ||
client.Search.WithTrackTotalHits(true), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.IsError() { | ||
return nil, fmt.Errorf("error searching documents: %s", res.String()) | ||
} | ||
|
||
var searchResult map[string]interface{} | ||
if err := json.NewDecoder(res.Body).Decode(&searchResult); err != nil { | ||
return nil, err | ||
} | ||
|
||
hits := searchResult["hits"].(map[string]interface{})["hits"].([]interface{}) | ||
result := &engine.GetRowsResult{ | ||
Columns: []engine.Column{ | ||
{Name: "document", Type: "Document"}, | ||
}, | ||
Rows: [][]string{}, | ||
} | ||
|
||
for _, hit := range hits { | ||
hitMap := hit.(map[string]interface{}) | ||
source := hitMap["_source"] | ||
id := hitMap["_id"] | ||
document := map[string]interface{}{} | ||
document["_id"] = id | ||
document["source"] = source | ||
jsonBytes, err := json.Marshal(document) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result.Rows = append(result.Rows, []string{string(jsonBytes)}) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (p *ElasticSearchPlugin) RawExecute(config *engine.PluginConfig, query string) (*engine.GetRowsResult, error) { | ||
return nil, errors.New("unsupported operation") | ||
} | ||
|
||
func NewElasticSearchPlugin() *engine.Plugin { | ||
return &engine.Plugin{ | ||
Type: engine.DatabaseType_ElasticSearch, | ||
PluginFunctions: &ElasticSearchPlugin{}, | ||
} | ||
} |
Oops, something went wrong.