Skip to content

Commit

Permalink
chore: Refactor host cpu analyzer for remote collection (#1664)
Browse files Browse the repository at this point in the history
* Refactor host cpu analyzer for remote collection

---------

Co-authored-by: Gerard Nguyen <[email protected]>
  • Loading branch information
hedge-sparrow and nvanthao authored Nov 6, 2024
1 parent f0b8de6 commit ea900a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 79 deletions.
105 changes: 26 additions & 79 deletions pkg/analyze/host_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -33,94 +34,40 @@ func (a *AnalyzeHostCPU) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostCPU) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents(collect.HostCPUPath)
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
}
func (a *AnalyzeHostCPU) CheckCondition(when string, data []byte) (bool, error) {

cpuInfo := collect.CPUInfo{}
if err := json.Unmarshal(contents, &cpuInfo); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal cpu info")
}

result := AnalyzeResult{
Title: a.Title(),
if err := json.Unmarshal(data, &cpuInfo); err != nil {
return false, fmt.Errorf("failed to unmarshal data into CPUInfo: %v", err)
}

for _, outcome := range hostAnalyzer.Outcomes {

if outcome.Fail != nil {
if outcome.Fail.When == "" {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return []*AnalyzeResult{&result}, nil
}

isMatch, err := compareHostCPUConditionalToActual(outcome.Fail.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
if err != nil {
return nil, errors.Wrap(err, "failed to compare")
}

if isMatch {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return []*AnalyzeResult{&result}, nil
}
} else if outcome.Warn != nil {
if outcome.Warn.When == "" {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return []*AnalyzeResult{&result}, nil
}
return compareHostCPUConditionalToActual(when, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)

isMatch, err := compareHostCPUConditionalToActual(outcome.Warn.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
if err != nil {
return nil, errors.Wrap(err, "failed to compare")
}

if isMatch {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return []*AnalyzeResult{&result}, nil
}
} else if outcome.Pass != nil {
if outcome.Pass.When == "" {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return []*AnalyzeResult{&result}, nil
}

isMatch, err := compareHostCPUConditionalToActual(outcome.Pass.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
if err != nil {
return nil, errors.Wrap(err, "failed to compare")
}
}

if isMatch {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI
func (a *AnalyzeHostCPU) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
result := AnalyzeResult{Title: a.Title()}

// Use the generic function to collect both local and remote data
collectedContents, err := retrieveCollectedContents(
getCollectedFileContents,
collect.HostCPUPath, // Local path
collect.NodeInfoBaseDir, // Remote base directory
collect.HostCPUFileName, // Remote file name
)
if err != nil {
return []*AnalyzeResult{&result}, err
}

return []*AnalyzeResult{&result}, nil
}
}
results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title())
if err != nil {
return nil, errors.Wrap(err, "failed to analyze OS version")
}

return []*AnalyzeResult{&result}, nil
return results, nil

}

func doCompareHostCPUMicroArchitecture(microarch string, flags []string) (res bool, err error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/collect/host_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type CPUInfo struct {
}

const HostCPUPath = `host-collectors/system/cpu.json`
const HostCPUFileName = `cpu.json`

type CollectHostCPU struct {
hostCollector *troubleshootv1beta2.CPU
Expand Down

0 comments on commit ea900a1

Please sign in to comment.