From 315b7c1381bc8d11e8a3ee459ebff8a07ca19954 Mon Sep 17 00:00:00 2001 From: Tine Date: Sat, 25 May 2024 13:45:25 +0200 Subject: [PATCH] feat: working target history --- .../server/activities/add_target_history.go | 28 ++++++++ .../activities/process_check_outcome.go | 11 --- internal/server/activities/targets_filter.go | 3 + internal/server/handlers/examples.yaml | 2 +- internal/server/workflows/check.go | 70 ++++++++++++------- .../temporal/activity_add_target_history.go | 20 ++++++ .../activity_process_check_outcome.go | 10 --- internal/temporal/temporal.go | 1 + pkg/api/checks.go | 14 ---- pkg/server/worker.go | 2 +- web/static/css/tailwind.css | 4 ++ .../pages/settings_checks_create.tmpl | 24 ++++--- .../pages/settings_checks_describe.tmpl | 24 ++++--- .../pages/settings_targets_create.tmpl | 14 ++-- .../pages/settings_targets_describe.tmpl | 11 ++- .../pages/settings_triggers_create.tmpl | 9 +-- .../pages/settings_triggers_describe.tmpl | 9 +-- 17 files changed, 162 insertions(+), 94 deletions(-) create mode 100644 internal/server/activities/add_target_history.go delete mode 100644 internal/server/activities/process_check_outcome.go create mode 100644 internal/temporal/activity_add_target_history.go delete mode 100644 internal/temporal/activity_process_check_outcome.go diff --git a/internal/server/activities/add_target_history.go b/internal/server/activities/add_target_history.go new file mode 100644 index 0000000..682f069 --- /dev/null +++ b/internal/server/activities/add_target_history.go @@ -0,0 +1,28 @@ +package activities + +import ( + "context" + + "github.com/mentos1386/zdravko/database/models" + "github.com/mentos1386/zdravko/internal/server/services" + "github.com/mentos1386/zdravko/internal/temporal" +) + +func (a *Activities) AddTargetHistory(ctx context.Context, param temporal.ActivityAddTargetHistoryParam) (*temporal.ActivityAddTargetHistoryResult, error) { + + status := models.TargetStatusUnknown + if param.Status == temporal.AddTargetHistoryStatusSuccess { + status = models.TargetStatusSuccess + } + if param.Status == temporal.AddTargetHistoryStatusFailure { + status = models.TargetStatusFailure + } + + err := services.AddHistoryForTarget(ctx, a.db, &models.TargetHistory{ + TargetId: param.Target.Id, + Status: status, + Note: param.Note, + }) + + return &temporal.ActivityAddTargetHistoryResult{}, err +} diff --git a/internal/server/activities/process_check_outcome.go b/internal/server/activities/process_check_outcome.go deleted file mode 100644 index 652c3ef..0000000 --- a/internal/server/activities/process_check_outcome.go +++ /dev/null @@ -1,11 +0,0 @@ -package activities - -import ( - "context" - - "github.com/mentos1386/zdravko/internal/temporal" -) - -func (a *Activities) ProcessCheckOutcome(ctx context.Context, param temporal.ActivityProcessCheckOutcomeParam) (*temporal.ActivityProcessCheckOutcomeResult, error) { - return nil, nil -} diff --git a/internal/server/activities/targets_filter.go b/internal/server/activities/targets_filter.go index 71ca2fe..537b84f 100644 --- a/internal/server/activities/targets_filter.go +++ b/internal/server/activities/targets_filter.go @@ -37,10 +37,12 @@ func (a *Activities) TargetsFilter(ctx context.Context, param temporal.ActivityT a.logger.Info("TargetsFilter", "target", target) targetWithMedatada := &struct { + Id string Name string Group string Metadata map[string]interface{} }{ + Id: target.Id, Name: target.Name, Group: target.Group, Metadata: metadata, @@ -57,6 +59,7 @@ func (a *Activities) TargetsFilter(ctx context.Context, param temporal.ActivityT } if value.Export().(bool) { filteredTargets = append(filteredTargets, &temporal.Target{ + Id: target.Id, Name: target.Name, Group: target.Group, Metadata: target.Metadata, diff --git a/internal/server/handlers/examples.yaml b/internal/server/handlers/examples.yaml index 72c91c2..f0bcc0d 100644 --- a/internal/server/handlers/examples.yaml +++ b/internal/server/handlers/examples.yaml @@ -85,7 +85,7 @@ filter: | target: | kind: Http - tags: + labels: production: "true" spec: url: "https://test.k6.io" diff --git a/internal/server/workflows/check.go b/internal/server/workflows/check.go index cfadf97..afaedec 100644 --- a/internal/server/workflows/check.go +++ b/internal/server/workflows/check.go @@ -6,53 +6,71 @@ import ( "time" "github.com/mentos1386/zdravko/internal/temporal" - "github.com/mentos1386/zdravko/pkg/api" "go.temporal.io/sdk/workflow" ) -func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal.WorkflowCheckParam) (api.CheckStatus, error) { +func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal.WorkflowCheckParam) error { workerGroupIds := param.WorkerGroupIds sort.Strings(workerGroupIds) - ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ - StartToCloseTimeout: 60 * time.Second, - TaskQueue: temporal.TEMPORAL_SERVER_QUEUE, - }) - targetsFilterParam := temporal.ActivityTargetsFilterParam{ - Filter: param.Filter, - } targetsFilterResult := temporal.ActivityTargetsFilterResult{} - err := workflow.ExecuteActivity(ctx, temporal.ActivityTargetsFilterName, targetsFilterParam).Get(ctx, &targetsFilterResult) + err := workflow.ExecuteActivity( + workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ + StartToCloseTimeout: 60 * time.Second, + TaskQueue: temporal.TEMPORAL_SERVER_QUEUE, + }), + temporal.ActivityTargetsFilterName, + temporal.ActivityTargetsFilterParam{ + Filter: param.Filter, + }, + ).Get(ctx, &targetsFilterResult) if err != nil { - return api.CheckStatusUnknown, err + return err } for _, target := range targetsFilterResult.Targets { for _, workerGroupId := range workerGroupIds { - ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ - StartToCloseTimeout: 60 * time.Second, - TaskQueue: workerGroupId, - }) - - heatlcheckParam := temporal.ActivityCheckParam{ - Script: param.Script, - Target: target, - } - var checkResult *temporal.ActivityCheckResult - err := workflow.ExecuteActivity(ctx, temporal.ActivityCheckName, heatlcheckParam).Get(ctx, &checkResult) + err := workflow.ExecuteActivity( + workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ + StartToCloseTimeout: 60 * time.Second, + TaskQueue: workerGroupId, + }), + temporal.ActivityCheckName, + temporal.ActivityCheckParam{ + Script: param.Script, + Target: target, + }, + ).Get(ctx, &checkResult) if err != nil { - return api.CheckStatusUnknown, err + return err } - status := api.CheckStatusFailure + status := temporal.AddTargetHistoryStatusFailure if checkResult.Success { - status = api.CheckStatusSuccess + status = temporal.AddTargetHistoryStatusSuccess + } + + var addTargetHistoryResult *temporal.ActivityAddTargetHistoryResult + err = workflow.ExecuteActivity( + workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ + StartToCloseTimeout: 60 * time.Second, + TaskQueue: temporal.TEMPORAL_SERVER_QUEUE, + }), + temporal.ActivityAddTargetHistoryName, + &temporal.ActivityAddTargetHistoryParam{ + Target: target, + Status: status, + Note: checkResult.Note, + }, + ).Get(ctx, &addTargetHistoryResult) + if err != nil { + return err } slog.Info("Check %s status: %s", param.CheckId, status) } } - return api.CheckStatusSuccess, nil + return nil } diff --git a/internal/temporal/activity_add_target_history.go b/internal/temporal/activity_add_target_history.go new file mode 100644 index 0000000..ad930cf --- /dev/null +++ b/internal/temporal/activity_add_target_history.go @@ -0,0 +1,20 @@ +package temporal + +type AddTargetHistoryStatus string + +const ( + AddTargetHistoryStatusSuccess AddTargetHistoryStatus = "SUCCESS" + AddTargetHistoryStatusFailure AddTargetHistoryStatus = "FAILURE" + AddTargetHistoryStatusUnknown AddTargetHistoryStatus = "UNKNOWN" +) + +type ActivityAddTargetHistoryParam struct { + Target *Target + Status AddTargetHistoryStatus + Note string +} + +type ActivityAddTargetHistoryResult struct { +} + +const ActivityAddTargetHistoryName = "ADD_TARGET_HISTORY" diff --git a/internal/temporal/activity_process_check_outcome.go b/internal/temporal/activity_process_check_outcome.go deleted file mode 100644 index aac2a36..0000000 --- a/internal/temporal/activity_process_check_outcome.go +++ /dev/null @@ -1,10 +0,0 @@ -package temporal - -type ActivityProcessCheckOutcomeParam struct { - Outcome string -} - -type ActivityProcessCheckOutcomeResult struct { -} - -const ActivityProcessCheckOutcomeName = "PROCESS_CHECK_OUTCOME" diff --git a/internal/temporal/temporal.go b/internal/temporal/temporal.go index 1867517..4b2d76c 100644 --- a/internal/temporal/temporal.go +++ b/internal/temporal/temporal.go @@ -13,6 +13,7 @@ import ( ) type Target struct { + Id string Name string Group string Metadata string diff --git a/pkg/api/checks.go b/pkg/api/checks.go index 04d35d4..778f64e 100644 --- a/pkg/api/checks.go +++ b/pkg/api/checks.go @@ -1,15 +1 @@ package api - -type CheckStatus string - -const ( - CheckStatusSuccess CheckStatus = "SUCCESS" - CheckStatusFailure CheckStatus = "FAILURE" - CheckStatusUnknown CheckStatus = "UNKNOWN" -) - -type ApiV1ChecksHistoryPOSTBody struct { - Status CheckStatus `json:"status"` - Note string `json:"note"` - WorkerGroupId string `json:"worker_group"` -} diff --git a/pkg/server/worker.go b/pkg/server/worker.go index feb6fa6..e8887bd 100644 --- a/pkg/server/worker.go +++ b/pkg/server/worker.go @@ -31,7 +31,7 @@ func NewWorker(temporalClient client.Client, cfg *config.ServerConfig, logger *s // Register Activities worker.RegisterActivityWithOptions(a.TargetsFilter, activity.RegisterOptions{Name: temporal.ActivityTargetsFilterName}) - worker.RegisterActivityWithOptions(a.ProcessCheckOutcome, activity.RegisterOptions{Name: temporal.ActivityProcessCheckOutcomeName}) + worker.RegisterActivityWithOptions(a.AddTargetHistory, activity.RegisterOptions{Name: temporal.ActivityAddTargetHistoryName}) return &Worker{ worker: worker, diff --git a/web/static/css/tailwind.css b/web/static/css/tailwind.css index c8319a6..33257a3 100644 --- a/web/static/css/tailwind.css +++ b/web/static/css/tailwind.css @@ -1798,6 +1798,10 @@ code { } @media (min-width: 640px) { + .sm\:col-span-2 { + grid-column: span 2 / span 2; + } + .sm\:w-auto { width: auto; } diff --git a/web/templates/pages/settings_checks_create.tmpl b/web/templates/pages/settings_checks_create.tmpl index 891c5bb..e967c2b 100644 --- a/web/templates/pages/settings_checks_create.tmpl +++ b/web/templates/pages/settings_checks_create.tmpl @@ -35,26 +35,26 @@ @yearly.

- -

+

With filter we specify what targets the check will run on. The must be a javascript expression that returns a boolean.

- -

+

Script is what determines the status of a service. You can read more about it on