Skip to content

Commit a2c734c

Browse files
committed
🔧 cleanup rest of regexp
1 parent dda1e93 commit a2c734c

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

‎main.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ var (
6161
updateInterval = 1000
6262
)
6363

64-
var dataRegex = regexp.MustCompile(`(?m)^\s*(\S.*?)\s+(\d+)\s+(\d+\.\d+)\s+\d+\.\d+\s+`)
64+
var (
65+
dataRegex = regexp.MustCompile(`(?m)^\s*(\S.*?)\s+(\d+)\s+(\d+\.\d+)\s+\d+\.\d+\s+`)
66+
outRegex = regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
67+
inRegex = regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
68+
readRegex = regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
69+
writeRegex = regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
70+
residencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
71+
frequencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
72+
re = regexp.MustCompile(`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?`)
73+
freqRe = regexp.MustCompile(`(\d+)\s*MHz:\s*(\d+)%`)
74+
)
6575

6676
func setupUI() {
6777
appleSiliconModel := getSOCInfo()
@@ -555,8 +565,7 @@ func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetr
555565
}
556566

557567
func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetrics) NetDiskMetrics {
558-
outRegex := regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
559-
inRegex := regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
568+
560569
outMatches := outRegex.FindStringSubmatch(powermetricsOutput)
561570
inMatches := inRegex.FindStringSubmatch(powermetricsOutput)
562571
if len(outMatches) == 3 {
@@ -567,8 +576,7 @@ func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetri
567576
netdiskMetrics.InPacketsPerSec, _ = strconv.ParseFloat(inMatches[1], 64)
568577
netdiskMetrics.InBytesPerSec, _ = strconv.ParseFloat(inMatches[2], 64)
569578
}
570-
readRegex := regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
571-
writeRegex := regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
579+
572580
readMatches := readRegex.FindStringSubmatch(powermetricsOutput)
573581
writeMatches := writeRegex.FindStringSubmatch(powermetricsOutput)
574582
if len(readMatches) == 3 {
@@ -588,8 +596,7 @@ func parseCPUMetrics(powermetricsOutput string, cpuMetrics CPUMetrics, modelName
588596
pCores := []int{}
589597
var eClusterActiveSum, pClusterActiveSum, eClusterFreqSum, pClusterFreqSum float64
590598
var eClusterCount, pClusterCount, eClusterActiveTotal, pClusterActiveTotal, eClusterFreqTotal, pClusterFreqTotal int
591-
residencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
592-
frequencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
599+
593600
if modelName == "Apple M3 Max" || modelName == "Apple M2 Max" { // For the M3/M2 Max, we need to manually parse the CPU Usage from the powermetrics output (as current bug in Apple's powermetrics)
594601
for _, line := range lines {
595602

@@ -809,8 +816,7 @@ func max(nums ...int) int {
809816
}
810817

811818
func parseGPUMetrics(powermetricsOutput string, gpuMetrics GPUMetrics) GPUMetrics {
812-
re := regexp.MustCompile(`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?`)
813-
freqRe := regexp.MustCompile(`(\d+)\s*MHz:\s*(\d+)%`)
819+
814820
lines := strings.Split(powermetricsOutput, "\n")
815821

816822
for _, line := range lines {

0 commit comments

Comments
 (0)