storage

package
v0.0.0-...-c2add7f Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package storage implements inetrface to diffent ways of storing metrics data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBConnPool

type DBConnPool interface {
	Acquire(ctx context.Context) (*pgxpool.Conn, error)
	Ping(ctx context.Context) error
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Close()
}

type DBConnPoolMock

type DBConnPoolMock struct {
	mock.Mock
}

func NewDBConnPoolMock

func NewDBConnPoolMock() *DBConnPoolMock

func (*DBConnPoolMock) Acquire

func (m *DBConnPoolMock) Acquire(ctx context.Context) (*pgxpool.Conn, error)

func (*DBConnPoolMock) Close

func (m *DBConnPoolMock) Close()

func (*DBConnPoolMock) Ping

func (m *DBConnPoolMock) Ping(ctx context.Context) error

func (*DBConnPoolMock) Query

func (m *DBConnPoolMock) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

func (*DBConnPoolMock) QueryRow

func (m *DBConnPoolMock) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

func (*DBConnPoolMock) SendBatch

func (m *DBConnPoolMock) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults

type DatabaseStorage

type DatabaseStorage struct {
	// contains filtered or unexported fields
}

DatabaseStorage implements database metrics storage.

func NewDatabaseStorage

func NewDatabaseStorage(pool DBConnPool) DatabaseStorage

NewDatabaseStorage creates new instance of DatabaseStorage.

func (DatabaseStorage) Close

func (d DatabaseStorage) Close(_ context.Context) error

Close closes all open connection to the database.

func (DatabaseStorage) Get

func (d DatabaseStorage) Get(ctx context.Context, key string) (Record, error)

Get returns stored metrics record.

func (DatabaseStorage) GetAll

func (d DatabaseStorage) GetAll(ctx context.Context) ([]Record, error)

GetAll returns all stored metrics.

func (DatabaseStorage) Ping

func (d DatabaseStorage) Ping(ctx context.Context) error

Ping verifies that connection to the database can be established.

func (DatabaseStorage) Push

func (d DatabaseStorage) Push(ctx context.Context, key string, record Record) error

Push records metric data.

func (DatabaseStorage) PushBatch

func (d DatabaseStorage) PushBatch(ctx context.Context, data map[string]Record) error

PushBatch records list of metrics data in single request to the database.

type FileBackedStorage

type FileBackedStorage struct {
	*MemStorage
	sync.Mutex
	// contains filtered or unexported fields
}

FileBackedStorage implements in-memory metrics storage with ability to dump/restore metrics data to/from disk.

func NewFileBackedStorage

func NewFileBackedStorage(storePath string, syncMode bool) *FileBackedStorage

NewFileBackedStorage creates new instance of FileBackedStorage.

func (*FileBackedStorage) Close

func (f *FileBackedStorage) Close(ctx context.Context) error

Close dumps all stored data to disk. The storage can be restored from this dump later.

func (*FileBackedStorage) Dump

func (f *FileBackedStorage) Dump(ctx context.Context) (err error)

Dump writes all stored data to disk. The storage can be restored from this dump later.

func (*FileBackedStorage) Push

func (f *FileBackedStorage) Push(ctx context.Context, key string, record Record) error

Push records metric data.

func (*FileBackedStorage) PushBatch

func (f *FileBackedStorage) PushBatch(ctx context.Context, data map[string]Record) error

PushBatch records list of metrics data.

func (*FileBackedStorage) Restore

func (f *FileBackedStorage) Restore() (err error)

Restore reads previously stored data from disk and populates the storage.

type MemStorage

type MemStorage struct {
	Data map[string]Record `json:"records"`
	sync.RWMutex
}

MemStorage implements in-memory metrics storage.

func NewMemStorage

func NewMemStorage() *MemStorage

NewMemStorage creates new instance of MemStorage.

func (*MemStorage) Close

func (m *MemStorage) Close(_ context.Context) error

Close has no effect on in-memory storage.

func (*MemStorage) Get

func (m *MemStorage) Get(_ context.Context, key string) (Record, error)

Get returns stored metrics record.

func (*MemStorage) GetAll

func (m *MemStorage) GetAll(_ context.Context) ([]Record, error)

GetAll returns all stored metrics.

func (*MemStorage) Push

func (m *MemStorage) Push(_ context.Context, key string, record Record) error

Push records metric data.

func (*MemStorage) PushBatch

func (m *MemStorage) PushBatch(_ context.Context, data map[string]Record) error

PushBatch records list of metrics data.

func (*MemStorage) Snapshot

func (m *MemStorage) Snapshot() *MemStorage

Snapshot creates independent copy of in-memory storage.

type Mock

type Mock struct {
	mock.Mock
}

func (*Mock) Close

func (m *Mock) Close(ctx context.Context) error

func (*Mock) Get

func (m *Mock) Get(ctx context.Context, key string) (Record, error)

func (*Mock) GetAll

func (m *Mock) GetAll(ctx context.Context) ([]Record, error)

func (*Mock) Push

func (m *Mock) Push(ctx context.Context, key string, record Record) error

func (*Mock) PushBatch

func (m *Mock) PushBatch(ctx context.Context, data map[string]Record) error

type Record

type Record struct {
	Name  string
	Value metrics.Metric
}

A Record is internal represenattion of metric data stored in any kind of storage.

func (Record) MarshalJSON

func (r Record) MarshalJSON() ([]byte, error)

func (*Record) UnmarshalJSON

func (r *Record) UnmarshalJSON(src []byte) error

type Storage

type Storage interface {
	Push(ctx context.Context, key string, record Record) error
	PushBatch(ctx context.Context, data map[string]Record) error
	Get(ctx context.Context, key string) (Record, error)
	GetAll(ctx context.Context) ([]Record, error)
	Close(ctx context.Context) error
}

func NewDataStore

func NewDataStore(pool *pgxpool.Pool, filePath string, storeInterval time.Duration) Storage

NewDataStore create new DataStore object encapsulating particular storage type. New storage is picked according to the following priority: - if DB connection was initialized, use database storage; - if filePath is set, use file backed storage; - otherwise store data in memory.

Jump to

Keyboard shortcuts

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