possibility for weather-query of multiple sensors + fix wrong flux-query

This commit is contained in:
Joel Schmid 2021-11-26 18:03:51 +01:00
parent 5c33c5ff14
commit 1512c29881
3 changed files with 22 additions and 8 deletions

View file

@ -120,13 +120,15 @@ func (api *weatherRestApi) getWeatherDataHandler(w http.ResponseWriter, r *http.
vars := mux.Vars(r)
id := vars["id"]
query, err := storage.ParseFromUrlQuery(r.URL.Query())
query, err := storage.ParseWeatherQuery(r.URL.Query())
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
query.SensorId, err = uuid.Parse(id)
sensorid, err := uuid.Parse(id)
query.SensorIds = append(query.SensorIds, sensorid)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return

View file

@ -61,6 +61,7 @@ func (storage *influxStorage) GetData(query *WeatherQuery) ([]*WeatherData, erro
func (storage *influxStorage) createFluxQuery(query *WeatherQuery) string {
fields := ""
concat := ""
sensorIds := ""
for sensorValueType, value := range query.Values {
if value {
@ -68,14 +69,25 @@ func (storage *influxStorage) createFluxQuery(query *WeatherQuery) string {
concat = "or"
}
}
concat = ""
for _, id := range query.SensorIds {
sensorIds = fmt.Sprintf("%v %v r[\"sensorId\"] == \"%v\"", sensorIds, concat, id)
concat = "or"
}
fromTemplate := fmt.Sprintf("from(bucket:\"%v\")", storage.config.Bucket)
rangeTemplate := fmt.Sprintf("|> range(start: %v, stop: %v)", query.Start.Format(time.RFC3339), query.End.Format(time.RFC3339))
measurementTemplate := fmt.Sprintf("|> filter(fn: (r) => r[\"_measurement\"] == \"%v\")", storage.measurement)
sensorIdsTemplate := fmt.Sprintf("|> filter(fn: (r) => r[\"sensorId\"] == \"%v\")", query.SensorId)
fields = fmt.Sprintf("|> filter(fn: (r) => %v )", strings.Trim(fields, " "))
sensorIdsTemplate := ""
if len(sensorIds) > 0 {
sensorIdsTemplate = fmt.Sprintf("|> filter(fn: (r) => %v )", strings.Trim(sensorIds, " "))
}
fieldsTemplate := ""
if len(fields) > 0 {
fieldsTemplate = fmt.Sprintf("|> filter(fn: (r) => %v )", strings.Trim(fields, " "))
}
fluxQuery := fmt.Sprintf("%v \n %v \n %v \n %v \n %v", fromTemplate, rangeTemplate, measurementTemplate, sensorIdsTemplate, fields)
fluxQuery := fmt.Sprintf("%v \n %v \n %v \n %v \n %v", fromTemplate, rangeTemplate, measurementTemplate, sensorIdsTemplate, fieldsTemplate)
return fluxQuery
}

View file

@ -12,7 +12,7 @@ import (
type WeatherQuery struct {
Start time.Time
End time.Time
SensorId uuid.UUID
SensorIds []uuid.UUID
MaxDataPoints int
Values map[SensorValueType]bool
}
@ -28,13 +28,13 @@ func NewWeatherQuery() *WeatherQuery {
func (query *WeatherQuery) Init() {
query.Start = time.Now().Add(-1 * time.Hour * 24 * 14)
query.End = time.Now()
query.SensorId = uuid.Nil
query.SensorIds = make([]uuid.UUID, 0)
for _, sensorValueType := range GetSensorValueTypes() {
query.Values[sensorValueType] = true
}
}
func ParseFromUrlQuery(query url.Values) (*WeatherQuery, error) {
func ParseWeatherQuery(query url.Values) (*WeatherQuery, error) {
result := NewWeatherQuery()
result.Init()