add commands to check /all/ boxes, working towards #4

The current implementation is quite limited, as we need to fetch each box
individually. Also, all notifications are sent to the default recipient.
Both issues could be resolved when integrating directly with the DB.
master
Norwin 5 years ago
parent 5abdd0debb
commit 2c2508c471

@ -6,6 +6,7 @@ import (
func init() { func init() {
checkCmd.AddCommand(checkBoxCmd) checkCmd.AddCommand(checkBoxCmd)
checkCmd.AddCommand(checkAllCmd)
rootCmd.AddCommand(checkCmd) rootCmd.AddCommand(checkCmd)
} }
@ -26,3 +27,12 @@ var checkBoxCmd = &cobra.Command{
return checkAndNotify(args) return checkAndNotify(args)
}, },
} }
var checkAllCmd = &cobra.Command{
Use: "all",
Short: "one-off check on all boxes registered on the opensensemap instance",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return checkAndNotifyAll()
},
}

@ -18,6 +18,7 @@ func init() {
viper.BindPFlags(watchCmd.PersistentFlags()) viper.BindPFlags(watchCmd.PersistentFlags())
watchCmd.AddCommand(watchBoxesCmd) watchCmd.AddCommand(watchBoxesCmd)
watchCmd.AddCommand(watchAllCmd)
rootCmd.AddCommand(watchCmd) rootCmd.AddCommand(watchCmd)
} }
@ -54,3 +55,27 @@ var watchBoxesCmd = &cobra.Command{
} }
}, },
} }
var watchAllCmd = &cobra.Command{
Use: "all",
Short: "watch all boxes registered on the map",
PreRun: func(cmd *cobra.Command, args []string) {
interval := viper.GetDuration("interval") * time.Minute
ticker = time.NewTicker(interval).C
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := checkAndNotifyAll()
if err != nil {
return err
}
for {
<-ticker
err = checkAndNotifyAll()
if err != nil {
// we already did retries, so exiting seems appropriate
return err
}
}
},
}

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -33,6 +34,26 @@ func BoxIdValidator(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func checkAndNotifyAll() error {
log.Info("getting list of boxes...")
// fetch all boxes first & extract their IDs
osem := core.NewOsemClient(viper.GetString("api"))
boxes, err := osem.GetAllBoxes()
if err != nil {
return err
}
boxIDs := make([]string, len(*boxes))
for i, box := range *boxes {
boxIDs[i] = box.Id
}
// then check each box individually. we only pass the ID
// and fetch again, because box metadata is different in
// GetAllBoxes and GetBox..
return checkAndNotify(boxIDs)
}
func checkAndNotify(boxIds []string) error { func checkAndNotify(boxIds []string) error {
boxLocalConfig := map[string]*core.NotifyConfig{} boxLocalConfig := map[string]*core.NotifyConfig{}
for _, boxID := range boxIds { for _, boxID := range boxIds {

@ -33,6 +33,16 @@ func (client *OsemClient) GetBox(boxId string) (*Box, error) {
return box, nil return box, nil
} }
func (client *OsemClient) GetAllBoxes() (*[]Box, error) {
boxes := &[]Box{}
fail := &OsemError{}
client.sling.New().Path("boxes").Receive(boxes, fail)
if fail.Message != "" {
return boxes, errors.New("could not fetch boxes: " + fail.Message)
}
return boxes, nil
}
type NotifyEvent struct { type NotifyEvent struct {
Type string `json:"type"` Type string `json:"type"`
Target string `json:"target"` Target string `json:"target"`

Loading…
Cancel
Save