-
Notifications
You must be signed in to change notification settings - Fork 140
render dashboards as collapsible tables #3518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jakubgalecki0
wants to merge
4
commits into
elastic:main
Choose a base branch
from
jakubgalecki0:render_dashboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e22f2dc
render dashboards as collapsible tables
jakubgalecki0 d35d460
Merge branch 'main' into render_dashboard
jakubgalecki0 e735c38
Merge branch 'main' into render_dashboard
jakubgalecki0 4684198
Merge branch 'main' into render_dashboard
jakubgalecki0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,91 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package docs | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| type dashboard struct { | ||
| Attributes struct { | ||
| Title string | ||
| Description string | ||
| } | ||
| } | ||
|
|
||
| func renderDashboards(packageRoot string) (string, error) { | ||
| dashboardsDir := filepath.Join(packageRoot, "kibana", "dashboard") | ||
|
|
||
| if _, err := os.Stat(dashboardsDir); os.IsNotExist(err) { | ||
| return "", nil | ||
| } | ||
|
|
||
| var dashboards []dashboard | ||
|
|
||
| err := filepath.WalkDir(dashboardsDir, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if path == dashboardsDir { | ||
| return nil | ||
| } | ||
|
|
||
| if d.IsDir() { | ||
| return filepath.SkipDir | ||
| } | ||
|
|
||
| if filepath.Ext(d.Name()) != ".json" { | ||
| return nil | ||
| } | ||
|
|
||
| rawDashboard, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read dashboard file: %w", err) | ||
| } | ||
|
|
||
| var dash dashboard | ||
| if err := json.Unmarshal(rawDashboard, &dash); err != nil { | ||
| return fmt.Errorf("failed to unmarshal dashboard JSON: %w", err) | ||
| } | ||
|
|
||
| dashboards = append(dashboards, dash) | ||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return "", fmt.Errorf("parsing dashboards failed: %w", err) | ||
| } | ||
|
|
||
| var builder strings.Builder | ||
|
|
||
| if len(dashboards) != 0 { | ||
| builder.WriteString("**The following dashboards are available:**\n\n") | ||
| renderDashboardsCollapsibleTable(&builder, dashboards) | ||
| builder.WriteString("\n") | ||
| } | ||
|
|
||
| return builder.String(), nil | ||
| } | ||
|
|
||
| func renderDashboardsCollapsibleTable(builder *strings.Builder, dashboards []dashboard) { | ||
| builder.WriteString("<details>\n") | ||
| builder.WriteString("<summary>View the dashboards</summary>\n\n") | ||
| builder.WriteString("| Dashboard | Description |\n") | ||
| builder.WriteString("|---|---|\n") | ||
| for _, d := range dashboards { | ||
| title := strings.TrimSpace(d.Attributes.Title) | ||
| description := strings.TrimSpace(strings.ReplaceAll(d.Attributes.Description, "\n", " ")) | ||
| fmt.Fprintf(builder, "| **%s** | %s |\n", | ||
| escaper.Replace(title), | ||
| escaper.Replace(description)) | ||
| } | ||
| builder.WriteString("\n</details>\n") | ||
| } | ||
This file contains hidden or 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,263 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package docs | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRenderDashboards(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| setupFunc func(t *testing.T) string | ||
| expectError bool | ||
| expectEmpty bool | ||
| validateFunc func(t *testing.T, result string) | ||
| }{ | ||
| { | ||
| name: "no dashboards directory", | ||
| setupFunc: func(t *testing.T) string { | ||
| return t.TempDir() | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: true, | ||
| }, | ||
| { | ||
| name: "empty dashboards directory", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: true, | ||
| }, | ||
| { | ||
| name: "single valid dashboard", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash := `{ | ||
| "attributes": { | ||
| "title": "[PostgreSQL OTel Copy] Overview", | ||
| "description": "Overview of PostgreSQL health and golden signals." | ||
| } | ||
| }` | ||
| dashFile := filepath.Join(dashboardsDir, "overview.json") | ||
| require.NoError(t, os.WriteFile(dashFile, []byte(dash), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Contains(t, result, "**The following dashboards are available:**") | ||
| assert.Contains(t, result, "<details>") | ||
| assert.Contains(t, result, "<summary>View the dashboards</summary>") | ||
| assert.Contains(t, result, "</details>") | ||
| assert.Contains(t, result, "| Dashboard | Description |") | ||
| assert.Contains(t, result, "|---|---|") | ||
| assert.Contains(t, result, "| **[PostgreSQL OTel Copy] Overview** | Overview of PostgreSQL health and golden signals. |") | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple valid dashboards", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash1 := `{ | ||
| "attributes": { | ||
| "title": "First Dashboard", | ||
| "description": "First description" | ||
| } | ||
| }` | ||
| dash2 := `{ | ||
| "attributes": { | ||
| "title": "Second Dashboard", | ||
| "description": "Second description" | ||
| } | ||
| }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "d1.json"), []byte(dash1), 0o644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "d2.json"), []byte(dash2), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Contains(t, result, "<details>") | ||
| assert.Contains(t, result, "<summary>View the dashboards</summary>") | ||
| assert.Contains(t, result, "</details>") | ||
| assert.Contains(t, result, "| Dashboard | Description |") | ||
| assert.Contains(t, result, "|---|---|") | ||
| assert.Contains(t, result, "| **First Dashboard** | First description |") | ||
| assert.Contains(t, result, "| **Second Dashboard** | Second description |") | ||
| }, | ||
| }, | ||
| { | ||
| name: "skip non-json files", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash := `{ | ||
| "attributes": { | ||
| "title": "Valid Dashboard", | ||
| "description": "Valid description" | ||
| } | ||
| }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "valid.json"), []byte(dash), 0o644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "ignore.txt"), []byte("ignored"), 0o644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "README.md"), []byte("# readme"), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Contains(t, result, "| **Valid Dashboard** | Valid description |") | ||
| assert.NotContains(t, result, "ignored") | ||
| assert.NotContains(t, result, "readme") | ||
| }, | ||
| }, | ||
| { | ||
| name: "skip subdirectories", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash := `{ | ||
| "attributes": { | ||
| "title": "Root Dashboard", | ||
| "description": "Root description" | ||
| } | ||
| }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "root.json"), []byte(dash), 0o644)) | ||
|
|
||
| subDir := filepath.Join(dashboardsDir, "subdir") | ||
| require.NoError(t, os.MkdirAll(subDir, 0o755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(subDir, "nested.json"), []byte(dash), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Equal(t, 1, strings.Count(result, "Root Dashboard")) | ||
| }, | ||
| }, | ||
| { | ||
| name: "unreadable file", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| unreadableFile := filepath.Join(dashboardsDir, "unreadable.json") | ||
| require.NoError(t, os.WriteFile(unreadableFile, []byte("content"), 0o000)) | ||
| return tmpDir | ||
| }, | ||
| expectError: true, | ||
| expectEmpty: false, | ||
| }, | ||
| { | ||
| name: "invalid json file", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| invalidJSON := `{ "attributes": { "title": "Invalid" }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "invalid.json"), []byte(invalidJSON), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: true, | ||
| expectEmpty: false, | ||
| }, | ||
| { | ||
| name: "special characters are escaped", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash := `{ | ||
| "attributes": { | ||
| "title": "Dashboard with *bold* and {braces}", | ||
| "description": "Description with <angle> and {curly} brackets" | ||
| } | ||
| }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "special.json"), []byte(dash), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Contains(t, result, `\*bold\*`) | ||
| assert.Contains(t, result, `\{braces\}`) | ||
| assert.Contains(t, result, `\<angle\>`) | ||
| assert.Contains(t, result, `\{curly\}`) | ||
| assert.NotContains(t, result, "*bold*") | ||
| assert.NotContains(t, result, "{braces}") | ||
| assert.NotContains(t, result, "<angle>") | ||
| }, | ||
| }, | ||
| { | ||
| name: "newlines in description are flattened", | ||
| setupFunc: func(t *testing.T) string { | ||
| tmpDir := t.TempDir() | ||
| dashboardsDir := filepath.Join(tmpDir, "kibana", "dashboard") | ||
| require.NoError(t, os.MkdirAll(dashboardsDir, 0o755)) | ||
|
|
||
| dash := `{ | ||
| "attributes": { | ||
| "title": "Multiline", | ||
| "description": "Line one.\nLine two." | ||
| } | ||
| }` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dashboardsDir, "ml.json"), []byte(dash), 0o644)) | ||
| return tmpDir | ||
| }, | ||
| expectError: false, | ||
| expectEmpty: false, | ||
| validateFunc: func(t *testing.T, result string) { | ||
| assert.Contains(t, result, "| **Multiline** | Line one. Line two. |") | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| packageRoot := tc.setupFunc(t) | ||
|
|
||
| result, err := renderDashboards(packageRoot) | ||
|
|
||
| if tc.expectError { | ||
| assert.Error(t, err) | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
|
|
||
| if tc.expectEmpty { | ||
| assert.Empty(t, result) | ||
| } else { | ||
| assert.NotEmpty(t, result) | ||
| if tc.validateFunc != nil { | ||
| tc.validateFunc(t, result) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,3 @@ | ||
| dependencies: | ||
| ecs: | ||
| reference: git@1.10 |
This file contains hidden or 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,5 @@ | ||
| # Readme | ||
|
|
||
| ## Dashboards | ||
|
|
||
| {{ dashboards }} |
This file contains hidden or 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,6 @@ | ||
| # newer versions go on top | ||
| - version: "0.0.1" | ||
| changes: | ||
| - description: Initial draft of the package | ||
| type: enhancement | ||
| link: https://github.com/elastic/integrations/pull/1 |
This file contains hidden or 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,15 @@ | ||
| # Readme | ||
|
|
||
| ## Dashboards | ||
|
|
||
| **The following dashboards are available:** | ||
|
|
||
| <details> | ||
| <summary>View the dashboards</summary> | ||
|
|
||
| | Dashboard | Description | | ||
| |---|---| | ||
| | **[Example] Dashboard** | An example dashboard for testing the rendering of dashboards in the package README. | | ||
|
|
||
| </details> | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
docs/dashboards.go:78The
escaperreplaces*,{,},<,>but not|, so pipe characters in dashboard titles or descriptions break the markdown table. A title like "CPU | Memory" produces an extra table column, corrupting the layout. Add|to the escaper replacement list.var escaper = strings.NewReplacer("*", "\\*", "{", "\\{", "}", "\\}", "<", "\\<", ">", "\\>")🤖 Copy this AI Prompt to have your agent fix this: