feat: Monitor groups and chart information
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.hostxtra.co.uk/mrhid6/vantage/server/internal/checker"
|
||||
@@ -21,6 +22,22 @@ func monCtx() (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(context.Background(), 5*time.Second)
|
||||
}
|
||||
|
||||
// MaxMonitorGroupLen bounds the display-only group label. It is a heading on
|
||||
// the monitors page, not an identifier, so the cap is about the layout rather
|
||||
// than storage.
|
||||
const MaxMonitorGroupLen = 48
|
||||
|
||||
// normaliseGroup collapses the ways two people write the same group. Grouping
|
||||
// is by exact string, so " Production " and "Production" must not become two
|
||||
// headings.
|
||||
func normaliseGroup(g string) (string, error) {
|
||||
g = strings.Join(strings.Fields(g), " ")
|
||||
if len([]rune(g)) > MaxMonitorGroupLen {
|
||||
return "", fmt.Errorf("group must be %d characters or fewer", MaxMonitorGroupLen)
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func SpecFor(m *models.Monitor) checker.Spec {
|
||||
return checker.Spec{
|
||||
Type: m.Type,
|
||||
@@ -126,6 +143,11 @@ func CreateMonitor(instanceID string, m *models.Monitor) (*models.Monitor, error
|
||||
if err := validateRunner(instanceID, m.Runner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group, err := normaliseGroup(m.Group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.Group = group
|
||||
m.InstanceID = instanceID
|
||||
m.MonitorID = uuid.NewString()
|
||||
m.CreatedAt = time.Now()
|
||||
@@ -158,6 +180,17 @@ func UpdateMonitor(instanceID, monitorID string, upd bson.M) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if raw, present := upd["group"]; present {
|
||||
g, ok := raw.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("group must be a string")
|
||||
}
|
||||
group, err := normaliseGroup(g)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
upd["group"] = group
|
||||
}
|
||||
if raw, present := upd["runner"]; present {
|
||||
runner, ok := raw.(string)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user