measurement

package
v0.0.0-...-030e278 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrZeroTimestamp is returned from NewMeasurement if the StorableMeasurement's timestamp is the zero timestamp.
	ErrZeroTimestamp = fmt.Errorf("measurement: timestamp cannot be nil")
)

Functions

func Max

func Max(measurements []StorableMeasurement) map[string]float32

func Mean

func Mean(measurements []StorableMeasurement) map[string]float32

func Min

func Min(measurements []StorableMeasurement) map[string]float32

func NewMeasurement

func NewMeasurement(sm *StorableMeasurement) (mpb.Measurement, error)

NewMeasurement converts a StorableMeasurement into the generated Measurement type, converting time.Time values into the protobuf-specific timestamp type. IMPORTANT: Keep up to date with the generated Measurement type (from measurement.proto).

func StdDev

func StdDev(measurements []StorableMeasurement) map[string]float32

Types

type Metric

type Metric struct {
	Name  string
	Abbrv string
	Unit  string
}

func GetMetric

func GetMetric(nameOrKey string) (Metric, bool)

type StorableMeasurement

type StorableMeasurement struct {
	DeviceID        string    `json:"-" datastore:"device_id"`
	Timestamp       time.Time `json:"-" datastore:"timestamp"`
	UploadTimestamp time.Time `json:"-" datastore:"upload_timestamp,omitempty"`

	// These metrics are the raw values reported by sensors. They must match the
	// metrics defined in the generated Measurement type (from measurement.proto).
	Temp *float32 `json:"temp,omitempty" datastore:"temp,omitempty" metric:"temp" unit:"°C"`
	PM25 *float32 `json:"pm25,omitempty" datastore:"pm25,omitempty" metric:"PM2.5" unit:"μg/m³"`
	PM10 *float32 `json:"pm10,omitempty" datastore:"pm10,omitempty" metric:"PM10" unit:"μg/m³"`
	RH   *float32 `json:"rh,omitempty" datastore:"rh,omitempty" metric:"RH" unit:"%"`

	// These metrics are derived from the raw values. They're not stored in the database
	// (the `datastore` tag is set to "-") but they are passed to the frontend in JSON form.
	// These values are populated by the FillDerivedMetrics method.
	AQI *float32 `json:"aqi,omitempty" datastore:"-" metric:"AQI" unit:""`
}

StorableMeasurement is equivalent to the generated Measurement type but it contains no protobuf-specific types. It therefore can be marshaled to JSON and written to Datastore. StorableMeasurement is marshaled to JSON in order to pass data to the frontend. Timestamp is handled specially in MarshalJSON. IMPORTANT: Keep up to date with the generated Measurement type (from measurement.proto).

func NewStorableMeasurement

func NewStorableMeasurement(m *mpb.Measurement) (StorableMeasurement, error)

NewStorableMeasurement converts the generated Measurement type to a StorableMeasurement, which contains no protobuf-specific types, and therefore can be marshaled to JSON and written to Datastore. IMPORTANT: Keep up to date with the generated Measurement type (from measurement.proto).

func (*StorableMeasurement) DBKey

func (sm *StorableMeasurement) DBKey() string

DBKey returns a string key suitable for Datastore. It promotes Device ID and timestamp into the key.

func (*StorableMeasurement) FillDerivedMetrics

func (sm *StorableMeasurement) FillDerivedMetrics()

func (StorableMeasurement) MarshalJSON

func (sm StorableMeasurement) MarshalJSON() ([]byte, error)

func (StorableMeasurement) String

func (sm StorableMeasurement) String() string

func (StorableMeasurement) StringValueMap

func (sm StorableMeasurement) StringValueMap() map[string]string

StringValueMap returns a map from metric name (as defined in struct tags) to string-formatted values including the unit. Nil fields are not included.

func (StorableMeasurement) ValueMap

func (sm StorableMeasurement) ValueMap() map[string]float32

ValueMap returns a map from metric name (as defined in struct tags) to values. Nil fields are not included.

Jump to

Keyboard shortcuts

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