Finalize events API

This commit is contained in:
braginini
2022-12-12 09:10:55 +01:00
parent 4440ad1271
commit c50d07b83f
9 changed files with 217 additions and 197 deletions

View File

@@ -3,43 +3,69 @@ package activity
import "time"
const (
// DeviceEvent describes an event that happened of a device (e.g, connected/disconnected)
DeviceEvent Type = "device"
// ManagementEvent describes an event that happened on a Management service (e.g., user added)
ManagementEvent Type = "management"
// PeerAddedByUser indicates that a user added a new peer to the system
PeerAddedByUser Activity = iota
// PeerAddedWithSetupKey indicates that a new peer joined the system using a setup key
PeerAddedWithSetupKey
// UserJoined indicates that a new user joined the account
UserJoined
// UserInvited indicates that a new user was invited to join the account
UserInvited
// AccountCreated indicates that a new account has been created
AccountCreated
)
const (
AddPeerByUserOperation Operation = iota
AddPeerWithKeyOperation
UserJoinedOperation
// PeerAddedByUserMessage is a human-readable text message of the PeerAddedByUser activity
PeerAddedByUserMessage string = "New peer added by a user"
// PeerAddedWithSetupKeyMessage is a human-readable text message of the PeerAddedWithSetupKey activity
PeerAddedWithSetupKeyMessage = "New peer added with a setup key"
//UserJoinedMessage is a human-readable text message of the UserJoined activity
UserJoinedMessage string = "New user joined"
//UserInvitedMessage is a human-readable text message of the UserInvited activity
UserInvitedMessage string = "New user invited"
//AccountCreatedMessage is a human-readable text message of the AccountCreated activity
AccountCreatedMessage string = "Account created"
)
const (
AddPeerByUserOperationMessage string = "Add new peer"
AddPeerWithKeyOperationMessage string = AddPeerByUserOperationMessage
UserJoinedOperationMessage string = "New user joined"
)
// Activity that triggered an Event
type Activity int
// MessageForOperation returns a string message for an Operation
func MessageForOperation(op Operation) string {
switch op {
case AddPeerByUserOperation:
return AddPeerByUserOperationMessage
case AddPeerWithKeyOperation:
return AddPeerWithKeyOperationMessage
case UserJoinedOperation:
return UserJoinedOperationMessage
// Message returns a string representation of an activity
func (a Activity) Message() string {
switch a {
case PeerAddedByUser:
return PeerAddedByUserMessage
case PeerAddedWithSetupKey:
return PeerAddedWithSetupKeyMessage
case UserJoined:
return UserJoinedMessage
case UserInvited:
return UserInvitedMessage
case AccountCreated:
return AccountCreatedMessage
default:
return "UNKNOWN_OPERATION"
return "UNKNOWN_ACTIVITY"
}
}
// Type of the Event
type Type string
// Operation is an action that triggered an Event
type Operation int
// StringCode returns a string code of the activity
func (a Activity) StringCode() string {
switch a {
case PeerAddedByUser:
return "user.peer.add"
case PeerAddedWithSetupKey:
return "setupkey.peer.add"
case UserJoined:
return "user.join"
case UserInvited:
return "user.invite"
case AccountCreated:
return "account.create"
default:
return "UNKNOWN_ACTIVITY"
}
}
// Store provides an interface to store or stream events.
type Store interface {
@@ -55,31 +81,26 @@ type Store interface {
type Event struct {
// Timestamp of the event
Timestamp time.Time
// Operation that was performed during the event
Operation string
// OperationCode that was performed during the event
OperationCode Operation
// Activity that was performed during the event
Activity Activity
// ID of the event (can be empty, meaning that it wasn't yet generated)
ID uint64
// Type of the event
Type Type
// ModifierID is the ID of an object that modifies a Target
ModifierID string
// TargetID is the ID of an object that a Modifier modifies
// InitiatorID is the ID of an object that initiated the event (e.g., a user)
InitiatorID string
// TargetID is the ID of an object that was effected by the event (e.g., a peer)
TargetID string
// AccountID where event happened
// AccountID is the ID of an account where the event happened
AccountID string
}
// Copy the event
func (e *Event) Copy() *Event {
return &Event{
Timestamp: e.Timestamp,
Operation: e.Operation,
ID: e.ID,
Type: e.Type,
ModifierID: e.ModifierID,
TargetID: e.TargetID,
AccountID: e.AccountID,
Timestamp: e.Timestamp,
Activity: e.Activity,
ID: e.ID,
InitiatorID: e.InitiatorID,
TargetID: e.TargetID,
AccountID: e.AccountID,
}
}

View File

@@ -11,12 +11,12 @@ import (
const (
SQLiteEventSinkDB = "events.db"
createTableQuery = "CREATE TABLE IF NOT EXISTS events " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT NOT NULL, " +
"operation INTEGER, " +
"type TEXT, " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"activity INTEGER, " +
"timestamp DATETIME, " +
"modifier TEXT," +
" target TEXT);"
"initiator_id TEXT," +
"account_id TEXT," +
" target_id TEXT);"
)
// SQLiteStore is the implementation of the activity.Store interface backed by SQLite
@@ -44,26 +44,23 @@ func processResult(result *sql.Rows) ([]*Event, error) {
events := make([]*Event, 0)
for result.Next() {
var id int64
var operation Operation
var operation Activity
var timestamp time.Time
var modifier string
var initiator string
var target string
var account string
var typ Type
err := result.Scan(&id, &operation, &timestamp, &modifier, &target, &account, &typ)
err := result.Scan(&id, &operation, &timestamp, &initiator, &target, &account)
if err != nil {
return nil, err
}
events = append(events, &Event{
Timestamp: timestamp,
OperationCode: operation,
Operation: MessageForOperation(operation),
ID: uint64(id),
Type: typ,
ModifierID: modifier,
TargetID: target,
AccountID: account,
Timestamp: timestamp,
Activity: operation,
ID: uint64(id),
InitiatorID: initiator,
TargetID: target,
AccountID: account,
})
}
@@ -76,8 +73,8 @@ func (store *SQLiteStore) Get(accountID string, offset, limit int, descending bo
if !descending {
order = "ASC"
}
stmt, err := store.db.Prepare(fmt.Sprintf("SELECT id, operation, timestamp, modifier, target, account, type"+
" FROM events WHERE account = ? ORDER BY timestamp %s LIMIT ? OFFSET ?;", order))
stmt, err := store.db.Prepare(fmt.Sprintf("SELECT id, activity, timestamp, initiator_id, target_id, account_id"+
" FROM events WHERE account_id = ? ORDER BY timestamp %s LIMIT ? OFFSET ?;", order))
if err != nil {
return nil, err
}
@@ -94,12 +91,12 @@ func (store *SQLiteStore) Get(accountID string, offset, limit int, descending bo
// Save an event in the SQLite events table
func (store *SQLiteStore) Save(event *Event) (*Event, error) {
stmt, err := store.db.Prepare("INSERT INTO events(operation, timestamp, modifier, target, account, type) VALUES(?, ?, ?, ?, ?, ?)")
stmt, err := store.db.Prepare("INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id) VALUES(?, ?, ?, ?, ?)")
if err != nil {
return nil, err
}
result, err := stmt.Exec(event.OperationCode, event.Timestamp, event.ModifierID, event.TargetID, event.AccountID, event.Type)
result, err := stmt.Exec(event.Activity, event.Timestamp, event.InitiatorID, event.TargetID, event.AccountID)
if err != nil {
return nil, err
}

View File

@@ -19,12 +19,11 @@ func TestNewSQLiteStore(t *testing.T) {
for i := 0; i < 10; i++ {
_, err = store.Save(&Event{
Timestamp: time.Now(),
OperationCode: AddPeerByUserOperation,
Type: ManagementEvent,
ModifierID: "user_" + fmt.Sprint(i),
TargetID: "peer_" + fmt.Sprint(i),
AccountID: accountID,
Timestamp: time.Now(),
Activity: PeerAddedByUser,
InitiatorID: "user_" + fmt.Sprint(i),
TargetID: "peer_" + fmt.Sprint(i),
AccountID: accountID,
})
if err != nil {
t.Fatal(err)