@@ -7,68 +7,123 @@ import (
77 "sync"
88)
99
10- type PerformanceData map [string ]interface {}
10+ // PerformanceData is a map with string keys and any values, essentially a dictionary
11+ // Since its an dictionary, always add add a label
12+ // "label" = "health_rate"
13+ // "value" = 80
14+ // "critical" = 160
15+ type PerformanceData map [string ]any
1116
12- var (
13- p []PerformanceData = []PerformanceData {}
14- pMutex = & sync.Mutex {}
15- )
17+ // This is a struct that users of the library should use.
18+ // They have to manage this object through functions in this file
19+ // Library clients should get an instance of this and keep calling its methods
20+ type PerformanceDataCollection struct {
21+ data []PerformanceData
22+ dataMutex * sync.Mutex
23+ }
24+
25+ // Returns an empty PerformanceDataCollection
26+ func NewPerformanceDataCollection () PerformanceDataCollection {
27+ return PerformanceDataCollection {
28+ data : make ([]PerformanceData , 0 ),
29+ dataMutex : & sync.Mutex {},
30+ }
31+ }
1632
17- // NewPerformanceData adds a PerformanceData object which can be expanded with further information
18- func NewPerformanceData (label string , value float64 ) * PerformanceData {
19- return NewPerformanceDataString (label , strconv .FormatFloat (value , 'f' , - 1 , 64 ))
33+ // Adds a new PerformanceData element to the data array
34+ // Use the label to get the reference to the PerformanceData later on
35+ func (collection * PerformanceDataCollection ) AddPerformanceData (label string , value string ) {
36+ collection .dataMutex .Lock ()
37+ collection .data = append (collection .data , PerformanceData {"label" : label , "value" : value })
38+ collection .dataMutex .Unlock ()
2039}
2140
22- // NewPerformanceDataString adds a PerformanceData object which can be expanded with further information
23- func NewPerformanceDataString (label , value string ) * PerformanceData {
24- pMutex .Lock ()
25- p = append (p , PerformanceData {"label" : label , "value" : value })
26- newOne := & (p [len (p )- 1 ])
27- pMutex .Unlock ()
28- return newOne
41+ // Calls collection.AddPerformanceData after converting float to string
42+ func (collection * PerformanceDataCollection ) AddPerformanceDataFloat64 (label string , value float64 ) {
43+ collection .AddPerformanceData (label , strconv .FormatFloat (value , 'f' , - 1 , 64 ))
2944}
3045
31- // Unit adds an unit string to the PerformanceData
32- func (p PerformanceData ) Unit (unit string ) PerformanceData {
33- p ["unit" ] = unit
34- return p
46+ // Internal function to find a PerformanceData with specified label
47+ func (collection * PerformanceDataCollection ) findPerformanceData (label string ) (* PerformanceData , error ) {
48+ for index , pd := range collection .data {
49+ if pd_label , ok := pd ["label" ]; ok && pd_label == label {
50+ return & collection .data [index ], nil
51+ }
52+ }
53+ return nil , fmt .Errorf ("no performance data with the label '%s' found" , label )
54+ }
55+
56+ // Adds a field called "unit" to the PerformanceData with the label
57+ func (collection * PerformanceDataCollection ) Unit (label string , unit string ) error {
58+ collection .dataMutex .Lock ()
59+ defer collection .dataMutex .Unlock ()
60+ pd , err := collection .findPerformanceData (label )
61+ if err != nil {
62+ return err
63+ }
64+ (* pd )["unit" ] = unit
65+ return nil
3566}
3667
37- // Warn adds the threshold to the PerformanceData
38- func (p PerformanceData ) Warn (warn * Threshold ) PerformanceData {
39- p ["warn" ] = warn
40- return p
68+ // Adds a field called "unit" to the PerformanceData with the label
69+ func (collection * PerformanceDataCollection ) Warn (label string , warn * Threshold ) error {
70+ collection .dataMutex .Lock ()
71+ defer collection .dataMutex .Unlock ()
72+ pd , err := collection .findPerformanceData (label )
73+ if err != nil {
74+ return err
75+ }
76+ (* pd )["warn" ] = warn
77+ return nil
4178}
4279
43- // Crit adds the threshold to the PerformanceData
44- func (p PerformanceData ) Crit (crit * Threshold ) PerformanceData {
45- p ["crit" ] = crit
46- return p
80+ // Adds a field called "unit" to the PerformanceData with the label
81+ func (collection * PerformanceDataCollection ) Crit (label string , crit * Threshold ) error {
82+ collection .dataMutex .Lock ()
83+ defer collection .dataMutex .Unlock ()
84+ pd , err := collection .findPerformanceData (label )
85+ if err != nil {
86+ return err
87+ }
88+ (* pd )["crit" ] = crit
89+ return nil
4790}
4891
49- // Min adds the float64 to the PerformanceData
50- func (p PerformanceData ) Min (min float64 ) PerformanceData {
51- p ["min" ] = min
52- return p
92+ // Adds a field called "unit" to the PerformanceData with the label
93+ func (collection * PerformanceDataCollection ) Min (label string , min float64 ) error {
94+ collection .dataMutex .Lock ()
95+ defer collection .dataMutex .Unlock ()
96+ pd , err := collection .findPerformanceData (label )
97+ if err != nil {
98+ return err
99+ }
100+ (* pd )["min" ] = min
101+ return nil
53102}
54103
55- // Min adds the float64 to the PerformanceData
56- func (p PerformanceData ) Max (max float64 ) PerformanceData {
57- p ["max" ] = max
58- return p
104+ // Adds a field called "unit" to the PerformanceData with the label
105+ func (collection * PerformanceDataCollection ) Max (label string , max float64 ) error {
106+ collection .dataMutex .Lock ()
107+ defer collection .dataMutex .Unlock ()
108+ pd , err := collection .findPerformanceData (label )
109+ if err != nil {
110+ return err
111+ }
112+ (* pd )["max" ] = max
113+ return nil
59114}
60115
61- // toString prints this PerformanceData
62- func (p PerformanceData ) toString () string {
116+ // internal function to print a PerformanceData
117+ func (pd PerformanceData ) toString () string {
63118 var toPrint bytes.Buffer
64119
65- toPrint .WriteString (fmt .Sprintf ("'%s'=%s" , p ["label" ], p ["value" ]))
66- if unit , ok := p ["unit" ]; ok {
120+ toPrint .WriteString (fmt .Sprintf ("'%s'=%s" , pd ["label" ], pd ["value" ]))
121+ if unit , ok := pd ["unit" ]; ok {
67122 toPrint .WriteString (unit .(string ))
68123 }
69124 toPrint .WriteString (";" )
70125 addThreshold := func (key string ) {
71- if value , ok := p [key ]; ok && value != nil {
126+ if value , ok := pd [key ]; ok && value != nil {
72127 if t := value .(* Threshold ); t != nil {
73128 toPrint .WriteString (t .input )
74129 }
@@ -79,7 +134,7 @@ func (p PerformanceData) toString() string {
79134 addThreshold ("crit" )
80135
81136 addFloat := func (key string ) {
82- if value , ok := p [key ]; ok {
137+ if value , ok := pd [key ]; ok {
83138 toPrint .WriteString (strconv .FormatFloat (value .(float64 ), 'f' , - 1 , 64 ))
84139 }
85140 }
@@ -90,14 +145,33 @@ func (p PerformanceData) toString() string {
90145 return toPrint .String ()
91146}
92147
93- // PrintPerformanceData prints all PerformanceData
94- func PrintPerformanceData () string {
148+ // Finds and prints the PerformanceData found in this collection
149+ func (collection * PerformanceDataCollection ) PrintPerformanceData (label string ) (string , error ) {
150+ collection .dataMutex .Lock ()
151+ defer collection .dataMutex .Unlock ()
152+ pd , err := collection .findPerformanceData (label )
153+ if err != nil {
154+ return "" , err
155+ }
156+
157+ return (* pd ).toString (), nil
158+ }
159+
160+ // Prints all PerformanceData to a string in this collection
161+ func (collection * PerformanceDataCollection ) PrintAllPerformanceData () string {
162+ collection .dataMutex .Lock ()
163+ defer collection .dataMutex .Unlock ()
95164 var toPrint bytes.Buffer
96- pMutex .Lock ()
97- for _ , perfData := range p {
165+ for _ , perfData := range collection .data {
98166 toPrint .WriteString (perfData .toString ())
99167 toPrint .WriteString (" " )
100168 }
101- pMutex .Unlock ()
102169 return toPrint .String ()
103170}
171+
172+ // Clears all PerformanceData stored in this collection
173+ func (collection * PerformanceDataCollection ) ClearPerformanceCollection () {
174+ collection .dataMutex .Lock ()
175+ defer collection .dataMutex .Unlock ()
176+ collection .data = make ([]PerformanceData , 0 )
177+ }
0 commit comments