Skip to content

Commit

Permalink
suppress headers in output for influx when they are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
corylanou committed Mar 13, 2017
1 parent a322571 commit f46a9e2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#7776](https://github.com/influxdata/influxdb/issues/7776): Add system information to /debug/vars.
- [#7553](https://github.com/influxdata/influxdb/issues/7553): Add modulo operator to the query language.
- [#8119](https://github.com/influxdata/influxdb/pull/8119): Add chunked/chunk size as setting/options in cli.
- [#8122](https://github.com/influxdata/influxdb/pull/8122): Suppress headers in output for influx cli when they are the same.

## v1.2.1 [2017-03-08]

Expand Down
94 changes: 79 additions & 15 deletions cmd/influx/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/signal"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -777,38 +778,103 @@ func (c *CommandLine) writeJSON(response *client.Response, w io.Writer) {
fmt.Fprintln(w, string(data))
}

func tagsEqual(prev, current map[string]string) bool {
if prev == nil && current == nil {
return true
}
if len(prev) != len(current) {
return false
}
return reflect.DeepEqual(prev, current)
}

func columnsEqual(prev, current []string) bool {
if prev == nil && current == nil {
return true
}
if len(prev) != len(current) {
return false
}
return reflect.DeepEqual(prev, current)
}

func headersEqual(prev, current models.Row) bool {
if prev.Name != current.Name {
return false
}
if prev.Tags == nil && current.Tags == nil {
}
if !tagsEqual(prev.Tags, current.Tags) {
return false
}
if !columnsEqual(prev.Columns, current.Columns) {
return false
}

return true
}

func (c *CommandLine) writeCSV(response *client.Response, w io.Writer) {
csvw := csv.NewWriter(w)
var previousHeaders models.Row
for _, result := range response.Results {
suppressHeaders := len(result.Series) > 0 && headersEqual(previousHeaders, result.Series[0])
if !suppressHeaders && len(result.Series) > 0 {
previousHeaders = models.Row{
Name: result.Series[0].Name,
Tags: result.Series[0].Tags,
Columns: result.Series[0].Columns,
}
}

// Create a tabbed writer for each result as they won't always line up
rows := c.formatResults(result, "\t")
rows := c.formatResults(result, "\t", suppressHeaders)
for _, r := range rows {
csvw.Write(strings.Split(r, "\t"))
}
csvw.Flush()
}
csvw.Flush()
}

func (c *CommandLine) writeColumns(response *client.Response, w io.Writer) {
// Create a tabbed writer for each result as they won't always line up
writer := new(tabwriter.Writer)
writer.Init(w, 0, 8, 1, ' ', 0)

for _, result := range response.Results {
var previousHeaders models.Row
for i, result := range response.Results {
// Print out all messages first
for _, m := range result.Messages {
fmt.Fprintf(w, "%s: %s.\n", m.Level, m.Text)
}
csv := c.formatResults(result, "\t")
for _, r := range csv {
// Check to see if the headers are the same as the previous row. If so, suppress them in the output
suppressHeaders := len(result.Series) > 0 && headersEqual(previousHeaders, result.Series[0])
if !suppressHeaders && len(result.Series) > 0 {
previousHeaders = models.Row{
Name: result.Series[0].Name,
Tags: result.Series[0].Tags,
Columns: result.Series[0].Columns,
}
}

// if we are not at the end of the results, and we aren't suppressing headers, put in a new line
// to break up the results
// We don't print one if we are the first result, or the last result
if !suppressHeaders && i > 0 && i < len(response.Results) {
fmt.Fprintln(writer, "")
}

rows := c.formatResults(result, "\t", suppressHeaders)
for _, r := range rows {
fmt.Fprintln(writer, r)
}
writer.Flush()

}
writer.Flush()
}

// formatResults will behave differently if you are formatting for columns or csv
func (c *CommandLine) formatResults(result client.Result, separator string) []string {
func (c *CommandLine) formatResults(result client.Result, separator string, suppressHeaders bool) []string {
rows := []string{}
// Create a tabbed writer for each result as they won't always line up
for i, row := range result.Series {
Expand All @@ -835,12 +901,12 @@ func (c *CommandLine) formatResults(result client.Result, separator string) []st
columnNames = append(columnNames, row.Columns...)

// Output a line separator if we have more than one set or results and format is column
if i > 0 && c.Format == "column" {
if i > 0 && c.Format == "column" && !suppressHeaders {
rows = append(rows, "")
}

// If we are column format, we break out the name/tag to separate lines
if c.Format == "column" {
if c.Format == "column" && !suppressHeaders {
if row.Name != "" {
n := fmt.Sprintf("name: %s", row.Name)
rows = append(rows, n)
Expand All @@ -851,10 +917,12 @@ func (c *CommandLine) formatResults(result client.Result, separator string) []st
}
}

rows = append(rows, strings.Join(columnNames, separator))
if !suppressHeaders {
rows = append(rows, strings.Join(columnNames, separator))
}

// if format is column, write dashes under each column
if c.Format == "column" {
if c.Format == "column" && !suppressHeaders {
lines := []string{}
for _, columnName := range columnNames {
lines = append(lines, strings.Repeat("-", len(columnName)))
Expand All @@ -878,10 +946,6 @@ func (c *CommandLine) formatResults(result client.Result, separator string) []st
}
rows = append(rows, strings.Join(values, separator))
}
// Output a line separator if in column format
if c.Format == "column" {
rows = append(rows, "")
}
}
return rows
}
Expand Down

0 comments on commit f46a9e2

Please sign in to comment.