fix(influx): support days as duration type

closes: #18639
pull/18659/head
Johnny Steenbergen 2020-06-22 19:22:03 -07:00 committed by Johnny Steenbergen
parent 5db8e2fd5e
commit 3c14db1c77
6 changed files with 86 additions and 25 deletions

View File

@ -22,6 +22,7 @@
1. [18602](https://github.com/influxdata/influxdb/pull/18602): Fix uint overflow during setup on 32bit systems
1. [18623](https://github.com/influxdata/influxdb/pull/18623): Drop support for --local flag within influx CLI
1. [18632](https://github.com/influxdata/influxdb/pull/18632): Prevents undefined queries in cells from erroring out in dashboards
1. [18658](https://github.com/influxdata/influxdb/pull/18658): Add support for 'd' day time identifier in the CLI for bucket and setup commands
## v2.0.0-beta.12 [2020-06-12]

View File

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/http"
@ -30,7 +29,7 @@ type cmdBucketBuilder struct {
name string
description string
org organization
retention time.Duration
retention string
}
func newCmdBucketBuilder(svcsFn bucketSVCsFn, opts genericCLIOpts) *cmdBucketBuilder {
@ -72,7 +71,7 @@ func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
opts.mustRegister(cmd)
cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.Flags().DurationVarP(&b.retention, "retention", "r", 0, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
b.org.register(cmd, false)
b.registerPrintFlags(cmd)
@ -89,10 +88,15 @@ func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
return err
}
dur, err := rawDurationToTimeDuration(b.retention)
if err != nil {
return err
}
bkt := &influxdb.Bucket{
Name: b.name,
Description: b.description,
RetentionPeriod: b.retention,
RetentionPeriod: dur,
}
bkt.OrgID, err = b.org.getID(orgSVC)
if err != nil {
@ -245,7 +249,7 @@ func (b *cmdBucketBuilder) cmdUpdate() *cobra.Command {
cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID (required)")
cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.MarkFlagRequired("id")
cmd.Flags().DurationVarP(&b.retention, "retention", "r", 0, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
return cmd
}
@ -268,8 +272,13 @@ func (b *cmdBucketBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er
if b.description != "" {
update.Description = &b.description
}
if b.retention != 0 {
update.RetentionPeriod = &b.retention
dur, err := rawDurationToTimeDuration(b.retention)
if err != nil {
return err
}
if dur != 0 {
update.RetentionPeriod = &dur
}
bkt, err := bktSVC.UpdateBucket(context.Background(), id, update)

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
@ -20,6 +21,7 @@ import (
"github.com/influxdata/influxdb/v2/internal/fs"
"github.com/influxdata/influxdb/v2/kit/cli"
"github.com/influxdata/influxdb/v2/pkg/httpc"
"github.com/influxdata/influxdb/v2/task/options"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -535,3 +537,45 @@ func newBucketService() (influxdb.BucketService, error) {
Client: client,
}, nil
}
func rawDurationToTimeDuration(raw string) (time.Duration, error) {
if raw == "" {
return 0, nil
}
if dur, err := time.ParseDuration(raw); err == nil {
return dur, nil
}
retention, err := options.ParseSignedDuration(raw)
if err != nil {
return 0, err
}
const day = 24 * time.Hour
var dur time.Duration
for _, d := range retention.Values {
if d.Magnitude < 0 {
return 0, errors.New("must be greater than 0")
}
mag := time.Duration(d.Magnitude)
switch d.Unit {
case "d":
dur += mag * (day)
case "m":
dur += mag * time.Minute
case "s":
dur += mag * time.Second
case "ms":
dur += mag * time.Minute
case "us":
dur += mag * time.Microsecond
case "ns":
dur += mag * time.Nanosecond
default:
return 0, errors.New("duration must be day(d), hour(h), min(m), sec(s), millisec(ms), microsec(us), or nanosec(ns)")
}
}
return dur, nil
}

View File

@ -25,7 +25,7 @@ var setupFlags struct {
name string
org string
password string
retention time.Duration
retention string
token string
username string
}
@ -41,7 +41,7 @@ func cmdSetup(f *globalFlags, opt genericCLIOpts) *cobra.Command {
cmd.Flags().StringVarP(&setupFlags.org, "org", "o", "", "primary organization name")
cmd.Flags().StringVarP(&setupFlags.bucket, "bucket", "b", "", "primary bucket name")
cmd.Flags().StringVarP(&setupFlags.name, "name", "n", "", "config name, only required if you already have existing configs")
cmd.Flags().DurationVarP(&setupFlags.retention, "retention", "r", -1, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&setupFlags.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().BoolVarP(&setupFlags.force, "force", "f", false, "skip confirmation prompt")
registerPrintOptions(cmd, &setupFlags.hideHeaders, &setupFlags.json)
@ -62,7 +62,7 @@ func cmdSetupUser(opt genericCLIOpts) *cobra.Command {
cmd.Flags().StringVarP(&setupFlags.org, "org", "o", "", "primary organization name")
cmd.Flags().StringVarP(&setupFlags.bucket, "bucket", "b", "", "primary bucket name")
cmd.Flags().StringVarP(&setupFlags.name, "name", "n", "", "config name, only required if you already have existing configs")
cmd.Flags().DurationVarP(&setupFlags.retention, "retention", "r", -1, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&setupFlags.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().BoolVarP(&setupFlags.force, "force", "f", false, "skip confirmation prompt")
registerPrintOptions(cmd, &setupFlags.hideHeaders, &setupFlags.json)
@ -220,18 +220,20 @@ func onboardingRequest() (*influxdb.OnboardingRequest, error) {
func nonInteractive() (*influxdb.OnboardingRequest, error) {
req := &influxdb.OnboardingRequest{
User: setupFlags.username,
Password: setupFlags.password,
Token: setupFlags.token,
Org: setupFlags.org,
Bucket: setupFlags.bucket,
// TODO: this manipulation is required by the API, something that
// we should fixup to be a duration instead
RetentionPeriod: uint(setupFlags.retention / time.Hour),
User: setupFlags.username,
Password: setupFlags.password,
Token: setupFlags.token,
Org: setupFlags.org,
Bucket: setupFlags.bucket,
RetentionPeriod: influxdb.InfiniteRetention,
}
if setupFlags.retention < 0 {
req.RetentionPeriod = influxdb.InfiniteRetention
dur, err := rawDurationToTimeDuration(setupFlags.retention)
if err != nil {
return nil, err
}
if dur > 0 {
req.RetentionPeriod = uint(dur / time.Hour)
}
return req, nil
}
@ -267,8 +269,14 @@ func interactive() (req *influxdb.OnboardingRequest, err error) {
} else {
req.Bucket = getInput(ui, "Please type your primary bucket name", "")
}
if setupFlags.retention >= 0 {
req.RetentionPeriod = uint(setupFlags.retention)
dur, err := rawDurationToTimeDuration(setupFlags.retention)
if err != nil {
return nil, err
}
if dur > 0 {
req.RetentionPeriod = uint(dur / time.Hour)
} else {
for {
rpStr := getInput(ui, "Please type your retention period in hours.\r\nOr press ENTER for infinite.", strconv.Itoa(influxdb.InfiniteRetention))

View File

@ -79,7 +79,7 @@ func (a *Duration) UnmarshalText(text []byte) error {
return nil
}
// UnmarshalText marshals text into a Duration.
// MarshalText marshals text into a Duration.
func (a Duration) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

View File

@ -26,8 +26,7 @@ func ParseSignedDuration(text string) (*ast.DurationLiteral, error) {
// TODO(jsternberg): This is copied from an internal package in flux to break a dependency
// on the parser package where this method is exposed.
// Consider exposing this properly in flux.
r, s := utf8.DecodeRuneInString(text)
if r == '-' {
if r, s := utf8.DecodeRuneInString(text); r == '-' {
d, err := parseDuration(text[s:])
if err != nil {
return nil, err