From 398694518537c8874931b0212657e47d1724d0ce Mon Sep 17 00:00:00 2001 From: Vijayan Balasubramanian Date: Fri, 7 Aug 2020 11:23:59 -0700 Subject: [PATCH] Handler: GetDetector handler to call controller Handler for cat command, this will get list of detector based on id and name pattern --- cli/internal/handler/ad/ad.go | 32 ++++++++++++ cli/internal/handler/ad/ad_test.go | 82 ++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/cli/internal/handler/ad/ad.go b/cli/internal/handler/ad/ad.go index 6fac3cff..3d78716a 100644 --- a/cli/internal/handler/ad/ad.go +++ b/cli/internal/handler/ad/ad.go @@ -192,3 +192,35 @@ func (h *Handler) StopAnomalyDetectorByID(detector string) error { } return nil } + +// GetAnomalyDetectorsByNamePattern gets detector based on detector name pattern +func GetAnomalyDetectorsByNamePattern(h *Handler, detector string) ([]*entity.DetectorOutput, error) { + return h.GetAnomalyDetectorsByNamePattern(detector) +} + +// GetAnomalyDetectorsByNamePattern gets detector based on detector name pattern +func (h *Handler) GetAnomalyDetectorsByNamePattern(name string) ([]*entity.DetectorOutput, error) { + + ctx := context.Background() + detectors, err := h.GetDetectorsByName(ctx, name, true) + if err != nil { + return nil, err + } + return detectors, nil +} + +// GetAnomalyDetectorByID gets detector based on detector id +func GetAnomalyDetectorByID(h *Handler, detector string) (*entity.DetectorOutput, error) { + return h.GetAnomalyDetectorByID(detector) +} + +// GetAnomalyDetectorByID gets detector based on detector id +func (h *Handler) GetAnomalyDetectorByID(name string) (*entity.DetectorOutput, error) { + + ctx := context.Background() + detector, err := h.GetDetector(ctx, name) + if err != nil { + return nil, err + } + return detector, nil +} diff --git a/cli/internal/handler/ad/ad_test.go b/cli/internal/handler/ad/ad_test.go index 34dfdd81..55cff918 100644 --- a/cli/internal/handler/ad/ad_test.go +++ b/cli/internal/handler/ad/ad_test.go @@ -216,3 +216,85 @@ func TestGenerateAnomalyDetector(t *testing.T) { assert.EqualValues(t, expected, actual) }) } + +func TestHandler_GetAnomalyDetectorByNamePattern(t *testing.T) { + ctx := context.Background() + mockCtrl := gomock.NewController(t) + detectorOutput := []*ad.DetectorOutput{ + { + ID: "detectorID", + Name: "detector", + Description: "Test detector", + TimeField: "timestamp", + Index: []string{"order*"}, + Features: []ad.Feature{ + { + Name: "total_order", + Enabled: true, + AggregationQuery: []byte(`{"total_order":{"sum":{"field":"value"}}}`), + }, + }, + Filter: []byte(`{"bool" : {"filter" : [{"exists" : {"field" : "value","boost" : 1.0}}],"adjust_pure_negative" : true,"boost" : 1.0}}`), + Interval: "5m", + Delay: "1m", + LastUpdatedAt: 1589441737319, + SchemaVersion: 0, + }, + } + defer mockCtrl.Finish() + t.Run("test get success", func(t *testing.T) { + mockedController := mocks.NewMockController(mockCtrl) + mockedController.EXPECT().GetDetectorsByName(ctx, "detector", true).Return(detectorOutput, nil) + instance := New(mockedController) + result, err := GetAnomalyDetectorsByNamePattern(instance, "detector") + assert.NoError(t, err) + assert.EqualValues(t, detectorOutput, result) + }) + t.Run("test get failure", func(t *testing.T) { + mockedController := mocks.NewMockController(mockCtrl) + mockedController.EXPECT().GetDetectorsByName(ctx, "detector", true).Return(nil, errors.New("failed to stop")) + instance := New(mockedController) + _, err := instance.GetAnomalyDetectorsByNamePattern("detector") + assert.EqualError(t, err, "failed to stop") + }) +} + +func TestHandler_GetAnomalyDetectorByID(t *testing.T) { + ctx := context.Background() + mockCtrl := gomock.NewController(t) + detectorOutput := &ad.DetectorOutput{ + ID: "detectorID", + Name: "detector", + Description: "Test detector", + TimeField: "timestamp", + Index: []string{"order*"}, + Features: []ad.Feature{ + { + Name: "total_order", + Enabled: true, + AggregationQuery: []byte(`{"total_order":{"sum":{"field":"value"}}}`), + }, + }, + Filter: []byte(`{"bool" : {"filter" : [{"exists" : {"field" : "value","boost" : 1.0}}],"adjust_pure_negative" : true,"boost" : 1.0}}`), + Interval: "5m", + Delay: "1m", + LastUpdatedAt: 1589441737319, + SchemaVersion: 0, + } + defer mockCtrl.Finish() + t.Run("test get by id success", func(t *testing.T) { + mockedController := mocks.NewMockController(mockCtrl) + mockedController.EXPECT().GetDetector(ctx, "detectorID").Return(detectorOutput, nil) + instance := New(mockedController) + result, err := GetAnomalyDetectorByID(instance, "detectorID") + assert.NoError(t, err) + assert.EqualValues(t, detectorOutput, result) + }) + t.Run("test get by id failure", func(t *testing.T) { + mockedController := mocks.NewMockController(mockCtrl) + mockedController.EXPECT().GetDetector(ctx, "detectorID").Return(nil, errors.New("failed to stop")) + instance := New(mockedController) + _, err := instance.GetAnomalyDetectorByID("detectorID") + assert.EqualError(t, err, "failed to stop") + }) +}