query max amount of datapoints
This commit is contained in:
parent
2466111488
commit
b8e8ce035c
2 changed files with 27 additions and 4 deletions
|
@ -2,6 +2,7 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -128,6 +129,16 @@ func GetOnlyQueriedFields(dataPoints []*WeatherData, query *WeatherQuery) []*Wea
|
||||||
for _, data := range dataPoints {
|
for _, data := range dataPoints {
|
||||||
data.OnlyQueriedValues(query)
|
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
|
return dataPoints
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,14 @@ type WeatherQuery struct {
|
||||||
Start time.Time
|
Start time.Time
|
||||||
End time.Time
|
End time.Time
|
||||||
SensorId uuid.UUID
|
SensorId uuid.UUID
|
||||||
|
MaxDataPoints int
|
||||||
Values map[SensorValueType]bool
|
Values map[SensorValueType]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewWeatherQuery creates a new empty WeatherQuery
|
//NewWeatherQuery creates a new empty WeatherQuery
|
||||||
func NewWeatherQuery() *WeatherQuery {
|
func NewWeatherQuery() *WeatherQuery {
|
||||||
query := new(WeatherQuery)
|
query := new(WeatherQuery)
|
||||||
|
query.MaxDataPoints = -1
|
||||||
query.Values = make(map[SensorValueType]bool)
|
query.Values = make(map[SensorValueType]bool)
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
@ -38,6 +40,7 @@ func ParseFromUrlQuery(query url.Values) (*WeatherQuery, error) {
|
||||||
|
|
||||||
start := query.Get("start")
|
start := query.Get("start")
|
||||||
end := query.Get("end")
|
end := query.Get("end")
|
||||||
|
max := query.Get("maxDataPoints")
|
||||||
|
|
||||||
if len(start) != 0 {
|
if len(start) != 0 {
|
||||||
if tval, err := time.Parse(time.RFC3339, start); err == nil {
|
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 {
|
for k, v := range query {
|
||||||
if k == "start" || k == "end" {
|
if k == "start" || k == "end" {
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Add table
Reference in a new issue