Skip to content

Commit fec58bd

Browse files
authored
Merge pull request #18 from featheredtoast/fix-production-stats
FIX: get consumption and production capacity from raw values
2 parents 0855707 + 3f0402d commit fec58bd

File tree

2 files changed

+8
-120
lines changed

2 files changed

+8
-120
lines changed

Companion/exporter/production_collector.go

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,22 @@ package exporter
22

33
import (
44
"log"
5-
"regexp"
6-
"strconv"
75
)
86

97
type ProductionCollector struct {
108
FRMAddress string
119
}
1210

13-
var prodPerMinRegex = regexp.MustCompile(`P: (?P<prod_current>[\d.]+)/(?P<prod_capacity>[\d.]+)/min - C: (?P<cons_current>[\d.]+)/(?P<cons_capacity>[\d.]+)/min`)
14-
1511
type ProductionDetails struct {
1612
ItemName string `json:"ItemName"`
17-
ProdPerMin string `json:"ProdPerMin"`
18-
ProdPercent *float64 `json:"ProdPercent"`
19-
ConsPercent *float64 `json:"ConsPercent"`
13+
ProdPercent float64 `json:"ProdPercent"`
14+
ConsPercent float64 `json:"ConsPercent"`
2015
CurrentProduction float64 `json:"CurrentProd"`
2116
CurrentConsumption float64 `json:"CurrentConsumed"`
2217
MaxProd float64 `json:"MaxProd"`
2318
MaxConsumed float64 `json:"MaxConsumed"`
2419
}
2520

