Esempio n. 1
0
// Ensure a reducer can combine data received from a mapper.
func TestReducer_Reduce(t *testing.T) {
	m := []*influxql.Mapper{
		influxql.NewMapper(influxql.MapSum,
			NewIterator([]string{"foo"}, []Point{
				{"2000-01-01T00:00:00Z", float64(10)},
				{"2000-01-01T00:01:00Z", float64(20)},
			}), 1*time.Minute),
		influxql.NewMapper(influxql.MapSum,
			NewIterator([]string{"bar"}, []Point{
				{"2000-01-01T00:00:00Z", float64(100)},
				{"2000-01-01T00:01:00Z", float64(200)},
			}), 1*time.Minute),
		influxql.NewMapper(influxql.MapSum, NewIterator([]string{"foo"}, []Point{
			{"2000-01-01T00:00:00Z", float64(1000)},
			{"2000-01-01T00:01:00Z", float64(2000)},
		}), 1*time.Minute)}

	r := influxql.NewReducer(influxql.ReduceSum, m)
	ch := r.Reduce().C()
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684800000000000, Values: "\x00\x03bar"}: float64(100)}) {
		t.Fatalf("unexpected data(0/bar): %#v", data)
	} else if data = <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684800000000000, Values: "\x00\x03foo"}: float64(1010)}) {
		t.Fatalf("unexpected data(0/foo): %#v", data)
	}
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684860000000000, Values: "\x00\x03bar"}: float64(200)}) {
		t.Fatalf("unexpected data(1/bar): %#v", data)
	} else if data = <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684860000000000, Values: "\x00\x03foo"}: float64(2020)}) {
		t.Fatalf("unexpected data(1/foo): %#v", data)
	}
	if data, ok := <-ch; data != nil {
		t.Fatalf("unexpected data(end): %#v", data)
	} else if ok {
		t.Fatalf("expected channel close")
	}
}
Esempio n. 2
0
// Ensure a mapper can process intervals across multiple iterators.
func TestMapper_Map(t *testing.T) {
	m := influxql.NewMapper(influxql.MapSum,
		NewIterator([]string{"foo"}, []Point{
			{"2000-01-01T00:00:00Z", float64(10)}, // first minute
			{"2000-01-01T00:00:30Z", float64(20)},
			{"2000-01-01T00:01:00Z", float64(30)}, // second minute
			{"2000-01-01T00:01:30Z", float64(40)},
			{"2000-01-01T00:03:00Z", float64(50)}, // fourth minute (skip third)
		}),
		1*time.Minute)

	ch := m.Map().C()
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684800000000000, Values: "\x00\x03foo"}: float64(30)}) {
		t.Fatalf("unexpected data(0/foo): %#v", data)
	}
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684860000000000, Values: "\x00\x03foo"}: float64(70)}) {
		t.Fatalf("unexpected data(1/foo): %#v", data)
	}
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684920000000000, Values: "\x00\x03foo"}: float64(0)}) {
		t.Fatalf("unexpected data(2/foo): %#v", data)
	}
	if data := <-ch; !reflect.DeepEqual(data, map[influxql.Key]interface{}{influxql.Key{Timestamp: 946684980000000000, Values: "\x00\x03foo"}: float64(50)}) {
		t.Fatalf("unexpected data(3/foo): %#v", data)
	}
}