db

package
v1.1.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalAuditMetadata added in v1.1.9

func MarshalAuditMetadata(metadata AuditMetadata) string

Helper function to create metadata JSON

Types

type AuditActionType added in v1.1.9

type AuditActionType string

AuditActionType defines the types of actions that can be logged

const (
	// Spammer actions
	AuditActionSpammerCreate  AuditActionType = "spammer_create"
	AuditActionSpammerUpdate  AuditActionType = "spammer_update"
	AuditActionSpammerDelete  AuditActionType = "spammer_delete"
	AuditActionSpammerStart   AuditActionType = "spammer_start"
	AuditActionSpammerPause   AuditActionType = "spammer_pause"
	AuditActionSpammerReclaim AuditActionType = "spammer_reclaim"

	// Client actions
	AuditActionClientUpdate      AuditActionType = "client_update"
	AuditActionClientGroupUpdate AuditActionType = "client_group_update"
	AuditActionClientNameUpdate  AuditActionType = "client_name_update"
	AuditActionClientTypeUpdate  AuditActionType = "client_type_update"
	AuditActionClientToggle      AuditActionType = "client_toggle"

	// Import/Export actions
	AuditActionSpammersImport AuditActionType = "spammers_import"
	AuditActionSpammersExport AuditActionType = "spammers_export"

	// Root wallet actions
	AuditActionRootWalletSend AuditActionType = "root_wallet_send"
)

type AuditEntityType added in v1.1.9

type AuditEntityType string

AuditEntityType defines the types of entities that can be logged

const (
	AuditEntitySpammer    AuditEntityType = "spammer"
	AuditEntityClient     AuditEntityType = "client"
	AuditEntitySystem     AuditEntityType = "system"
	AuditEntityRootWallet AuditEntityType = "root_wallet"
)

type AuditLog added in v1.1.9

type AuditLog struct {
	ID         int64  `db:"id"`
	UserEmail  string `db:"user_email"`
	ActionType string `db:"action_type"`
	EntityType string `db:"entity_type"`
	EntityID   string `db:"entity_id"`
	EntityName string `db:"entity_name"`
	Diff       string `db:"diff"`
	Metadata   string `db:"metadata"`
	Timestamp  int64  `db:"timestamp"`
}

AuditLog represents an audit log entry in the database

type AuditLogFilters added in v1.1.9

type AuditLogFilters struct {
	UserEmail  string
	ActionType string
	EntityType string
	EntityID   string
	StartTime  int64
	EndTime    int64
	Limit      int
	Offset     int
}

AuditLogFilters represents filters for querying audit logs

type AuditMetadata added in v1.1.9

type AuditMetadata map[string]interface{}

AuditMetadata represents additional metadata for an audit log entry

func UnmarshalAuditMetadata added in v1.1.9

func UnmarshalAuditMetadata(metadataStr string) AuditMetadata

Helper function to parse metadata JSON

type ClientConfig added in v1.1.9

type ClientConfig struct {
	RpcUrl     string `db:"rpc_url"`     // RPC URL used as primary key identifier
	Name       string `db:"name"`        // Human-readable name for the client
	Tags       string `db:"tags"`        // Comma-separated tags for client categorization
	ClientType string `db:"client_type"` // Type of client: 'client' or 'builder'
	Enabled    bool   `db:"enabled"`     // Whether the client is enabled for use
	CreatedAt  int64  `db:"created_at"`  // Unix timestamp when the config was created
	UpdatedAt  int64  `db:"updated_at"`  // Unix timestamp when the config was last modified
}

ClientConfig represents a database entity for storing client configuration. Maps to the "client_configs" table with RPC URL as primary key and persistent settings for name, tags, and enabled state.

func (*ClientConfig) GetTagsAsSlice added in v1.1.9

func (c *ClientConfig) GetTagsAsSlice() []string

GetTagsAsSlice parses the comma-separated tags string into a slice of strings. Returns an empty slice if tags is empty or contains only whitespace.

func (*ClientConfig) SetTagsFromSlice added in v1.1.9

