From 942f3ca6ac8cfe3e42140d74383ccc9bc00cb811 Mon Sep 17 00:00:00 2001
From: Chris Goller <goller@gmail.com>
Date: Mon, 7 Nov 2016 16:29:39 -0600
Subject: [PATCH] Add groupby options to layouts

---
 bolt/internal/internal.go    | 14 ++++---
 bolt/internal/internal.pb.go | 74 ++++++++++++++++++------------------
 bolt/internal/internal.proto |  1 +
 canned/cpu.json              |  3 +-
 canned/load.json             |  3 +-
 canned/mem.json              |  3 +-
 canned/new_apps.sh           |  5 ++-
 chronograf.go                |  7 ++--
 8 files changed, 60 insertions(+), 50 deletions(-)

diff --git a/bolt/internal/internal.go b/bolt/internal/internal.go
index 6733ab9f0..efdc91bb9 100644
--- a/bolt/internal/internal.go
+++ b/bolt/internal/internal.go
@@ -106,9 +106,10 @@ func MarshalLayout(l chronograf.Layout) ([]byte, error) {
 		queries := make([]*Query, len(c.Queries))
 		for j, q := range c.Queries {
 			queries[j] = &Query{
-				Command: q.Command,
-				DB:      q.DB,
-				RP:      q.RP,
+				Command:  q.Command,
+				DB:       q.DB,
+				RP:       q.RP,
+				GroupBys: q.GroupBys,
 			}
 		}
 		cells[i] = &Cell{
@@ -144,9 +145,10 @@ func UnmarshalLayout(data []byte, l *chronograf.Layout) error {
 		queries := make([]chronograf.Query, len(c.Queries))
 		for j, q := range c.Queries {
 			queries[j] = chronograf.Query{
-				Command: q.Command,
-				DB:      q.DB,
-				RP:      q.RP,
+				Command:  q.Command,
+				DB:       q.DB,
+				RP:       q.RP,
+				GroupBys: q.GroupBys,
 			}
 		}
 		cells[i] = chronograf.Cell{
diff --git a/bolt/internal/internal.pb.go b/bolt/internal/internal.pb.go
index 7c9586bbb..9f2e610d4 100644
--- a/bolt/internal/internal.pb.go
+++ b/bolt/internal/internal.pb.go
@@ -120,9 +120,10 @@ func (m *Cell) GetQueries() []*Query {
 }
 
 type Query struct {
-	Command string `protobuf:"bytes,1,opt,name=Command,json=command,proto3" json:"Command,omitempty"`
-	DB      string `protobuf:"bytes,2,opt,name=DB,json=dB,proto3" json:"DB,omitempty"`
-	RP      string `protobuf:"bytes,3,opt,name=RP,json=rP,proto3" json:"RP,omitempty"`
+	Command  string   `protobuf:"bytes,1,opt,name=Command,json=command,proto3" json:"Command,omitempty"`
+	DB       string   `protobuf:"bytes,2,opt,name=DB,json=dB,proto3" json:"DB,omitempty"`
+	RP       string   `protobuf:"bytes,3,opt,name=RP,json=rP,proto3" json:"RP,omitempty"`
+	GroupBys []string `protobuf:"bytes,4,rep,name=GroupBys,json=groupBys" json:"GroupBys,omitempty"`
 }
 
 func (m *Query) Reset()                    { *m = Query{} }
@@ -155,37 +156,38 @@ func init() {
 func init() { proto.RegisterFile("internal.proto", fileDescriptorInternal) }
 
 var fileDescriptorInternal = []byte{
-	// 505 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x93, 0xcf, 0xae, 0xd3, 0x3c,
-	0x10, 0xc5, 0xe5, 0x26, 0x4e, 0x9a, 0xe9, 0xa7, 0x7e, 0xc8, 0x42, 0x28, 0x42, 0x2c, 0xaa, 0x88,
-	0x45, 0xd9, 0xdc, 0x05, 0x3c, 0x41, 0x6f, 0xc3, 0xa2, 0x50, 0x7a, 0x8b, 0x4b, 0x97, 0x2c, 0x4c,
-	0x32, 0xe8, 0x06, 0xf2, 0x0f, 0xc7, 0xa6, 0xcd, 0x96, 0x2d, 0x3c, 0x06, 0x6f, 0xc0, 0x0b, 0x22,
-	0xbb, 0x2e, 0xa9, 0x04, 0xba, 0xba, 0xcb, 0x33, 0x67, 0xa2, 0xf9, 0xcd, 0x99, 0x18, 0xa6, 0x45,
-	0xad, 0x50, 0xd6, 0xa2, 0xbc, 0x6a, 0x65, 0xa3, 0x1a, 0x36, 0x3e, 0xeb, 0xe4, 0x17, 0x81, 0xc9,
-	0xcb, 0x63, 0x5b, 0x36, 0x52, 0xa8, 0xa2, 0xa9, 0xd9, 0x14, 0x46, 0x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c, 0xef, 0xe2, 0x38, 0xe6, 0x51, 0xbd, 0x16, 0xad, 0x58, 0xa5, 0x76,
-	0x53, 0x8f, 0x07, 0x9f, 0xad, 0xfa, 0x10, 0xd8, 0x17, 0xfb, 0xe2, 0x77, 0x00, 0x00, 0x00, 0xff,
-	0xff, 0x61, 0x43, 0x41, 0x73, 0xc3, 0x03, 0x00, 0x00,
+	// 519 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x93, 0xcf, 0x8e, 0xd3, 0x30,
+	0x10, 0xc6, 0xe5, 0x26, 0xce, 0x1f, 0x17, 0x2d, 0xc8, 0x42, 0x28, 0x42, 0x1c, 0xaa, 0x88, 0x43,
+	0xb9, 0xec, 0x01, 0x9e, 0xa0, 0x6d, 0x10, 0x2a, 0x94, 0x6e, 0x71, 0xe9, 0x71, 0x0f, 0x26, 0x31,
+	0x6c, 0x20, 0x7f, 0x8c, 0x63, 0xd3, 0xe6, 0xca, 0x15, 0x1e, 0x83, 0x37, 0xe0, 0x05, 0xd1, 0xb8,
+	0x2e, 0xa9, 0x04, 0x5a, 0xed, 0xf1, 0x9b, 0x6f, 0xa2, 0xf9, 0xcd, 0x37, 0x0e, 0xb9, 0x28, 0x1b,
+	0x2d, 0x54, 0xc3, 0xab, 0x4b, 0xa9, 0x5a, 0xdd, 0xd2, 0xe8, 0xa4, 0xd3, 0xdf, 0x88, 0x8c, 0x5f,
+	0x1e, 0x64, 0xd5, 0x2a, 0xae, 0xcb, 0xb6, 0xa1, 0x17, 0x64, 0xb4, 0xcc, 0x12, 0x34, 0x41, 0x53,
+	0x8f, 0x8d, 0xca, 0x8c, 0x52, 0xe2, 0xaf, 0x79, 0x2d, 0x92, 0xd1, 0x04, 0x4d, 0x63, 0xe6, 0x37,
+	0xbc, 0x16, 0xf4, 0x11, 0x09, 0x76, 0x9d, 0x50, 0xcb, 0x2c, 0xf1, 0x6c, 0x5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x10, 0xd8, 0xbf, 0xf9, 0xc5, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xb2,
+	0xa3, 0xd2, 0x74, 0xdf, 0x03, 0x00, 0x00,
 }
diff --git a/bolt/internal/internal.proto b/bolt/internal/internal.proto
index 745d0afb2..84678367c 100644
--- a/bolt/internal/internal.proto
+++ b/bolt/internal/internal.proto
@@ -51,6 +51,7 @@ message Query {
     string Command          = 1; // Command is the query itself
     string DB               = 2; // DB the database for the query (optional)
     string RP               = 3; // RP is a retention policy and optional;
+    repeated string GroupBys= 4; // GroupBys define the groups to combine in the query
 }
 
 message AlertRule {
diff --git a/canned/cpu.json b/canned/cpu.json
index 1687e307f..2bb8544b5 100644
--- a/canned/cpu.json
+++ b/canned/cpu.json
@@ -12,7 +12,8 @@
  		"queries": [{
  			"query": "SELECT \"usage_user\" FROM \"telegraf\"..\"cpu\"",
  			"db": "telegraf",
- 			"rp": "autogen"
+ 			"rp": "autogen",
+  			"groupbys": []
 		}]
  	}]
  }
diff --git a/canned/load.json b/canned/load.json
index a63572029..c0aba845e 100644
--- a/canned/load.json
+++ b/canned/load.json
@@ -12,7 +12,8 @@
  		"queries": [{
  			"query": "SELECT \"load1\" FROM \"telegraf\"..\"system\"",
  			"db": "telegraf",
- 			"rp": "autogen"
+ 			"rp": "autogen",
+ 			"groupbys": []
 		}]
  	}]
  }
