Documentation ¶
Overview ¶
Package reading provides the full application stack for sensor readings. Including transport, persistence and event handling.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventHandler ¶
type EventHandler struct {
// contains filtered or unexported fields
}
The EventHandler type is used to handle inbound readings and persist them to the Repository implementation.
func NewEventHandler ¶
func NewEventHandler(readings Repository, logger *log.Logger) *EventHandler
NewEventHandler returns a new instance of the EventHandler type. Inbound events will be stored using the provided Repository implementation.
func (*EventHandler) HandleEvent ¶
func (h *EventHandler) HandleEvent(ctx context.Context, message json.RawMessage) error
HandleEvent handles an inbound JSON payload. It expects the payload to be unmarshallable into a storage.Reading type. Once decoded, the reading is persisted via the Repository implementation.
type EventWriter ¶
type EventWriter interface {
Write(ctx context.Context, message json.RawMessage) error
}
The EventWriter interface describes types that can publish messages onto an event stream.
type ForEachFunc ¶
The ForEachFunc type is a function invoked when calling PostgresRepository.ForEachOnDate.
type HTTP ¶
type HTTP struct {
// contains filtered or unexported fields
}
The HTTP type is responsible for handling HTTP requests and publishing events onto an event sink.
func NewHTTP ¶
func NewHTTP(events EventWriter, logger *log.Logger) *HTTP
NewHTTP returns a new instance of the HTTP type that will publish Reading events onto the provided EventWriter implementation to the desired subject. The HTTP.Register method should be used to register the handling methods onto an HTTP router.
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.