Skip to content

Commit

Permalink
Merge pull request #31 from lidofinance/feat/unique-key
Browse files Browse the repository at this point in the history
Feat/unique key
  • Loading branch information
sergeyWh1te authored Oct 8, 2024
2 parents e7e312c + 63d7f31 commit 1ecd683
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions brief/databus/finding.dto.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"blockNumber": {
"type": "integer"
},
"findingBotTimestamp": {
"type": "integer"
},
"txHash": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions generated/databus/finding.dto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions internal/app/worker/worker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package worker

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -161,8 +162,7 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
return
}

// TODO to think on better hashing fields
key := computeSHA256Hash(msg.Data())
key := findingToUniqueHash(finding)

countKey := fmt.Sprintf(countTemplate, consumer.Name, key)
statusKey := fmt.Sprintf(statusTemplate, consumer.Name, key)
Expand Down Expand Up @@ -408,6 +408,24 @@ func computeSHA256Hash(data []byte) string {
return hex.EncodeToString(hash[:])
}

func findingToUniqueHash(f *databus.FindingDtoJson) string {
var buffer bytes.Buffer

if f.UniqueKey != nil {
return *f.UniqueKey
}

buffer.WriteString(f.Team)
buffer.WriteString(f.BotName)
buffer.WriteString(f.AlertId)
buffer.WriteString(f.Name)
buffer.WriteString(string(f.Severity))

uniqueKey := computeSHA256Hash(buffer.Bytes())

return uniqueKey
}

func (w *findingWorker) SetSendingStatus(ctx context.Context, countKey, statusKey string) (bool, error) {
luaScript := `
local count = tonumber(redis.call("GET", KEYS[1]))
Expand Down

0 comments on commit 1ecd683

Please sign in to comment.