-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Ensure excludePanelID and includePanelDataID works together
* Dont filter panels based on includePanelID and excludePanelID too soon. Always include all the panels on the dashboard struct and only render the ones based on query parameters * Ensure we do not leave gaps in report when panels are excluded in the report in simple layout. * Add an example report with tabular data in docs Signed-off-by: Mahendra Paipuri <[email protected]>
- Loading branch information
1 parent
52f4ec8
commit f62c440
Showing
7 changed files
with
232 additions
and
172 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
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,77 @@ | ||
package report | ||
|
||
import ( | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/mahendrapaipuri/grafana-dashboard-reporter-app/pkg/plugin/dashboard" | ||
) | ||
|
||
// remove removes a element by value in slice and returns a new slice. | ||
func remove[T comparable](l []T, item T) []T { | ||
out := make([]T, 0) | ||
|
||
for _, element := range l { | ||
if element != item { | ||
out = append(out, element) | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
// selectPanels returns panel indexes to render based on IncludePanelIDs and ExcludePanelIDs | ||
// config parameters. | ||
func selectPanels(panels []dashboard.Panel, includeIDs, excludeIDs []string, defaultInclude bool) []int { | ||
var renderPanels []int | ||
|
||
// If includeIDs is empty and default behaviour is to include all, setuo | ||
// includeIDs | ||
if len(includeIDs) == 0 && defaultInclude { | ||
for _, p := range panels { | ||
includeIDs = append(includeIDs, p.ID) | ||
} | ||
} | ||
|
||
for iPanel, panel := range panels { | ||
// Attempt to convert panel ID to int. If we succeed, do direct | ||
// comparison else do prefix check | ||
var doDirectComp bool | ||
if _, err := strconv.ParseInt(panel.ID, 10, 0); err == nil { | ||
doDirectComp = true | ||
} | ||
|
||
for _, id := range includeIDs { | ||
if !doDirectComp { | ||
if strings.HasPrefix(panel.ID, id) && !slices.Contains(renderPanels, iPanel) { | ||
renderPanels = append(renderPanels, iPanel) | ||
} | ||
} else { | ||
if panel.ID == id && !slices.Contains(renderPanels, iPanel) { | ||
renderPanels = append(renderPanels, iPanel) | ||
} | ||
} | ||
} | ||
|
||
exclude := false | ||
|
||
for _, id := range excludeIDs { | ||
if !doDirectComp { | ||
if strings.HasPrefix(panel.ID, id) { | ||
exclude = true | ||
} | ||
} else { | ||
if panel.ID == id { | ||
exclude = true | ||
} | ||
} | ||
} | ||
|
||
if exclude && slices.Contains(renderPanels, iPanel) { | ||
renderPanels = remove(renderPanels, iPanel) | ||
} | ||
} | ||
|
||
return renderPanels | ||
} |
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,117 @@ | ||
package report | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mahendrapaipuri/grafana-dashboard-reporter-app/pkg/plugin/dashboard" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestPanelSelector(t *testing.T) { | ||
Convey("When selecting panels based on integer panel IDs", t, func() { | ||
allPanels := []dashboard.Panel{ | ||
{ID: "1"}, {ID: "2"}, {ID: "3"}, {ID: "4"}, {ID: "15"}, {ID: "26"}, {ID: "37"}, | ||
} | ||
cases := map[string]struct { | ||
IncludeIDs, ExcludeIDs []string | ||
DefaultInclude bool | ||
Result []int | ||
}{ | ||
"empty_true": { | ||
nil, | ||
nil, | ||
true, | ||
[]int{0, 1, 2, 3, 4, 5, 6}, | ||
}, | ||
"empty_false": { | ||
nil, | ||
nil, | ||
false, | ||
nil, | ||
}, | ||
"include": { | ||
[]string{"1", "4", "3"}, | ||
nil, | ||
true, | ||
[]int{0, 2, 3}, | ||
}, | ||
"exclude": { | ||
nil, | ||
[]string{"2", "4", "3"}, | ||
true, | ||
[]int{0, 4, 5, 6}, | ||
}, | ||
"exclude_false": { | ||
nil, | ||
[]string{"2", "4", "3"}, | ||
false, | ||
nil, | ||
}, | ||
"include_and_exclude_1": { | ||
[]string{"1", "4", "6"}, | ||
[]string{"2", "4", "3"}, | ||
true, | ||
[]int{0}, | ||
}, | ||
"include_and_exclude_2": { | ||
[]string{"1"}, | ||
[]string{"2"}, | ||
true, | ||
[]int{0}, | ||
}, | ||
} | ||
|
||
for clName, cl := range cases { | ||
filteredPanels := selectPanels(allPanels, cl.IncludeIDs, cl.ExcludeIDs, cl.DefaultInclude) | ||
|
||
Convey("Panels should be properly selected: "+clName, func() { | ||
So(filteredPanels, ShouldResemble, cl.Result) | ||
}) | ||
} | ||
}) | ||
|
||
// For Grafana >= v11.3.0 | ||
Convey("When filtering png panels based on string panel IDs", t, func() { | ||
allPanels := []dashboard.Panel{ | ||
{ID: "panel-1-clone-0"}, {ID: "panel-1-clone-1"}, {ID: "panel-3"}, {ID: "panel-4"}, {ID: "panel-5"}, {ID: "panel-6"}, {ID: "panel-7"}, | ||
} | ||
cases := map[string]struct { | ||
IncludeIDs, ExcludeIDs []string | ||
DefaultInclude bool | ||
Result []int | ||
}{ | ||
"include": { | ||
[]string{"panel-1", "panel-4", "panel-6"}, | ||
nil, | ||
true, | ||
[]int{0, 1, 3, 5}, | ||
}, | ||
"exclude": { | ||
nil, | ||
[]string{"panel-1", "panel-4", "panel-3"}, | ||
true, | ||
[]int{4, 5, 6}, | ||
}, | ||
"exclude_false": { | ||
nil, | ||
[]string{"panel-1", "panel-4", "panel-3"}, | ||
false, | ||
nil, | ||
}, | ||
"include_and_exclude": { | ||
[]string{"panel-1", "panel-4", "panel-6"}, | ||
[]string{"panel-2", "panel-4", "panel-3"}, | ||
true, | ||
[]int{0, 1, 5}, | ||
}, | ||
} | ||
|
||
for clName, cl := range cases { | ||
filteredPanels := selectPanels(allPanels, cl.IncludeIDs, cl.ExcludeIDs, cl.DefaultInclude) | ||
|
||
Convey("Panels should be properly filtered: "+clName, func() { | ||
So(filteredPanels, ShouldResemble, cl.Result) | ||
}) | ||
} | ||
}) | ||
} |
Oops, something went wrong.