-
Notifications
You must be signed in to change notification settings - Fork 36
Get detector #207
Get detector #207
Conversation
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
cli/internal/gateway/ad/ad.go
Outdated
@@ -59,6 +61,56 @@ func (g *gateway) buildCreateURL() (*url.URL, error) { | |||
return endpoint, nil | |||
} | |||
|
|||
// CreateDetector Creates an anomaly detector job. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 .
05c5426
to
88e6869
Compare
cli/cmd/cat.go
Outdated
}, | ||
} | ||
|
||
func getDetectors(commandHandler *ad.Handler, args []string, get func(*ad.Handler, string) ([]*entity.DetectorOutput, error)) []*entity.DetectorOutput { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
98101f2
to
37f4e4c
Compare
|
||
func init() { | ||
esadCmd.AddCommand(catCmd) | ||
catCmd.Flags().BoolP("name", "", true, "Input is name or pattern") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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,
},
},
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed this from documentation:
https://opendistro.github.io/for-elasticsearch-docs/docs/ad/api/#search-detector
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.