-
Notifications
You must be signed in to change notification settings - Fork 40
RHINENG-22325: inventory views #2014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichaelMraka
wants to merge
7
commits into
RedHatInsights:master
Choose a base branch
from
MichaelMraka:pr1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c79540d
RHINENG-22325: configure topic for Inventory Views
MichaelMraka d414287
RHINENG-22325: structs for Inventory Views message
MichaelMraka 71279a3
RHINENG-22325: use only necessary subset of Template attributes for I…
MichaelMraka 8f9bdc9
RHINENG-22325: extend KafkaMessage with Headers
MichaelMraka a9776c1
RHINENG-22325: sent inventory view event after evaluation
MichaelMraka f63f3e0
RHINENG-22325: add missing org_id into evaluator tests
MichaelMraka f5dd08b
RHINENG-22325: inventory views tests
MichaelMraka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package inventory_views | ||
|
|
||
| import ( | ||
| "app/base/models" | ||
| "app/base/utils" | ||
| "time" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| type InventoryViewsHost struct { | ||
| // Inventory ID (UUID) of the host | ||
| ID string `json:"id"` | ||
| Data InventoryViewsHostData `json:"data"` | ||
| } | ||
|
|
||
| type InventoryViewsHostData struct { | ||
| ApplicableRhsaCount int `json:"applicable_rhsa_count"` | ||
| ApplicableRhbaCount int `json:"applicable_rhba_count"` | ||
| ApplicableRheaCount int `json:"applicable_rhea_count"` | ||
| ApplicableOtherCount int `json:"applicable_other_count"` | ||
| InstallableRhsaCount int `json:"installable_rhsa_count"` | ||
| InstallableRhbaCount int `json:"installable_rhba_count"` | ||
| InstallableRheaCount int `json:"installable_rhea_count"` | ||
| InstallableOtherCount int `json:"installable_other_count"` | ||
| PackagesInstalled int `json:"packages_installed"` | ||
| PackagesInstallable int `json:"packages_installable"` | ||
| PackagesApplicable int `json:"packages_applicable"` | ||
| TemplateName *string `json:"template_name"` | ||
| TemplateUUID *string `json:"template_uuid"` | ||
| } | ||
|
|
||
| type InventoryViewsEvent struct { | ||
| OrgID string `json:"org_id"` | ||
| Timestamp string `json:"timestamp"` | ||
| Hosts []InventoryViewsHost `json:"hosts"` | ||
| } | ||
|
|
||
| func MakeInventoryViewsEvent(tx *gorm.DB, orgID string, systems []models.SystemPlatform) ( | ||
| InventoryViewsEvent, error) { | ||
| templates, err := FindSystemsTemplates(tx, systems) | ||
| if err != nil { | ||
| return InventoryViewsEvent{}, err | ||
| } | ||
| hosts := MakeInventoryViewsHosts(systems, templates) | ||
| return InventoryViewsEvent{OrgID: orgID, Timestamp: time.Now().Format(time.RFC3339), Hosts: hosts}, nil | ||
| } | ||
|
|
||
| func MakeInventoryViewsHosts(systems []models.SystemPlatform, | ||
| templates map[int64]models.TemplateBase) []InventoryViewsHost { | ||
| hosts := make([]InventoryViewsHost, len(systems)) | ||
| for i, system := range systems { | ||
| hosts[i] = InventoryViewsHost{ | ||
| ID: system.InventoryID, | ||
| Data: InventoryViewsHostData{ | ||
| ApplicableRhsaCount: system.ApplicableAdvisorySecCountCache, | ||
| ApplicableRhbaCount: system.ApplicableAdvisoryBugCountCache, | ||
| ApplicableRheaCount: system.ApplicableAdvisoryEnhCountCache, | ||
| ApplicableOtherCount: system.ApplicableAdvisoryCountCache - system.ApplicableAdvisorySecCountCache - | ||
| system.ApplicableAdvisoryBugCountCache - system.ApplicableAdvisoryEnhCountCache, | ||
| InstallableRhsaCount: system.InstallableAdvisorySecCountCache, | ||
| InstallableRhbaCount: system.InstallableAdvisoryBugCountCache, | ||
| InstallableRheaCount: system.InstallableAdvisoryEnhCountCache, | ||
| InstallableOtherCount: system.InstallableAdvisoryCountCache - system.InstallableAdvisorySecCountCache - | ||
| system.InstallableAdvisoryBugCountCache - system.InstallableAdvisoryEnhCountCache, | ||
| PackagesInstalled: system.PackagesInstalled, | ||
| PackagesInstallable: system.PackagesInstallable, | ||
| PackagesApplicable: system.PackagesApplicable, | ||
| }, | ||
| } | ||
| if system.TemplateID != nil { | ||
| template, ok := templates[*system.TemplateID] | ||
| if ok { | ||
| hosts[i].Data.TemplateName = &template.Name | ||
| hosts[i].Data.TemplateUUID = &template.UUID | ||
| } else { | ||
| utils.LogWarn("template_id", system.TemplateID, "template not found") | ||
| } | ||
| } | ||
| } | ||
| return hosts | ||
| } | ||
|
|
||
| func FindSystemsTemplates(tx *gorm.DB, systems []models.SystemPlatform) (map[int64]models.TemplateBase, error) { | ||
| templateIDs := make([]int64, 0, len(systems)) | ||
| if len(systems) == 0 { | ||
| return nil, nil | ||
| } | ||
| for _, system := range systems { | ||
| if system.TemplateID == nil { | ||
| continue | ||
| } | ||
| templateIDs = append(templateIDs, *system.TemplateID) | ||
| } | ||
|
|
||
| if len(templateIDs) == 0 { | ||
| return nil, nil | ||
| } | ||
| templates := make([]models.TemplateBase, 0, len(templateIDs)) | ||
| q := tx.Model(&models.TemplateBase{}). | ||
| Where("rh_account_id = ? AND id IN (?)", systems[0].RhAccountID, templateIDs) | ||
| err := q.Find(&templates).Error | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| templatesMap := make(map[int64]models.TemplateBase, len(templates)) | ||
| for _, t := range templates { | ||
| templatesMap[t.ID] = t | ||
| } | ||
| return templatesMap, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package inventory_views | ||
|
|
||
| import ( | ||
| "app/base/core" | ||
| "app/base/database" | ||
| "app/base/models" | ||
| "app/base/utils" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const rhAccountID = 1 | ||
|
|
||
| func TestMakeInventoryViewsEvent(t *testing.T) { | ||
| utils.SkipWithoutDB(t) | ||
| core.SetupTestEnvironment() | ||
| tx := database.DB.Begin() | ||
| defer tx.Rollback() | ||
|
|
||
| var rhAccount models.RhAccount | ||
| assert.NoError(t, tx.Where("id = ?", rhAccountID).First(&rhAccount).Error) | ||
| assert.NotEmpty(t, *rhAccount.OrgID) | ||
|
|
||
| var systems []models.SystemPlatform | ||
| assert.NoError(t, tx.Where("rh_account_id = ? AND id in (1,3)", rhAccountID). | ||
| Order("id").Find(&systems).Error) | ||
| assert.Equal(t, 2, len(systems)) | ||
|
|
||
| event, err := MakeInventoryViewsEvent(tx, *rhAccount.OrgID, systems) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, *rhAccount.OrgID, event.OrgID) | ||
| assert.NotEmpty(t, event.Timestamp) | ||
| _, err = time.Parse(time.RFC3339, event.Timestamp) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Verify hosts | ||
| assert.Equal(t, 2, len(event.Hosts)) | ||
|
|
||
| assert.Equal(t, InventoryViewsHost{ | ||
| ID: "00000000-0000-0000-0000-000000000001", | ||
| Data: InventoryViewsHostData{2, 3, 3, 0, 2, 2, 1, 0, 0, 0, 0, | ||
| utils.PtrString("temp1-1"), utils.PtrString("99900000-0000-0000-0000-000000000001")}, | ||
| }, event.Hosts[0]) | ||
|
|
||
| assert.Equal(t, InventoryViewsHost{ | ||
| ID: "00000000-0000-0000-0000-000000000003", | ||
| Data: InventoryViewsHostData{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, | ||
| utils.PtrString("temp2-1"), utils.PtrString("99900000-0000-0000-0000-000000000002")}, | ||
| }, event.Hosts[1]) | ||
| } | ||
|
|
||
| func TestMakeInventoryViewsEventEmpty(t *testing.T) { | ||
| utils.SkipWithoutDB(t) | ||
| core.SetupTestEnvironment() | ||
|
|
||
| tx := database.DB.Begin() | ||
| defer tx.Rollback() | ||
|
|
||
| event, err := MakeInventoryViewsEvent(tx, "test-org", []models.SystemPlatform{}) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "test-org", event.OrgID) | ||
| assert.Equal(t, 0, len(event.Hosts)) | ||
| assert.NotEmpty(t, event.Timestamp) | ||
| } | ||
|
|
||
| func TestMakeInventoryViewsEventNoTemplate(t *testing.T) { | ||
| utils.SkipWithoutDB(t) | ||
| core.SetupTestEnvironment() | ||
|
|
||
| tx := database.DB.Begin() | ||
| defer tx.Rollback() | ||
|
|
||
| var rhAccount models.RhAccount | ||
| assert.NoError(t, tx.Where("id = ?", rhAccountID).First(&rhAccount).Error) | ||
| assert.NotEmpty(t, *rhAccount.OrgID) | ||
| orgID := *rhAccount.OrgID | ||
|
|
||
| var systems []models.SystemPlatform | ||
| assert.NoError(t, tx.Where("rh_account_id = ? AND id in (4)", rhAccountID). | ||
| Order("id").Find(&systems).Error) | ||
| assert.Equal(t, 1, len(systems)) | ||
|
|
||
| // Should not error, but template fields should be nil | ||
| event, err := MakeInventoryViewsEvent(tx, orgID, systems) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 1, len(event.Hosts)) | ||
|
|
||
| host := event.Hosts[0] | ||
| assert.Equal(t, "00000000-0000-0000-0000-000000000004", host.ID) | ||
| // Template fields should be nil when template is not found | ||
| assert.Nil(t, host.Data.TemplateName) | ||
| assert.Nil(t, host.Data.TemplateUUID) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package mqueue | ||
|
|
||
| import "github.com/bytedance/sonic" | ||
| import ( | ||
| "github.com/bytedance/sonic" | ||
| "github.com/segmentio/kafka-go" | ||
| ) | ||
|
|
||
| func MessageFromJSON(k string, v interface{}) (KafkaMessage, error) { | ||
| func MessageFromJSON(k string, v interface{}, h []kafka.Header) (KafkaMessage, error) { | ||
| var m KafkaMessage | ||
| var err error | ||
|
|
||
| m.Key = []byte(k) | ||
| m.Headers = h | ||
| m.Value, err = sonic.Marshal(v) | ||
| return m, err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we have this function for each struct?