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

Commit

Permalink
Entity: Added entity to deserialize response
Browse files Browse the repository at this point in the history
Used by GetDetectorController to deserialize
response from gateway to type.
  • Loading branch information
VijayanB committed Aug 12, 2020
1 parent 17da589 commit 0848abc
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Empty file.
29 changes: 29 additions & 0 deletions cli/internal/entity/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,32 @@ type Container struct {
type SearchResponse struct {
Hits Container `json:"hits"`
}

type Metadata CreateDetector

type AnomalyDetector struct {
Metadata
SchemaVersion int32 `json:"schema_version"`
LastUpdateTime uint64 `json:"last_update_time"`
}

//DetectorResponse represents detector's setting
type DetectorResponse struct {
ID string `json:"_id"`
AnomalyDetector AnomalyDetector `json:"anomaly_detector"`
}

//DetectorOutput represents detector's setting displayed to user
type DetectorOutput struct {
ID string
Name string `json:"name"`
Description string `json:"description"`
TimeField string `json:"time_field"`
Index []string `json:"indices"`
Features []Feature `json:"features"`
Filter json.RawMessage `json:"filter_query"`
Interval string `json:"detection_interval"`
Delay string `json:"window_delay"`
LastUpdatedAt uint64 `json:"last_update_time"`
SchemaVersion int32 `json:"schema_version"`
}
80 changes: 80 additions & 0 deletions cli/internal/entity/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,83 @@ func getSearchRequest() interface{} {
},
}
}

func TestGetDetectorResponse(t *testing.T) {
t.Run("deserialization success", func(t *testing.T) {
var actual DetectorResponse
expected := DetectorResponse{
ID: "m4ccEnIBTXsGi3mvMt9p",
AnomalyDetector: AnomalyDetector{
Metadata: Metadata{
Name: "test-detector",
Description: "Test detector",
TimeField: "timestamp",
Index: []string{"order*"},
Features: []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: Interval{
Period: Period{
Duration: 1,
Unit: "Minutes",
},
},
Delay: Interval{
Period: Period{
Duration: 1,
Unit: "Minutes",
},
},
},
SchemaVersion: 0,
LastUpdateTime: 1589441737319,
},
}
responseJSON := `
{
"_id" : "m4ccEnIBTXsGi3mvMt9p",
"_version" : 1,
"_primary_term" : 1,
"_seq_no" : 3,
"anomaly_detector" : {
"name" : "test-detector",
"description" : "Test detector",
"time_field" : "timestamp",
"indices" : [
"order*"
],
"filter_query" : {"bool" : {"filter" : [{"exists" : {"field" : "value","boost" : 1.0}}],"adjust_pure_negative" : true,"boost" : 1.0}},
"detection_interval" : {
"period" : {
"interval" : 1,
"unit" : "Minutes"
}
},
"window_delay" : {
"period" : {
"interval" : 1,
"unit" : "Minutes"
}
},
"schema_version" : 0,
"feature_attributes" : [
{
"feature_id" : "mYccEnIBTXsGi3mvMd8_",
"feature_name" : "total_order",
"feature_enabled" : true,
"aggregation_query" : {"total_order":{"sum":{"field":"value"}}}
}
],
"last_update_time" : 1589441737319
}
}
`
_ = json.Unmarshal([]byte(responseJSON), &actual)
assert.EqualValues(t, expected, actual)
})
}

0 comments on commit 0848abc

Please sign in to comment.