func (c *ClientConfig) SetTagsFromSlice(tags []string)

SetTagsFromSlice converts a slice of tags into a comma-separated string. Trims whitespace from each tag and filters out empty strings.

type Database

type Database struct {
	ReaderDb *sqlx.DB // Database connection for read operations
	// contains filtered or unexported fields
}

Database manages SQLite database connections with WAL mode and connection pooling. It provides both reader and writer connections with mutex protection for write operations and embedded schema migration support using goose.

func NewDatabase

func NewDatabase(config *SqliteDatabaseConfig, logger logrus.FieldLogger) *Database

NewDatabase creates a new Database instance with the specified configuration and logger. The database connections are not initialized until Init() is called.

func (*Database) ApplyEmbeddedDbSchema

func (d *Database) ApplyEmbeddedDbSchema(version int64) error

ApplyEmbeddedDbSchema applies database schema migrations using embedded SQL files. Supports different migration strategies: -2 (all), -1 (one up), or specific version. Uses goose migration library with allowMissing option for flexible schema management.

func (*Database) Close

func (d *Database) Close() error

Close closes the database writer connection. Should be called during application shutdown to ensure proper cleanup.

func (*Database) CountAuditLogs added in v1.1.9

func (d *Database) CountAuditLogs(filters AuditLogFilters) (int64, error)

CountAuditLogs returns the total count of audit logs matching the filters

func (*Database) DeleteClientConfig added in v1.1.9

func (d *Database) DeleteClientConfig(tx *sqlx.Tx, rpcUrl string) error

DeleteClientConfig removes a client config record from the database within a transaction. Permanently deletes the client config for the specified RPC URL. Returns an error if the deletion fails or the config doesn't exist.

func (*Database) DeleteSpammer

func (d *Database) DeleteSpammer(tx *sqlx.Tx, id int64) error

DeleteSpammer removes a spammer record from the database within a transaction. Permanently deletes the spammer and all associated data. Returns an error if the deletion fails or the spammer doesn't exist.

func (*Database) GetAuditLogByID added in v1.1.9

func (d *Database) GetAuditLogByID(id int64) (*AuditLog, error)

GetAuditLogByID retrieves a single audit log entry by ID

func (*Database) GetAuditLogs added in v1.1.9

func (d *Database) GetAuditLogs(filters AuditLogFilters) ([]*AuditLog, error)

GetAuditLogs retrieves audit logs with optional filtering

func (*Database) GetClientConfig added in v1.1.9

func (d *Database) GetClientConfig(rpcUrl string) (*ClientConfig, error)

GetClientConfig retrieves a single client config by RPC URL from the database. Returns the client config entity or an error if not found or database access fails.

func (*Database) GetClientConfigs added in v1.1.9

func (d *Database) GetClientConfigs() ([]*ClientConfig, error)

GetClientConfigs retrieves all client configs from the database ordered by creation time. Returns a slice of client config entities or an error if database access fails.

func (*Database) GetSpammer

func (d *Database) GetSpammer(id int64) (*Spammer, error)

GetSpammer retrieves a single spammer by ID from the database. Returns the spammer entity or an error if not found or database access fails.

func (*Database) GetSpammers

func (d *Database) GetSpammers() ([]*Spammer, error)

GetSpammers retrieves all spammers from the database ordered by creation time (newest first). Returns a slice of spammer entities or an error if database access fails.

func (*Database) GetSpamoorState

func (d *Database) GetSpamoorState(key string, returnValue interface{}) (interface{}, error)

GetSpamoorState retrieves and deserializes a state value by key. The returnValue parameter should be a pointer to the target type for unmarshaling. Returns the deserialized value or an error if the key doesn't exist or JSON parsing fails.

func (*Database) Init

func (d *Database) Init() error

Init initializes the database connections with WAL mode and connection pooling. Sets default connection limits (50 max open, 10 max idle) if not specified. Enables WAL mode for better concurrent access and configures connection timeouts.

func (*Database) InsertAuditLog added in v1.1.9

func (d *Database) InsertAuditLog(tx *sqlx.Tx, log *AuditLog) error

