Example #1
0
func TestEpochToTime(t *testing.T) {
	now := time.Now()

	tests := []struct {
		name      string
		epoch     int64
		precision string
		expected  time.Time
	}{
		{name: "nanoseconds", epoch: now.UnixNano(), precision: "n", expected: now},
		{name: "microseconds", epoch: now.Round(time.Microsecond).UnixNano() / int64(time.Microsecond), precision: "u", expected: now.Round(time.Microsecond)},
		{name: "milliseconds", epoch: now.Round(time.Millisecond).UnixNano() / int64(time.Millisecond), precision: "ms", expected: now.Round(time.Millisecond)},
		{name: "seconds", epoch: now.Round(time.Second).UnixNano() / int64(time.Second), precision: "s", expected: now.Round(time.Second)},
		{name: "minutes", epoch: now.Round(time.Minute).UnixNano() / int64(time.Minute), precision: "m", expected: now.Round(time.Minute)},
		{name: "hours", epoch: now.Round(time.Hour).UnixNano() / int64(time.Hour), precision: "h", expected: now.Round(time.Hour)},
	}

	for _, test := range tests {
		t.Logf("testing %q\n", test.name)
		tm, e := client.EpochToTime(test.epoch, test.precision)
		if e != nil {
			t.Fatalf("unexpected error: expected %v, actual: %v", nil, e)
		}
		if tm != test.expected {
			t.Fatalf("unexpected time: expected %v, actual %v", test.expected, tm)
		}
	}
}
func (c *InfluxdbClient_v088) Query(q client090.Query) (*client090.Response, error) {
	series, err := c.client.Query(q.Command, "ms")
	// fmt.Println(series, err)

	res := client090.Result{
		Series: []influxql.Row{},
		Err:    fmt.Errorf(""),
	}

	// v0.8.8  [
	//   {
	//     "name": "log_lines",
	//     "columns": ["time", "sequence_number", "line"],
	//     "points": [
	//       [1400425947368, 1, "this line is first"],
	//       [1400425947368, 2, "and this is second"]
	//     ]
	//   }
	// ]

	// type Row struct {
	// 	Name    string            `json:"name,omitempty"`
	// 	Tags    map[string]string `json:"tags,omitempty"`
	// 	Columns []string          `json:"columns"`
	// 	Values  [][]interface{}   `json:"values,omitempty"`
	// 	Err     error             `json:"err,omitempty"`
	// }
	// fmt.Println("-------------------------")
outer:
	for _, ss := range series {
		// fmt.Println(ss.Name)
		// fmt.Println(ss.GetColumns())
		// fmt.Println(ss.GetPoints())

		idx_time := -1
	inner:
		for idx, v := range ss.GetColumns() {
			if v == "time" {
				idx_time = idx
				break inner
			}
		}
		points := ss.GetPoints()

		if idx_time >= 0 {
			// convert time back
			for _, point := range points {
				//TODO: here maybe problematic
				ms, err := client090.EpochToTime(int64(point[idx_time].(float64)), "ms")
				if err != nil {
					points = ss.GetPoints()
					break outer
				}
				point[idx_time] = ms
			}
		}

		row := influxql.Row{
			Name:    ss.GetName(),
			Columns: ss.GetColumns(),
			Values:  points,
		}
		res.Series = append(res.Series, row)
	}
	results := client090.Response{
		Results: []client090.Result{res},
		Err:     err,
	}

	return &results, err
}