Skip to content

Commit

Permalink
"SHOW TAG VALUES" now outputs series per tag key
Browse files Browse the repository at this point in the history
This change tightens up the type used for "Series" so the pre-existing
sort method can be used.
  • Loading branch information
otoolep committed Feb 24, 2015
1 parent 5cf40e9 commit 86b91ea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
11 changes: 7 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,16 +1367,16 @@ 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 {
tagKeys = append(tagKeys, k)
}
}

// 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 {
Expand All @@ -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)
}
}
}
Expand Down
37 changes: 18 additions & 19 deletions httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
},
},
Expand All @@ -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"}),
Expand All @@ -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"}),
},
Expand All @@ -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"}),
},
Expand All @@ -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"}),
},
Expand All @@ -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"}),
},
},
Expand Down
22 changes: 16 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand All @@ -2166,6 +2175,7 @@ func (s *Server) executeShowTagValuesStatement(stmt *influxql.ShowTagValuesState
result.Series = append(result.Series, r)
}

sort.Sort(result.Series)
return result
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 86b91ea

Please sign in to comment.