Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system activity tracking and event store #636

Merged
merged 18 commits into from
Jan 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add GetEvents test to account manager
braginini committed Dec 28, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6c0e16070ec9d151986d32a95fe83a60700da2a9
36 changes: 30 additions & 6 deletions management/server/activity/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package activity

import "time"
import (
"sync"
"time"
)

const (
// PeerAddedByUser indicates that a user added a new peer to the system
@@ -165,23 +168,44 @@ type Store interface {
Close() error
}

// NoopEventStore implements the Store interface without doing any operations
// NoopEventStore implements the Store interface storing data in-memory
type NoopEventStore struct {
mu sync.Mutex
nextID uint64
events []*Event
}

// Save sets the Event.ID to 1
func (store *NoopEventStore) Save(event *Event) (*Event, error) {
event.ID = 1
store.mu.Lock()
defer store.mu.Unlock()
if store.events == nil {
store.events = make([]*Event, 0)
}
event.ID = store.nextID
store.nextID++
store.events = append(store.events, event)
return event, nil
}

// Get returns an empty list of events
// Get returns a list of ALL events that belong to the given accountID without taking offset, limit and order into consideration
func (store *NoopEventStore) Get(accountID string, offset, limit int, descending bool) ([]*Event, error) {
return []*Event{}, nil
store.mu.Lock()
defer store.mu.Unlock()
events := make([]*Event, 0)
for _, event := range store.events {
if event.AccountID == accountID {
events = append(events, event)
}
}
return events, nil
}

// Close doesn't close anything
// Close cleans up the event list
func (store *NoopEventStore) Close() error {
store.mu.Lock()
defer store.mu.Unlock()
store.events = make([]*Event, 0)
return nil
}

63 changes: 63 additions & 0 deletions management/server/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package server

import (
"github.com/netbirdio/netbird/management/server/activity"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func generateAndStoreEvents(t *testing.T, manager *DefaultAccountManager, typ activity.Activity, initiatorID, targetID,
accountID string, count int) {
for i := 0; i < count; i++ {
_, err := manager.eventStore.Save(&activity.Event{
Timestamp: time.Now(),
Activity: typ,
InitiatorID: initiatorID,
TargetID: targetID,
AccountID: accountID,
})
if err != nil {
t.Fatal(err)
}
}
}

func TestDefaultAccountManager_GetEvents(t *testing.T) {
manager, err := createManager(t)
if err != nil {
return
}

accountID := "accountID"

t.Run("get empty events list", func(t *testing.T) {
events, err := manager.GetEvents(accountID, userID)
if err != nil {
return
}
assert.Len(t, events, 0)
_ = manager.eventStore.Close() //nolint
})

t.Run("get events", func(t *testing.T) {
generateAndStoreEvents(t, manager, activity.PeerAddedByUser, userID, "peer", accountID, 10)
events, err := manager.GetEvents(accountID, userID)
if err != nil {
return
}

assert.Len(t, events, 10)
_ = manager.eventStore.Close() //nolint
})

t.Run("get events without duplicates", func(t *testing.T) {
generateAndStoreEvents(t, manager, activity.UserJoined, userID, "", accountID, 10)
events, err := manager.GetEvents(accountID, userID)
if err != nil {
return
}
assert.Len(t, events, 1)
_ = manager.eventStore.Close() //nolint
})
}