26-
func (pd *ProductionDetails) parseProdPerMin() (bool, map[string]string) {
27-
match := prodPerMinRegex.FindStringSubmatch(pd.ProdPerMin)
28-
29-
if len(match) < 1 {
30-
return false, nil
31-
}
32-
33-
paramsMap := make(map[string]string)
34-
for i, name := range prodPerMinRegex.SubexpNames() {
35-
if i > 0 && i <= len(match) {
36-
paramsMap[name] = match[i]
37-
}
38-
}
39-
40-
return true, paramsMap
41-
}
42-
43-
func (pd *ProductionDetails) ItemProductionCapacity() *float64 {
44-
hasMatched, params := pd.parseProdPerMin()
45-
46-
if !hasMatched {
47-
return nil
48-
}
49-
50-
value := params["prod_capacity"]
51-
52-
v, err := strconv.ParseFloat(value, 64)
53-
54-
if err != nil {
55-
return nil
56-
}
57-
58-
return &v
59-
}
60-
61-
func (pd *ProductionDetails) ItemConsumptionCapacity() *float64 {
62-
hasMatched, params := pd.parseProdPerMin()
63-
64-
if !hasMatched {
65-
return nil
66-
}
67-
68-
value := params["cons_capacity"]
69-
70-
v, err := strconv.ParseFloat(value, 64)
71-
72-
if err != nil {
73-
return nil
74-
}
75-
76-
return &v
77-
}
78-
7921
func NewProductionCollector(frmAddress string) *ProductionCollector {
8022
return &ProductionCollector{
8123
FRMAddress: frmAddress,
@@ -94,18 +36,9 @@ func (c *ProductionCollector) Collect() {
9436
ItemsProducedPerMin.WithLabelValues(d.ItemName).Set(d.CurrentProduction)
9537
ItemsConsumedPerMin.WithLabelValues(d.ItemName).Set(d.CurrentConsumption)
9638

97-
ItemProductionCapacityPercent.WithLabelValues(d.ItemName).Set(*d.ProdPercent)
98-
ItemConsumptionCapacityPercent.WithLabelValues(d.ItemName).Set(*d.ConsPercent)
99-
100-
prodCapacity := d.ItemProductionCapacity()
101-
consCapacity := d.ItemConsumptionCapacity()
102-
103-
if prodCapacity != nil {
104-
ItemProductionCapacityPerMinute.WithLabelValues(d.ItemName).Set(*prodCapacity)
105-
}
106-
107-
if consCapacity != nil {
108-
ItemConsumptionCapacityPerMinute.WithLabelValues(d.ItemName).Set(*consCapacity)
109-
}
39+
ItemProductionCapacityPercent.WithLabelValues(d.ItemName).Set(d.ProdPercent)
40+
ItemConsumptionCapacityPercent.WithLabelValues(d.ItemName).Set(d.ConsPercent)
41+
ItemProductionCapacityPerMinute.WithLabelValues(d.ItemName).Set(d.MaxProd)
42+
ItemConsumptionCapacityPerMinute.WithLabelValues(d.ItemName).Set(d.MaxConsumed)
11043
}
11144
}

Companion/exporter/production_collector_test.go

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
package exporter_test
22

33
import (
4-
"fmt"
5-
64
"github.com/AP-Hunt/FicsitRemoteMonitoringCompanion/m/v2/exporter"
75
. "github.com/onsi/ginkgo/v2"
86
. "github.com/onsi/gomega"
97
)
108

11-
func prodPerMin(prodActual float32, prodCapacity float32, consActual float32, consCapacity float32) string {
12-
return fmt.Sprintf("P: %f/%f/min - C: %f/%f/min", prodActual, prodCapacity, consActual, consCapacity)
13-
}
14-
15-
func prodPerMinWithoutCapacity(prodActual float32, consActual float32) string {
16-
// Inconsistent spacing in format string is correct according to the API
17-
return fmt.Sprintf("P:%f/min - C: %f/min", prodActual, consActual)
18-
}
19-
209
var _ = Describe("ProductionCollector", func() {
2110
var collector *exporter.ProductionCollector
2211

@@ -27,9 +16,8 @@ var _ = Describe("ProductionCollector", func() {
2716
FRMServer.ReturnsProductionData([]exporter.ProductionDetails{
2817
{
2918
ItemName: "Iron Rod",
30-
ProdPerMin: prodPerMin(10, 100, 40, 200),
31-
ProdPercent: f(0.1),
32-
ConsPercent: f(0.2),
19+
ProdPercent: 0.1,
20+
ConsPercent: 0.2,
3321
CurrentProduction: 10,
3422
CurrentConsumption: 40,
3523
MaxProd: 100.0,
@@ -95,38 +83,5 @@ var _ = Describe("ProductionCollector", func() {
9583
Expect(val).To(Equal(float64(200)))
9684
})
9785

98-
Describe("when capacity metrics aren't supplied", func() {
99-
100-
BeforeEach(func() {
101-
FRMServer.ReturnsProductionData([]exporter.ProductionDetails{
102-
{
103-
ItemName: "Iron Rod",
104-
ProdPerMin: prodPerMinWithoutCapacity(10, 40),
105-
ProdPercent: f(1.0),
106-
ConsPercent: f(1.0),
107-
CurrentProduction: 10,
108-
CurrentConsumption: 40,
109-
MaxProd: 100.0,
110-
MaxConsumed: 200.0,
111-
},
112-
})
113-
})
114-
115-
It("sets 'item_production_capacity_per_min' to zero", func() {
116-
collector.Collect()
117-
118-
val, err := gaugeValue(exporter.ItemProductionCapacityPerMinute, "Iron Rod")
119-
Expect(err).ToNot(HaveOccurred())
120-
Expect(val).To(Equal(float64(0)))
121-
})
122-
123-
It("sets 'item_consumption_capacity_per_min' to zero", func() {
124-
collector.Collect()
125-
126-
val, err := gaugeValue(exporter.ItemConsumptionCapacityPerMinute, "Iron Rod")
127-
Expect(err).ToNot(HaveOccurred())
128-
Expect(val).To(Equal(float64(0)))
129-
})
130-
})
13186
})
13287
})

0 commit comments

Comments
 (0)