-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
historical_uptime: track missing observations by guardians
Signed-off-by: bingyuyap <[email protected]>
- Loading branch information
Showing
9 changed files
with
797 additions
and
19 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
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,9 @@ | ||
package common | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
ExpiryDuration = 30 * time.Hour | ||
) |
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,64 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/dgraph-io/badger/v3" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type badgerZapLogger struct { | ||
*zap.Logger | ||
} | ||
|
||
func (l badgerZapLogger) Errorf(f string, v ...interface{}) { | ||
l.Error(fmt.Sprintf(f, v...)) | ||
} | ||
|
||
func (l badgerZapLogger) Warningf(f string, v ...interface{}) { | ||
l.Warn(fmt.Sprintf(f, v...)) | ||
} | ||
|
||
func (l badgerZapLogger) Infof(f string, v ...interface{}) { | ||
l.Info(fmt.Sprintf(f, v...)) | ||
} | ||
|
||
func (l badgerZapLogger) Debugf(f string, v ...interface{}) { | ||
l.Debug(fmt.Sprintf(f, v...)) | ||
} | ||
|
||
type Database struct { | ||
db *badger.DB | ||
} | ||
|
||
func OpenDb(logger *zap.Logger, dataDir *string) *Database { | ||
var options badger.Options | ||
|
||
if dataDir != nil { | ||
dbPath := path.Join(*dataDir, "db") | ||
if err := os.MkdirAll(dbPath, 0700); err != nil { | ||
logger.Fatal("failed to create database directory", zap.Error(err)) | ||
} | ||
|
||
options = badger.DefaultOptions(dbPath) | ||
} else { | ||
options = badger.DefaultOptions("").WithInMemory(true) | ||
} | ||
|
||
options = options.WithLogger(badgerZapLogger{logger}) | ||
|
||
db, err := badger.Open(options) | ||
if err != nil { | ||
logger.Fatal("failed to open database", zap.Error(err)) | ||
} | ||
|
||
return &Database{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (db *Database) Close() error { | ||
return db.db.Close() | ||
} |
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,46 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/dgraph-io/badger/v3" | ||
) | ||
|
||
// Index keys formats | ||
const ( | ||
// Format for metricsChecked index key: metricsChecked|<bool>|<messageID> | ||
metricsCheckedIndexKeyFmt = "metricsChecked|%t|%s" | ||
) | ||
|
||
// CreateOrUpdateIndex creates or updates indexes for a message | ||
func CreateOrUpdateIndex(txn *badger.Txn, message *Message) error { | ||
// Index for metricsChecked | ||
mcKey := fmt.Sprintf(metricsCheckedIndexKeyFmt, message.MetricsChecked, message.MessageID) | ||
if err := txn.Set([]byte(mcKey), []byte(message.MessageID)); err != nil { | ||
return fmt.Errorf("failed to set metricsChecked index: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// parseMetricsCheckedIndexKey helper function to parse index key and extract values | ||
func parseMetricsCheckedIndexKey(key []byte) (bool, string, error) { | ||
keyStr := string(key) // Convert byte slice to string | ||
parts := strings.Split(keyStr, "|") | ||
if len(parts) != 3 || parts[0] != "metricsChecked" { | ||
return false, "", fmt.Errorf("invalid key format") | ||
} | ||
|
||
// Parse the metricsChecked value from the string to a bool | ||
metricsChecked, err := strconv.ParseBool(parts[1]) | ||
if err != nil { | ||
return false, "", fmt.Errorf("error parsing metricsChecked value from key: %w", err) | ||
} | ||
|
||
// The MessageID is the last part of the key | ||
messageID := parts[2] | ||
|
||
return metricsChecked, messageID, nil | ||
} |
Oops, something went wrong.