func (d *DummyIn) GetFieldRangeByName(name string, start time.Time, end time.Time) ([]common.Field, error) { var err error var startField common.Field var midField common.Field var endField common.Field startTs := start.UnixNano() endTs := end.UnixNano() if startTs-endTs < 10 || endTs-startTs < 10 { } startField.Name = name startField.Tags = make(map[string]string) startField.Values = make(map[string]interface{}) startField.Values[`time`] = startTs // same TS, emit only one record if startTs == endTs { return []common.Field{startField}, err } endField.Name = name endField.Tags = make(map[string]string) endField.Values = make(map[string]interface{}) endField.Values[`time`] = endTs // very small spread, emit only start and end if startTs-endTs < time.Second.Nanoseconds() || endTs-startTs < time.Second.Nanoseconds() { return []common.Field{startField, endField}, err } midField.Name = name midField.Tags = make(map[string]string) midField.Values = make(map[string]interface{}) midField.Values[`time`] = (startTs + endTs) / 2 return []common.Field{startField, midField, endField}, err }
func (influx *Influx09Input) GetFieldRangeByName(name string, start time.Time, end time.Time) ([]common.Field, error) { var datapoints []common.Field var err error q := client.NewQuery( fmt.Sprintf(`SELECT * FROM "%s" WHERE time > %d AND time < %d`, name, start.UnixNano(), end.UnixNano()), influx.Db, `ns`, ) res, err := influx.Client.Query(q) if res.Error() != nil { err = res.Error() return datapoints, err } for _, result := range res.Results { for _, ser := range result.Series { columns := ser.Columns for _, value := range ser.Values { var f common.Field f.Name = ser.Name f.Tags = ser.Tags f.Values = make(map[string]interface{}) for j, value := range value { // influxdb lib is a bit funny if w, ok := value.(json.Number); ok { // check if it is an int or float a, _ := w.Float64() if a == float64(int64(a)) { f.Values[columns[j]], _ = w.Int64() } else { f.Values[columns[j]] = a } } else { f.Values[columns[j]] = value } } datapoints = append(datapoints, f) } } } return datapoints, err }
func TestWriter(t *testing.T) { var f common.Field f.Name = "zupa" f.Values = make(map[string]interface{}) f.Values[`time`] = time.Now() f.Values[`cake`] = "lie" f.Values[`other`] = time.Now() writeCh := make(chan *common.Field, 1) writeCh <- &f var sql SQLiteOut sql.Init("t-data/router-test") err := sql.writers.RunWriter(writeCh, []string{`t-data`, `point-writer-test.sqlite`}, false) Convey("WritePoint", t, func() { So(err, ShouldBeNil) }) close(writeCh) // close channel so writer exits sql.writers.Shutdown() }
func TestRouter(t *testing.T) { var f common.Field var sql SQLiteOut sql.Init("t-data/router-test") f.Name = "zupad" f.Values = make(map[string]interface{}) f.Values[`time`] = time.Now() f.Values[`data`] = "lie" f.Values[`other_thing`] = time.Now() Convey("Create writer", t, func() { So(sql.writers, ShouldNotBeNil) }) ch := make(chan *common.Field, 128) sql.routers.Add(1) go sql.route(ch) ch <- &f ch <- &f ch <- &f ch <- &f close(ch) sql.writers.Shutdown() }
func TestQuoting(t *testing.T) { var f common.Field f.Name = "bad-name-test" f.Values = make(map[string]interface{}) f.Values[`time`] = time.Now() f.Values[`cake`] = "lie" f.Values[`some-long-name`] = time.Now() f.Values[`name with spaces`] = time.Now() f.Values[`ga@#&*$H*&GD&*!@GTbage`] = time.Now() writeCh := make(chan *common.Field, 10) writeCh <- &f writeCh <- &f writeCh <- &f var sql SQLiteOut sql.Init("t-data/quote-test") err := sql.writers.RunWriter(writeCh, []string{`t-data`, `quoted-writer-test.sqlite`}, false) Convey("WriteGarbageFields", t, func() { So(err, ShouldBeNil) }) close(writeCh) // close channel so writer exits sql.writers.Shutdown() }