Skip to content

Commit

Permalink
Merge pull request #4065 from sbouchex/cmd_add_precision
Browse files Browse the repository at this point in the history
Added precision support in cmd client
  • Loading branch information
otoolep committed Sep 14, 2015
2 parents 3bd7b50 + 5656ba1 commit 05b2e49
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
With this release InfluxDB is moving to Go 1.5.

### Features
- [#4065](https://github.com/influxdb/influxdb/pull/4065): Added precision support in cmd client
- [#4050](https://github.com/influxdb/influxdb/pull/4050): Add stats to collectd
- [#3771](https://github.com/influxdb/influxdb/pull/3771): Close idle Graphite TCP connections
- [#3755](https://github.com/influxdb/influxdb/issues/3755): Add option to build script. Thanks @fg2it
Expand Down
11 changes: 11 additions & 0 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Config struct {
Password string
UserAgent string
Timeout time.Duration
Precision string
}

// NewConfig will create a config to be used in connecting to the client
Expand All @@ -95,6 +96,7 @@ type Client struct {
password string
httpClient *http.Client
userAgent string
precision string
}

const (
Expand All @@ -112,6 +114,7 @@ func NewClient(c Config) (*Client, error) {
password: c.Password,
httpClient: &http.Client{Timeout: c.Timeout},
userAgent: c.UserAgent,
precision: c.Precision,
}
if client.userAgent == "" {
client.userAgent = "InfluxDBClient"
Expand All @@ -125,6 +128,11 @@ func (c *Client) SetAuth(u, p string) {
c.password = p
}

// SetPrecision will update the precision
func (c *Client) SetPrecision(precision string) {
c.precision = precision
}

// Query sends a command to the server and returns the Response
func (c *Client) Query(q Query) (*Response, error) {
u := c.url
Expand All @@ -133,6 +141,9 @@ func (c *Client) Query(q Query) (*Response, error) {
values := u.Query()
values.Set("q", q.Command)
values.Set("db", q.Database)
if c.precision != "" {
values.Set("epoch", c.precision)
}
u.RawQuery = values.Encode()

req, err := http.NewRequest("GET", u.String(), nil)
Expand Down
32 changes: 32 additions & 0 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (
const (
// defaultFormat is the default format of the results when issuing queries
defaultFormat = "column"

// defaultPrecision is the default timestamp format of the results when issuing queries
defaultPrecision = "ns"

// defaultPPS is the default points per second that the import will throttle at
// by default it's 0, which means it will not throttle
Expand All @@ -49,6 +52,7 @@ type CommandLine struct {
Version string
Pretty bool // controls pretty print for json
Format string // controls the output format. Valid values are json, csv, or column
Precision string
WriteConsistency string
Execute string
ShowVersion bool
Expand All @@ -69,6 +73,7 @@ func main() {
fs.StringVar(&c.Database, "database", c.Database, "Database to connect to the server.")
fs.BoolVar(&c.Ssl, "ssl", false, "Use https for connecting to cluster.")
fs.StringVar(&c.Format, "format", defaultFormat, "Format specifies the format of the server responses: json, csv, or column.")
fs.StringVar(&c.Precision, "precision", defaultPrecision, "Precision specifies the format of the timestamp: rfc3339,h,m,s,ms,u or ns.")
fs.StringVar(&c.WriteConsistency, "consistency", "any", "Set write consistency level: any, one, quorum, or all.")
fs.BoolVar(&c.Pretty, "pretty", false, "Turns on pretty print for the json format.")
fs.StringVar(&c.Execute, "execute", c.Execute, "Execute command and quit.")
Expand Down Expand Up @@ -99,6 +104,8 @@ func main() {
Execute command and quit.
-format 'json|csv|column'
Format specifies the format of the server responses: json, csv, or column.
-precision 'rfc3339|h|m|s|ms|u|ns'
Precision specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns.
-consistency 'any|one|quorum|all'
Set write consistency level: any, one, quorum, or all
-pretty
Expand Down Expand Up @@ -158,6 +165,8 @@ Examples:
}

if c.Execute != "" {
// Modify precision before executing query
c.SetPrecision(c.Precision)
if err := c.ExecuteQuery(c.Execute); err != nil {
c.Line.Close()
os.Exit(1)
Expand All @@ -184,6 +193,7 @@ Examples:
config.URL = u
config.Compressed = c.Compressed
config.PPS = c.PPS
config.Precision = c.Precision

i := v8.NewImporter(config)
if err := i.Import(); err != nil {
Expand Down Expand Up @@ -249,6 +259,8 @@ func (c *CommandLine) ParseCommand(cmd string) bool {
c.help()
case strings.HasPrefix(lcmd, "format"):
c.SetFormat(cmd)
case strings.HasPrefix(lcmd, "precision"):
c.SetPrecision(cmd)
case strings.HasPrefix(lcmd, "consistency"):
c.SetWriteConsistency(cmd)
case strings.HasPrefix(lcmd, "settings"):
Expand Down Expand Up @@ -295,6 +307,7 @@ func (c *CommandLine) connect(cmd string) error {
config.Username = c.Username
config.Password = c.Password
config.UserAgent = "InfluxDBShell/" + version
config.Precision = c.Precision
cl, err := client.NewClient(config)
if err != nil {
return fmt.Errorf("Could not create client %s", err)
Expand Down Expand Up @@ -351,6 +364,24 @@ func (c *CommandLine) use(cmd string) {
fmt.Printf("Using database %s\n", d)
}

func (c *CommandLine) SetPrecision(cmd string) {
// Remove the "precision" keyword if it exists
cmd = strings.TrimSpace(strings.Replace(cmd, "precision", "", -1))
// normalize cmd
cmd = strings.ToLower(cmd)

switch cmd {
case "h","m","s","ms","u","ns":
c.Precision = cmd
c.Client.SetPrecision(c.Precision)
case "rfc3339":
c.Precision = ""
c.Client.SetPrecision(c.Precision)
default:
fmt.Printf("Unknown precision %q. Please use rfc3339, h, m, s, ms, u or ns.\n", cmd)
}
}

func (c *CommandLine) SetFormat(cmd string) {
// Remove the "format" keyword if it exists
cmd = strings.TrimSpace(strings.Replace(cmd, "format", "", -1))
Expand Down Expand Up @@ -674,6 +705,7 @@ func (c *CommandLine) help() {
pretty toggle pretty print
use <db_name> set current databases
format <format> set the output format: json, csv, or column
precision <format> set the timestamp format: h,m,s,ms,u,ns
consistency <level> set write consistency level: any, one, quorum, or all
settings output the current settings for the shell
exit quit the influx shell
Expand Down

0 comments on commit 05b2e49

Please sign in to comment.