github.com/kubeshop/testkube@v1.17.23/pkg/tcl/cloudtcl/data/testworkflow/execution.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //	https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package testworkflow
    10  
    11  import (
    12  	"context"
    13  
    14  	"github.com/kubeshop/testkube/pkg/tcl/repositorytcl/testworkflow"
    15  
    16  	"google.golang.org/grpc"
    17  
    18  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    19  	"github.com/kubeshop/testkube/pkg/cloud"
    20  	"github.com/kubeshop/testkube/pkg/cloud/data/executor"
    21  )
    22  
    23  var _ testworkflow.Repository = (*CloudRepository)(nil)
    24  
    25  type CloudRepository struct {
    26  	executor executor.Executor
    27  }
    28  
    29  func NewCloudRepository(client cloud.TestKubeCloudAPIClient, grpcConn *grpc.ClientConn, apiKey string) *CloudRepository {
    30  	return &CloudRepository{executor: executor.NewCloudGRPCExecutor(client, grpcConn, apiKey)}
    31  }
    32  
    33  func (r *CloudRepository) Get(ctx context.Context, id string) (testkube.TestWorkflowExecution, error) {
    34  	req := ExecutionGetRequest{ID: id}
    35  	process := func(v ExecutionGetResponse) testkube.TestWorkflowExecution {
    36  		return v.WorkflowExecution
    37  	}
    38  	return pass(r.executor, ctx, req, process)
    39  }
    40  
    41  func (r *CloudRepository) GetByNameAndTestWorkflow(ctx context.Context, name, workflowName string) (result testkube.TestWorkflowExecution, err error) {
    42  	req := ExecutionGetByNameAndWorkflowRequest{Name: name, WorkflowName: workflowName}
    43  	process := func(v ExecutionGetResponse) testkube.TestWorkflowExecution {
    44  		return v.WorkflowExecution
    45  	}
    46  	return pass(r.executor, ctx, req, process)
    47  }
    48  
    49  func (r *CloudRepository) GetLatestByTestWorkflow(ctx context.Context, workflowName string) (*testkube.TestWorkflowExecution, error) {
    50  	req := ExecutionGetLatestByWorkflowRequest{WorkflowName: workflowName}
    51  	process := func(v ExecutionGetLatestByWorkflowResponse) *testkube.TestWorkflowExecution {
    52  		return v.WorkflowExecution
    53  	}
    54  	return pass(r.executor, ctx, req, process)
    55  }
    56  
    57  func (r *CloudRepository) GetLatestByTestWorkflows(ctx context.Context, workflowNames []string) (executions []testkube.TestWorkflowExecutionSummary, err error) {
    58  	req := ExecutionGetLatestByWorkflowsRequest{WorkflowNames: workflowNames}
    59  	process := func(v ExecutionGetLatestByWorkflowsResponse) []testkube.TestWorkflowExecutionSummary {
    60  		return v.WorkflowExecutions
    61  	}
    62  	return pass(r.executor, ctx, req, process)
    63  }
    64  
    65  func (r *CloudRepository) GetRunning(ctx context.Context) (result []testkube.TestWorkflowExecution, err error) {
    66  	req := ExecutionGetRunningRequest{}
    67  	process := func(v ExecutionGetRunningResponse) []testkube.TestWorkflowExecution {
    68  		return v.WorkflowExecutions
    69  	}
    70  	return pass(r.executor, ctx, req, process)
    71  }
    72  
    73  func (r *CloudRepository) GetExecutionsTotals(ctx context.Context, filter ...testworkflow.Filter) (totals testkube.ExecutionsTotals, err error) {
    74  	req := ExecutionGetExecutionTotalsRequest{Filter: mapFilters(filter)}
    75  	process := func(v ExecutionGetExecutionTotalsResponse) testkube.ExecutionsTotals {
    76  		return v.Totals
    77  	}
    78  	return pass(r.executor, ctx, req, process)
    79  }
    80  
    81  func (r *CloudRepository) GetExecutions(ctx context.Context, filter testworkflow.Filter) (result []testkube.TestWorkflowExecution, err error) {
    82  	req := ExecutionGetExecutionsRequest{Filter: filter.(*testworkflow.FilterImpl)}
    83  	process := func(v ExecutionGetExecutionsResponse) []testkube.TestWorkflowExecution {
    84  		return v.WorkflowExecutions
    85  	}
    86  	return pass(r.executor, ctx, req, process)
    87  }
    88  
    89  func (r *CloudRepository) GetExecutionsSummary(ctx context.Context, filter testworkflow.Filter) (result []testkube.TestWorkflowExecutionSummary, err error) {
    90  	req := ExecutionGetExecutionsSummaryRequest{Filter: filter.(*testworkflow.FilterImpl)}
    91  	process := func(v ExecutionGetExecutionsSummaryResponse) []testkube.TestWorkflowExecutionSummary {
    92  		return v.WorkflowExecutions
    93  	}
    94  	return pass(r.executor, ctx, req, process)
    95  }
    96  
    97  func (r *CloudRepository) Insert(ctx context.Context, result testkube.TestWorkflowExecution) (err error) {
    98  	req := ExecutionInsertRequest{WorkflowExecution: result}
    99  	return passNoContent(r.executor, ctx, req)
   100  }
   101  
   102  func (r *CloudRepository) Update(ctx context.Context, result testkube.TestWorkflowExecution) (err error) {
   103  	req := ExecutionUpdateRequest{WorkflowExecution: result}
   104  	return passNoContent(r.executor, ctx, req)
   105  }
   106  
   107  func (r *CloudRepository) UpdateResult(ctx context.Context, id string, result *testkube.TestWorkflowResult) (err error) {
   108  	req := ExecutionUpdateResultRequest{ID: id, Result: result}
   109  	return passNoContent(r.executor, ctx, req)
   110  }
   111  
   112  func (r *CloudRepository) UpdateOutput(ctx context.Context, id string, output []testkube.TestWorkflowOutput) (err error) {
   113  	req := ExecutionUpdateOutputRequest{ID: id, Output: output}
   114  	return passNoContent(r.executor, ctx, req)
   115  }
   116  
   117  // DeleteByTestWorkflow deletes execution results by workflow
   118  func (r *CloudRepository) DeleteByTestWorkflow(ctx context.Context, workflowName string) (err error) {
   119  	req := ExecutionDeleteByWorkflowRequest{WorkflowName: workflowName}
   120  	return passNoContent(r.executor, ctx, req)
   121  }
   122  
   123  // DeleteAll deletes all execution results
   124  func (r *CloudRepository) DeleteAll(ctx context.Context) (err error) {
   125  	req := ExecutionDeleteAllRequest{}
   126  	return passNoContent(r.executor, ctx, req)
   127  }
   128  
   129  // DeleteByTestWorkflows deletes execution results by workflows
   130  func (r *CloudRepository) DeleteByTestWorkflows(ctx context.Context, workflowNames []string) (err error) {
   131  	req := ExecutionDeleteByWorkflowsRequest{WorkflowNames: workflowNames}
   132  	return passNoContent(r.executor, ctx, req)
   133  }
   134  
   135  // GetTestWorkflowMetrics returns test executions metrics
   136  func (r *CloudRepository) GetTestWorkflowMetrics(ctx context.Context, name string, limit, last int) (metrics testkube.ExecutionsMetrics, err error) {
   137  	req := ExecutionGetWorkflowMetricsRequest{Name: name, Limit: limit, Last: last}
   138  	process := func(v ExecutionGetWorkflowMetricsResponse) testkube.ExecutionsMetrics {
   139  		return v.Metrics
   140  	}
   141  	return pass(r.executor, ctx, req, process)
   142  }