mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00

* added a MqttMonitor module with levelDB and periodic cleanup * initialized in the app * passed to dao keeper * added conversion methods (string2unixtime, byte ToJSON) * removed obsolete keeper code * maded RDDLToken.Factor public * added explicit mqtt client to the monitor module * restart mqtt connection in mqttmonitor on connection loss * adjusted mqttmock structure to be compatible * added some linter exclusions to let the monitor tool pass * created a MockMqttMonitor interface and mock object * used this to pass tests * made the MockMqttMonitor a global object so that it can be easily mocked * removed MockMqttMonitor from the app/keeper initialization * adjusted test cases to register "active machines" to the mqttmonitor * added mutex in mocks to protect against data races * defined mocks for the dao tests * clear separation between interface and mqtt-Monitor * added another waiting block to ensure the tx went through (multi-threading issue, race condition) during tests this failed sometimes * added memstorage to test instead of a file based DB Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package monitor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
)
|
|
|
|
type LastSeenEvent struct {
|
|
Address string `binding:"required" json:"address"`
|
|
Timestamp int64 `binding:"required" json:"timestamp"`
|
|
}
|
|
|
|
func (mms *MqttMonitor) AddParticipant(address string, lastSeenTS int64) (err error) {
|
|
// store receive address - planetmint address pair
|
|
var lastSeen LastSeenEvent
|
|
lastSeen.Address = address
|
|
lastSeen.Timestamp = lastSeenTS
|
|
|
|
lastSeenBytes, err := json.Marshal(lastSeen)
|
|
if err != nil {
|
|
mms.Log("[Monitor] Error serializing ConversionRequest: " + err.Error())
|
|
return
|
|
}
|
|
increaseCounter := false
|
|
// returns an error if the entry does not exist (we have to increase the counter in this case)
|
|
_, err = mms.db.Get([]byte(address), nil)
|
|
if err != nil {
|
|
increaseCounter = true
|
|
}
|
|
mms.dbMutex.Lock()
|
|
if increaseCounter {
|
|
mms.numberOfElements++
|
|
}
|
|
err = mms.db.Put([]byte(address), lastSeenBytes, nil)
|
|
mms.dbMutex.Unlock()
|
|
if err != nil {
|
|
log.Println("[Monitor] storing addresses in DB: " + err.Error())
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (mms *MqttMonitor) deleteEntry(key []byte) (err error) {
|
|
mms.dbMutex.Lock()
|
|
err = mms.db.Delete(key, nil)
|
|
mms.numberOfElements--
|
|
mms.dbMutex.Unlock()
|
|
return
|
|
}
|
|
|
|
func (mms *MqttMonitor) getAmountOfElements() (amount int64, err error) {
|
|
iter := mms.db.NewIterator(nil, nil)
|
|
defer iter.Release()
|
|
|
|
for iter.Next() {
|
|
amount++
|
|
}
|
|
|
|
// Check for any errors encountered during iteration
|
|
if err := iter.Error(); err != nil {
|
|
log.Println("[Monitor] " + err.Error())
|
|
}
|
|
return
|
|
}
|
|
func (mms *MqttMonitor) getDataFromIter(iter iterator.Iterator) (lastSeen LastSeenEvent, err error) {
|
|
key := iter.Key()
|
|
value := iter.Value()
|
|
err = json.Unmarshal(value, &lastSeen)
|
|
if err != nil {
|
|
mms.Log("[Monitor] Failed to unmarshal entry: " + string(key) + " - " + err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
func (mms *MqttMonitor) CleanupDB() {
|
|
// Create an iterator for the database
|
|
iter := mms.db.NewIterator(nil, nil)
|
|
defer iter.Release() // Make sure to release the iterator at the end
|
|
|
|
// Iterate over all elements in the database
|
|
for iter.Next() && !mms.IsTerminated() {
|
|
// Use iter.Key() and iter.Value() to access the key and value
|
|
lastSeen, err := mms.getDataFromIter(iter)
|
|
if err != nil {
|
|
mms.Log("[Monitor] Failed to unmarshal entry: " + string(iter.Key()) + " - " + err.Error())
|
|
continue
|
|
}
|
|
timeThreshold := time.Now().Add(-1 * mms.CleanupPeriodicityInMinutes * time.Minute).Unix()
|
|
if lastSeen.Timestamp <= timeThreshold {
|
|
// If the entry is older than 12 hours, delete it
|
|
err := mms.deleteEntry(iter.Key())
|
|
if err != nil {
|
|
mms.Log("[Monitor] Failed to delete entry: " + err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check for any errors encountered during iteration
|
|
if err := iter.Error(); err != nil {
|
|
mms.Log(err.Error())
|
|
}
|
|
}
|