|
| 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.TemplateBase) ( |
| 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.TemplateBase, error) { |
| 73 | + templateIDs := make([]int64, 0, len(systems)) |
| 74 | + if len(systems) == 0 { |
| 75 | + return nil, nil |
| 76 | + } |
| 77 | + for _, system := range systems { |
| 78 | + if system.TemplateID == nil { |
| 79 | + continue |
| 80 | + } |
| 81 | + templateIDs = append(templateIDs, *system.TemplateID) |
| 82 | + } |
| 83 | + |
| 84 | + if len(templateIDs) == 0 { |
| 85 | + return nil, nil |
| 86 | + } |
| 87 | + templates := make([]models.TemplateBase, 0, len(templateIDs)) |
| 88 | + q := tx.Model(&models.TemplateBase{}). |
| 89 | + Where("rh_account_id = ? AND id IN (?)", systems[0].RhAccountID, templateIDs) |
| 90 | + err := q.Find(&templates).Error |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + templatesMap := make(map[int64]models.Template, len(templates)) |
| 96 | + for _, t := range templates { |
| 97 | + templatesMap[t.ID] = t |
| 98 | + } |
| 99 | + return templatesMap, nil |
| 100 | +} |
0 commit comments