Skip to content

Commit

Permalink
feat: working target history
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed May 25, 2024
1 parent 034f530 commit 315b7c1
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 94 deletions.
28 changes: 28 additions & 0 deletions internal/server/activities/add_target_history.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 0 additions & 11 deletions internal/server/activities/process_check_outcome.go

This file was deleted.

3 changes: 3 additions & 0 deletions internal/server/activities/targets_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/server/handlers/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ filter: |
target: |
kind: Http
tags:
labels:
production: "true"
spec:
url: "https://test.k6.io"
Expand Down
70 changes: 44 additions & 26 deletions internal/server/workflows/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions internal/temporal/activity_add_target_history.go
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 0 additions & 10 deletions internal/temporal/activity_process_check_outcome.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/temporal/temporal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type Target struct {
Id string
Name string
Group string
Metadata string
Expand Down
14 changes: 0 additions & 14 deletions pkg/api/checks.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 1 addition & 1 deletion pkg/server/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions web/static/css/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,10 @@ code {
}

@media (min-width: 640px) {
.sm\:col-span-2 {
grid-column: span 2 / span 2;
}

.sm\:w-auto {
width: auto;
}
Expand Down
24 changes: 16 additions & 8 deletions web/templates/pages/settings_checks_create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@
<code>@yearly</code>.
</p>
<label for="filter">Filter</label>
<textarea required id="filter" name="filter" class="h-12">
<textarea required id="filter" name="filter" class="sm:col-span-2 h-12">
{{ ScriptUnescapeString .ExampleFilter }}</textarea
>
<div
id="editor-filter"
class="hidden block w-full h-12 rounded-lg border border-gray-300 overflow-hidden"
class="hidden sm:col-span-2 block w-full h-12 rounded-lg border border-gray-300 overflow-hidden"
></div>
<p>
<p class="sm:col-span-2">
With filter we specify what targets the check will run on. The must be a
javascript expression that returns a boolean.
</p>
<label for="script">Script</label>
<textarea required id="script" name="script" class="h-96">
<textarea required id="script" name="script" class="sm:col-span-2 h-96">
{{ ScriptUnescapeString .ExampleScript }}</textarea
>
<div
id="editor-script"
class="hidden block w-full h-96 rounded-lg border border-gray-300 overflow-hidden"
class="hidden sm:col-span-2 block w-full h-96 rounded-lg border border-gray-300 overflow-hidden"
></div>
<p>
<p class="sm:col-span-2">
Script is what determines the status of a service. You can read more
about it on
<a target="_blank" href="https://k6.io/docs/using-k6/http-requests/"
Expand All @@ -68,7 +68,13 @@
<script src="/static/monaco/vs/loader.js"></script>
<script>
const items = [
{ name: "filter", language: "javascript" },
{
name: "filter",
language: "javascript",
options: {
quickSuggestions: false,
},
},
{ name: "script", language: "javascript" },
];

Expand All @@ -80,7 +86,7 @@
}

window.editors = {};
for (const { name, language } of items) {
for (const { name, language, options = {} } of items) {
const textarea = document.getElementById(name);
const editor = document.getElementById("editor-" + name);

Expand All @@ -96,6 +102,8 @@
codeLens: false,
contextmenu: false,
scrollBeyondLastLine: false,
wordWrap: "on",
...options,
});

const resizeObserver = new ResizeObserver((entries) => {
Expand Down
24 changes: 16 additions & 8 deletions web/templates/pages/settings_checks_describe.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@
<code>@yearly</code>.
</p>
<label for="filter">Filter</label>
<textarea required id="filter" name="filter" class="h-12">
<textarea required id="filter" name="filter" class="sm:col-span-2 h-12">
{{ ScriptUnescapeString .Check.Filter }}</textarea
>
<div
id="editor-filter"
class="hidden block w-full h-12 rounded-lg border border-gray-300 overflow-hidden"
class="hidden sm:col-span-2 block w-full h-12 rounded-lg border border-gray-300 overflow-hidden"
></div>
<p>
<p class="sm:col-span-2">
With filter we specify what targets the check will run on. The must be a
javascript expression that returns a boolean.
</p>
<label for="script">Script</label>
<textarea required id="script" name="script" class="h-96">
<textarea required id="script" name="script" class="sm:col-span-2 h-96">
{{ ScriptUnescapeString .Check.Script }}</textarea
>
<div
id="editor-script"
class="block w-full h-96 rounded-lg border border-gray-300 overflow-hidden hidden"
class="hidden sm:col-span-2 block w-full h-96 rounded-lg border border-gray-300 overflow-hidden"
></div>
<p>
<p class="sm:col-span-2">
Script is what determines the status of a service. You can read more
about it on
<a target="_blank" href="https://k6.io/docs/using-k6/http-requests/"
Expand Down Expand Up @@ -184,7 +184,13 @@
<script src="/static/monaco/vs/loader.js"></script>
<script>
const items = [
{ name: "filter", language: "javascript" },
{
name: "filter",
language: "javascript",
options: {
quickSuggestions: false,
},
},
{ name: "script", language: "javascript" },
];

Expand All @@ -196,7 +202,7 @@
}

window.editors = {};
for (const { name, language } of items) {
for (const { name, language, options = {} } of items) {
const textarea = document.getElementById(name);
const editor = document.getElementById("editor-" + name);

Expand All @@ -212,6 +218,8 @@
codeLens: false,
contextmenu: false,
scrollBeyondLastLine: false,
wordWrap: "on",
...options,
});

const resizeObserver = new ResizeObserver((entries) => {
Expand Down
Loading

0 comments on commit 315b7c1

Please sign in to comment.