forked from flashcatcloud/categraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
48 lines (41 loc) · 1.08 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package nvidia_smi
import (
"fmt"
"strconv"
"strings"
)
//nolint:gomnd
func transformRawValue(rawValue string, valueMultiplier float64) (float64, error) {
trimmed := strings.TrimSpace(rawValue)
if strings.HasPrefix(trimmed, "0x") {
return hexToDecimal(trimmed)
}
val := strings.ToLower(trimmed)
switch val {
case "enabled", "yes", "active":
return 1, nil
case "disabled", "no", "not active":
return 0, nil
case "default":
return 0, nil
case "exclusive_thread":
return 1, nil
case "prohibited":
return 2, nil
case "exclusive_process":
return 3, nil
default:
return parseSanitizedValueWithBestEffort(val, valueMultiplier)
}
}
func parseSanitizedValueWithBestEffort(sanitizedValue string, valueMultiplier float64) (float64, error) {
allNums := numericRegex.FindAllString(sanitizedValue, 2) //nolint:gomnd
if len(allNums) != 1 {
return -1, fmt.Errorf("%w: %s", ErrParseNumber, sanitizedValue)
}
parsed, err := strconv.ParseFloat(allNums[0], 64)
if err != nil {
return -1, fmt.Errorf("failed to parse float: %w", err)
}
return parsed * valueMultiplier, nil
}