Example #1
0
func windHandler(w http.ResponseWriter, r *http.Request, db *data.Database) {
	t, _ := computeTime(r.FormValue("time"))

	var queries []map[string]string
	err := json.Unmarshal([]byte(r.FormValue("query")), &queries)
	if err != nil {
		jww.ERROR.Println(err)
		return
	}

	var result struct {
		Data  [][]float64
		Label []string
	}

	result.Data = make([][]float64, len(queries))
	result.Label = make([]string, len(queries))

	rxp := regexp.MustCompile(`\[([^]]*)\]`)
	for index, querymap := range queries {
		result.Label[index] = querymap["label"]
		result.Data[index] = make([]float64, 32)

		id := "%"
		if _, ok := querymap["id"]; ok {
			id = querymap["id"]
		}
		datatype := "%"
		if _, ok := querymap["type"]; ok {
			datatype = querymap["type"]
		}
		if _, ok := querymap["label"]; ok {
			result.Label[index] = querymap["label"]
		} else {
			result.Label[index] = "Unknown"
		}
		cols := rxp.FindStringSubmatch(datatype)
		var col string
		if len(cols) > 1 {
			col = cols[1]
		} else {
			col = "avg"
		}

		key := rxp.ReplaceAllString(datatype, "")

		for row := range db.QueryWind(t, key, col, id, 0) {
			speed := units.NewSpeedMetersPerSecond(row.Value)
			result.Data[index][int(row.Dir)], err = speed.Get(viper.GetStringMapString("units")["WindSpeed"])
			if err != nil {
				jww.ERROR.Println(err)
				return
			}
		}
	}

	json.NewEncoder(w).Encode(result)
}
Example #2
0
func changeHandler(w http.ResponseWriter, r *http.Request, db *data.Database) {
	datatypes := strings.Split(r.FormValue("key"), ",")
	ids := strings.Split(r.FormValue("id"), ",")
	channel, err := strconv.Atoi(r.FormValue("channel"))
	if err != nil {
		channel = 0
	}

	t, _ := computeTime(r.FormValue("time"))

	var result struct {
		Change []float64
	}

	unitmap := viper.GetStringMapString("units")
	index := 0
	for _, datatype := range datatypes {
		for _, id := range ids {
			if id == "" {
				id = "%"
			}
			old, err := db.QueryFirst(t, datatype, id, channel)
			if err != nil {
			}

			now, err := db.QueryLast(t, datatype, id, channel)
			if err != nil {
			}

			value := now - old
			var delta float64

			delta = convertUnit(unitmap, r.FormValue("type"), value)
			result.Change = append(result.Change, delta)

			index++
		}
	}
	json.NewEncoder(w).Encode(result)
}
Example #3
0
func sumData(thedata *map[mapKey][]float64, db *data.Database) {
	direction := regexp.MustCompile(`Dir$`)

	timestamp := time.Now().UTC().Unix()

	for key, slice := range *thedata {
		var min, max, avg float64
		switch {
		case direction.MatchString(key.Key):
			min = circularmean(slice)
			max = min
			avg = min
		default:
			min = minimum(slice)
			max = maximum(slice)
			avg = mean(slice)
		}
		err := db.InsertRow(timestamp, key.ID, key.Channel, key.Serial, key.Key, min, max, avg)
		if err != nil {
			jww.ERROR.Printf("%s\n", err.Error())
		}
	}
	*thedata = make(map[mapKey][]float64)
}
Example #4
0
func dataHandler(w http.ResponseWriter, r *http.Request, db *data.Database) {
	var queries []map[string]string
	err := json.Unmarshal([]byte(r.FormValue("query")), &queries)
	if err != nil {
		jww.ERROR.Println(err)
		return
	}
	//channel := r.FormValue("channel")

	t, interval := computeTime(r.FormValue("time"))

	var result struct {
		Data      [][]interface{}
		Errorbars [][]interface{}
		Label     []string
	}
	result.Data = make([][]interface{}, len(queries))
	result.Errorbars = make([][]interface{}, len(queries))
	result.Label = make([]string, len(queries))

	rxp := regexp.MustCompile(`\[([^]]*)\]`)
	for index, querymap := range queries {
		id := "%"
		if _, ok := querymap["id"]; ok {
			id = querymap["id"]
		}
		datatype := "%"
		if _, ok := querymap["type"]; ok {
			datatype = querymap["type"]
		}
		if _, ok := querymap["label"]; ok {
			result.Label[index] = querymap["label"]
		} else {
			result.Label[index] = "Unknown"
		}

		key := rxp.ReplaceAllString(datatype, "")
		var rows <-chan data.Row
		if interval > 1 {
			rows = db.QueryRowsInterval(t, key, id, interval)
		} else {
			rows = db.QueryRows(t, key, id)
		}

		col := rxp.FindStringSubmatch(datatype)

		unitmap := viper.GetStringMapString("units")
		for row := range rows {
			_, off := time.Unix(row.Timestamp, 0).Zone()
			t := (time.Unix(row.Timestamp, 0).Unix() + int64(off)) * 1000
			sub := make([]interface{}, 2)
			sub[0] = t
			var value float64
			if len(col) > 1 {
				switch col[1] {
				case "min":
					value = row.Min
				case "max":
					value = row.Max
				case "avg":
					value = row.Avg
				default:
					value = row.Avg
				}
			} else {
				value = row.Avg
			}
			sub[1] = convertUnit(unitmap, r.FormValue("type"), value)
			result.Data[index] = append(result.Data[index], sub)

			sub = make([]interface{}, 3)
			sub[0] = t
			sub[1] = convertUnit(unitmap, r.FormValue("type"), row.Min)
			sub[2] = convertUnit(unitmap, r.FormValue("type"), row.Max)
			result.Errorbars[index] = append(result.Errorbars[index], sub)
		}
		index++
	}
	json.NewEncoder(w).Encode(result)
}