-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
database: replace badger with redis (#28)
- Loading branch information
Showing
16 changed files
with
342 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,85 @@ | ||
package memory | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ushis/gesundheit/db" | ||
"github.com/ushis/gesundheit/db/badger" | ||
"github.com/ushis/gesundheit/result" | ||
) | ||
|
||
func init() { | ||
db.Register("memory", New) | ||
} | ||
|
||
type Database struct { | ||
db map[string]map[string]result.Event | ||
} | ||
|
||
func New(_ func(interface{}) error) (db.Database, error) { | ||
return badger.New(badger.Opts{Persistent: false, Path: ""}) | ||
return Database{make(map[string]map[string]result.Event)}, nil | ||
} | ||
|
||
func (db Database) Close() error { | ||
return nil | ||
} | ||
|
||
func (db Database) Handle(e result.Event) error { | ||
if checks, ok := db.db[e.NodeName]; ok { | ||
checks[e.CheckId] = e | ||
} else { | ||
checks = make(map[string]result.Event) | ||
checks[e.CheckId] = e | ||
db.db[e.NodeName] = checks | ||
} | ||
return nil | ||
} | ||
|
||
func (db Database) GetEvents() ([]result.Event, error) { | ||
events := []result.Event{} | ||
|
||
for _, checks := range db.db { | ||
for _, event := range checks { | ||
if !isExpired(event) { | ||
events = append(events, event) | ||
} | ||
} | ||
} | ||
return events, nil | ||
} | ||
|
||
func (db Database) GetEventsByNode(name string) ([]result.Event, error) { | ||
checks, ok := db.db[name] | ||
|
||
if !ok { | ||
return []result.Event{}, nil | ||
} | ||
events := []result.Event{} | ||
|
||
for _, event := range checks { | ||
if !isExpired(event) { | ||
events = append(events, event) | ||
} | ||
} | ||
return events, nil | ||
} | ||
|
||
func (db Database) GetLatestEventByNode(name string) (event result.Event, ok bool, err error) { | ||
events, _ := db.GetEventsByNode(name) | ||
|
||
if len(events) == 0 { | ||
return event, false, nil | ||
} | ||
event = events[0] | ||
|
||
for _, e := range events[1:] { | ||
if e.Timestamp.After(event.Timestamp) { | ||
event = e | ||
} | ||
} | ||
return event, true, nil | ||
} | ||
|
||
func isExpired(e result.Event) bool { | ||
ttl := time.Duration(e.CheckInterval) * time.Second * 2 | ||
return time.Since(e.Timestamp) > ttl | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.