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) }