Skip to content

Commit

Permalink
FS-1289: create a table to hold event history for servers (#77)
Browse files Browse the repository at this point in the history
* add an event history and generate ORM

* api, client, and tests for managing event history

* add validator tags, use errors.Is()
  • Loading branch information
DoctorVin authored Jul 2, 2024
1 parent 198ee18 commit 52e6895
Show file tree
Hide file tree
Showing 18 changed files with 2,922 additions and 46 deletions.
22 changes: 22 additions & 0 deletions db/migrations/00005_add_event_history_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE public.event_history (
event_id UUID NOT NULL PRIMARY KEY,
event_type STRING NOT NULL,
event_start TIMESTAMPTZ NOT NULL,
event_end TIMESTAMPTZ NOT NULL,
target_server UUID NOT NULL REFERENCES public.servers(id) ON DELETE CASCADE,
parameters JSON,
final_state STRING NOT NULL,
final_status JSON
);

CREATE INDEX evt_history_target ON public.event_history (target_server ASC) INCLUDE (event_type, event_start, event_end);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE public.event_history;
DROP INDEX evt_history_target;
-- +goose StatementEnd
60 changes: 60 additions & 0 deletions internal/dbtools/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/types"
Expand Down Expand Up @@ -96,6 +97,9 @@ var (
FixtureBiosConfigSet *models.BiosConfigSet
FixtureBiosConfigComponents []*models.BiosConfigComponent
FixtureBiosConfigSettings [][]*models.BiosConfigSetting

FixtureEventHistoryServer *models.Server
FixtureEventHistories []*models.EventHistory
)

func addFixtures(t *testing.T) error {
Expand Down Expand Up @@ -170,6 +174,10 @@ func addFixtures(t *testing.T) error {
return err
}

if err := setupEventHistoryFixtures(ctx, testDB); err != nil {
return err
}

if err := setupConfigSet(ctx, testDB); err != nil {
return err
}
Expand Down Expand Up @@ -716,6 +724,58 @@ func setupInventoryFixture(ctx context.Context, db *sqlx.DB) error {
return nil
}

func setupEventHistoryFixtures(ctx context.Context, db *sqlx.DB) error {
FixtureEventHistoryServer = &models.Server{
Name: null.StringFrom("event-history"),
FacilityCode: null.StringFrom("tf2"),
}

if err := FixtureEventHistoryServer.Insert(ctx, db, boil.Infer()); err != nil {
return errors.Wrap(err, "event history server fixture")
}

FixtureEventHistories = []*models.EventHistory{
{
EventID: uuid.New().String(),
EventType: "test event",
EventStart: time.Now().Add(-5 * time.Hour),
EventEnd: time.Now().Add(-4 * time.Hour),
TargetServer: FixtureEventHistoryServer.ID,
Parameters: null.JSONFrom([]byte(`{"msg": "test event"}`)),
FinalState: "succeeded",
FinalStatus: null.JSONFrom([]byte(`{"status": "some status"}`)),
},
{
EventID: uuid.New().String(),
EventType: "test event",
EventStart: time.Now().Add(-3 * time.Hour),
EventEnd: time.Now().Add(-2 * time.Hour),
TargetServer: FixtureEventHistoryServer.ID,
Parameters: null.JSONFrom([]byte(`{"msg": "test event"}`)),
FinalState: "failed",
FinalStatus: null.JSONFrom([]byte(`{"status": "bad status"}`)),
},
{
EventID: uuid.New().String(),
EventType: "test event",
EventStart: time.Now().Add(-1 * time.Hour),
EventEnd: time.Now().Add(-30 * time.Minute),
TargetServer: FixtureEventHistoryServer.ID,
Parameters: null.JSONFrom([]byte(`{"msg": "test event"}`)),
FinalState: "succeeded",
FinalStatus: null.JSONFrom([]byte(`{"status": "some status"}`)),
},
}

for idx, evt := range FixtureEventHistories {
if err := evt.Insert(ctx, db, boil.Infer()); err != nil {
errStr := fmt.Sprintf("event history %d", idx)
return errors.Wrap(err, errStr)
}
}
return nil
}

func setupConfigSet(ctx context.Context, db *sqlx.DB) error {
settings := [][]*models.BiosConfigSetting{
{
Expand Down
22 changes: 22 additions & 0 deletions internal/models/boil_suites_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/models/boil_table_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/models/crdb_suites_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 52e6895

Please sign in to comment.