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

Commit

Permalink
Mapper: function to map from json to structure
Browse files Browse the repository at this point in the history
Mapper to convert gateway response to structure.
  • Loading branch information
VijayanB committed Aug 12, 2020
1 parent 0848abc commit 153fe52
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
47 changes: 44 additions & 3 deletions cli/internal/mapper/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func getFeatureAggregationQuery(name string, agg string, field string) ([]byte,
for key := range userTypeToESType {
allowedTypes = append(allowedTypes, key)
}
return nil, fmt.Errorf("invlaid aggeration type: '%s', only allowed types are: %s ", agg, strings.Join(allowedTypes, ","))
return nil, fmt.Errorf("invalid aggeration type: '%s', only allowed types are: %s ", agg, strings.Join(allowedTypes, ","))
}
agg = val
return []byte(fmt.Sprintf(`{
Expand Down Expand Up @@ -77,16 +77,24 @@ func getUnit(request string) (*string, error) {
//extract last character
unit := strings.ToLower(request[len(request)-1:])
if unit != minutesKey {
return nil, fmt.Errorf("invlaid unit: '%v' in %v, only %s (%s) is supported", unit, request, minutesKey, minutes)
return nil, fmt.Errorf("invalid unit: '%v' in %v, only %s (%s) is supported", unit, request, minutesKey, minutes)
}
return mapper.StringToStringPtr(minutes), nil
}

func getUnitKey(request string) (*string, error) {

if request != minutes {
return nil, fmt.Errorf("invalid request: '%v', only %s is supported", request, minutes)
}
return mapper.StringToStringPtr(minutesKey), nil
}

func getDuration(request string) (*int32, error) {
//extract last but one character
duration, err := strconv.Atoi(request[:len(request)-1])
if err != nil {
return nil, fmt.Errorf("invlaid duration: %v, due to {%v}", request, err)
return nil, fmt.Errorf("invalid duration: %v, due to {%v}", request, err)
}
if duration < 0 {
return nil, fmt.Errorf("duration must be positive integer")
Expand Down Expand Up @@ -114,6 +122,15 @@ func mapToInterval(request string) (*ad.Interval, error) {
}, nil
}

func mapIntervalToStringPtr(request ad.Interval) (*string, error) {
duration := request.Period.Duration
unit, err := getUnitKey(request.Period.Unit)
if err != nil {
return nil, err
}
return mapper.StringToStringPtr(fmt.Sprintf("%d%s", duration, *unit)), nil
}

//MapToCreateDetector maps to CreateDetector
func MapToCreateDetector(request ad.CreateDetectorRequest) (*ad.CreateDetector, error) {

Expand Down Expand Up @@ -184,3 +201,27 @@ func MapToDetectors(searchResponse []byte, name string) ([]ad.Detector, error) {
}
return result, nil
}

func MapToDetectorOutput(response ad.DetectorResponse) (*ad.DetectorOutput, error) {
delay, err := mapIntervalToStringPtr(response.AnomalyDetector.Delay)
if err != nil {
return nil, err
}
interval, err := mapIntervalToStringPtr(response.AnomalyDetector.Interval)
if err != nil {
return nil, err
}
return &ad.DetectorOutput{
ID: response.ID,
Name: response.AnomalyDetector.Name,
Description: response.AnomalyDetector.Description,
TimeField: response.AnomalyDetector.TimeField,
Index: response.AnomalyDetector.Index,
Features: response.AnomalyDetector.Features,
Filter: response.AnomalyDetector.Filter,
Interval: mapper.StringPtrToString(interval),
Delay: mapper.StringPtrToString(delay),
LastUpdatedAt: response.AnomalyDetector.LastUpdateTime,
SchemaVersion: response.AnomalyDetector.SchemaVersion,
}, nil
}
71 changes: 71 additions & 0 deletions cli/internal/mapper/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,74 @@ func TestMapToDetectors(t *testing.T) {
assert.ElementsMatch(t, []ad.Detector{}, actual)
})
}

func TestMapToDetectorOutput(t *testing.T) {
expected := ad.DetectorOutput{
ID: "m4ccEnIBTXsGi3mvMt9p",
Name: "test-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,
}
input := ad.DetectorResponse{
ID: "m4ccEnIBTXsGi3mvMt9p",
AnomalyDetector: ad.AnomalyDetector{
Metadata: ad.Metadata{
Name: "test-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: ad.Interval{
Period: ad.Period{
Duration: 5,
Unit: "Minutes",
},
},
Delay: ad.Interval{
Period: ad.Period{
Duration: 1,
Unit: "Minutes",
},
},
},
SchemaVersion: 0,
LastUpdateTime: 1589441737319,
},
}
t.Run("maps output success", func(t *testing.T) {
actual, err := MapToDetectorOutput(input)
assert.NoError(t, err)
assert.EqualValues(t, *actual, expected)
})
t.Run("maps output failed", func(t *testing.T) {
corruptIntervalInput := input
corruptIntervalInput.AnomalyDetector.Delay = ad.Interval{
Period: ad.Period{
Duration: 5,
Unit: "Hour",
},
}
_, err := MapToDetectorOutput(corruptIntervalInput)
assert.EqualError(t, err, "invalid request: 'Hour', only Minutes is supported")
})
}

0 comments on commit 153fe52

Please sign in to comment.