Skip to content

Commit ea900a1

Browse files
chore: Refactor host cpu analyzer for remote collection (#1664)
* Refactor host cpu analyzer for remote collection --------- Co-authored-by: Gerard Nguyen <[email protected]>
1 parent f0b8de6 commit ea900a1

File tree

2 files changed

+27
-79
lines changed

2 files changed

+27
-79
lines changed

pkg/analyze/host_cpu.go

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"slices"
67
"strconv"
78
"strings"
@@ -33,94 +34,40 @@ func (a *AnalyzeHostCPU) IsExcluded() (bool, error) {
3334
return isExcluded(a.hostAnalyzer.Exclude)
3435
}
3536

36-
func (a *AnalyzeHostCPU) Analyze(
37-
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
38-
) ([]*AnalyzeResult, error) {
39-
hostAnalyzer := a.hostAnalyzer
40-
41-
contents, err := getCollectedFileContents(collect.HostCPUPath)
42-
if err != nil {
43-
return nil, errors.Wrap(err, "failed to get collected file")
44-
}
37+
func (a *AnalyzeHostCPU) CheckCondition(when string, data []byte) (bool, error) {
4538

4639
cpuInfo := collect.CPUInfo{}
47-
if err := json.Unmarshal(contents, &cpuInfo); err != nil {
48-
return nil, errors.Wrap(err, "failed to unmarshal cpu info")
49-
}
50-
51-
result := AnalyzeResult{
52-
Title: a.Title(),
40+
if err := json.Unmarshal(data, &cpuInfo); err != nil {
41+
return false, fmt.Errorf("failed to unmarshal data into CPUInfo: %v", err)
5342
}
5443

55-
for _, outcome := range hostAnalyzer.Outcomes {
56-
57-
if outcome.Fail != nil {
58-
if outcome.Fail.When == "" {
59-
result.IsFail = true
60-
result.Message = outcome.Fail.Message
61-
result.URI = outcome.Fail.URI
62-
63-
return []*AnalyzeResult{&result}, nil
64-
}
65-
66-
isMatch, err := compareHostCPUConditionalToActual(outcome.Fail.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
67-
if err != nil {
68-
return nil, errors.Wrap(err, "failed to compare")
69-
}
70-
71-
if isMatch {
72-
result.IsFail = true
73-
result.Message = outcome.Fail.Message
74-
result.URI = outcome.Fail.URI
75-
76-
return []*AnalyzeResult{&result}, nil
77-
}
78-
} else if outcome.Warn != nil {
79-
if outcome.Warn.When == "" {
80-
result.IsWarn = true
81-
result.Message = outcome.Warn.Message
82-
result.URI = outcome.Warn.URI
83-
84-
return []*AnalyzeResult{&result}, nil
85-
}
44+
return compareHostCPUConditionalToActual(when, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
8645

87-
isMatch, err := compareHostCPUConditionalToActual(outcome.Warn.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
88-
if err != nil {
89-
return nil, errors.Wrap(err, "failed to compare")
90-
}
91-
92-
if isMatch {
93-
result.IsWarn = true
94-
result.Message = outcome.Warn.Message
95-
result.URI = outcome.Warn.URI
96-
97-
return []*AnalyzeResult{&result}, nil
98-
}
99-
} else if outcome.Pass != nil {
100-
if outcome.Pass.When == "" {
101-
result.IsPass = true
102-
result.Message = outcome.Pass.Message
103-
result.URI = outcome.Pass.URI
104-
105-
return []*AnalyzeResult{&result}, nil
106-
}
107-
108-
isMatch, err := compareHostCPUConditionalToActual(outcome.Pass.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
109-
if err != nil {
110-
return nil, errors.Wrap(err, "failed to compare")
111-
}
46+
}
11247

113-
if isMatch {
114-
result.IsPass = true
115-
result.Message = outcome.Pass.Message
116-
result.URI = outcome.Pass.URI
48+
func (a *AnalyzeHostCPU) Analyze(
49+
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
50+
) ([]*AnalyzeResult, error) {
51+
result := AnalyzeResult{Title: a.Title()}
52+
53+
// Use the generic function to collect both local and remote data
54+
collectedContents, err := retrieveCollectedContents(
55+
getCollectedFileContents,
56+
collect.HostCPUPath, // Local path
57+
collect.NodeInfoBaseDir, // Remote base directory
58+
collect.HostCPUFileName, // Remote file name
59+
)
60+
if err != nil {
61+
return []*AnalyzeResult{&result}, err
62+
}
11763

118-
return []*AnalyzeResult{&result}, nil
119-
}
120-
}
64+
results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title())
65+
if err != nil {
66+
return nil, errors.Wrap(err, "failed to analyze OS version")
12167
}
12268

123-
return []*AnalyzeResult{&result}, nil
69+
return results, nil
70+
12471
}
12572

12673
func doCompareHostCPUMicroArchitecture(microarch string, flags []string) (res bool, err error) {

pkg/collect/host_cpu.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type CPUInfo struct {
1818
}
1919

2020
const HostCPUPath = `host-collectors/system/cpu.json`
21+
const HostCPUFileName = `cpu.json`
2122

2223
type CollectHostCPU struct {
2324
hostCollector *troubleshootv1beta2.CPU

0 commit comments

Comments
 (0)