Skip to content

Commit

Permalink
feat: check history and ui fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed May 25, 2024
1 parent 813c76f commit da53c04
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 97 deletions.
9 changes: 6 additions & 3 deletions database/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ const (
type TargetHistory struct {
CreatedAt *Time `db:"created_at"`

TargetId string `db:"target_id"`
Status TargetStatus `db:"status"`
Note string `db:"note"`
TargetId string `db:"target_id"`
WorkerGroupId string `db:"worker_group_id"`
CheckId string `db:"check_id"`

Status TargetStatus `db:"status"`
Note string `db:"note"`
}
10 changes: 7 additions & 3 deletions database/sqlite/migrations/2024-02-27-initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,19 @@ END;
-- +migrate StatementEnd

CREATE TABLE target_histories (
target_id TEXT NOT NULL,
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 (target_id, created_at),
CONSTRAINT fk_target_histories_target FOREIGN KEY (target_id) REFERENCES targets(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
Expand Down
8 changes: 5 additions & 3 deletions internal/server/activities/add_target_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func (a *Activities) AddTargetHistory(ctx context.Context, param temporal.Activi
}

err := services.AddHistoryForTarget(ctx, a.db, &models.TargetHistory{
TargetId: param.Target.Id,
Status: status,
Note: param.Note,
TargetId: param.Target.Id,
WorkerGroupId: param.WorkerGroupId,
CheckId: param.CheckId,
Status: status,
Note: param.Note,
})

return &temporal.ActivityAddTargetHistoryResult{}, err
Expand Down
3 changes: 2 additions & 1 deletion internal/server/handlers/settings_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (h *BaseHandler) SettingsChecksGET(c echo.Context) error {
for i, check := range checks {
state, err := services.GetCheckState(context.Background(), h.temporal, check.Id)
if err != nil {
return err
h.logger.Error("Failed to get check state", "error", err)
state = models.CheckStateUnknown
}
checksWithState[i] = &CheckWithWorkerGroupsAndState{
CheckWithWorkerGroups: check,
Expand Down
2 changes: 1 addition & 1 deletion internal/server/services/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func CreateOrUpdateCheckSchedule(
ID: getScheduleId(check.Id),
Workflow: internaltemporal.WorkflowCheckName,
Args: args,
TaskQueue: "default",
TaskQueue: internaltemporal.TEMPORAL_SERVER_QUEUE,
RetryPolicy: &temporal.RetryPolicy{
MaximumAttempts: 3,
},
Expand Down
68 changes: 53 additions & 15 deletions internal/server/services/check_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ import (
"fmt"
"time"

"github.com/mentos1386/zdravko/internal/temporal"
"go.temporal.io/api/enums/v1"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/sdk/client"
)

type CheckHistory struct {
CheckId string
Status string
Duration time.Duration
CheckId string
Status string
Duration time.Duration
StartTime time.Time
EndTime time.Time
WorkerGroupName string
Note string
}

func GetLastNCheckHistory(ctx context.Context, temporal client.Client, n int32) ([]*CheckHistory, error) {
func GetLastNCheckHistory(ctx context.Context, t client.Client, n int32) ([]*CheckHistory, error) {
var checkHistory []*CheckHistory

response, err := temporal.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
response, err := t.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
PageSize: n,
})
if err != nil {
Expand All @@ -29,21 +35,37 @@ func GetLastNCheckHistory(ctx context.Context, temporal client.Client, n int32)

for _, execution := range executions {
scheduleId := string(execution.GetSearchAttributes().GetIndexedFields()["TemporalScheduledById"].Data)
checkId := scheduleId[len("check-"):]

// Remove the quotes around the checkId and the prefix.
checkId := scheduleId[len("\"check-") : len(scheduleId)-1]

var result temporal.WorkflowCheckResult
if execution.Status != enums.WORKFLOW_EXECUTION_STATUS_RUNNING {
workflowRun := t.GetWorkflow(ctx, execution.GetExecution().GetWorkflowId(), execution.GetExecution().GetRunId())
err := workflowRun.Get(ctx, &result)
if err != nil {
return nil, err
}
}

checkHistory = append(checkHistory, &CheckHistory{
CheckId: checkId,
Duration: execution.CloseTime.AsTime().Sub(execution.StartTime.AsTime()),
Status: execution.Status.String(),
CheckId: checkId,
Duration: execution.CloseTime.AsTime().Sub(execution.StartTime.AsTime()),
StartTime: execution.StartTime.AsTime(),
EndTime: execution.CloseTime.AsTime(),
Status: execution.Status.String(),
WorkerGroupName: execution.GetTaskQueue(),
Note: result.Note,
})
}

return checkHistory, nil
}

func GetCheckHistoryForCheck(ctx context.Context, temporal client.Client, checkId string) ([]*CheckHistory, error) {
func GetCheckHistoryForCheck(ctx context.Context, t client.Client, checkId string) ([]*CheckHistory, error) {
var checkHistory []*CheckHistory

response, err := temporal.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
response, err := t.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
PageSize: 10,
Query: fmt.Sprintf(`TemporalScheduledById = "%s"`, getScheduleId(checkId)),
})
Expand All @@ -55,11 +77,27 @@ func GetCheckHistoryForCheck(ctx context.Context, temporal client.Client, checkI

for _, execution := range executions {
scheduleId := string(execution.GetSearchAttributes().GetIndexedFields()["TemporalScheduledById"].Data)
checkId := scheduleId[len("check-"):]

// Remove the quotes around the checkId and the prefix.
checkId := scheduleId[len("\"check-") : len(scheduleId)-1]

var result temporal.WorkflowCheckResult
if execution.Status != enums.WORKFLOW_EXECUTION_STATUS_RUNNING {
workflowRun := t.GetWorkflow(ctx, execution.GetExecution().GetWorkflowId(), execution.GetExecution().GetRunId())
err := workflowRun.Get(ctx, &result)
if err != nil {
return nil, err
}
}

checkHistory = append(checkHistory, &CheckHistory{
CheckId: checkId,
Duration: execution.CloseTime.AsTime().Sub(execution.StartTime.AsTime()),
Status: execution.Status.String(),
CheckId: checkId,
Duration: execution.CloseTime.AsTime().Sub(execution.StartTime.AsTime()),
StartTime: execution.StartTime.AsTime(),
EndTime: execution.CloseTime.AsTime(),
Status: execution.Status.String(),
WorkerGroupName: execution.GetTaskQueue(),
Note: result.Note,
})
}

Expand Down
22 changes: 18 additions & 4 deletions internal/server/services/targets_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package services
import (
"context"

"github.com/mentos1386/zdravko/database/models"
"github.com/jmoiron/sqlx"
"github.com/mentos1386/zdravko/database/models"
)

type TargetHistory struct {
*models.TargetHistory
TargetName string `db:"target_name"`
TargetName string `db:"target_name"`
WorkerGroupName string `db:"worker_group_name"`
CheckName string `db:"check_name"`
}

func GetLastNTargetHistory(ctx context.Context, db *sqlx.DB, n int) ([]*TargetHistory, error) {
var targetHistory []*TargetHistory
err := db.SelectContext(ctx, &targetHistory, `
SELECT
th.*,
t.name AS target_name
t.name AS target_name,
wg.name AS worker_group_name,
c.name AS check_name
FROM target_histories th
LEFT JOIN targets t ON th.target_id = t.id
LEFT JOIN worker_groups wg ON th.worker_group_id = wg.id
LEFT JOIN checks c ON th.check_id = c.id
WHERE th.target_id = $1
ORDER BY th.created_at DESC
LIMIT $1
Expand All @@ -32,9 +38,13 @@ func GetTargetHistoryForTarget(ctx context.Context, db *sqlx.DB, targetId string
err := db.SelectContext(ctx, &targetHistory, `
SELECT
th.*,
t.name AS target_name
t.name AS target_name,
wg.name AS worker_group_name,
c.name AS check_name
FROM target_histories th
LEFT JOIN targets t ON th.target_id = t.id
LEFT JOIN worker_groups wg ON th.worker_group_id = wg.id
LEFT JOIN checks c ON th.check_id = c.id
WHERE th.target_id = $1
ORDER BY th.created_at DESC
`, targetId)
Expand All @@ -46,10 +56,14 @@ func AddHistoryForTarget(ctx context.Context, db *sqlx.DB, history *models.Targe
`
INSERT INTO target_histories (
target_id,
worker_group_id,
check_id,
status,
note
) VALUES (
:target_id,
:worker_group_id,
:check_id,
:status,
:note
)`,
Expand Down
23 changes: 12 additions & 11 deletions internal/server/workflows/check.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package workflows

import (
"log/slog"
"sort"
"time"

"github.com/mentos1386/zdravko/internal/temporal"
"go.temporal.io/sdk/workflow"
)

func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal.WorkflowCheckParam) error {
func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal.WorkflowCheckParam) (*temporal.WorkflowCheckResult, error) {
workerGroupIds := param.WorkerGroupIds
sort.Strings(workerGroupIds)

Expand All @@ -25,7 +24,7 @@ func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal
},
).Get(ctx, &targetsFilterResult)
if err != nil {
return err
return nil, err
}

for _, target := range targetsFilterResult.Targets {
Expand All @@ -43,7 +42,7 @@ func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal
},
).Get(ctx, &checkResult)
if err != nil {
return err
return nil, err
}

status := temporal.AddTargetHistoryStatusFailure
Expand All @@ -59,18 +58,20 @@ func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal
}),
temporal.ActivityAddTargetHistoryName,
&temporal.ActivityAddTargetHistoryParam{
Target: target,
Status: status,
Note: checkResult.Note,
Target: target,
WorkerGroupId: workerGroupId,
CheckId: param.CheckId,
Status: status,
Note: checkResult.Note,
},
).Get(ctx, &addTargetHistoryResult)
if err != nil {
return err
return nil, err
}

slog.Info("Check %s status: %s", param.CheckId, status)
}
}

return nil
return &temporal.WorkflowCheckResult{
Note: "Check workflow completed",
}, nil
}
8 changes: 5 additions & 3 deletions internal/temporal/activity_add_target_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const (
)

type ActivityAddTargetHistoryParam struct {
Target *Target
Status AddTargetHistoryStatus
Note string
Target *Target
WorkerGroupId string
CheckId string
Status AddTargetHistoryStatus
Note string
}

type ActivityAddTargetHistoryResult struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/temporal/workflow_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ type WorkflowCheckParam struct {
WorkerGroupIds []string
}

type WorkflowCheckResult struct {
Note string
}

const WorkflowCheckName = "CHECK_WORKFLOW"
10 changes: 10 additions & 0 deletions web/static/css/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,11 @@ video {
background-color: rgb(253 186 116 / var(--tw-bg-opacity));
}

.bg-purple-100 {
--tw-bg-opacity: 1;
background-color: rgb(243 232 255 / var(--tw-bg-opacity));
}

.bg-red-100 {
--tw-bg-opacity: 1;
background-color: rgb(254 226 226 / var(--tw-bg-opacity));
Expand Down Expand Up @@ -1225,6 +1230,11 @@ video {
color: rgb(22 101 52 / var(--tw-text-opacity));
}

.text-purple-800 {
--tw-text-opacity: 1;
color: rgb(107 33 168 / var(--tw-text-opacity));
}

.text-red-600 {
--tw-text-opacity: 1;
color: rgb(220 38 38 / var(--tw-text-opacity));
Expand Down
Loading

0 comments on commit da53c04

Please sign in to comment.