Convert monitor to http server

This commit is contained in:
Bel LaPointe
2018-10-08 19:34:34 -06:00
parent 1c3ff9a8d2
commit 4946e53b57
4 changed files with 176 additions and 28 deletions

View File

@@ -1,21 +1,48 @@
package monitor
import (
"encoding/json"
"errors"
"time"
"github.com/golang-collections/go-datastructures/queue"
)
type Duration struct {
time.Duration
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
type Item struct {
url string
interval time.Duration
URL string
Interval Duration
next time.Time
}
func NewItem(url string, interval time.Duration) *Item {
func NewItem(URL string, Interval time.Duration) *Item {
return &Item{
url: url,
interval: interval,
URL: URL,
Interval: Duration{Interval},
next: time.Now(),
}
}
@@ -30,15 +57,15 @@ func (item *Item) Compare(other queue.Item) int {
} else if item.next.After(j.next) {
return 1
}
if item.interval < j.interval {
if item.Interval.Duration < j.Interval.Duration {
return -1
} else if item.interval > j.interval {
} else if item.Interval.Duration > j.Interval.Duration {
return 1
}
return 0
}
func (item *Item) increment() *Item {
item.next = time.Now().Add(item.interval)
item.next = time.Now().Add(item.Interval.Duration)
return item
}