Skip to content

Commit e006713

Browse files
Copilotharp-intel
andcommitted
Refactor GPU parsing with helper functions
- Added extractPCIIDFromBrackets helper function - Added removeBracketedSuffix helper function - Simplified product and vendor parsing logic - Improved code readability and maintainability Co-authored-by: harp-intel <78619061+harp-intel@users.noreply.github.com>
1 parent d96ff2b commit e006713

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

internal/report/table_helpers.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,29 @@ type GPU struct {
14891489
Resources string
14901490
}
14911491

1492+
// extractPCIIDFromBrackets extracts the PCI vendor:device ID from bracketed text
1493+
// Example: "Product Name [1414:5353]" returns "1414:5353"
1494+
func extractPCIIDFromBrackets(text string) string {
1495+
if idx := strings.Index(text, "["); idx != -1 {
1496+
if endIdx := strings.Index(text, "]"); endIdx != -1 {
1497+
ids := text[idx+1 : endIdx]
1498+
if strings.Contains(ids, ":") {
1499+
return ids
1500+
}
1501+
}
1502+
}
1503+
return ""
1504+
}
1505+
1506+
// removeBracketedSuffix removes bracketed suffix from text
1507+
// Example: "Microsoft Corporation [1414]" returns "Microsoft Corporation"
1508+
func removeBracketedSuffix(text string) string {
1509+
if idx := strings.Index(text, "["); idx != -1 {
1510+
return strings.TrimSpace(text[:idx])
1511+
}
1512+
return text
1513+
}
1514+
14921515
func gpuInfoFromOutput(outputs map[string]script.ScriptOutput) []GPU {
14931516
gpus := []GPU{}
14941517

@@ -1528,24 +1551,14 @@ func gpuInfoFromOutput(outputs map[string]script.ScriptOutput) []GPU {
15281551
// Parse the fields
15291552
if strings.HasPrefix(trimmedLine, "product:") {
15301553
product := strings.TrimSpace(strings.TrimPrefix(trimmedLine, "product:"))
1531-
// Extract device ID from brackets if present (e.g., "Product Name [vendor:device]")
1532-
if idx := strings.Index(product, "["); idx != -1 {
1533-
if endIdx := strings.Index(product, "]"); endIdx != -1 {
1534-
ids := product[idx+1 : endIdx]
1535-
if strings.Contains(ids, ":") {
1536-
currentGPU.PCIID = ids
1537-
}
1538-
product = strings.TrimSpace(product[:idx])
1539-
}
1554+
// Extract PCI ID from brackets if present (e.g., "Product Name [vendor:device]")
1555+
if pciID := extractPCIIDFromBrackets(product); pciID != "" {
1556+
currentGPU.PCIID = pciID
15401557
}
1541-
currentGPU.Model = product
1558+
currentGPU.Model = removeBracketedSuffix(product)
15421559
} else if strings.HasPrefix(trimmedLine, "vendor:") {
15431560
vendor := strings.TrimSpace(strings.TrimPrefix(trimmedLine, "vendor:"))
1544-
// Remove vendor ID from brackets if present
1545-
if idx := strings.Index(vendor, "["); idx != -1 {
1546-
vendor = strings.TrimSpace(vendor[:idx])
1547-
}
1548-
currentGPU.Manufacturer = vendor
1561+
currentGPU.Manufacturer = removeBracketedSuffix(vendor)
15491562
} else if strings.HasPrefix(trimmedLine, "bus info:") {
15501563
busInfo := strings.TrimSpace(strings.TrimPrefix(trimmedLine, "bus info:"))
15511564
currentGPU.BusInfo = busInfo

0 commit comments

Comments
 (0)