Skip to content

Commit d414287

Browse files
committed
RHINENG-22325: structs for Inventory Views message
1 parent c79540d commit d414287

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package inventory_views
2+
3+
import (
4+
"app/base/models"
5+
"app/base/utils"
6+
7+
"gorm.io/gorm"
8+
)
9+
10+
type InventoryViewsHost struct {
11+
// Inventory ID (UUID) of the host
12+
ID string `json:"id"`
13+
Data InventoryViewsHostData `json:"data"`
14+
}
15+
16+
type InventoryViewsHostData struct {
17+
ApplicableRhsaCount int `json:"applicable_rhsa_count"`
18+
ApplicableRhbaCount int `json:"applicable_rhba_count"`
19+
ApplicableRheaCount int `json:"applicable_rhea_count"`
20+
ApplicableOtherCount int `json:"applicable_other_count"`
21+
InstallableRhsaCount int `json:"installable_rhsa_count"`
22+
InstallableRhbaCount int `json:"installable_rhba_count"`
23+
InstallableRheaCount int `json:"installable_rhea_count"`
24+
InstallableOtherCount int `json:"installable_other_count"`
25+
PackagesInstalled int `json:"packages_installed"`
26+
PackagesInstallable int `json:"packages_installable"`
27+
PackagesApplicable int `json:"packages_applicable"`
28+
TemplateName *string `json:"template_name"`
29+
TemplateUUID *string `json:"template_uuid"`
30+
}
31+
32+
type InventoryViewsEvent struct {
33+
OrgID string `json:"org_id"`
34+
Timestamp string `json:"timestamp"`
35+
Hosts []InventoryViewsHost `json:"hosts"`
36+
}
37+
38+
func MakeInventoryViewsHosts(systems []models.SystemPlatform,
39+
templates map[int64]models.Template) []InventoryViewsHost {
40+
hosts := make([]InventoryViewsHost, len(systems))
41+
for i, system := range systems {
42+
hosts[i] = InventoryViewsHost{
43+
ID: system.InventoryID,
44+
Data: InventoryViewsHostData{
45+
ApplicableRhsaCount: system.ApplicableAdvisorySecCountCache,
46+
ApplicableRhbaCount: system.ApplicableAdvisoryBugCountCache,
47+
ApplicableRheaCount: system.ApplicableAdvisoryEnhCountCache,
48+
ApplicableOtherCount: system.ApplicableAdvisoryCountCache - system.ApplicableAdvisorySecCountCache -
49+
system.ApplicableAdvisoryBugCountCache - system.ApplicableAdvisoryEnhCountCache,
50+
InstallableRhsaCount: system.InstallableAdvisorySecCountCache,
51+
InstallableRhbaCount: system.InstallableAdvisoryBugCountCache,
52+
InstallableRheaCount: system.InstallableAdvisoryEnhCountCache,
53+
InstallableOtherCount: system.InstallableAdvisoryCountCache - system.InstallableAdvisorySecCountCache -
54+
system.InstallableAdvisoryBugCountCache - system.InstallableAdvisoryEnhCountCache,
55+
PackagesInstalled: system.PackagesInstalled,
56+
PackagesInstallable: system.PackagesInstallable,
57+
PackagesApplicable: system.PackagesApplicable,
58+
},
59+
}
60+
if system.TemplateID != nil {
61+
template, ok := templates[*system.TemplateID]
62+
if ok {
63+
hosts[i].Data.TemplateName = &template.Name
64+
hosts[i].Data.TemplateUUID = &template.UUID
65+
} else {
66+
utils.LogWarn("template_id", system.TemplateID, "template not found")
67+
}
68+
}
69+
}
70+
return hosts
71+
}
72+
73+
func FindSystemsTemplates(tx *gorm.DB, systems []models.SystemPlatform) (map[int64]models.Template, error) {
74+
templateIDs := make([]int64, 0, len(systems))
75+
if len(systems) == 0 {
76+
return nil, nil
77+
}
78+
for _, system := range systems {
79+
if system.TemplateID == nil {
80+
continue
81+
}
82+
templateIDs = append(templateIDs, *system.TemplateID)
83+
}
84+
85+
if len(templateIDs) == 0 {
86+
return nil, nil
87+
}
88+
templates := make([]models.Template, 0, len(templateIDs))
89+
q := tx.Model(&models.Template{}).
90+
Where("rh_account_id = ? AND id IN (?)", systems[0].RhAccountID, templateIDs)
91+
err := q.Find(&templates).Error
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
templatesMap := make(map[int64]models.Template, len(templates))
97+
for _, t := range templates {
98+
templatesMap[t.ID] = t
99+
}
100+
return templatesMap, nil
101+
}

0 commit comments

Comments
 (0)