diff --git a/database.go b/database.go index 95643d2f24..c7b64aef2c 100644 --- a/database.go +++ b/database.go @@ -526,7 +526,7 @@ func (db *Database) UnmarshalJSON(data []byte) error { return nil } -func (db *Database) Measurements() (a []*Measurement) { +func (db *Database) Measurements() (a Measurements) { db.mu.RLock() m := db.server.meta db.mu.RUnlock() @@ -540,16 +540,22 @@ func (db *Database) Measurements() (a []*Measurement) { // Measurement represents a collection of time series in a database type Measurement struct { - Name string - Series []*Series - Fields []*Fields + Name string `json:"name,omitempty"` + Series []*Series `json:"series,omitempty"` + Fields []*Fields `json:"fields,omitempty"` +} + +type Measurements []*Measurement + +func (m Measurement) String() string { + return string(mustMarshalJSON(m)) } // Field represents a series field. type Field struct { - ID uint8 `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type FieldType + ID uint8 `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type FieldType `json:"field"` } type FieldType int @@ -562,11 +568,6 @@ const ( Binary ) -// String returns a string representation of the field. -func (f *Field) String() string { - return fmt.Sprintf("Name: %s, ID: %d", f.Name, f.ID) -} - // Fields represents a list of fields. type Fields []*Field diff --git a/database_test.go b/database_test.go index d91c23a0fb..88d922b5af 100644 --- a/database_test.go +++ b/database_test.go @@ -440,7 +440,7 @@ func TestDatabase_CreateShardIfNotExist(t *testing.T) { } } -func TestDatabase_Series(t *testing.T) { +func TestDatabase_Measurements(t *testing.T) { s := OpenServer(NewMessagingClient()) defer s.Close() s.CreateDatabase("foo") @@ -458,24 +458,29 @@ func TestDatabase_Series(t *testing.T) { t.Fatal(err) } - r := db.Series() - resultContains(r, t, "cpu_load", tags) + r := db.Measurements() + m := []*influxdb.Measurement{ + &influxdb.Measurement{ + Name: "cpu_load", + Series: []*influxdb.Series{ + &influxdb.Series{ + ID: uint32(1), + Tags: map[string]string{"host": "servera.influx.com", "region": "uswest"}}}}} + if !measurementsEqual(r, m) { + t.Fatalf("Mesurements not the same:\n%s\n%s", r, m) + } } -func resultContains(result []*influxdb.Measurement, t *testing.T, name string, tags map[string]string) { - if len(result) == 0 { - t.Fatal("No results") +func measurementsEqual(l influxdb.Measurements, r influxdb.Measurements) bool { + if len(l) != len(r) { + return false } - for _, m := range result { - if m.Name == name { - for _, s := range m.Series { - if reflect.DeepEqual(s.Tags, tags) { - return - } - } + for i, ll := range l { + if !reflect.DeepEqual(ll, r[i]) { + return false } } - t.Fatalf("Result didn't contain name %s with tags: %s", name, tags) + return true } func TestDatabaseSeriesByTagNames(t *testing.T) {