reading

package
v0.1.0-alpha7 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package reading provides persistence logic for sensor readings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ForEachFunc

type ForEachFunc func(ctx context.Context, reading Reading) error

The ForEachFunc type is a function invoked when calling PostgresRepository.ForEachOnDate.

type PostgresRepository

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

The PostgresRepository type is used to persist Reading data into a PostgreSQL instance.

func NewPostgresRepository

func NewPostgresRepository(db *sql.DB) *PostgresRepository

NewPostgresRepository returns a new instance of the PostgresRepository type that will perform queries against the provided sql.DB instance.

func (*PostgresRepository) ForEachOnDate

func (pr *PostgresRepository) ForEachOnDate(ctx context.Context, date time.Time, fn ForEachFunc) error

ForEachOnDate iterates through all readings stored in the database on the date component of the given time. For each record, the ForEachFunc is invoked. Iteration will stop when there are no more records, the context is cancelled or the ForEachFunc returns an error. Readings are processed in batches of 100 at the time.

func (*PostgresRepository) Save

func (pr *PostgresRepository) Save(ctx context.Context, reading Reading) error

Save the reading to the database. If a reading already exists for the sensor at the given timestamp, do nothing.

type Reading

type Reading struct {
	Sensor    string    `json:"sensor"`
	Value     float64   `json:"value"`
	Timestamp time.Time `json:"timestamp"`
}

The Reading type describes a single sensor reading as stored in the database.

func (Reading) String

func (r Reading) String() string

String returns a string representation of the reading.

func (Reading) Valid

func (r Reading) Valid() bool

Valid returns true if the Reading is deemed to be in a valid state. This means a sensor name, a zero or positive value and a non-zero timestamp.

Jump to

Keyboard shortcuts

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