Cloud migration: Refactor get by org ID to dashboard svc (#98646)

This commit is contained in:
Stephanie Hingtgen
2025-01-07 23:33:45 +02:00
committed by GitHub
parent 9b1ecaedda
commit 58ed8a9ec2
11 changed files with 167 additions and 26 deletions
@@ -1048,6 +1048,21 @@ func (d *dashboardStore) GetAllDashboards(ctx context.Context) ([]*dashboards.Da
return dashboards, nil
}
func (d *dashboardStore) GetAllDashboardsByOrgId(ctx context.Context, orgID int64) ([]*dashboards.Dashboard, error) {
ctx, span := tracer.Start(ctx, "dashboards.database.GetAllDashboardsByOrgId")
defer span.End()
var dashs = make([]*dashboards.Dashboard, 0)
err := d.store.WithDbSession(ctx, func(session *db.Session) error {
// "deleted IS NULL" is to avoid deleted dashboards
return session.Where("org_id = ? AND deleted IS NULL", orgID).Find(&dashs)
})
if err != nil {
return nil, err
}
return dashs, nil
}
func (d *dashboardStore) GetSoftDeletedExpiredDashboards(ctx context.Context, duration time.Duration) ([]*dashboards.Dashboard, error) {
ctx, span := tracer.Start(ctx, "dashboards.database.GetSoftDeletedExpiredDashboards")
defer span.End()
@@ -266,6 +266,32 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
assert.Equal(t, len(queryResult), 1)
})
t.Run("Should be able to get all dashboards for an org", func(t *testing.T) {
setup()
dash1 := insertTestDashboard(t, dashboardStore, "org3test1", 3, 0, "", false, "org 1 test 1")
dash2 := insertTestDashboard(t, dashboardStore, "org3test2", 3, 0, "", false, "org 1 test 2")
dash3 := insertTestDashboard(t, dashboardStore, "org4test1", 4, 0, "", false, "org 2 test 1")
dashs, err := dashboardStore.GetAllDashboardsByOrgId(context.Background(), 3)
require.NoError(t, err)
require.Equal(t, len(dashs), 2)
uids := []string{}
for _, d := range dashs {
uids = append(uids, d.UID)
}
require.Contains(t, uids, dash1.UID)
require.Contains(t, uids, dash2.UID)
dashs, err = dashboardStore.GetAllDashboardsByOrgId(context.Background(), 4)
require.NoError(t, err)
require.Equal(t, len(dashs), 1)
require.Equal(t, dash3.UID, dashs[0].UID)
dashs, err = dashboardStore.GetAllDashboardsByOrgId(context.Background(), 5)
require.NoError(t, err)
require.Equal(t, len(dashs), 0)
})
t.Run("Should be able to create dashboard", func(t *testing.T) {
setup()
cmd := dashboards.SaveDashboardCommand{