From 86b91ead9486ab66ebcd76c017476fe25e456662 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sun, 22 Feb 2015 22:25:27 -0800 Subject: [PATCH] "SHOW TAG VALUES" now outputs series per tag key This change tightens up the type used for "Series" so the pre-existing sort method can be used. --- database.go | 11 +++++++---- httpd/handler_test.go | 37 ++++++++++++++++++------------------- server.go | 22 ++++++++++++++++------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/database.go b/database.go index 97cfa9a83af..8087a6d2f60 100644 --- a/database.go +++ b/database.go @@ -1367,7 +1367,7 @@ func (m *Measurement) tagKeys() []string { return keys } -func (m *Measurement) tagValuesByKeyAndSeriesID(tagKeys []string, ids seriesIDs) stringSet { +func (m *Measurement) tagValuesByKeyAndSeriesID(tagKeys []string, ids seriesIDs) map[string]stringSet { // If no tag keys were passed, get all tag keys for the measurement. if len(tagKeys) == 0 { for k := range m.seriesByTagKeyValue { @@ -1375,8 +1375,8 @@ func (m *Measurement) tagValuesByKeyAndSeriesID(tagKeys []string, ids seriesIDs) } } - // Make a set to hold all tag values found. - tagValues := newStringSet() + // Mapping between tag keys to all existing tag values. + tagValues := make(map[string]stringSet, 0) // Iterate all series to collect tag values. for _, id := range ids { @@ -1389,7 +1389,10 @@ func (m *Measurement) tagValuesByKeyAndSeriesID(tagKeys []string, ids seriesIDs) // from this series, if they exist. for _, tagKey := range tagKeys { if tagVal, ok := s.Tags[tagKey]; ok { - tagValues.add(tagVal) + if _, ok = tagValues[tagKey]; !ok { + tagValues[tagKey] = newStringSet() + } + tagValues[tagKey].add(tagVal) } } } diff --git a/httpd/handler_test.go b/httpd/handler_test.go index 95f666b38e5..f6d8882a77e 100644 --- a/httpd/handler_test.go +++ b/httpd/handler_test.go @@ -2035,18 +2035,11 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "cpu", - Columns: []string{"tagValue"}, + Name: "hostTagValues", + Columns: []string{"host"}, Values: [][]interface{}{ str2iface([]string{"server01"}), str2iface([]string{"server02"}), - }, - }, - { - Name: "gpu", - Columns: []string{"tagValue"}, - Values: [][]interface{}{ - str2iface([]string{"server02"}), str2iface([]string{"server03"}), }, }, @@ -2063,8 +2056,8 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "cpu", - Columns: []string{"tagValue"}, + Name: "hostTagValues", + Columns: []string{"host"}, Values: [][]interface{}{ str2iface([]string{"server01"}), str2iface([]string{"server02"}), @@ -2083,8 +2076,8 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "cpu", - Columns: []string{"tagValue"}, + Name: "hostTagValues", + Columns: []string{"host"}, Values: [][]interface{}{ str2iface([]string{"server01"}), }, @@ -2102,8 +2095,8 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "gpu", - Columns: []string{"tagValue"}, + Name: "hostTagValues", + Columns: []string{"host"}, Values: [][]interface{}{ str2iface([]string{"server03"}), }, @@ -2121,8 +2114,8 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "gpu", - Columns: []string{"tagValue"}, + Name: "regionTagValues", + Columns: []string{"region"}, Values: [][]interface{}{ str2iface([]string{"caeast"}), }, @@ -2140,10 +2133,16 @@ func TestHandler_serveShowTagValues(t *testing.T) { { Series: []*influxql.Row{ { - Name: "cpu", - Columns: []string{"tagValue"}, + Name: "hostTagValues", + Columns: []string{"host"}, Values: [][]interface{}{ str2iface([]string{"server01"}), + }, + }, + { + Name: "regionTagValues", + Columns: []string{"region"}, + Values: [][]interface{}{ str2iface([]string{"uswest"}), }, }, diff --git a/server.go b/server.go index e2eb0eb37b6..e624cda4f70 100644 --- a/server.go +++ b/server.go @@ -2126,9 +2126,10 @@ func (s *Server) executeShowTagValuesStatement(stmt *influxql.ShowTagValuesState // Make result. result := &Result{ - Series: make(influxql.Rows, 0, len(measurements)), + Series: make(influxql.Rows, 0), } + tagValues := make(map[string]stringSet) for _, m := range measurements { var ids seriesIDs @@ -2148,14 +2149,22 @@ func (s *Server) executeShowTagValuesStatement(stmt *influxql.ShowTagValuesState ids = m.seriesIDs } - tagValues := m.tagValuesByKeyAndSeriesID(stmt.TagKeys, ids) + for k, v := range m.tagValuesByKeyAndSeriesID(stmt.TagKeys, ids) { + _, ok := tagValues[k] + if !ok { + tagValues[k] = v + } + tagValues[k] = tagValues[k].union(v) + } + } + for k, v := range tagValues { r := &influxql.Row{ - Name: m.Name, - Columns: []string{"tagValue"}, + Name: k + "TagValues", + Columns: []string{k}, } - vals := tagValues.list() + vals := v.list() sort.Strings(vals) for _, val := range vals { @@ -2166,6 +2175,7 @@ func (s *Server) executeShowTagValuesStatement(stmt *influxql.ShowTagValuesState result.Series = append(result.Series, r) } + sort.Sort(result.Series) return result } @@ -2613,7 +2623,7 @@ func (s *Server) processor(client MessagingClient, done chan struct{}) { // Result represents a resultset returned from a single statement. type Result struct { - Series []*influxql.Row + Series influxql.Rows Err error }