Skip to content

Commit

Permalink
add references to dashboard and dcrease number of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Aug 31, 2021
1 parent bc5503f commit 283a353
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions libbeat/dashboards/kibana_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,14 @@ func (loader KibanaLoader) ImportDashboard(file string) error {
return fmt.Errorf("fail to read dashboard from file %s: %v", file, err)
}

content = ReplaceIndexInDashboardObject(loader.config.Index, content)
content = EncodeJSONObjects(content)
content = ReplaceStringInDashboard("CHANGEME_HOSTNAME", loader.hostname, content)

err = loader.importReferences(file, content)
if err != nil {
return fmt.Errorf("error loading references of dashboard: %+v", err)
}
content = loader.formatDashboardAssets(content)

var obj common.MapStr
err = json.Unmarshal(content, &obj)
dashboardWithReferences, err := loader.addReferences(file, content)
if err != nil {
return err
return fmt.Errorf("error getting references of dashboard: %+v", err)
}

if err := loader.client.ImportMultiPartFormFile(importAPI, params, correctExtension(file), obj.String()); err != nil {
if err := loader.client.ImportMultiPartFormFile(importAPI, params, correctExtension(file), dashboardWithReferences); err != nil {
return fmt.Errorf("error dashboard asset: %+v", err)
}

Expand All @@ -184,14 +176,15 @@ type dashboardReference struct {
Type string `json:"type"`
}

func (loader KibanaLoader) importReferences(path string, dashboard []byte) error {
func (loader KibanaLoader) addReferences(path string, dashboard []byte) (string, error) {
var d dashboardObj
err := json.Unmarshal(dashboard, &d)
if err != nil {
return fmt.Errorf("failed to parse dashboard references: %+v", err)
return "", fmt.Errorf("failed to parse dashboard references: %+v", err)
}

base := filepath.Dir(path)
var result string
for _, ref := range d.References {
if ref.Type == "index-pattern" {
continue
Expand All @@ -200,12 +193,35 @@ func (loader KibanaLoader) importReferences(path string, dashboard []byte) error
if _, ok := loader.loadedAssets[referencePath]; ok {
continue
}
err := loader.ImportDashboard(referencePath)
refContents, err := ioutil.ReadFile(referencePath)
if err != nil {
return "", fmt.Errorf("fail to read referenced asset from file %s: %v", referencePath, err)
}
refContents = loader.formatDashboardAssets(refContents)
refContentsWithReferences, err := loader.addReferences(referencePath, refContents)
if err != nil {
return fmt.Errorf("error loading reference of %s: %s %s: %+v", path, ref.Type, ref.ID, err)
return "", fmt.Errorf("failed to get references of %s: %+v", referencePath, err)
}

result += refContentsWithReferences
loader.loadedAssets[referencePath] = true
}
return nil

var res common.MapStr
err = json.Unmarshal(dashboard, &res)
if err != nil {
return "", fmt.Errorf("failed to convert asset: %+v", err)
}
result += res.String() + "\n"

return result, nil
}

func (loader KibanaLoader) formatDashboardAssets(content []byte) []byte {
content = ReplaceIndexInDashboardObject(loader.config.Index, content)
content = EncodeJSONObjects(content)
content = ReplaceStringInDashboard("CHANGEME_HOSTNAME", loader.hostname, content)
return content
}

func correctExtension(file string) string {
Expand Down

0 comments on commit 283a353

Please sign in to comment.