Commit e52a9642 authored by Mike Jones's avatar Mike Jones 馃尪
Browse files

Connect to InfluxDB, stubs for plotting

parent 890828a6
package main
import (
"log"
"github.com/davecgh/go-spew/spew"
"git.netsplit.uk/stella-irc/modstats/internal/app/modstats/util"
)
func main() {
config := util.NewConfig()
influxdb := config.NewInfluxDB()
spew.Dump(influxdb)
log.Println("Connected to InfluxDB")
}
package util
import (
"log"
"os"
"strings"
)
const (
influxAddrKey = "INFLUXDB_ADDRESS"
influxUserKey = "INFLUXDB_USERNAME"
influxPassKey = "INFLUXDB_PASSWORD"
)
type Config struct {
InfluxDB struct {
Address string
Username string
Password string
}
}
// Check() validates the config has all required values
func (c *Config) Check() bool {
return c.InfluxDB.Address != "" &&
c.InfluxDB.Username != "" &&
c.InfluxDB.Password != ""
}
// NewConfig() creates a new Config{} from environment variables
func NewConfig() *Config {
c := &Config{}
c.InfluxDB.Address = os.Getenv(influxAddrKey)
c.InfluxDB.Password = os.Getenv(influxPassKey)
c.InfluxDB.Username = os.Getenv(influxUserKey)
if !c.Check() {
log.Fatalf("A required environment variable is missing. Required: %s.",
strings.Join([]string{
influxAddrKey,
influxPassKey,
influxUserKey,
}, ", "),
)
}
return c
}
package util
import (
"log"
influxdb "github.com/influxdata/influxdb/client/v2"
)
const defaultCount = 1
// For our use case, all data structures will look the same: an IRC nickname and
// the table to insert into
type Data struct {
Count int32
Table string
Nickname string
}
// NewInfluxDB() attempts to connect to a remote InfluxDB server
func (c *Config) NewInfluxDB() *influxdb.Client {
idb, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
Addr: c.InfluxDB.Address,
Username: c.InfluxDB.Username,
Password: c.InfluxDB.Password,
})
if err != nil {
log.Fatalf("Could not connect InfluxDB client: %s", err.Error())
}
defer idb.Close()
return &idb
}
func (d *Data) Plot() {}
// Sending ++ or -- will use the default count (of 1). Specifying a count in
// Data{} will use that instead.
func (d *Data) GetCount() int32 {
if d.Count != 0 {
return d.Count
}
return defaultCount
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment