Skip to content

Commit

Permalink
Merge pull request #5 from mentos1386/target-instead-of-checks
Browse files Browse the repository at this point in the history
Target instead of checks
  • Loading branch information
mentos1386 authored May 25, 2024
2 parents 73835b5 + da53c04 commit 91fddaa
Show file tree
Hide file tree
Showing 83 changed files with 2,224 additions and 1,056 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Golang selfhosted Status/Healthcheck monitoring app.

Mostly just a project to test [temporal.io](https://temporal.io/).

### Roadmap
- [x] SSO Support for authentication.
- [x] SQLite for database.
Expand Down Expand Up @@ -37,9 +35,12 @@ Demo is available at https://zdravko.mnts.dev.

```sh
# Configure
# You will need to configure an SSO provider
# This can be github for example.
cp example.env .env

# Generate JWT key
# Copy the values to your .env
just generate-jwt-key

# Start development environment
Expand Down
8 changes: 4 additions & 4 deletions cmd/zdravko/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"sync"
"syscall"

"code.tjo.space/mentos1386/zdravko/internal/config"
"code.tjo.space/mentos1386/zdravko/pkg/server"
"code.tjo.space/mentos1386/zdravko/pkg/temporal"
"code.tjo.space/mentos1386/zdravko/pkg/worker"
"github.com/mentos1386/zdravko/internal/config"
"github.com/mentos1386/zdravko/pkg/server"
"github.com/mentos1386/zdravko/pkg/temporal"
"github.com/mentos1386/zdravko/pkg/worker"
)

type StartableAndStoppable interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/kv/badger.go → database/badger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kv
package database

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion internal/kv/kv.go → database/kv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kv
package database

import "time"

Expand Down
100 changes: 46 additions & 54 deletions database/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ type OAuth2State struct {
ExpiresAt *Time `db:"expires_at"`
}

type CheckStatus string

const (
CheckStatusSuccess CheckStatus = "SUCCESS"
CheckStatusFailure CheckStatus = "FAILURE"
CheckStatusError CheckStatus = "ERROR"
CheckStatusUnknown CheckStatus = "UNKNOWN"
)

type CheckState string

const (
Expand All @@ -62,45 +53,24 @@ const (
CheckStateUnknown CheckState = "UNKNOWN"
)

type CheckVisibility string

const (
CheckVisibilityPublic CheckVisibility = "PUBLIC"
CheckVisibilityPrivate CheckVisibility = "PRIVATE"
CheckVisibilityUnknown CheckVisibility = "UNKNOWN"
)

type Check struct {
CreatedAt *Time `db:"created_at"`
UpdatedAt *Time `db:"updated_at"`

Id string `db:"id"`
Name string `db:"name"`
Group string `db:"group"`
Visibility CheckVisibility `db:"visibility"`
Id string `db:"id"`
Name string `db:"name"`

Schedule string `db:"schedule"`
Script string `db:"script"`
Filter string `db:"filter"`
}

type CheckWithWorkerGroups struct {
Check

// List of worker group names
WorkerGroups []string
}

type CheckHistory struct {
CreatedAt *Time `db:"created_at"`

CheckId string `db:"check_id"`
Status CheckStatus `db:"status"`
Note string `db:"note"`

WorkerGroupId string `db:"worker_group_id"`
WorkerGroupName string `db:"worker_group_name"`
}

type WorkerGroup struct {
CreatedAt *Time `db:"created_at"`
UpdatedAt *Time `db:"updated_at"`
Expand All @@ -116,15 +86,6 @@ type WorkerGroupWithChecks struct {
Checks []string
}

type TriggerStatus string

const (
TriggerStatusSuccess TriggerStatus = "SUCCESS"
TriggerStatusFailure TriggerStatus = "FAILURE"
TriggerStatusError TriggerStatus = "ERROR"
TriggerStatusUnknown TriggerStatus = "UNKNOWN"
)

type TriggerState string

const (
Expand All @@ -133,14 +94,6 @@ const (
TriggerStateUnknown TriggerState = "UNKNOWN"
)

type TriggerVisibility string

const (
TriggerVisibilityPublic TriggerVisibility = "PUBLIC"
TriggerVisibilityPrivate TriggerVisibility = "PRIVATE"
TriggerVisibilityUnknown TriggerVisibility = "UNKNOWN"
)

type Trigger struct {
CreatedAt *Time `db:"created_at"`
UpdatedAt *Time `db:"updated_at"`
Expand All @@ -150,10 +103,49 @@ type Trigger struct {
Script string `db:"script"`
}

type TriggerHistory struct {
type TargetVisibility string

const (
TargetVisibilityPublic TargetVisibility = "PUBLIC"
TargetVisibilityPrivate TargetVisibility = "PRIVATE"
TargetVisibilityUnknown TargetVisibility = "UNKNOWN"
)

type TargetState string

const (
TargetStateActive TargetState = "ACTIVE"
TargetStatePaused TargetState = "PAUSED"
TargetStateUnknown TargetState = "UNKNOWN"
)

type Target struct {
CreatedAt *Time `db:"created_at"`
UpdatedAt *Time `db:"updated_at"`

Id string `db:"id"`
Name string `db:"name"`
Group string `db:"group"`
Visibility TargetVisibility `db:"visibility"`
State TargetState `db:"state"`
Metadata string `db:"metadata"`
}

type TargetStatus string

const (
TargetStatusSuccess TargetStatus = "SUCCESS"
TargetStatusFailure TargetStatus = "FAILURE"
TargetStatusUnknown TargetStatus = "UNKNOWN"
)

type TargetHistory struct {
CreatedAt *Time `db:"created_at"`

TargetId string `db:"target_id"`
WorkerGroupId string `db:"worker_group_id"`
CheckId string `db:"check_id"`

TriggerId string `db:"trigger_id"`
Status TriggerStatus `db:"status"`
Note string `db:"note"`
Status TargetStatus `db:"status"`
Note string `db:"note"`
}
57 changes: 31 additions & 26 deletions database/sqlite/migrations/2024-02-27-initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ CREATE TABLE oauth2_states (
CREATE TABLE checks (
id TEXT NOT NULL,
name TEXT NOT NULL,
"group" TEXT NOT NULL,
schedule TEXT NOT NULL,
script TEXT NOT NULL,

visibility TEXT NOT NULL,
filter TEXT NOT NULL,

created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),

PRIMARY KEY (id),
CONSTRAINT unique_checks_name UNIQUE (name)
) STRICT;


-- +migrate StatementBegin
CREATE TRIGGER checks_updated_timestamp AFTER UPDATE ON checks BEGIN
UPDATE checks SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ') WHERE id = NEW.id;
Expand All @@ -39,7 +36,6 @@ CREATE TABLE worker_groups (
PRIMARY KEY (id),
CONSTRAINT unique_worker_groups_name UNIQUE (name)
) STRICT;

-- +migrate StatementBegin
CREATE TRIGGER worker_groups_updated_timestamp AFTER UPDATE ON worker_groups BEGIN
UPDATE worker_groups SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ') WHERE id = NEW.id;
Expand All @@ -55,19 +51,6 @@ CREATE TABLE check_worker_groups (
CONSTRAINT fk_check_worker_groups_check FOREIGN KEY (check_id) REFERENCES checks(id) ON DELETE CASCADE
) STRICT;

CREATE TABLE check_histories (
check_id TEXT NOT NULL,
worker_group_id TEXT NOT NULL,

status TEXT NOT NULL,
note TEXT NOT NULL,

created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),

PRIMARY KEY (check_id, worker_group_id, created_at),
CONSTRAINT fk_check_histories_check FOREIGN KEY (check_id) REFERENCES checks(id) ON DELETE CASCADE,
CONSTRAINT fk_check_histories_worker_group FOREIGN KEY (worker_group_id) REFERENCES worker_groups(id) ON DELETE CASCADE
) STRICT;

CREATE TABLE triggers (
id TEXT NOT NULL,
Expand All @@ -80,34 +63,56 @@ CREATE TABLE triggers (
PRIMARY KEY (id),
CONSTRAINT unique_triggers_name UNIQUE (name)
) STRICT;


-- +migrate StatementBegin
CREATE TRIGGER triggers_updated_timestamp AFTER UPDATE ON triggers BEGIN
UPDATE triggers SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ') WHERE id = NEW.id;
END;
-- +migrate StatementEnd

CREATE TABLE trigger_histories (
trigger_id TEXT NOT NULL,
CREATE TABLE targets (
id TEXT NOT NULL,
name TEXT NOT NULL,
"group" TEXT NOT NULL,

visibility TEXT NOT NULL,
state TEXT NOT NULL,

metadata TEXT NOT NULL,

created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),

PRIMARY KEY (id),
CONSTRAINT unique_targets_name UNIQUE (name)
) STRICT;
-- +migrate StatementBegin
CREATE TRIGGER targets_updated_timestamp AFTER UPDATE ON targets BEGIN
UPDATE targets SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ') WHERE id = NEW.id;
END;
-- +migrate StatementEnd

CREATE TABLE target_histories (
target_id TEXT NOT NULL,
worker_group_id TEXT NOT NULL,
check_id TEXT NOT NULL,

status TEXT NOT NULL,
note TEXT NOT NULL,

created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ')),

PRIMARY KEY (trigger_id, created_at),
CONSTRAINT fk_trigger_histories_trigger FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE
PRIMARY KEY (target_id, worker_group_id, check_id, created_at),
CONSTRAINT fk_target_histories_target FOREIGN KEY (target_id) REFERENCES targets(id) ON DELETE CASCADE,
CONSTRAINT fk_target_histories_worker_group FOREIGN KEY (worker_group_id) REFERENCES worker_groups(id) ON DELETE CASCADE,
CONSTRAINT fk_target_histories_check FOREIGN KEY (check_id) REFERENCES checks(id) ON DELETE CASCADE
) STRICT;

-- +migrate Down
DROP TABLE oauth2_states;
DROP TABLE check_worker_groups;
DROP TABLE worker_groups;
DROP TRIGGER worker_groups_updated_timestamp;
DROP TABLE check_histories;
DROP TABLE checks;
DROP TRIGGER checks_updated_timestamp;
DROP TABLE triggers;
DROP TABLE trigger_histories;
DROP TRIGGER triggers_updated_timestamp;
6 changes: 3 additions & 3 deletions deploy/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ primary_region = 'waw'
ROOT_URL = 'https://zdravko.mnts.dev'
TEMPORAL_SERVER_HOST = 'server.process.zdravko.internal:7233'

TEMPORAL_DATABASE_PATH = '/data/temporal-10.db'
SQLITE_DATABASE_PATH = '/data/zdravko-10.db'
KEYVALUE_DATABASE_PATH = '/data/zdravko_kv-10.db'
TEMPORAL_DATABASE_PATH = '/data/temporal-11.db'
SQLITE_DATABASE_PATH = '/data/zdravko-11.db'
KEYVALUE_DATABASE_PATH = '/data/zdravko_kv-11.db'

[processes]
server = '--temporal --server'
Expand Down
7 changes: 5 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
SESSION_SECRET=your_secret

# To generate keys, run "just generate-jwt-key"
JWT_PUBLIC_KEY=""
JWT_PRIVATE_KEY=""
# When running `just run` or `just run-worker`
# This doesn't have to be set, as it's read from the file
#
#JWT_PUBLIC_KEY=""
#JWT_PRIVATE_KEY=""

# To generate worker token, go to website and
# create new worker. Then copy the token.
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module code.tjo.space/mentos1386/zdravko
module github.com/mentos1386/zdravko

go 1.21.6

require (
github.com/dgraph-io/badger/v4 v4.2.0
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d
github.com/go-playground/validator/v10 v10.18.0
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/gorilla/sessions v1.2.2
Expand All @@ -20,7 +21,7 @@ require (
go.temporal.io/api v1.27.0
go.temporal.io/sdk v1.26.0-rc.2
go.temporal.io/server v1.22.4
golang.org/x/exp v0.0.0-20231127185646-65229373498e
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/oauth2 v0.17.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
Expand Down Expand Up @@ -177,14 +177,14 @@ require (
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/tools v0.21.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.155.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
Loading

0 comments on commit 91fddaa

Please sign in to comment.