Skip to content

Commit 6ea8bc2

Browse files
committed
RHINENG-22325: structs for Inventory Views message
1 parent 9db1500 commit 6ea8bc2

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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, templates map[int64]models.Template) (
39+
[]InventoryViewsHost, error) {
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+
utils.LogWarn("template_id", system.TemplateID, "template not found")
64+
}
65+
hosts[i].Data.TemplateName = &template.Name
66+
hosts[i].Data.TemplateUUID = &template.UUID
67+
}
68+
}
69+
return hosts, nil
70+
}
71+
72+
func FindSystemsTemplates(tx *gorm.DB, systems []*models.SystemPlatform) (map[int64]models.Template, error) {
73+
templateIDs := make([]int64, 0, len(systems))
74+
for _, system := range systems {
75+
if system.TemplateID == nil {
76+
continue
77+
}
78+
templateIDs = append(templateIDs, *system.TemplateID)
79+
}
80+
81+
templates := make([]models.Template, 0, len(templateIDs))
82+
q := tx.Model(&models.Template{}).Where("id IN (?)", templateIDs).Select("id, name, uuid")
83+
err := q.Find(&templates).Error
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
templatesMap := make(map[int64]models.Template, len(templates))
89+
for _, t := range templates {
90+
templatesMap[t.ID] = t
91+
}
92+
return templatesMap, nil
93+
}

0 commit comments

Comments
 (0)