query max amount of datapoints

This commit is contained in:
Joel Schmid 2021-05-07 22:25:44 +02:00
parent 2466111488
commit b8e8ce035c
2 changed files with 27 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package storage
import (
"errors"
"math"
"math/rand"
"time"
@ -128,6 +129,16 @@ func GetOnlyQueriedFields(dataPoints []*WeatherData, query *WeatherQuery) []*Wea
for _, data := range dataPoints {
data.OnlyQueriedValues(query)
}
if query.MaxDataPoints >= 0 && len(dataPoints) > query.MaxDataPoints {
var result = make([]*WeatherData, query.MaxDataPoints)
division := float64(len(dataPoints)) / float64(query.MaxDataPoints)
for i := 0; i < query.MaxDataPoints; i++ {
result[i] = dataPoints[int(math.Round(float64(i)*division))]
}
return result
}
return dataPoints
}

View file

@ -10,15 +10,17 @@ import (
)
type WeatherQuery struct {
Start time.Time
End time.Time
SensorId uuid.UUID
Values map[SensorValueType]bool
Start time.Time
End time.Time
SensorId uuid.UUID
MaxDataPoints int
Values map[SensorValueType]bool
}
//NewWeatherQuery creates a new empty WeatherQuery
func NewWeatherQuery() *WeatherQuery {
query := new(WeatherQuery)
query.MaxDataPoints = -1
query.Values = make(map[SensorValueType]bool)
return query
}
@ -38,6 +40,7 @@ func ParseFromUrlQuery(query url.Values) (*WeatherQuery, error) {
start := query.Get("start")
end := query.Get("end")
max := query.Get("maxDataPoints")
if len(start) != 0 {
if tval, err := time.Parse(time.RFC3339, start); err == nil {
@ -57,6 +60,15 @@ func ParseFromUrlQuery(query url.Values) (*WeatherQuery, error) {
}
}
if len(max) != 0 {
if tval, err := strconv.Atoi(max); err == nil {
result.MaxDataPoints = tval
} else if err != nil {
fmt.Println(err)
return nil, err
}
}
for k, v := range query {
if k == "start" || k == "end" {
continue