monitor a good
Former-commit-id: 5a37bfdfe208d3d1a71e6b4924209d0c8e6f53a0
This commit is contained in:
124
monitor/item.go
Normal file
124
monitor/item.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"local/rssmon3/config"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/golang-collections/go-datastructures/queue"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
const nsLast = "nsLast"
|
||||
const nsInterval = "nsInterval"
|
||||
|
||||
var never = time.Date(2999, time.January, 1, 1, 1, 1, 1, time.UTC)
|
||||
var forever = time.Duration(time.Hour * 99999)
|
||||
|
||||
func NewItem(key string, interval time.Duration) (*Item, error) {
|
||||
i := &Item{
|
||||
Key: key,
|
||||
}
|
||||
|
||||
if err := i.setInterval(interval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := i.setLast(time.Now().Add(-2 * interval)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Item{
|
||||
Key: key,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (i *Item) Compare(other queue.Item) int {
|
||||
j := other.(*Item)
|
||||
iNext := i.Last().Add(i.Interval())
|
||||
jNext := j.Last().Add(j.Interval())
|
||||
if iNext.Before(jNext) {
|
||||
return 1
|
||||
} else if jNext.Before(iNext) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Item) Interval() time.Duration {
|
||||
t, err := i.getInterval()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return forever
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (i *Item) getInterval() (time.Duration, error) {
|
||||
t := time.Duration(0)
|
||||
b, err := config.Values().DB.Get(i.Key, nsInterval)
|
||||
if err != nil {
|
||||
return forever, err
|
||||
}
|
||||
buff := bytes.NewBuffer(b)
|
||||
dec := gob.NewDecoder(buff)
|
||||
err = dec.Decode(&t)
|
||||
return t, err
|
||||
}
|
||||
|
||||
func (i *Item) setInterval(t time.Duration) error {
|
||||
buff := bytes.NewBuffer(nil)
|
||||
enc := gob.NewEncoder(buff)
|
||||
if err := enc.Encode(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := config.Values().DB.Set(i.Key, buff.Bytes(), nsInterval); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Item) Last() time.Time {
|
||||
t, err := i.getLast()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (i *Item) Mark() {
|
||||
if err := i.setLast(time.Now()); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Item) setLast(t time.Time) error {
|
||||
buff := bytes.NewBuffer(nil)
|
||||
enc := gob.NewEncoder(buff)
|
||||
if err := enc.Encode(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := config.Values().DB.Set(i.Key, buff.Bytes(), nsLast); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Item) getLast() (time.Time, error) {
|
||||
t := time.Now()
|
||||
b, err := config.Values().DB.Get(i.Key, nsLast)
|
||||
if err != nil {
|
||||
return never, err
|
||||
}
|
||||
buff := bytes.NewBuffer(b)
|
||||
dec := gob.NewDecoder(buff)
|
||||
if err := dec.Decode(&t); err != nil {
|
||||
return never, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
Reference in New Issue
Block a user