11package metrics
22
33import (
4- "github.com/hikhvar/mqtt2prometheus/pkg/config"
5- "github.com/prometheus/client_golang/prometheus"
64 "reflect"
75 "testing"
86 "time"
7+
8+ "github.com/hikhvar/mqtt2prometheus/pkg/config"
9+ "github.com/prometheus/client_golang/prometheus"
910)
1011
1112func TestParser_parseMetric (t * testing.T ) {
@@ -50,6 +51,32 @@ func TestParser_parseMetric(t *testing.T) {
5051 Topic : "" ,
5152 },
5253 },
54+ {
55+ name : "scaled string value" ,
56+ fields : fields {
57+ map [string ][]config.MetricConfig {
58+ "temperature" : []config.MetricConfig {
59+ {
60+ PrometheusName : "temperature" ,
61+ ValueType : "gauge" ,
62+ MQTTValueScale : 0.01 ,
63+ },
64+ },
65+ },
66+ },
67+ args : args {
68+ metricPath : "temperature" ,
69+ deviceID : "dht22" ,
70+ value : "12.6" ,
71+ },
72+ want : Metric {
73+ Description : prometheus .NewDesc ("temperature" , "" , []string {"sensor" , "topic" }, nil ),
74+ ValueType : prometheus .GaugeValue ,
75+ Value : 0.126 ,
76+ IngestTime : testNow (),
77+ Topic : "" ,
78+ },
79+ },
5380 {
5481 name : "string value failure" ,
5582 fields : fields {
@@ -94,6 +121,58 @@ func TestParser_parseMetric(t *testing.T) {
94121 Topic : "" ,
95122 },
96123 },
124+ {
125+ name : "scaled float value" ,
126+ fields : fields {
127+ map [string ][]config.MetricConfig {
128+ "humidity" : []config.MetricConfig {
129+ {
130+ PrometheusName : "humidity" ,
131+ ValueType : "gauge" ,
132+ MQTTValueScale : 0.01 ,
133+ },
134+ },
135+ },
136+ },
137+ args : args {
138+ metricPath : "humidity" ,
139+ deviceID : "dht22" ,
140+ value : 12.6 ,
141+ },
142+ want : Metric {
143+ Description : prometheus .NewDesc ("humidity" , "" , []string {"sensor" , "topic" }, nil ),
144+ ValueType : prometheus .GaugeValue ,
145+ Value : 0.126 ,
146+ IngestTime : testNow (),
147+ Topic : "" ,
148+ },
149+ },
150+ {
151+ name : "negative scaled float value" ,
152+ fields : fields {
153+ map [string ][]config.MetricConfig {
154+ "humidity" : []config.MetricConfig {
155+ {
156+ PrometheusName : "humidity" ,
157+ ValueType : "gauge" ,
158+ MQTTValueScale : - 2 ,
159+ },
160+ },
161+ },
162+ },
163+ args : args {
164+ metricPath : "humidity" ,
165+ deviceID : "dht22" ,
166+ value : 12.6 ,
167+ },
168+ want : Metric {
169+ Description : prometheus .NewDesc ("humidity" , "" , []string {"sensor" , "topic" }, nil ),
170+ ValueType : prometheus .GaugeValue ,
171+ Value : - 25.2 ,
172+ IngestTime : testNow (),
173+ Topic : "" ,
174+ },
175+ },
97176 {
98177 name : "bool value true" ,
99178 fields : fields {
@@ -119,6 +198,32 @@ func TestParser_parseMetric(t *testing.T) {
119198 Topic : "" ,
120199 },
121200 },
201+ {
202+ name : "scaled bool value" ,
203+ fields : fields {
204+ map [string ][]config.MetricConfig {
205+ "enabled" : []config.MetricConfig {
206+ {
207+ PrometheusName : "enabled" ,
208+ ValueType : "gauge" ,
209+ MQTTValueScale : 0.5 ,
210+ },
211+ },
212+ },
213+ },
214+ args : args {
215+ metricPath : "enabled" ,
216+ deviceID : "dht22" ,
217+ value : true ,
218+ },
219+ want : Metric {
220+ Description : prometheus .NewDesc ("enabled" , "" , []string {"sensor" , "topic" }, nil ),
221+ ValueType : prometheus .GaugeValue ,
222+ Value : 0.5 ,
223+ IngestTime : testNow (),
224+ Topic : "" ,
225+ },
226+ },
122227 {
123228 name : "bool value false" ,
124229 fields : fields {
0 commit comments