Skip to content

Commit

Permalink
database: replace badger with redis (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ushis authored Mar 5, 2022
1 parent 3c64c8b commit 99566fe
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 342 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ jobs:
sudo install -Dm755 bin/gesundheit gesundheit/usr/bin/gesundheit
sudo install -Dm644 src/pkg/gesundheit.service gesundheit/usr/lib/systemd/system/gesundheit.service
sudo install -Dm644 src/pkg/gesundheit.sysusers gesundheit/usr/lib/sysusers.d/gesundheit.conf
sudo install -Dm644 src/pkg/gesundheit.tmpfiles gesundheit/usr/lib/tmpfiles.d/gesundheit.conf
sudo install -Dm644 src/pkg/gesundheit.toml gesundheit/etc/gesundheit/gesundheit.toml
sudo install -dm755 gesundheit/etc/gesundheit/modules.d
dpkg-deb --build gesundheit "gesundheit_${GITHUB_REF_NAME}_${{ matrix.arch }}.deb"
Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,49 @@ gesundheit -conf /etc/gesundheit/gesundheit.toml
</tr>
</tbody>
</table>

### Databases

<table>
<thead>
<tr>
<th>Module</th>
<th>Description</th>
<th>Config</th>
<th>Config Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>memory</strong></td>
<td>
In memory database suitable for simple setups without
persistence requirements
</td>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="4"><strong>redis</strong></td>
<td rowspan="4"><a href="https://redis.io/">Redis</a> adapter</td>
<td>Address</td>
<td>Address of the redis server, e.g. <code>127.0.0.1:6379</td>
</tr>
<tr>
<td>DB</td>
<td>Redis database to use, e.g. <code>0</code></td>
</tr>
<tr>
<td>Username</td>
<td>
Redis username, e.g. <code>"gesundheit"</code>
</td>
</tr>
<tr>
<td>Password</td>
<td>
Redis password, e.g. <code>"secret"</code>
</td>
</tr>
</tbody>
</table>
2 changes: 2 additions & 0 deletions check/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"time"

"github.com/google/uuid"
"github.com/ushis/gesundheit/node"
"github.com/ushis/gesundheit/result"
)
Expand Down Expand Up @@ -65,6 +66,7 @@ func (r Runner) run(ctx context.Context, events chan<- result.Event) {
CheckDescription: r.description,
CheckInterval: checkInterval,
StatusHistory: statusHistory,
Id: uuid.New().String(),
Status: res.Status,
Message: res.Message,
Timestamp: time.Now(),
Expand Down
146 changes: 0 additions & 146 deletions db/badger/database.go

This file was deleted.

75 changes: 73 additions & 2 deletions db/memory/database.go
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
}
23 changes: 0 additions & 23 deletions db/persistent/database.go

This file was deleted.

Loading

0 comments on commit 99566fe

Please sign in to comment.