Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Handler: GetDetector handler to call controller
Browse files Browse the repository at this point in the history
Handler for cat command, this will get list of detector
based on id and name pattern
  • Loading branch information
VijayanB committed Aug 12, 2020
1 parent 9243f3c commit 3986945
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cli/internal/handler/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
82 changes: 82 additions & 0 deletions cli/internal/handler/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

0 comments on commit 3986945

Please sign in to comment.