Convert monitor to http server
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user