Compare commits

3 Commits

Author SHA1 Message Date
yo
fe6ca819f4 v0.2.4: BUGFIX deep copy nodes and edges 2025-01-13 19:18:17 +01:00
yo
101bdb92a3 Change sample name, praise syntax coloration god 2025-01-12 21:15:31 +01:00
yo
55ea9b6edf Change sample name, praise syntax coloration god 2025-01-12 21:15:13 +01:00
3 changed files with 62 additions and 16 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
nodegopher
config.yaml*
!config.yaml.sample
!config.sample.yml
main.go.*

View File

@ -49,7 +49,7 @@ datasources:
timeout: 10
- name: router01_net_down_rate_perten
type: query
address: 'http://prometheus.moon.lan:9090'
address: 'http://prometheus.local.lan:9090'
query: 'rate(node_network_receive_bytes_total{device="igb0", instance="router01.local.lan:9100", job="node"}[30s])/62500000*10'
timeout: 10

74
main.go
View File

@ -34,7 +34,9 @@ import (
)
const (
gVersion = "0.2.3"
gVersion = "0.2.4"
// Default datasource timeout is 10 seconds
gDefaultDSTimeout = 10
)
type PromDataSourceConfig struct {
@ -120,7 +122,7 @@ func (d *PromDataSourceConfig) GetData(timeRange *MyRange) (float64, error) {
result, warnings, err = v1api.QueryRange(ctx, d.Query, rng, v1.WithTimeout(time.Duration(d.Timeout)*time.Second))
}
if err != nil {
log.Errorf("DataSourceConfig.GetData: Error querying Prometheus: %v\n", err)
log.Errorf("DataSourceConfig.GetData: Error querying Prometheus: %v. Query is: %s\n", err, d.Query)
return 0.0, err
}
if len(warnings) > 0 {
@ -425,6 +427,48 @@ func toggleDebug() {
}
}
// Deep copy src Node into a new memory space
func newNodeClone(src *Node) *Node {
return &Node{
Name: src.Name,
Id: src.Id,
Title: src.Title,
Subtitle: src.Subtitle,
MainStat: src.MainStat,
MainStatQuery: src.MainStatQuery,
MainStatFormat: src.MainStatFormat,
SecondaryStat: src.SecondaryStat,
SecondaryStatQuery: src.SecondaryStatQuery,
SecondaryStatFormat: src.SecondaryStatFormat,
Color: src.Color,
Icon: src.Icon,
NodeRadius: src.NodeRadius,
Highlighted: src.Highlighted,
HighlightedQuery: src.HighlightedQuery,
}
}
// Deep copy src Edge into a new memory space
func newEdgeClone(src *Edge) *Edge {
return &Edge{
Id: src.Id,
Source: src.Source,
Target: src.Target,
MainStat: src.MainStat,
MainStatQuery: src.MainStatQuery,
MainStatFormat: src.MainStatFormat,
SecondaryStat: src.SecondaryStat,
SecondaryStatQuery: src.SecondaryStatQuery,
SecondaryStatFormat: src.SecondaryStatFormat,
Color: src.Color,
Thickness: src.Thickness,
ThicknessQuery: src.ThicknessQuery,
Highlighted: src.Highlighted,
HighlightedQuery: src.HighlightedQuery,
StrokeDashArray: src.StrokeDashArray,
}
}
func reloadConfigFile() {
// First reread config file
if err := viper.ReadInConfig(); err != nil {
@ -451,7 +495,7 @@ func reloadConfigFile() {
case "chinese":
gPrinter = message.NewPrinter(language.Chinese)
default:
log.Errorf("Language not implented: %s. Fallback to english\n", viper.Get("language").(string))
log.Errorf("Language not implemented: %s. Fallback to english\n", viper.Get("language").(string))
gPrinter = message.NewPrinter(language.English)
}
@ -478,19 +522,17 @@ func reloadConfigFile() {
Edges []Edge `yaml:"edges"`
}{}
yaml.Unmarshal(yd, &tmp)
var graphNodes []Item
var graphEdges []Item
for _, n := range tmp.Nodes {
graphNodes = append(graphNodes, &n)
}
for _, e := range tmp.Edges {
graphEdges = append(graphEdges, &e)
}
graph := Graph{
Name: tmp.Name,
Nodes: graphNodes,
Edges: graphEdges,
}
for _, n := range tmp.Nodes {
// Deep copy Node so garbage collecting tmp won't pull the carpet under our feet
graph.Nodes = append(graph.Nodes, newNodeClone(&n))
}
for _, e := range tmp.Edges {
// Deep copy Edge
graph.Edges = append(graph.Edges, newEdgeClone(&e))
}
gGraphs = append(gGraphs, graph)
}
@ -504,6 +546,10 @@ func reloadConfigFile() {
yd, _ := yaml.Marshal(d)
var ds PromDataSourceConfig
yaml.Unmarshal(yd, &ds)
// Set default Values
if ds.Timeout == 0 {
ds.Timeout = gDefaultDSTimeout
}
gDataSources = append(gDataSources, ds)
}
}