@@ -26,16 +26,16 @@ func (j joinsT) apply(tx *gorm.DB) *gorm.DB {
2626 return tx
2727}
2828
29- func Systems (tx * gorm.DB , accountID int , workspaceIDs [ ]string , joins ... join ) * gorm.DB {
29+ func Systems (tx * gorm.DB , accountID int , groups map [ string ]string , joins ... join ) * gorm.DB {
3030 tx = tx .Table ("system_inventory si" ).
3131 Joins ("JOIN system_patch spatch ON si.id = spatch.system_id AND si.rh_account_id = spatch.rh_account_id" ).
3232 Where ("si.rh_account_id = ?" , accountID )
3333 tx = (joinsT )(joins ).apply (tx )
34- return ApplyInventoryWorkspaceFilter (tx , workspaceIDs )
34+ return ApplyInventoryWorkspaceFilter (tx , groups )
3535}
3636
37- func SystemAdvisories (tx * gorm.DB , accountID int , workspaceIDs [ ]string , joins ... join ) * gorm.DB {
38- tx = Systems (tx , accountID , workspaceIDs ).
37+ func SystemAdvisories (tx * gorm.DB , accountID int , groups map [ string ]string , joins ... join ) * gorm.DB {
38+ tx = Systems (tx , accountID , groups ).
3939 Joins ("JOIN system_advisories sa on sa.system_id = si.id AND sa.rh_account_id = ?" , accountID )
4040 return (joinsT )(joins ).apply (tx )
4141}
@@ -46,8 +46,8 @@ func SystemPackagesShort(tx *gorm.DB, accountID int, joins ...join) *gorm.DB {
4646 return (joinsT )(joins ).apply (tx )
4747}
4848
49- func SystemPackages (tx * gorm.DB , accountID int , workspaceIDs [ ]string , joins ... join ) * gorm.DB {
50- tx = Systems (tx , accountID , workspaceIDs ).
49+ func SystemPackages (tx * gorm.DB , accountID int , groups map [ string ]string , joins ... join ) * gorm.DB {
50+ tx = Systems (tx , accountID , groups ).
5151 Joins ("JOIN system_package2 spkg on spkg.system_id = si.id AND spkg.rh_account_id = ?" , accountID ).
5252 Joins ("JOIN package p on p.id = spkg.package_id" ).
5353 Joins ("JOIN package_name pn on pn.id = spkg.name_id" )
@@ -65,9 +65,9 @@ func PackageByName(tx *gorm.DB, pkgName string, joins ...join) *gorm.DB {
6565 return (joinsT )(joins ).apply (tx )
6666}
6767
68- func SystemAdvisoriesByInventoryID (tx * gorm.DB , accountID int , workspaceIDs [ ]string , inventoryID string ,
68+ func SystemAdvisoriesByInventoryID (tx * gorm.DB , accountID int , groups map [ string ]string , inventoryID string ,
6969 joins ... join ) * gorm.DB {
70- tx = SystemAdvisories (tx , accountID , workspaceIDs ).Where ("si.inventory_id = ?::uuid" , inventoryID )
70+ tx = SystemAdvisories (tx , accountID , groups ).Where ("si.inventory_id = ?::uuid" , inventoryID )
7171 return (joinsT )(joins ).apply (tx )
7272}
7373
@@ -240,11 +240,21 @@ func ReadReplicaConfigured() bool {
240240 return len (utils .CoreCfg .DBReadReplicaHost ) > 0 && utils .CoreCfg .DBReadReplicaPort != 0
241241}
242242
243- func ApplyInventoryWorkspaceFilter (tx * gorm.DB , workspaceIDs []string ) * gorm.DB {
244- if len (workspaceIDs ) == 0 {
245- utils .LogWarn ("there should always be some workspaces, at least root workspace" )
243+ func ApplyInventoryWorkspaceFilter (tx * gorm.DB , groups map [string ]string ) * gorm.DB {
244+ if _ , ok := groups [utils .KeyGrouped ]; ! ok {
245+ if _ , ok := groups [utils .KeyUngrouped ]; ok {
246+ // show only systems with '[]' group
247+ return tx .Where ("si.workspaces = '[]'" )
248+ }
249+ // return query without WHERE if there are no groups
250+ return tx
251+ }
252+
253+ db := DB .Where ("si.workspaces @> ANY (?::jsonb[])" , groups [utils .KeyGrouped ])
254+ if _ , ok := groups [utils .KeyUngrouped ]; ok {
255+ db = db .Or ("si.workspaces = '[]'" )
246256 }
247- return tx .Where ("si.workspace_id IN (?)" , workspaceIDs )
257+ return tx .Where (db )
248258}
249259
250260// LEFT JOIN templates to spatch (system_patch)
0 commit comments