Skip to content

Commit

Permalink
RHINENG-10791: common advisory_metadata joins
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Jul 2, 2024
1 parent b482680 commit 3b0d93d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
21 changes: 19 additions & 2 deletions base/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ func PackageByName(tx *gorm.DB, pkgName string) *gorm.DB {
return Packages(tx).Where("pn.name = ?", pkgName)
}

func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID string) *gorm.DB {
return SystemAdvisories(tx, accountID, groups).Where("sp.inventory_id = ?::uuid", inventoryID)
func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID string,
joins ...join) *gorm.DB {
tx = SystemAdvisories(tx, accountID, groups).Where("sp.inventory_id = ?::uuid", inventoryID)
return (joinsT)(joins).apply(tx)
}

func SystemAdvisoriesBySystemID(tx *gorm.DB, accountID int, systemID int64) *gorm.DB {
query := systemAdvisoriesQuery(tx, accountID).Where("sp.id = ?", systemID)
return query
}

func AdvisoryMetadata(tx *gorm.DB) *gorm.DB {
tx = tx.Table("advisory_metadata am")
return JoinAdvisoryType(tx)
}

func systemAdvisoriesQuery(tx *gorm.DB, accountID int) *gorm.DB {
query := tx.Table("system_advisories sa").Select("sa.*").
Joins("join system_platform sp ON sa.rh_account_id = sp.rh_account_id AND sa.system_id = sp.id").
Expand Down Expand Up @@ -256,3 +263,13 @@ func JoinTemplates(tx *gorm.DB) *gorm.DB {
return tx.Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id").
Joins("LEFT JOIN template t ON sp.template_id = t.id AND sp.rh_account_id = t.rh_account_id")
}

// JOIN advisory_metadata to sa (system_advisories)
func JoinAdvisoryMetadata(tx *gorm.DB) *gorm.DB {
return tx.Joins("JOIN advisory_metadata am ON am.id = sa.advisory_id")
}

// JOIN advisory_type to am (advisory_metadata)
func JoinAdvisoryType(tx *gorm.DB) *gorm.DB {
return tx.Joins("JOIN advisory_type at ON am.advisory_type_id = at.id")
}
6 changes: 2 additions & 4 deletions manager/controllers/advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,9 @@ func AdvisoriesListIDsHandler(c *gin.Context) {
}

func buildQueryAdvisories(db *gorm.DB, account int) *gorm.DB {
query := db.Table("advisory_metadata am").
query := database.AdvisoryMetadata(db).
Select(AdvisoriesSelect).
Joins("JOIN advisory_account_data aad ON am.id = aad.advisory_id").
Joins("JOIN advisory_type at ON am.advisory_type_id = at.id").
Where("aad.rh_account_id = ?", account)
return query
}
Expand All @@ -231,9 +230,8 @@ func buildQueryAdvisoriesTagged(db *gorm.DB, filters map[string]FilterData, acco
subq := buildAdvisoryAccountDataQuery(db, account, groups)
subq, _ = ApplyInventoryFilter(filters, subq, "sp.inventory_id")

query := db.Table("advisory_metadata am").
query := database.AdvisoryMetadata(db).
Select(AdvisoriesSelect).
Joins("JOIN advisory_type at ON am.advisory_type_id = at.id").
Joins("JOIN (?) aad ON am.id = aad.advisory_id", subq)

return query
Expand Down
3 changes: 1 addition & 2 deletions manager/controllers/advisory_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,8 @@ func AdvisorySystemsListIDsHandler(c *gin.Context) {

func buildAdvisorySystemsQuery(db *gorm.DB, account int, groups map[string]string, advisoryName string) *gorm.DB {
selectQuery := AdvisorySystemsSelect
query := database.SystemAdvisories(db, account, groups, database.JoinTemplates).
query := database.SystemAdvisories(db, account, groups, database.JoinTemplates, database.JoinAdvisoryMetadata).
Select(selectQuery).
Joins("JOIN advisory_metadata am ON am.id = sa.advisory_id").
Joins("LEFT JOIN status st ON sa.status_id = st.id").
Where("am.name = ?", advisoryName).
Where("sp.stale = false")
Expand Down
5 changes: 2 additions & 3 deletions manager/controllers/system_advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ func SystemAdvisoriesIDsHandler(c *gin.Context) {
}

func buildSystemAdvisoriesQuery(db *gorm.DB, account int, groups map[string]string, inventoryID string) *gorm.DB {
query := database.SystemAdvisoriesByInventoryID(db, account, groups, inventoryID).
Joins("JOIN advisory_metadata am on am.id = sa.advisory_id").
Joins("JOIN advisory_type at ON am.advisory_type_id = at.id").
query := database.SystemAdvisoriesByInventoryID(db, account, groups, inventoryID,
database.JoinAdvisoryMetadata, database.JoinAdvisoryType).
Joins("JOIN status ON sa.status_id = status.id").
Select(SystemAdvisoriesSelect)
return query
Expand Down
6 changes: 2 additions & 4 deletions manager/controllers/systems_advisories_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ func advisoriesSystemsQuery(c *gin.Context, db *gorm.DB, acc int, groups map[str
systems := req.Systems
advisories := req.Advisories
// get all advisories for all systems in the account (with inventory.hosts join)
advq := database.Systems(db, acc, groups).
Distinct("am.id, am.name").
advq := database.SystemAdvisories(db, acc, groups, database.JoinAdvisoryMetadata).
Distinct("am.id, am.name")
// we need to join system_advisories to make `limit` work properly
// without this join it can happen that we display less items on some pages
Joins("JOIN system_advisories sa ON sp.id = sa.system_id AND sa.rh_account_id = ?", acc).
Joins("JOIN advisory_metadata am ON am.id = sa.advisory_id")
if len(advisories) > 0 {
advq = advq.Where("am.name in (?)", advisories)
}
Expand Down

0 comments on commit 3b0d93d

Please sign in to comment.