示例#1
0
func TestSummarize(t *testing.T) {
	start := time.Date(2016, time.Month(1), 21, 0, 0, 0, 0, time.UTC)
	end := start.Add(36 * time.Hour)
	step := time.Hour
	NaN := math.NaN()

	ts1, err := ts.NewTimeSeriesOfTimeRange("ts1", start, end, step, 1)
	checkErr(t, err)
	if ts1 == nil {
		t.Errorf("FAIL(ts1): can't be nil, if we want to continue with other tests")
		return
	}

	ts1NaN, err := ts.NewTimeSeriesOfData("ts1NaN", start, step,
		[]float64{
			1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN,
			1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN,
			1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN, 1, NaN,
		})
	checkErr(t, err)
	if ts1NaN == nil {
		t.Errorf("FAIL(ts1NaN): can't be nil, if we want to continue with other tests")
		return
	}

	tss := []struct {
		Got *ts.TimeSeries
		Exp *TestSeries
	}{
		{
			Got: Summarize(ts1, 24*time.Hour),
			Exp: &TestSeries{
				Key:   "Summarize(24h0m0s)(ts1)",
				Start: start,
				End:   start.Add(2 * 24 * time.Hour),
				Step:  24 * time.Hour,
				Data:  []float64{24, 12},
			},
		},
		{
			Got: Summarize(ts1NaN, 24*time.Hour),
			Exp: &TestSeries{
				Key:   "Summarize(24h0m0s)(ts1NaN)",
				Start: start,
				End:   start.Add(2 * 24 * time.Hour),
				Step:  24 * time.Hour,
				Data:  []float64{12, 6},
			},
		},
	}

	for _, pair := range tss {
		fmt.Printf("%s\n%s\n\n", pair.Got, pair.Exp)
		checkTimeSeries(t, pair.Got, pair.Exp)
	}
	//t.Errorf("here")
}
示例#2
0
func ChartSlice(tss ts.TimeSeriesSlice) (template.JS, error) {
	if tss == nil {
		return template.JS(""), nil
	}
	start := tss.Start()
	end := tss.End()
	step, ok := tss.Step()
	if !ok {
		return "", errors.New("time series step is not equal")
	}

	dummyTS, err := ts.NewTimeSeriesOfTimeRange("dummyTS", start, end, step, 0)
	if err != nil {
		return "", err
	}

	t, err := timeString(dummyTS)
	if err != nil {
		return "", err
	}
	s := bytes.NewBufferString(t)

	for i, _ := range tss {
		if err := s.WriteByte(','); err != nil {
			return "", err
		}
		if err := s.WriteByte('\n'); err != nil {
			return "", err
		}

		v, err := valueString(&tss[i], start, end)
		if err != nil {
			return "", err
		}
		if _, err := s.WriteString(v); err != nil {
			return "", err
		}
	}
	return template.JS(s.String()), nil
}
func TestTransforms(t *testing.T) {
	start := time.Date(2016, time.Month(1), 14, 10, 0, 0, 0, time.UTC)
	end := start.Add(6 * time.Minute)
	step := time.Minute
	NaN := math.NaN()

	ts1, err := ts.NewTimeSeriesOfTimeRange("ts1", start, end, step, 1)
	checkErr(t, err)
	if ts1 == nil {
		t.Errorf("FAIL(ts1): can't be nil, if we want to continue with other tests")
		return
	}

	ts1NaN, err := ts.NewTimeSeriesOfData("ts1NaN", start, step,
		[]float64{1, NaN, NaN, 1, 1, NaN})
	checkErr(t, err)
	if ts1NaN == nil {
		t.Errorf("FAIL(ts1NaN): can't be nil, if we want to continue with other tests")
		return
	}

	tsRaise, err := ts.NewTimeSeriesOfData("tsRaise", start, step,
		[]float64{NaN, 1, 1, 0, 1, NaN, 2, 2})
	checkErr(t, err)
	if tsRaise == nil {
		t.Errorf("FAIL(tsRaise): can't be nil, if we want to continue with other tests")
		return
	}

	tss := []struct {
		Got *ts.TimeSeries
		Exp *TestSeries
	}{
		{
			Got: ts1.Transform(&CumulativeSum{}),
			Exp: &TestSeries{
				Key:   "CumulativeSum(ts1)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{1, 2, 3, 4, 5, 6},
			},
		},
		{
			Got: ts1NaN.Transform(&CumulativeSum{}),
			Exp: &TestSeries{
				Key:   "CumulativeSum(ts1NaN)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{1, 1, 1, 2, 3, 3},
			},
		},
		{
			Got: ts1.Transform(&DivideBy{2}),
			Exp: &TestSeries{
				Key:   (&DivideBy{2}).Name() + "(ts1)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{0.5, 0.5, 0.5, 0.5, 0.5, 0.5},
			},
		},
		{
			Got: ts1NaN.Transform(&DivideBy{2}),
			Exp: &TestSeries{
				Key:   (&DivideBy{2}).Name() + "(ts1NaN)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{0.5, NaN, NaN, 0.5, 0.5, NaN},
			},
		},
		{
			Got: ts1.Transform(&MultiplyBy{2}),
			Exp: &TestSeries{
				Key:   (&MultiplyBy{2}).Name() + "(ts1)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{2, 2, 2, 2, 2, 2},
			},
		},
		{
			Got: ts1NaN.Transform(&MultiplyBy{2}),
			Exp: &TestSeries{
				Key:   (&MultiplyBy{2}).Name() + "(ts1NaN)",
				Start: start,
				End:   end,
				Step:  step,
				Data:  []float64{2, NaN, NaN, 2, 2, NaN},
			},
		},
		{
			Got: tsRaise.Transform(&MarkRaise{}),
			Exp: &TestSeries{
				Key:   (&MarkRaise{}).Name() + "(tsRaise)",
				Start: start,
				End:   start.Add(8 * step),
				Step:  step,
				Data:  []float64{NaN, 0, 0, 0, 1, NaN, 1, 0},
			},
		},
	}

	for _, pair := range tss {
		fmt.Printf("%s\n%s\n\n", pair.Got, pair.Exp)
		checkTimeSeries(t, pair.Got, pair.Exp)
	}
	//t.Errorf("here")
}