InsertAuditLog creates a new audit log entry in the database within a transaction

func (*Database) InsertClientConfig added in v1.1.9

func (d *Database) InsertClientConfig(tx *sqlx.Tx, config *ClientConfig) error

InsertClientConfig creates a new client config record in the database within a transaction. Sets creation and update timestamps automatically. Returns an error if the insertion fails or transaction is invalid.

func (*Database) InsertSpammer

func (d *Database) InsertSpammer(tx *sqlx.Tx, spammer *Spammer) error

InsertSpammer creates a new spammer record in the database within a transaction. Updates the spammer's ID field with the generated database ID after insertion. Returns an error if the insertion fails or transaction is invalid.

func (*Database) RunDBTransaction

func (d *Database) RunDBTransaction(handler func(tx *sqlx.Tx) error) error

RunDBTransaction executes a function within a database transaction with automatic rollback. The transaction is protected by a mutex to ensure sequential write operations. Automatically rolls back on error and commits on success.

func (*Database) SetSpamoorState

func (d *Database) SetSpamoorState(tx *sqlx.Tx, key string, value interface{}) error

SetSpamoorState stores a state value by key with JSON serialization. If tx is nil, creates and manages its own transaction automatically. Uses INSERT OR REPLACE to handle both new entries and updates atomically. Returns an error if JSON marshaling or database operation fails.

func (*Database) UpdateClientConfig added in v1.1.9

func (d *Database) UpdateClientConfig(tx *sqlx.Tx, config *ClientConfig) error

UpdateClientConfig modifies an existing client config record in the database within a transaction. Updates name, tags, enabled state, and update timestamp. RPC URL and creation time remain unchanged. Returns an error if the update fails or the config doesn't exist.

func (*Database) UpdateSpammer

func (d *Database) UpdateSpammer(tx *sqlx.Tx, spammer *Spammer) error

UpdateSpammer modifies an existing spammer record in the database within a transaction. Updates all mutable fields: name, description, config, status, and state. The ID and creation timestamp remain unchanged.

func (*Database) UpsertClientConfig added in v1.1.9

func (d *Database) UpsertClientConfig(tx *sqlx.Tx, config *ClientConfig) error

UpsertClientConfig creates or updates a client config record in the database within a transaction. If the RPC URL exists, updates the record; otherwise creates a new one. Returns an error if the operation fails.

type Spammer

type Spammer struct {
	ID          int64  `db:"id"`          // Unique identifier for the spammer instance
	Scenario    string `db:"scenario"`    // Name of the scenario to execute
	Name        string `db:"name"`        // Human-readable name for the spammer
	Description string `db:"description"` // Detailed description of what this spammer does
	Config      string `db:"config"`      // YAML configuration for scenario and wallet settings
	Status      int    `db:"status"`      // Current execution status (see SpammerStatus constants)
	CreatedAt   int64  `db:"created_at"`  // Unix timestamp when the spammer was created
	State       string `db:"state"`       // Persistent state data for scenario execution
}

Spammer represents a database entity for storing spammer configuration and state. Maps to the "spammers" table with fields for scenario definition, execution status, and persistent state management. The status field tracks execution state using SpammerStatus constants.

type SpamoorState

type SpamoorState struct {
	Key   string `db:"key"`   // Unique identifier for the state entry
	Value string `db:"value"` // JSON-serialized value for the state data
}

SpamoorState represents a key-value pair for storing application-wide configuration state. Maps to the "spamoor_state" table for persisting settings like scenario counters, first launch flags, and other global application state that survives restarts.

type SqliteDatabaseConfig

type SqliteDatabaseConfig struct {
	File         string `yaml:"file"`         // Database file path
	MaxOpenConns int    `yaml:"maxOpenConns"` // Maximum number of open connections to the database
	MaxIdleConns int    `yaml:"maxIdleConns"` // Maximum number of idle connections in the pool
}

SqliteDatabaseConfig defines the configuration for SQLite database connections. It specifies the database file path and connection pool limits for managing concurrent database access efficiently.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL