You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2017/01/13 23:36:06 UTC

[10/29] incubator-trafficcontrol git commit: Add influxdb config object, Flags(), QueryDB(), and Create() methods

Add influxdb config object, Flags(), QueryDB(), and Create() methods


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/3d95afa4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/3d95afa4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/3d95afa4

Branch: refs/heads/master
Commit: 3d95afa4a3de560e293fe85bad26d426e75ebacb
Parents: 9d913ed
Author: sbogacz <sb...@zvelo.com>
Authored: Tue Jan 10 19:42:48 2017 -0700
Committer: David Neuman <da...@gmail.com>
Committed: Fri Jan 13 23:33:56 2017 +0000

----------------------------------------------------------------------
 traffic_stats/influxdb/config.go | 51 +++++++++++++++++++++++++++++++++++
 traffic_stats/influxdb/query.go  | 33 +++++++++++++++++++++++
 2 files changed, 84 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3d95afa4/traffic_stats/influxdb/config.go
----------------------------------------------------------------------
diff --git a/traffic_stats/influxdb/config.go b/traffic_stats/influxdb/config.go
new file mode 100644
index 0000000..376d4f5
--- /dev/null
+++ b/traffic_stats/influxdb/config.go
@@ -0,0 +1,51 @@
+package influxdb
+
+import (
+	"flag"
+	"fmt"
+	"strings"
+
+	influx "github.com/influxdata/influxdb/client/v2"
+	"github.com/pkg/errors"
+)
+
+// Config holds the requisite information to connect to an influx db instance
+// prefix is an optional variable that is set via the Flags method, so as to
+// distinguish between flags for different influxdb instances
+type Config struct {
+	prefix   string
+	User     string
+	Password string
+	URL      string
+}
+
+// Flags configures the cli flags for the config. If more than one config is
+// present for a program, it should be differentiated by a different prefix, so
+// that the flag names don't collide
+func (c *Config) Flags(prefix string) {
+	c.prefix = prefix
+	flag.StringVar(&c.URL, flagName(c.prefix, "url"), "http://localhost:8086", "The influxdb url and port")
+	flag.StringVar(&c.User, flagName(c.prefix, "user"), "", "The influxdb username to connect to the db")
+	flag.StringVar(&c.Password, flagName(c.prefix, "password"), "", "The influxdb password to connect to the db")
+}
+
+// NewHTTPClient tries to use the given configuration to
+func (c *Config) NewHTTPClient() (influx.Client, error) {
+	client, err := influx.NewHTTPClient(influx.HTTPConfig{
+		Addr:     c.URL,
+		Username: c.User,
+		Password: c.Password,
+	})
+	if err != nil {
+		return nil, errors.Wrap(err, "Error creating influx client")
+	}
+	_, _, err = client.Ping(10)
+	return client, errors.Wrap(err, "Error creating influx client")
+}
+
+func flagName(prefix, name string) string {
+	if prefix != "" {
+		prefix = fmt.Sprintf("%s-", prefix)
+	}
+	return strings.ToLower(fmt.Sprintf("%s%s", prefix, name))
+}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3d95afa4/traffic_stats/influxdb/query.go
----------------------------------------------------------------------
diff --git a/traffic_stats/influxdb/query.go b/traffic_stats/influxdb/query.go
new file mode 100644
index 0000000..d1afa76
--- /dev/null
+++ b/traffic_stats/influxdb/query.go
@@ -0,0 +1,33 @@
+package influxdb
+
+import (
+	influx "github.com/influxdata/influxdb/client/v2"
+	"github.com/pkg/errors"
+)
+
+// QueryDB takes an influx client interface, cmd, and db strings. It tries to
+// execute the query on the given client, and returns a slice of influx.Result,
+// and an error, if any were present
+func QueryDB(client influx.Client, cmd, db string) (res []influx.Result, err error) {
+	q := influx.Query{
+		Command:  cmd,
+		Database: db,
+	}
+	response, err := client.Query(q)
+	if err != nil {
+		return res, errors.Wrapf(err, "failed to execute cmd: %s on db %s", cmd, db)
+	}
+	if response.Error() != nil {
+		return res, errors.Wrapf(response.Error(), "got error in query response for cmd: %s on db %s", cmd, db)
+	}
+	res = response.Results
+
+	return res, err
+}
+
+// Create takes an influx.Client, and a create cmd in order to create objects
+// in the influx db (it doesn't require a db argument like QueryDB)
+func Create(client influx.Client, cmd string) (err error) {
+	_, err = QueryDB(client, cmd, "")
+	return err
+}