Skip to content

Commit

Permalink
chore(changelog): change values if given timestamp exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldtinoco committed Sep 13, 2023
1 parent 16cd971 commit 30abbef
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/changelog/changelog.go
@@ -1,6 +1,10 @@
package changelog

import "time"
import (
"time"

"github.com/aquasecurity/tracee/pkg/logger"
)

type item[T any] struct {
timestamp time.Time // timestamp of the change
Expand Down Expand Up @@ -98,21 +102,32 @@ func (clv *Changelog[T]) Set(value T, targetTime time.Time) {

// setAt sets the value of the changelog at the given time.
func (clv *Changelog[T]) setAt(value T, targetTime time.Time) {
if _, ok := clv.timestamps[targetTime]; ok {
return // already set
// If the timestamp is already set, update that value only.
_, ok := clv.timestamps[targetTime]
if ok {
index := clv.findIndex(targetTime)
if !clv.changes[index].timestamp.Equal(targetTime) { // sanity check only (time exists already)
logger.Debugw("changelog error: timestamp mismatch")
return
}
// Should an debug/error be logged if the value is different ?
clv.changes[index].value = value
return
}

entry := item[T]{
timestamp: targetTime,
value: value,
}

// Insert the new entry in the changelog, keeping the list sorted by timestamp.
idx := clv.findIndex(entry.timestamp)
clv.changes = append(clv.changes, item[T]{})
copy(clv.changes[idx+1:], clv.changes[idx:])
clv.changes[idx] = entry

clv.timestamps[targetTime] = struct{}{} // mark timestamp as set
// Mark the timestamp as set.
clv.timestamps[targetTime] = struct{}{}
}

// findIndex returns the index of the first item in the changelog that is after the given time.
Expand Down

0 comments on commit 30abbef

Please sign in to comment.