Chore: Advisor stats (#103711)

This commit is contained in:
Andres Martinez Gotor
2025-04-10 10:51:00 +02:00
committed by GitHub
parent 3f3a4c1e8a
commit 89c70fcdcf
7 changed files with 314 additions and 9 deletions
@@ -0,0 +1,106 @@
package advisor
import (
"context"
"github.com/grafana/grafana-app-sdk/k8s"
"github.com/grafana/grafana-app-sdk/resource"
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks/datasourcecheck"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks/plugincheck"
"github.com/grafana/grafana/pkg/services/apiserver"
apiserverrequest "github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
"github.com/grafana/grafana/pkg/setting"
)
type AdvisorStats interface {
ReportSummary(ctx context.Context) (*ReportInfo, error)
}
type Service struct {
cfg *setting.Cfg
namespace string
clientGenerator func(ctx context.Context) (resource.Client, error)
}
func ProvideService(
cfg *setting.Cfg,
restConfigProvider apiserver.RestConfigProvider,
) (*Service, error) {
namespace := "default"
if cfg.StackID != "" {
namespace = apiserverrequest.GetNamespaceMapper(cfg)(1)
}
return &Service{
cfg: cfg,
namespace: namespace,
clientGenerator: func(ctx context.Context) (resource.Client, error) {
kubeConfig, err := restConfigProvider.GetRestConfig(ctx)
if err != nil {
return nil, err
}
clientGenerator := k8s.NewClientRegistry(*kubeConfig, k8s.ClientConfig{})
return clientGenerator.ClientFor(advisorv0alpha1.CheckKind())
},
}, nil
}
type ReportInfo struct {
PluginsOutdated int
PluginsDeprecated int
DatasourcesUnhealthy int
}
func isMoreRecent(check1 resource.Object, check2 resource.Object) bool {
return check1.GetCommonMetadata().CreationTimestamp.After(check2.GetCommonMetadata().CreationTimestamp)
}
// findLatestCheck returns the most recent check of the specified type from the list
func findLatestCheck(checkList []resource.Object, checkType string) *advisorv0alpha1.Check {
var latestCheck *advisorv0alpha1.Check
for _, check := range checkList {
currentCheckType := check.GetLabels()[checks.TypeLabel]
if currentCheckType != checkType {
continue
}
if latestCheck == nil || isMoreRecent(check, latestCheck) {
latestCheck = check.(*advisorv0alpha1.Check)
}
}
return latestCheck
}
func (s *Service) ReportSummary(ctx context.Context) (*ReportInfo, error) {
client, err := s.clientGenerator(ctx)
if err != nil {
return nil, err
}
checkList, err := client.List(ctx, s.namespace, resource.ListOptions{})
if err != nil {
return nil, err
}
latestPluginCheck := findLatestCheck(checkList.GetItems(), plugincheck.CheckID)
latestDatasourceCheck := findLatestCheck(checkList.GetItems(), datasourcecheck.CheckID)
reportInfo := &ReportInfo{}
if latestPluginCheck != nil {
for _, failure := range latestPluginCheck.CheckStatus.Report.Failures {
if failure.StepID == plugincheck.UpdateStepID {
reportInfo.PluginsOutdated++
} else if failure.StepID == plugincheck.DeprecationStepID {
reportInfo.PluginsDeprecated++
}
}
}
if latestDatasourceCheck != nil {
for _, failure := range latestDatasourceCheck.CheckStatus.Report.Failures {
if failure.StepID == datasourcecheck.HealthCheckStepID {
reportInfo.DatasourcesUnhealthy++
}
}
}
return reportInfo, nil
}
@@ -0,0 +1,163 @@
package advisor
import (
"context"
"testing"
"time"
"github.com/grafana/grafana-app-sdk/resource"
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks/datasourcecheck"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks/plugincheck"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestService_ReportSummary(t *testing.T) {
now := time.Now()
earlier := now.Add(-1 * time.Hour)
tests := []struct {
name string
config *setting.Cfg
restConfigErr error
listItems []resource.Object
listErr error
expectedReport *ReportInfo
expectedErr error
}{
{
name: "should return correct report with multiple checks",
config: &setting.Cfg{
StackID: "test-stack",
},
listItems: []resource.Object{
&advisorv0alpha1.Check{
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: metav1.Time{Time: earlier},
Labels: map[string]string{
checks.TypeLabel: plugincheck.CheckID,
},
},
CheckStatus: advisorv0alpha1.CheckStatus{
Report: advisorv0alpha1.CheckV0alpha1StatusReport{
Failures: []advisorv0alpha1.CheckReportFailure{
{StepID: plugincheck.UpdateStepID},
},
},
},
},
&advisorv0alpha1.Check{
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: metav1.Time{Time: now},
Labels: map[string]string{
checks.TypeLabel: plugincheck.CheckID,
},
},
CheckStatus: advisorv0alpha1.CheckStatus{
Report: advisorv0alpha1.CheckV0alpha1StatusReport{
Failures: []advisorv0alpha1.CheckReportFailure{
{StepID: plugincheck.UpdateStepID},
{StepID: plugincheck.DeprecationStepID},
},
},
},
},
&advisorv0alpha1.Check{
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: metav1.Time{Time: now},
Labels: map[string]string{
checks.TypeLabel: datasourcecheck.CheckID,
},
},
CheckStatus: advisorv0alpha1.CheckStatus{
Report: advisorv0alpha1.CheckV0alpha1StatusReport{
Failures: []advisorv0alpha1.CheckReportFailure{
{StepID: datasourcecheck.HealthCheckStepID},
{StepID: datasourcecheck.HealthCheckStepID},
},
},
},
},
},
expectedReport: &ReportInfo{
PluginsOutdated: 1,
PluginsDeprecated: 1,
DatasourcesUnhealthy: 2,
},
},
{
name: "should handle empty check list",
config: &setting.Cfg{
StackID: "test-stack",
},
listItems: []resource.Object{},
expectedReport: &ReportInfo{
PluginsOutdated: 0,
PluginsDeprecated: 0,
DatasourcesUnhealthy: 0,
},
},
{
name: "should handle list error",
config: &setting.Cfg{
StackID: "test-stack",
},
listErr: assert.AnError,
expectedErr: assert.AnError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
client := &mockClient{
listItems: tt.listItems,
listErr: tt.listErr,
}
service := &Service{
cfg: tt.config,
namespace: "stacks-0",
clientGenerator: func(ctx context.Context) (resource.Client, error) { return client, nil },
}
// Execute
report, err := service.ReportSummary(context.Background())
// Verify
if tt.expectedErr != nil {
assert.Error(t, err)
assert.Equal(t, tt.expectedErr, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expectedReport, report)
})
}
}
type mockClient struct {
resource.Client
listItems []resource.Object
listErr error
}
func (m *mockClient) List(ctx context.Context, namespace string, opts resource.ListOptions) (resource.ListObject, error) {
if m.listErr != nil {
return nil, m.listErr
}
return &mockListObject{items: m.listItems}, nil
}
type mockListObject struct {
resource.ListObject
items []resource.Object
}
func (m *mockListObject) GetItems() []resource.Object {
return m.items
}
@@ -33,6 +33,7 @@ import (
"github.com/grafana/grafana/pkg/services/caching"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/oauthtoken"
"github.com/grafana/grafana/pkg/services/pluginsintegration/advisor"
"github.com/grafana/grafana/pkg/services/pluginsintegration/angulardetectorsprovider"
"github.com/grafana/grafana/pkg/services/pluginsintegration/angularinspector"
"github.com/grafana/grafana/pkg/services/pluginsintegration/angularpatternsstore"
@@ -129,6 +130,8 @@ var WireSet = wire.NewSet(
pluginassets.ProvideService,
plugininstaller.ProvidePreinstall,
wire.Bind(new(plugininstaller.Preinstall), new(*plugininstaller.PreinstallImpl)),
advisor.ProvideService,
wire.Bind(new(advisor.AdvisorStats), new(*advisor.Service)),
)
// WireExtensionSet provides a wire.ProviderSet of plugin providers that can be