-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
561 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package metrics | ||
|
||
var luna2000Metrics = []Metric{ | ||
{ | ||
Namespace: "luna2000", | ||
Name: "running_status", | ||
Help: "The running status", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
{ | ||
Namespace: "luna2000", | ||
Name: "charging_status", | ||
Help: "The charging status", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
{ | ||
Namespace: "luna2000", | ||
Name: "bus_voltage", | ||
Help: "The bus voltage", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
{ | ||
Namespace: "luna2000", | ||
Name: "battery_capacity", | ||
Help: "The battery capacity", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
{ | ||
Namespace: "luna2000", | ||
Name: "total_charge", | ||
Help: "The total charge", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
{ | ||
Namespace: "luna2000", | ||
Name: "total_discharge", | ||
Help: "The total discharge", | ||
Fields: []string{ | ||
"inverter", | ||
"battery", | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type MetricValue struct { | ||
Fields map[string]string | ||
Values []float64 | ||
} | ||
|
||
type Metric struct { | ||
Namespace string | ||
Name string | ||
Help string | ||
Fields []string | ||
|
||
Values []MetricValue | ||
PrometheusGauge *prometheus.GaugeVec | ||
} | ||
|
||
var metrics = []Metric{} | ||
|
||
func init() { | ||
metrics = append(metrics, sun2000Metrics...) | ||
metrics = append(metrics, luna2000Metrics...) | ||
metrics = append(metrics, powerMeterMetrics...) | ||
|
||
for index, metric := range metrics { | ||
metric.Values = []MetricValue{} | ||
metric.PrometheusGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: metric.Namespace, | ||
Name: metric.Name, | ||
Help: metric.Help, | ||
}, | ||
metric.Fields, | ||
) | ||
|
||
metrics[index] = metric | ||
} | ||
} | ||
|
||
func AddMetric(namespace string, name string, help string, fields []string) *Metric { | ||
metrics = append(metrics, Metric{ | ||
Namespace: namespace, | ||
Name: name, | ||
Help: help, | ||
Fields: fields, | ||
Values: []MetricValue{}, | ||
PrometheusGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Name: name, | ||
Help: help, | ||
}, fields), | ||
}) | ||
|
||
return &metrics[len(metrics) - 1] | ||
} | ||
|
||
func GetMetric(namespace string, name string) *Metric { | ||
for _, metric := range metrics { | ||
if metric.Namespace == namespace && metric.Name == name { | ||
return &metric | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetMetricValueAverageLastMin(namespace string, name string, labels map[string]string, minutes uint) float64 { | ||
var newMetric *Metric | ||
for _, metric := range metrics { | ||
if metric.Namespace == namespace && metric.Name == name { | ||
newMetric = &metric | ||
break | ||
} | ||
} | ||
|
||
if newMetric == nil { | ||
return 0 | ||
} | ||
|
||
var matches *MetricValue | ||
for _, metricValue := range newMetric.Values { | ||
match := true | ||
for key, value := range labels { | ||
if metricValue.Fields[key] != value { | ||
match = false | ||
break | ||
} | ||
} | ||
|
||
if !match { | ||
continue | ||
} | ||
|
||
matches = &metricValue | ||
} | ||
|
||
if matches == nil { | ||
return 0 | ||
} | ||
|
||
var total float64 | ||
for _, value := range matches.Values { | ||
total += value | ||
} | ||
|
||
return total / float64(len(matches.Values)) | ||
} | ||
|
||
func SetMetricValue(namespace string, name string, labels map[string]string, value float64) { | ||
var newMetric *Metric | ||
var metricIndex int | ||
for i, metric := range metrics { | ||
if metric.Namespace == namespace && metric.Name == name { | ||
newMetric = &metric | ||
metricIndex = i | ||
break | ||
} | ||
} | ||
|
||
if newMetric == nil { | ||
return | ||
} | ||
|
||
var matches *MetricValue | ||
for _, metricValue := range newMetric.Values { | ||
match := true | ||
for key, value := range labels { | ||
if metricValue.Fields[key] != value { | ||
match = false | ||
break | ||
} | ||
} | ||
|
||
if !match { | ||
continue | ||
} | ||
|
||
matches = &metricValue | ||
} | ||
|
||
if matches != nil { | ||
matches.Values = append(matches.Values, value) | ||
if len(matches.Values) > 5760 { // 1 days worth of data if we have a value every 15 seconds | ||
matches.Values = matches.Values[1:] | ||
} | ||
} else { | ||
newMetric.Values = append(newMetric.Values, MetricValue{ | ||
Fields: labels, | ||
Values: []float64{value}, | ||
}) | ||
} | ||
|
||
metrics[metricIndex] = *newMetric | ||
newMetric.PrometheusGauge.With(labels).Set(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
AddMetric("tests", "get_test", "A test testing the get function", []string{ | ||
"inverter", | ||
"string", | ||
}) | ||
|
||
metric := GetMetric("tests", "get_test") | ||
if metric == nil { | ||
t.Fatalf("Metric not found") | ||
} | ||
|
||
if metric.Name != "get_test" { | ||
t.Fatalf("Incorrect name") | ||
} | ||
} | ||
|
||
func TestSet(t *testing.T) { | ||
AddMetric("tests", "set_test", "A test testing the set function", []string{ | ||
"inverter", | ||
"string", | ||
}) | ||
|
||
SetMetricValue("tests", "set_test", map[string]string{ | ||
"inverter": "1", | ||
"string": "1", | ||
}, 1.0) | ||
|
||
metric := GetMetric("tests", "set_test") | ||
if metric == nil { | ||
t.Fatalf("Metric not found") | ||
} | ||
|
||
if metric.Values[0].Value != 1.0 { | ||
t.Fatalf("Incorrect value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package metrics | ||
|
||
var powerMeterMetrics = []Metric{ | ||
{ | ||
Namespace: "power_meter", | ||
Name: "status", | ||
Help: "The status of the power meter", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "phase_voltage", | ||
Help: "The phase voltage", | ||
Fields: []string{ | ||
"inverter", | ||
"phase", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "phase_current", | ||
Help: "The phase current", | ||
Fields: []string{ | ||
"inverter", | ||
"phase", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "active_power", | ||
Help: "The active power", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "reactive_power", | ||
Help: "The reactive power", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "power_factor", | ||
Help: "The power factor", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "frequency", | ||
Help: "The frequency", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "positive_active_electricity", | ||
Help: "The positive active electricity", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "reverse_active_power", | ||
Help: "The reverse active power", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "accumulated_reactive_power", | ||
Help: "The accumulated reactive power", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "line_voltage", | ||
Help: "The line voltage", | ||
Fields: []string{ | ||
"inverter", | ||
"line", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "phase_active_power", | ||
Help: "The phase active power", | ||
Fields: []string{ | ||
"inverter", | ||
"phase", | ||
}, | ||
}, | ||
{ | ||
Namespace: "power_meter", | ||
Name: "model_result", | ||
Help: "The power meter model result", | ||
Fields: []string{ | ||
"inverter", | ||
}, | ||
}, | ||
} |
Oops, something went wrong.