Skip to content

Commit

Permalink
Merge pull request #48 from lidofinance/required-unique-key
Browse files Browse the repository at this point in the history
Required unique key
  • Loading branch information
sergeyWh1te authored Nov 28, 2024
2 parents 02799af + 8ce2283 commit 8999d6b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 28.11.2024
1. Field "uniqueKey" is required for getting quorum.

## 16.10.2024
1. Added dynamic yaml notification config

Expand Down
2 changes: 1 addition & 1 deletion brief/databus/finding.dto.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"type": "string"
}
},
"required": ["severity", "alertId", "name", "description", "botName", "team"],
"required": ["severity", "alertId", "name", "description", "botName", "team", "uniqueKey"],
"definitions": {
"Severity": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions cmd/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func main() {
MaxAge: 10 * time.Minute,
Subjects: env.CollectNatsSubjects(notificationConfig),
MaxMsgSize: maxMsgSize,
Retention: jetstream.InterestPolicy,
})
if err != nil && !errors.Is(err, nats.ErrStreamNameAlreadyInUse) {
fmt.Printf("could not create %s stream error: %v\n", natsStreamName, err)
Expand Down
6 changes: 2 additions & 4 deletions generated/databus/block.dto.go

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

13 changes: 7 additions & 6 deletions generated/databus/finding.dto.go

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

28 changes: 1 addition & 27 deletions internal/pkg/consumer/consumer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package consumer

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log/slog"
Expand Down Expand Up @@ -208,7 +205,7 @@ func (c *Consumer) GetConsumeHandler(ctx context.Context) func(msg jetstream.Msg
return
}

key := findingToUniqueHash(finding)
key := finding.UniqueKey
countKey := fmt.Sprintf(countTemplate, c.name, key)
statusKey := fmt.Sprintf(statusTemplate, c.name, key)

Expand Down Expand Up @@ -405,26 +402,3 @@ func (c *Consumer) ackMessage(msg jetstream.Msg) {
c.log.Error(fmt.Sprintf(`Could not ack msg: %v`, ackErr))
}
}

func computeSHA256Hash(data []byte) string {
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}

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

if f.UniqueKey != nil {
buffer.WriteString(f.Team)
buffer.WriteString(f.BotName)
buffer.WriteString(*f.UniqueKey)
} else {
buffer.WriteString(f.Team)
buffer.WriteString(f.BotName)
buffer.WriteString(f.AlertId)
buffer.WriteString(f.Name)
buffer.WriteString(string(f.Severity))
}

return computeSHA256Hash(buffer.Bytes())
}

0 comments on commit 8999d6b

Please sign in to comment.