Stats: Optimize getting folder stats (#103033)

This commit is contained in:
Stephanie Hingtgen
2025-03-28 20:17:50 -05:00
committed by GitHub
parent 28b142e951
commit f84654d162
16 changed files with 109 additions and 27 deletions
+2 -2
View File
@@ -307,13 +307,13 @@ func (d *dashboardStore) Count(ctx context.Context, scopeParams *quota.ScopePara
return u, nil
}
func (d *dashboardStore) CountInOrg(ctx context.Context, orgID int64) (int64, error) {
func (d *dashboardStore) CountInOrg(ctx context.Context, orgID int64, isFolder bool) (int64, error) {
type result struct {
Count int64
}
r := result{}
if err := d.store.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
rawSQL := fmt.Sprintf("SELECT COUNT(*) AS count FROM dashboard WHERE org_id=? AND is_folder=%s", d.store.GetDialect().BooleanStr(false))
rawSQL := fmt.Sprintf("SELECT COUNT(*) AS count FROM dashboard WHERE org_id=? AND is_folder=%s", d.store.GetDialect().BooleanStr(isFolder))
if _, err := sess.SQL(rawSQL, orgID).Get(&r); err != nil {
return err
}
@@ -85,13 +85,17 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
require.Positive(t, len(savedFolder.UID))
})
t.Run("Should be able to get dashboard counts per org", func(t *testing.T) {
t.Run("Should be able to get counts per org", func(t *testing.T) {
setup()
count, err := dashboardStore.CountInOrg(context.Background(), 1)
count, err := dashboardStore.CountInOrg(context.Background(), 1, false)
require.NoError(t, err)
require.Equal(t, int64(3), count)
count, err = dashboardStore.CountInOrg(context.Background(), 2)
count, err = dashboardStore.CountInOrg(context.Background(), 2, false)
require.NoError(t, err)
require.Equal(t, int64(1), count)
count, err = dashboardStore.CountInOrg(context.Background(), 1, true)
require.NoError(t, err)
require.Equal(t, int64(1), count)
})