2024-05-23 16:33:30 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-05-25 17:52:37 +00:00
|
|
|
"github.com/mentos1386/zdravko/internal/temporal"
|
|
|
|
"go.temporal.io/api/enums/v1"
|
2024-05-23 16:33:30 +00:00
|
|
|
"go.temporal.io/api/workflowservice/v1"
|
|
|
|
"go.temporal.io/sdk/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CheckHistory struct {
|
2024-05-25 17:52:37 +00:00
|
|
|
CheckId string
|
|
|
|
Status string
|
|
|
|
Duration time.Duration
|
|
|
|
StartTime time.Time
|
|
|
|
EndTime time.Time
|
|
|
|
WorkerGroupName string
|
|
|
|
Note string
|
2024-05-23 16:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-25 17:52:37 +00:00
|
|
|
func GetLastNCheckHistory(ctx context.Context, t client.Client, n int32) ([]*CheckHistory, error) {
|
2024-05-23 16:33:30 +00:00
|
|
|
var checkHistory []*CheckHistory
|
|
|
|
|
2024-05-25 17:52:37 +00:00
|
|
|
response, err := t.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
|
2024-05-23 16:33:30 +00:00
|
|
|
PageSize: n,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return checkHistory, err
|
|
|
|
}
|
|
|
|
|
|
|
|
executions := response.GetExecutions()
|
|
|
|
|
|
|
|
for _, execution := range executions {
|
|
|
|
scheduleId := string(execution.GetSearchAttributes().GetIndexedFields()["TemporalScheduledById"].Data)
|
2024-05-25 17:52:37 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
checkHistory = append(checkHistory, &CheckHistory{
|
2024-05-25 17:52:37 +00:00
|
|
|
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,
|
2024-05-23 16:33:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkHistory, nil
|
|
|
|
}
|
|
|
|
|
2024-05-25 17:52:37 +00:00
|
|
|
func GetCheckHistoryForCheck(ctx context.Context, t client.Client, checkId string) ([]*CheckHistory, error) {
|
2024-05-23 16:33:30 +00:00
|
|
|
var checkHistory []*CheckHistory
|
|
|
|
|
2024-05-25 17:52:37 +00:00
|
|
|
response, err := t.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
|
2024-05-23 16:33:30 +00:00
|
|
|
PageSize: 10,
|
|
|
|
Query: fmt.Sprintf(`TemporalScheduledById = "%s"`, getScheduleId(checkId)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return checkHistory, err
|
|
|
|
}
|
|
|
|
|
|
|
|
executions := response.GetExecutions()
|
|
|
|
|
|
|
|
for _, execution := range executions {
|
|
|
|
scheduleId := string(execution.GetSearchAttributes().GetIndexedFields()["TemporalScheduledById"].Data)
|
2024-05-25 17:52:37 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
checkHistory = append(checkHistory, &CheckHistory{
|
2024-05-25 17:52:37 +00:00
|
|
|
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,
|
2024-05-23 16:33:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkHistory, nil
|
|
|
|
}
|