Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit f944dc6

Browse files
authored
Add DataTypes method to analyzers service (#15)
Closes #14
1 parent 0a52142 commit f944dc6

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

analyzer.go

+30
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type AnalyzerService interface {
4343
Run(context.Context, string, Observable, time.Duration) (*Report, error)
4444
StartJob(context.Context, string, Observable) (*Job, *http.Response, error)
4545
NewMultiRun(context.Context, time.Duration) *MultiRun
46+
DataTypes(context.Context) ([]string, error)
4647
}
4748

4849
// AnalyzerServiceOp handles analyzer methods from Cortex API
@@ -193,3 +194,32 @@ func (a *AnalyzerServiceOp) StartJob(ctx context.Context, anid string, o Observa
193194

194195
return &j, resp, nil
195196
}
197+
198+
// DataTypes returns all available data types that Cortex can analyse.
199+
// The entries are not sorted.
200+
func (a *AnalyzerServiceOp) DataTypes(ctx context.Context) ([]string, error) {
201+
ans, _, err := a.List(ctx)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
dtm := make(map[string]bool)
207+
for _, a := range ans {
208+
addDataTypes(dtm, a.DataTypeList)
209+
}
210+
211+
var dts []string
212+
for k := range dtm {
213+
dts = append(dts, k)
214+
}
215+
216+
return dts, nil
217+
}
218+
219+
func addDataTypes(m map[string]bool, dts []string) {
220+
for _, dt := range dts {
221+
if _, ok := m[dt]; !ok {
222+
m[dt] = true
223+
}
224+
}
225+
}

analyzer_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ func TestListByTypeAnalyzers(t *testing.T) {
4343
}
4444
}
4545

46+
func TestDataTypes(t *testing.T) {
47+
client, mux, _, closer := setup()
48+
defer closer()
49+
50+
mux.HandleFunc("/"+analyzersURL, func(w http.ResponseWriter, r *http.Request) {
51+
w.WriteHeader(http.StatusOK)
52+
w.Write(analyzersJSON)
53+
})
54+
55+
got, err := client.Analyzers.DataTypes(context.Background())
56+
if err != nil {
57+
t.Errorf("Analyzer.DataTypes returned error: %v", err)
58+
}
59+
if want := dataTypes; !reflect.DeepEqual(want, got) {
60+
t.Errorf("Analyzer.DataTypes = %+v, want %+v", got, want)
61+
}
62+
}
63+
4664
var analyzerType = "ip"
4765

4866
var analyzersJSON = []byte(`
@@ -109,3 +127,5 @@ var wantList = []Analyzer{
109127
Version: "3.0",
110128
},
111129
}
130+
131+
var dataTypes = []string{"ip"}

0 commit comments

Comments
 (0)