feat(nested folders): Add CountAlertRulesInFolder to ngalert store (#58269)
* chore: refactor CountDashboardsInFolder to use the more efficient Count() sql function * feat(nested folders): Add CountAlertRulesInFolder to ngalert store This commit adds CountAlertRulesInFolder and a new model for the CountAlertRulesQuery. It returns a count of alert rules associated with a given orgID and parent folder UID. (the namespace referenced inside alert rules is the parent folder). I'm not sure where this belongs in the ngalert service, so that will come in a future commit.
This commit is contained in:
@@ -1018,17 +1018,20 @@ func (d *DashboardStore) GetDashboardTags(ctx context.Context, query *models.Get
|
||||
})
|
||||
}
|
||||
|
||||
// CountDashboardsInFolder returns a count of all dashboards associated with the
|
||||
// given parent folder ID.
|
||||
//
|
||||
// This will be updated to take CountDashboardsInFolderQuery as an argument and
|
||||
// lookup dashboards using the ParentFolderUID when the NestedFolder
|
||||
// implementation is complete.
|
||||
// lookup dashboards using the ParentFolderUID when dashboards are associated with a parent folder UID instead of ID.
|
||||
func (d *DashboardStore) CountDashboardsInFolder(
|
||||
ctx context.Context, req *dashboards.CountDashboardsInFolderRequest) (int64, error) {
|
||||
var dashboards = make([]*models.Dashboard, 0)
|
||||
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var count int64
|
||||
var err error
|
||||
err = d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
session := sess.In("folder_id", req.FolderID).In("org_id", req.OrgID).
|
||||
In("is_folder", d.store.GetDialect().BooleanStr(false))
|
||||
err := session.Find(&dashboards)
|
||||
count, err = session.Count(&models.Dashboard{})
|
||||
return err
|
||||
})
|
||||
return int64(len(dashboards)), err
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@ type DashboardSearchProjection struct {
|
||||
|
||||
type CountDashboardsInFolderQuery struct {
|
||||
FolderUID string
|
||||
OrgID int64
|
||||
}
|
||||
|
||||
// Note for reviewers: I wasn't sure what to name this. It's not actually a DTO
|
||||
// CountDashboardsInFolderRequest is the request passed from the service to the
|
||||
// store layer. The FolderID will be replaced with FolderUID when dashboards are
|
||||
// updated with parent folder UIDs.
|
||||
// TODO: CountDashboardsInFolderRequest is the request passed from the service
|
||||
// to the store layer. The FolderID will be replaced with FolderUID when
|
||||
// dashboards are updated with parent folder UIDs.
|
||||
type CountDashboardsInFolderRequest struct {
|
||||
FolderID int64
|
||||
OrgID int64
|
||||
|
||||
Reference in New Issue
Block a user