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

Get detector #207

Merged
merged 7 commits into from
Aug 20, 2020
Merged

Conversation

VijayanB
Copy link
Member

@VijayanB VijayanB commented Aug 7, 2020

Cat command to concatenate and print detectors
Added get method to get detector configuration

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link

codecov bot commented Aug 7, 2020

Codecov Report

Merging #207 into master will increase coverage by 0.19%.
The diff coverage is 80.41%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #207      +/-   ##
============================================
+ Coverage     72.21%   72.41%   +0.19%     
- Complexity     1289     1290       +1     
============================================
  Files           139      139              
  Lines          5979     6073      +94     
  Branches        469      469              
============================================
+ Hits           4318     4398      +80     
- Misses         1457     1464       +7     
- Partials        204      211       +7     
Flag Coverage Δ Complexity Δ
#cli 80.30% <80.41%> (+0.56%) 0.00 <0.00> (ø)
#plugin 71.36% <ø> (+0.01%) 1290.00 <ø> (+1.00)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
cli/internal/gateway/ad/ad.go 65.38% <64.70%> (-0.14%) 0.00 <0.00> (ø)
cli/internal/controller/ad/ad.go 77.38% <67.74%> (+<0.01%) 0.00 <0.00> (ø)
cli/internal/mapper/ad/ad.go 86.42% <90.90%> (+1.88%) 0.00 <0.00> (ø)
cli/internal/handler/ad/ad.go 90.09% <100.00%> (+1.66%) 0.00 <0.00> (ø)
...sticsearch/ad/indices/AnomalyDetectionIndices.java 62.31% <0.00%> (+0.72%) 24.00% <0.00%> (+1.00%)

@VijayanB VijayanB requested review from wnbts, kaituo and ylwu-amzn August 7, 2020 20:30
@VijayanB VijayanB self-assigned this Aug 8, 2020
@VijayanB VijayanB added the enhancement New feature or request label Aug 8, 2020
@@ -59,6 +61,56 @@ func (g *gateway) buildCreateURL() (*url.URL, error) {
return endpoint, nil
}

// CreateDetector Creates an anomaly detector job.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to remove this line? In go, if a method is exported, it is practice to write comment else linter will complain. At line 114, i have a method CreateDetector, hence the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad. I thought the following is a commented out code block. Does Go has block comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I updated with block comments.

cli/cmd/cat.go Outdated
}

//executeByNames gets detector output based on name as argument
func executeByNames(commandHandler *ad.Handler, args []string) []*entity.DetectorOutput {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks same as execyteByID. Am I missing anything?

Copy link
Member Author

@VijayanB VijayanB Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.
execute by name calls method ad.GetAnomalyDetectorsByNamePattern which returns []*entity.DetectorOutput, while
executeby id calls method ad.GetAnomalyDetectorsByID which returns *entity.DetectorOutput. Thats way i am not able to pass function as parameter. In go, type has to be matched.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to pass a boolean or enum say "by field". And then you call different functions based on this by field?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I wrapped the method for executeByID and used flags to differentiate action .

cli/internal/entity/ad/ad.go Show resolved Hide resolved
@VijayanB VijayanB requested a review from kaituo August 12, 2020 20:55
@VijayanB VijayanB force-pushed the get-detector branch 2 times, most recently from 05c5426 to 88e6869 Compare August 12, 2020 21:32
cli/cmd/cat.go Outdated
},
}

func getDetectors(commandHandler *ad.Handler, args []string, get func(*ad.Handler, string) ([]*entity.DetectorOutput, error)) []*entity.DetectorOutput {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems very long. How about breaking it into two lines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Changed

cli/cmd/cat.go Outdated
}

//executeByID gets detector output based on ID as argument
func executeByID(commandHandler *ad.Handler, ID string) ([]*entity.DetectorOutput, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not to execute a detector by ID. How about rename it as "getDetectorByID"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Changed

cli/cmd/cat.go Outdated
if idStatus {
action = executeByID
}
if results := getDetectors(commandHandler, args, action); results != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If results is nil, what message will user see?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So only possibility getDetectors can get nil is if there is an error from GET method. In that case it will display the error message ( why get failed ). This was handled inside getDetectors. I refactored now to return err and print outside now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only possibility getDetectors can get nil is if there is an error from GET method

If user input an invalid detector id, will throw an error or return empty results ? In print method for _, d := range results {, if results is empty, will not print anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is will be either json output or error message per detector.

if results == nil {
return
}
for _, d := range results {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it best practice to use single-letter variable name in Go? How about replace d with detector ? That will be easier to read the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/golang/go/wiki/CodeReviewComments#variable-names
The convention is you only need more descriptive if it is used far away from declaration. Especially for iterators, single variable name is preferred.

@VijayanB VijayanB force-pushed the get-detector branch 2 times, most recently from 98101f2 to 37f4e4c Compare August 14, 2020 22:44

func init() {
esadCmd.AddCommand(catCmd)
catCmd.Flags().BoolP("name", "", true, "Input is name or pattern")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message "Input is name or pattern" is visible to user? Have you confirmed the wording with tech writer? I feel we should say Input is detector name or name pattern, Input is detector id

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, i will fix this accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will approve this PR. Please fix this in another PR.

cli/cmd/cat.go Outdated
if idStatus {
action = executeByID
}
if results := getDetectors(commandHandler, args, action); results != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only possibility getDetectors can get nil is if there is an error from GET method

If user input an invalid detector id, will throw an error or return empty results ? In print method for _, d := range results {, if results is empty, will not print anything?

//GetDetectorsByName get detector based on name pattern. It first calls SearchDetectorByName and then
// gets lists of detectorId and call GetDetector to get individual detector configuration
func (c controller) GetDetectorsByName(ctx context.Context, pattern string, display bool) ([]*entity.DetectorOutput, error) {
matchedDetectors, err := c.getDetectorsToProcess(ctx, "fetch", pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What use cases have you tested for the name pattern? SearchDetectorByName method is using match query. Why not use regexp ?

	payload := entity.SearchRequest{
		Query: entity.SearchQuery{
			Match: entity.Match{
				Name: name,
			},
		},
	}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do some test for different regex use cases? Like "test.*", "test[0-9]" etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create an issue to update search detecter using regex and will submit new PR with test cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every exported go method should have comments for readability and usage.
Add appropriate commands and sample data is copied from
https://opendistro.github.io/for-elasticsearch-docs/docs/ad/api
Added function and test to check get response
and regenerated mocks as well
Used by GetDetectorController to deserialize
response from gateway to type.
Mapper to convert gateway response to structure.
GetDetector will call gateway to get configuration
GetDetectorByName will first call SearchDetector to get ID from Name pattern.
Later, it will call GetDetector to get details.
Handler for cat command, this will get list of detector
based on id and name pattern
Created command to accept list of detectors to concatenate and
print detectors. This is similar to cat command in unix.
@VijayanB VijayanB merged commit 4b52e24 into opendistro-for-elasticsearch:master Aug 20, 2020
@VijayanB VijayanB added feature new feature and removed enhancement New feature or request labels Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants