Repository: datreeio/datree
Pull request #596 (2022-05-01 11:51:50)

feat: add evaluation duration


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index a054137..2added1 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -56,17 +56,18 @@ type FormattedResults struct {
 }
 
 type EvaluationRequestData struct {
-	Token              string
-	ClientId           string
-	CliVersion         string
-	K8sVersion         string
-	PolicyName         string
-	CiContext          *ciContext.CIContext
-	RulesData          []cliClient.RuleData
-	FilesData          []cliClient.FileData
-	FailedYamlFiles    []string
-	FailedK8sFiles     []string
-	PolicyCheckResults FailedRulesByFiles
+	Token                     string
+	ClientId                  string
+	CliVersion                string
+	K8sVersion                string
+	PolicyName                string
+	CiContext                 *ciContext.CIContext
+	RulesData                 []cliClient.RuleData
+	FilesData                 []cliClient.FileData
+	FailedYamlFiles           []string
+	FailedK8sFiles            []string
+	PolicyCheckResults        FailedRulesByFiles
+	EvaluationDurationSeconds float64
 }
 
 var OSInfoFn = NewOSInfo
@@ -79,11 +80,12 @@ func (e *Evaluator) SendEvaluationResult(evaluationRequestData EvaluationRequest
 		Token:      evaluationRequestData.Token,
 		PolicyName: evaluationRequestData.PolicyName,
 		Metadata: &cliClient.Metadata{
-			CliVersion:      evaluationRequestData.CliVersion,
-			Os:              osInfo.OS,
-			PlatformVersion: osInfo.PlatformVersion,
-			KernelVersion:   osInfo.KernelVersion,
-			CIContext:       evaluationRequestData.CiContext,
+			CliVersion:                evaluationRequestData.CliVersion,
+			Os:                        osInfo.OS,
+			PlatformVersion:           osInfo.PlatformVersion,
+			KernelVersion:             osInfo.KernelVersion,
+			CIContext:                 evaluationRequestData.CiContext,
+			EvaluationDurationSeconds: evaluationRequestData.EvaluationDurationSeconds,
 		},
 		FailedYamlFiles:    evaluationRequestData.FailedYamlFiles,
 		FailedK8sFiles:     evaluationRequestData.FailedK8sFiles,
diff --git a/cmd/root.go b/cmd/root.go
index beddf1e..a51e631 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -1,6 +1,8 @@
 package cmd
 
 import (
+	"time"
+
 	"github.com/datreeio/datree/bl/evaluation"
 	"github.com/datreeio/datree/bl/files"
 	"github.com/datreeio/datree/bl/messager"
@@ -31,6 +33,7 @@ var rootCmd = &cobra.Command{
 var CliVersion string
 
 func NewRootCommand(app *App) *cobra.Command {
+	startTime := time.Now()
 
 	rootCmd.AddCommand(test.New(&test.TestCommandContext{
 		CliVersion:     CliVersion,
@@ -43,6 +46,7 @@ func NewRootCommand(app *App) *cobra.Command {
 		CliClient:      app.Context.CliClient,
 		FilesExtractor: app.Context.FilesExtractor,
 		CiContext:      app.Context.CiContext,
+		StartTime:      startTime,
 	}))
 
 	rootCmd.AddCommand(kustomize.New(&test.TestCommandContext{
@@ -55,6 +59,7 @@ func NewRootCommand(app *App) *cobra.Command {
 		K8sValidator:   app.Context.K8sValidator,
 		CliClient:      app.Context.CliClient,
 		FilesExtractor: app.Context.FilesExtractor,
+		StartTime:      startTime,
 	}, &kustomize.KustomizeContext{CommandRunner: app.Context.CommandRunner}))
 
 	rootCmd.AddCommand(version.New(&version.VersionCommandContext{
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index 8b749ac..acf8d81 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -23,11 +23,12 @@ type TestCommandFlags struct {
 }
 
 type Metadata struct {
-	CliVersion      string               `json:"cliVersion"`
-	Os              string               `json:"os"`
-	PlatformVersion string               `json:"platformVersion"`
-	KernelVersion   string               `json:"kernelVersion"`
-	CIContext       *ciContext.CIContext `json:"ciContext"`
+	CliVersion                string               `json:"cliVersion"`
+	Os                        string               `json:"os"`
+	PlatformVersion           string               `json:"platformVersion"`
+	KernelVersion             string               `json:"kernelVersion"`
+	CIContext                 *ciContext.CIContext `json:"ciContext"`
+	EvaluationDurationSeconds float64              `json:"evaluationDurationSeconds"`
 }
 
 type SendEvaluationResultsResponse struct {


Test patch:
diff --git a/bl/evaluation/evaluator_test.go b/bl/evaluation/evaluator_test.go
index aa4a2b8..ec1cf16 100644
--- a/bl/evaluation/evaluator_test.go
+++ b/bl/evaluation/evaluator_test.go
@@ -75,17 +75,18 @@ func TestSendEvaluationResult(t *testing.T) {
 		expectedSendEvaluationResultsResponse := &cliClient.SendEvaluationResultsResponse{EvaluationId: 1, PromptMessage: promptMessage}
 
 		evaluationRequestData := EvaluationRequestData{
-			Token:              token,
-			ClientId:           clientId,
-			CliVersion:         cliVersion,
-			K8sVersion:         k8sVersion,
-			PolicyName:         policyName,
-			CiContext:          ciContext,
-			RulesData:          []cliClient.RuleData{},
-			FilesData:          []cliClient.FileData{},
-			FailedYamlFiles:    []string{},
-			FailedK8sFiles:     []string{},
-			PolicyCheckResults: nil,
+			Token:                     token,
+			ClientId:                  clientId,
+			CliVersion:                cliVersion,
+			K8sVersion:                k8sVersion,
+			PolicyName:                policyName,
+			CiContext:                 ciContext,
+			RulesData:                 []cliClient.RuleData{},
+			FilesData:                 []cliClient.FileData{},
+			FailedYamlFiles:           []string{},
+			FailedK8sFiles:            []string{},
+			PolicyCheckResults:        nil,
+			EvaluationDurationSeconds: 0,
 		}
 
 		sendEvaluationResultsResponse, _ := evaluator.SendEvaluationResult(evaluationRequestData)
@@ -96,11 +97,12 @@ func TestSendEvaluationResult(t *testing.T) {
 			Token:      evaluationRequestData.Token,
 			PolicyName: evaluationRequestData.PolicyName,
 			Metadata: &cliClient.Metadata{
-				CliVersion:      evaluationRequestData.CliVersion,
-				Os:              osInfo.OS,
-				PlatformVersion: osInfo.PlatformVersion,
-				KernelVersion:   osInfo.KernelVersion,
-				CIContext:       evaluationRequestData.CiContext,
+				CliVersion:                evaluationRequestData.CliVersion,
+				Os:                        osInfo.OS,
+				PlatformVersion:           osInfo.PlatformVersion,
+				KernelVersion:             osInfo.KernelVersion,
+				CIContext:                 evaluationRequestData.CiContext,
+				EvaluationDurationSeconds: evaluationRequestData.EvaluationDurationSeconds,
 			},
 			FailedYamlFiles:    evaluationRequestData.FailedYamlFiles,
 			FailedK8sFiles:     evaluationRequestData.FailedK8sFiles,
diff --git a/cmd/test/main.go b/cmd/test/main.go
index a273ebf..07f7e2c 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -10,6 +10,7 @@ import (
 	"regexp"
 	"strings"
 	"sync"
+	"time"
 
 	"github.com/datreeio/datree/bl/evaluation"
 	"github.com/datreeio/datree/bl/files"
@@ -139,6 +140,7 @@ type TestCommandContext struct {
 	Reader         Reader
 	CliClient      CliClient
 	FilesExtractor files.FilesExtractorInterface
+	StartTime      time.Time
 }
 
 func LoadVersionMessages(ctx *TestCommandContext, args []string, cmd *cobra.Command) error {
@@ -511,20 +513,23 @@ func evaluate(ctx *TestCommandContext, filesPaths []string, prerunData *TestComm
 	}
 
 	ciContext := ciContext.Extract()
-
+	endEvaluationTime := time.Now()
+	EvaluationDurationSeconds := endEvaluationTime.Sub(ctx.StartTime).Seconds()
 	evaluationRequestData := evaluation.EvaluationRequestData{
-		Token:              prerunData.Token,
-		ClientId:           prerunData.ClientId,
-		CliVersion:         ctx.CliVersion,
-		K8sVersion:         prerunData.K8sVersion,
-		PolicyName:         policyName,
-		CiContext:          ciContext,
-		RulesData:          policyCheckResultData.RulesData,
-		FilesData:          policyCheckResultData.FilesData,
-		FailedYamlFiles:    failedYamlFiles,
-		FailedK8sFiles:     failedK8sFiles,
-		PolicyCheckResults: policyCheckResultData.RawResults,
+		Token:                     prerunData.Token,
+		ClientId:                  prerunData.ClientId,
+		CliVersion:                ctx.CliVersion,
+		K8sVersion:                prerunData.K8sVersion,
+		PolicyName:                policyName,
+		CiContext:                 ciContext,
+		RulesData:                 policyCheckResultData.RulesData,
+		FilesData:                 policyCheckResultData.FilesData,
+		FailedYamlFiles:           failedYamlFiles,
+		FailedK8sFiles:            failedK8sFiles,
+		PolicyCheckResults:        policyCheckResultData.RawResults,
+		EvaluationDurationSeconds: EvaluationDurationSeconds,
 	}
+
 	sendEvaluationResultsResponse, err := ctx.Evaluator.SendEvaluationResult(evaluationRequestData)
 
 	if err != nil {