mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-24 02:13:55 +00:00
Add option for refresh interval
This commit is contained in:
parent
47d5027807
commit
c6c3a0c452
35
config.go
35
config.go
|
@ -13,6 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
envVarListenAddress = "NETATMO_EXPORTER_ADDR"
|
envVarListenAddress = "NETATMO_EXPORTER_ADDR"
|
||||||
envVarLogLevel = "NETATMO_LOG_LEVEL"
|
envVarLogLevel = "NETATMO_LOG_LEVEL"
|
||||||
|
envVarRefreshInterval = "NETATMO_REFRESH_INTERVAL"
|
||||||
envVarStaleDuration = "NETATMO_AGE_STALE"
|
envVarStaleDuration = "NETATMO_AGE_STALE"
|
||||||
envVarNetatmoClientID = "NETATMO_CLIENT_ID"
|
envVarNetatmoClientID = "NETATMO_CLIENT_ID"
|
||||||
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
|
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
|
||||||
|
@ -21,20 +22,23 @@ const (
|
||||||
|
|
||||||
flagListenAddress = "addr"
|
flagListenAddress = "addr"
|
||||||
flagLogLevel = "log-level"
|
flagLogLevel = "log-level"
|
||||||
|
flagRefreshInterval = "refresh-interval"
|
||||||
flagStaleDuration = "age-stale"
|
flagStaleDuration = "age-stale"
|
||||||
flagNetatmoClientID = "client-id"
|
flagNetatmoClientID = "client-id"
|
||||||
flagNetatmoClientSecret = "client-secret"
|
flagNetatmoClientSecret = "client-secret"
|
||||||
flagNetatmoUsername = "username"
|
flagNetatmoUsername = "username"
|
||||||
flagNetatmoPassword = "password"
|
flagNetatmoPassword = "password"
|
||||||
|
|
||||||
defaultStaleDuration = 30 * time.Minute
|
defaultRefreshInterval = 8 * time.Minute
|
||||||
|
defaultStaleDuration = 30 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultConfig = config{
|
defaultConfig = config{
|
||||||
Addr: ":9210",
|
Addr: ":9210",
|
||||||
LogLevel: logLevel(logrus.InfoLevel),
|
LogLevel: logLevel(logrus.InfoLevel),
|
||||||
StaleDuration: defaultStaleDuration,
|
RefreshInterval: defaultRefreshInterval,
|
||||||
|
StaleDuration: defaultStaleDuration,
|
||||||
}
|
}
|
||||||
|
|
||||||
errNoBinaryName = errors.New("need the binary name as first argument")
|
errNoBinaryName = errors.New("need the binary name as first argument")
|
||||||
|
@ -66,10 +70,11 @@ func (l *logLevel) Set(value string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Addr string
|
Addr string
|
||||||
LogLevel logLevel
|
LogLevel logLevel
|
||||||
StaleDuration time.Duration
|
RefreshInterval time.Duration
|
||||||
Netatmo netatmo.Config
|
StaleDuration time.Duration
|
||||||
|
Netatmo netatmo.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(args []string, getenv func(string) string) (config, error) {
|
func parseConfig(args []string, getenv func(string) string) (config, error) {
|
||||||
|
@ -82,6 +87,7 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
|
||||||
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
|
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
|
||||||
flagSet.StringVarP(&cfg.Addr, flagListenAddress, "a", cfg.Addr, "Address to listen on.")
|
flagSet.StringVarP(&cfg.Addr, flagListenAddress, "a", cfg.Addr, "Address to listen on.")
|
||||||
flagSet.Var(&cfg.LogLevel, flagLogLevel, "Sets the minimum level output through logging.")
|
flagSet.Var(&cfg.LogLevel, flagLogLevel, "Sets the minimum level output through logging.")
|
||||||
|
flagSet.DurationVar(&cfg.RefreshInterval, flagRefreshInterval, cfg.RefreshInterval, "Time interval used for internal caching of NetAtmo sensor data.")
|
||||||
flagSet.DurationVar(&cfg.StaleDuration, flagStaleDuration, cfg.StaleDuration, "Data age to consider as stale. Stale data does not create metrics anymore.")
|
flagSet.DurationVar(&cfg.StaleDuration, flagStaleDuration, cfg.StaleDuration, "Data age to consider as stale. Stale data does not create metrics anymore.")
|
||||||
flagSet.StringVarP(&cfg.Netatmo.ClientID, flagNetatmoClientID, "i", cfg.Netatmo.ClientID, "Client ID for NetAtmo app.")
|
flagSet.StringVarP(&cfg.Netatmo.ClientID, flagNetatmoClientID, "i", cfg.Netatmo.ClientID, "Client ID for NetAtmo app.")
|
||||||
flagSet.StringVarP(&cfg.Netatmo.ClientSecret, flagNetatmoClientSecret, "s", cfg.Netatmo.ClientSecret, "Client secret for NetAtmo app.")
|
flagSet.StringVarP(&cfg.Netatmo.ClientSecret, flagNetatmoClientSecret, "s", cfg.Netatmo.ClientSecret, "Client secret for NetAtmo app.")
|
||||||
|
@ -113,6 +119,10 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
|
||||||
return config{}, errNoNetatmoPassword
|
return config{}, errNoNetatmoPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.StaleDuration < cfg.RefreshInterval {
|
||||||
|
return config{}, fmt.Errorf("stale duration smaller than refresh interval: %s < %s", cfg.StaleDuration, cfg.RefreshInterval)
|
||||||
|
}
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +137,15 @@ func applyEnvironment(cfg *config, getenv func(string) string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if envRefreshInterval := getenv(envVarRefreshInterval); envRefreshInterval != "" {
|
||||||
|
duration, err := time.ParseDuration(envRefreshInterval)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.RefreshInterval = duration
|
||||||
|
}
|
||||||
|
|
||||||
if envStaleDuration := getenv(envVarStaleDuration); envStaleDuration != "" {
|
if envStaleDuration := getenv(envVarStaleDuration); envStaleDuration != "" {
|
||||||
duration, err := time.ParseDuration(envStaleDuration)
|
duration, err := time.ParseDuration(envStaleDuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -39,9 +39,10 @@ func TestParseConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
env: map[string]string{},
|
env: map[string]string{},
|
||||||
wantConfig: config{
|
wantConfig: config{
|
||||||
Addr: defaultConfig.Addr,
|
Addr: defaultConfig.Addr,
|
||||||
LogLevel: logLevel(logrus.InfoLevel),
|
LogLevel: logLevel(logrus.InfoLevel),
|
||||||
StaleDuration: defaultStaleDuration,
|
RefreshInterval: defaultRefreshInterval,
|
||||||
|
StaleDuration: defaultStaleDuration,
|
||||||
Netatmo: netatmo.Config{
|
Netatmo: netatmo.Config{
|
||||||
ClientID: "id",
|
ClientID: "id",
|
||||||
ClientSecret: "secret",
|
ClientSecret: "secret",
|
||||||
|
@ -59,6 +60,7 @@ func TestParseConfig(t *testing.T) {
|
||||||
env: map[string]string{
|
env: map[string]string{
|
||||||
envVarListenAddress: ":8080",
|
envVarListenAddress: ":8080",
|
||||||
envVarLogLevel: "debug",
|
envVarLogLevel: "debug",
|
||||||
|
envVarRefreshInterval: "5m",
|
||||||
envVarStaleDuration: "10m",
|
envVarStaleDuration: "10m",
|
||||||
envVarNetatmoClientID: "id",
|
envVarNetatmoClientID: "id",
|
||||||
envVarNetatmoClientSecret: "secret",
|
envVarNetatmoClientSecret: "secret",
|
||||||
|
@ -66,9 +68,10 @@ func TestParseConfig(t *testing.T) {
|
||||||
envVarNetatmoPassword: "password",
|
envVarNetatmoPassword: "password",
|
||||||
},
|
},
|
||||||
wantConfig: config{
|
wantConfig: config{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
LogLevel: logLevel(logrus.DebugLevel),
|
LogLevel: logLevel(logrus.DebugLevel),
|
||||||
StaleDuration: 10 * time.Minute,
|
RefreshInterval: 5 * time.Minute,
|
||||||
|
StaleDuration: 10 * time.Minute,
|
||||||
Netatmo: netatmo.Config{
|
Netatmo: netatmo.Config{
|
||||||
ClientID: "id",
|
ClientID: "id",
|
||||||
ClientSecret: "secret",
|
ClientSecret: "secret",
|
||||||
|
|
Loading…
Reference in a new issue