diff --git a/canned/mem.json b/canned/mem.json
index 2542f270b..7371bcab5 100644
--- a/canned/mem.json
+++ b/canned/mem.json
@@ -12,7 +12,8 @@
  		"queries": [{
  			"query": "SELECT \"used_percent\" FROM \"telegraf\"..\"mem\"",
  			"db": "telegraf",
- 			"rp": "autogen"
+ 			"rp": "autogen",
+  			"groupbys": []
 		}]
  	}]
  }
diff --git a/canned/new_apps.sh b/canned/new_apps.sh
index a39c4564e..c76aa97a3 100755
--- a/canned/new_apps.sh
+++ b/canned/new_apps.sh
@@ -8,7 +8,7 @@ show_help() {
 cat << EOF
 Usage: ${0##*/} MEASUREMENT
 Generate new layout for MEASUREMENT.  File created will be named
-MEASUREMENT_UUID.json with UUID being generated from the uuidgen command.
+MEASUREMENT.json with UUID being generated from the uuidgen command.
 
     -h          display this help and exit
 EOF
@@ -54,7 +54,8 @@ cat > $APP_FILE << EOF
  		"queries": [{
  			"query": "select used_percent from disk",
  			"db": "telegraf",
- 			"rp": "autogen"
+ 			"rp": "autogen",
+  			"groupbys": []
 		}]
  	}]
  }
diff --git a/chronograf.go b/chronograf.go
index bb8a25db1..20da8ba23 100644
--- a/chronograf.go
+++ b/chronograf.go
@@ -54,9 +54,10 @@ type TimeSeries interface {
 
 // Query retrieves a Response from a TimeSeries.
 type Query struct {
-	Command string `json:"query"` // Command is the query itself
-	DB      string `json:"db"`    // DB is optional and if empty will not be used.
-	RP      string `json:"rp"`    // RP is a retention policy and optional; if empty will not be used.
+	Command  string   `json:"query"`    // Command is the query itself
+	DB       string   `json:"db"`       // DB is optional and if empty will not be used.
+	RP       string   `json:"rp"`       // RP is a retention policy and optional; if empty will not be used.
+	GroupBys []string `json:"groupbys"` // GroupBys collate the query by these tags
 }
 
 // Response is the result of a query against a TimeSeries