allow filtering via box attributes in check|watch all
This commit is contained in:
parent
83fb1e7d76
commit
17c80cecab
4 changed files with 64 additions and 8 deletions
|
@ -5,6 +5,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
checkAllCmd.PersistentFlags().StringVarP(&date, "date", "", "", "filter boxes by date AND phenomenon")
|
||||||
|
checkAllCmd.PersistentFlags().StringVarP(&exposure, "exposure", "", "", "filter boxes by exposure")
|
||||||
|
checkAllCmd.PersistentFlags().StringVarP(&grouptag, "grouptag", "", "", "filter boxes by grouptag")
|
||||||
|
checkAllCmd.PersistentFlags().StringVarP(&model, "model", "", "", "filter boxes by model")
|
||||||
|
checkAllCmd.PersistentFlags().StringVarP(&phenomenon, "phenomenon", "", "", "filter boxes by phenomenon AND date")
|
||||||
checkCmd.AddCommand(checkBoxCmd)
|
checkCmd.AddCommand(checkBoxCmd)
|
||||||
checkCmd.AddCommand(checkAllCmd)
|
checkCmd.AddCommand(checkAllCmd)
|
||||||
rootCmd.AddCommand(checkCmd)
|
rootCmd.AddCommand(checkCmd)
|
||||||
|
@ -33,6 +38,9 @@ var checkAllCmd = &cobra.Command{
|
||||||
Short: "one-off check on all boxes registered on the opensensemap instance",
|
Short: "one-off check on all boxes registered on the opensensemap instance",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
return checkAndNotifyAll()
|
|
||||||
|
// no flag validation, as the API already does a good job at that
|
||||||
|
|
||||||
|
return checkAndNotifyAll(parseBoxFilters())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,11 @@ func init() {
|
||||||
watchInterval int
|
watchInterval int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
watchAllCmd.PersistentFlags().StringVarP(&date, "date", "", "", "filter boxes by date")
|
||||||
|
watchAllCmd.PersistentFlags().StringVarP(&exposure, "exposure", "", "", "filter boxes by exposure")
|
||||||
|
watchAllCmd.PersistentFlags().StringVarP(&grouptag, "grouptag", "", "", "filter boxes by grouptag")
|
||||||
|
watchAllCmd.PersistentFlags().StringVarP(&model, "model", "", "", "filter boxes by model")
|
||||||
|
watchAllCmd.PersistentFlags().StringVarP(&phenomenon, "phenomenon", "", "", "filter boxes by phenomenon")
|
||||||
watchCmd.PersistentFlags().IntVarP(&watchInterval, "interval", "i", 30, "interval to run checks in minutes")
|
watchCmd.PersistentFlags().IntVarP(&watchInterval, "interval", "i", 30, "interval to run checks in minutes")
|
||||||
viper.BindPFlags(watchCmd.PersistentFlags())
|
viper.BindPFlags(watchCmd.PersistentFlags())
|
||||||
|
|
||||||
|
@ -65,13 +70,14 @@ var watchAllCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
err := checkAndNotifyAll()
|
filters := parseBoxFilters()
|
||||||
|
err := checkAndNotifyAll(filters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
<-ticker
|
<-ticker
|
||||||
err = checkAndNotifyAll()
|
err = checkAndNotifyAll(filters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// we already did retries, so exiting seems appropriate
|
// we already did retries, so exiting seems appropriate
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -34,12 +34,12 @@ func BoxIdValidator(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAndNotifyAll() error {
|
func checkAndNotifyAll(filters core.BoxFilters) error {
|
||||||
log.Info("getting list of boxes...")
|
log.Info("getting list of boxes...")
|
||||||
|
|
||||||
// fetch all boxes first & extract their IDs
|
// fetch all boxes first & extract their IDs
|
||||||
osem := core.NewOsemClient(viper.GetString("api"))
|
osem := core.NewOsemClient(viper.GetString("api"))
|
||||||
boxes, err := osem.GetAllBoxes()
|
boxes, err := osem.GetAllBoxes(filters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -91,3 +91,32 @@ func checkAndNotify(boxIds []string) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ( // values are set during cli flag parsing of checkAllCmd & watchAllCmd
|
||||||
|
date string
|
||||||
|
exposure string
|
||||||
|
grouptag string
|
||||||
|
model string
|
||||||
|
phenomenon string
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseBoxFilters () core.BoxFilters {
|
||||||
|
filters := core.BoxFilters{}
|
||||||
|
if date != "" {
|
||||||
|
filters.Date = date // TODO: parse date & format as ISO date?
|
||||||
|
}
|
||||||
|
if exposure != "" {
|
||||||
|
filters.Exposure = exposure
|
||||||
|
}
|
||||||
|
if grouptag != "" {
|
||||||
|
filters.Grouptag = grouptag
|
||||||
|
}
|
||||||
|
if model != "" {
|
||||||
|
filters.Model = model
|
||||||
|
}
|
||||||
|
if phenomenon != "" {
|
||||||
|
filters.Phenomenon = phenomenon
|
||||||
|
}
|
||||||
|
|
||||||
|
return filters
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,14 @@ type OsemError struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BoxFilters struct {
|
||||||
|
Date string `url:"date,omitempty"`
|
||||||
|
Exposure string `url:"exposure,omitempty"`
|
||||||
|
Grouptag string `url:"grouptag,omitempty"`
|
||||||
|
Model string `url:"model,omitempty"`
|
||||||
|
Phenomenon string `url:"phenomenon,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type OsemClient struct {
|
type OsemClient struct {
|
||||||
sling *sling.Sling
|
sling *sling.Sling
|
||||||
}
|
}
|
||||||
|
@ -36,10 +44,10 @@ func (client *OsemClient) GetBox(boxId string) (*Box, error) {
|
||||||
return box, nil
|
return box, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *OsemClient) GetAllBoxes() (*[]Box, error) {
|
func (client *OsemClient) GetAllBoxes(params BoxFilters) (*[]BoxMinimal, error) {
|
||||||
boxes := &[]Box{}
|
boxes := &[]BoxMinimal{}
|
||||||
fail := &OsemError{}
|
fail := &OsemError{}
|
||||||
_, err := client.sling.New().Path("boxes").Receive(boxes, fail)
|
_, err := client.sling.New().Path("boxes?minimal=true").QueryStruct(params).Receive(boxes, fail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -81,3 +89,8 @@ type Box struct {
|
||||||
Sensors []Sensor `json:"sensors"`
|
Sensors []Sensor `json:"sensors"`
|
||||||
NotifyConf *NotifyConfig `json:"healthcheck"`
|
NotifyConf *NotifyConfig `json:"healthcheck"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BoxMinimal struct {
|
||||||
|
Id string `json:"_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue