-
Notifications
You must be signed in to change notification settings - Fork 3
/
samler.go
194 lines (169 loc) · 4.33 KB
/
samler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
SaMLer - Smart Meter data colletor at the edge
Copyright (C) 2024 Florian Heubeck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"io/fs"
"log"
"os"
"strings"
"time"
diskqueue "github.com/nsqio/go-diskqueue"
)
type Measurement struct {
Ident string
Unit string
Prefix string
Suffix string
Value float64
Time time.Time
}
type samler struct {
messageChannel chan Measurement
send func(Measurement) bool
cacheLocation string
identFilter []string
}
var memo = make(map[string]Measurement)
func debug(msg string, measure *Measurement) {
if debugFlag {
fmt.Printf("%s: %s - %f\n", msg, measure.Ident, measure.Value)
}
}
func RunSamler(
messageChannel chan Measurement,
send func(Measurement) bool,
cacheLocation string,
identFilter string,
) {
samler := samler{
messageChannel: messageChannel,
send: send,
cacheLocation: cacheLocation,
identFilter: toFilterList(identFilter),
}
go processLoop(&samler)
}
func toFilterList(identFilter string) []string {
rawIdentFilterList := strings.Split(identFilter, ",")
tmpIdentFilterList := make([]string, len(rawIdentFilterList))
count := 0
for i, v := range rawIdentFilterList {
tmpIdentFilterList[i] = strings.TrimSpace(v)
if len(tmpIdentFilterList[i]) > 0 && tmpIdentFilterList[i] != "-" {
count += 1
}
}
identFilterList := make([]string, count)
i := 0
for _, v := range tmpIdentFilterList {
if len(v) > 0 && v != "-" {
identFilterList[i] = v
i += 1
}
}
return identFilterList
}
func shouldSendAndMemorize(measure Measurement, identFilter []string) bool {
if !isRelevant(measure.Ident, identFilter) {
return false
}
key := fmt.Sprintf("%s#%s#%s", measure.Prefix, measure.Ident, measure.Suffix)
previous, ok := memo[key]
if !ok || previous.Value != measure.Value || previous.Time.Add(60*time.Second).Before(time.Now()) {
debug("Memorized", &measure)
memo[key] = measure
return true
} else {
debug("Skipped", &measure)
return false
}
}
func isRelevant(ident string, identFilter []string) bool {
if len(identFilter) == 0 {
return true
}
for _, filter := range identFilter {
if ident == filter {
return true
}
}
return false
}
func processLoop(ctx *samler) {
fmt.Printf("Init DiskQueue at %s\n", ctx.cacheLocation)
if err := os.MkdirAll(ctx.cacheLocation, fs.ModePerm); err != nil {
log.Fatal(err)
}
diskQueue := diskqueue.New("cached", ctx.cacheLocation, 10485760, 4, 1<<10, 4096, 10*time.Second, dqLog)
defer diskQueue.Close()
readChan := diskQueue.ReadChan()
peekChan := diskQueue.PeekChan()
circuitOpen := false
send := func(measure Measurement) bool {
success := ctx.send(measure)
if !success {
circuitOpen = true
log.Printf("Circuit open")
go func() {
time.Sleep(30 * time.Second)
circuitOpen = false
log.Printf("Circuit closed")
}()
}
return success
}
writeToDisk := func(measure Measurement) {
debug("Writing to disk", &measure)
if jsonData, err := json.Marshal(measure); err == nil {
diskQueue.Put(jsonData)
} else {
log.Fatal("Could not serialize measure", err)
}
}
peekFromDisk := func() Measurement {
message := <-peekChan
var measure Measurement
if err := json.Unmarshal(message, &measure); err != nil {
log.Fatal("Failed to deserialize msg", err)
}
debug("Read from disk", &measure)
return measure
}
removeLastPeeked := func() {
<-readChan
}
// process disk messages
go func() {
for {
if circuitOpen {
time.Sleep(30 * time.Second)
}
measurement := peekFromDisk()
if send(measurement) {
removeLastPeeked()
}
}
}()
for {
measurement := <-ctx.messageChannel
if shouldSendAndMemorize(measurement, ctx.identFilter) {
if circuitOpen || !send(measurement) {
writeToDisk(measurement)
}
}
}
}