Skip to content

Commit

Permalink
improve log message for invalid plugin sid value
Browse files Browse the repository at this point in the history
  • Loading branch information
rkspx committed Mar 15, 2022
1 parent c54bb01 commit 6317d7a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
28 changes: 15 additions & 13 deletions internal/pkg/dpluger/es6client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,47 +72,48 @@ func (es *es6Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t
return
}

agg, found := searchResult.Aggregations.Terms("finalAgg")
roots, found := searchResult.Aggregations.Terms("finalAgg")
if !found {
err = errors.New("cannot find aggregation finalAgg in ES query result")
return
}
count := len(agg.Buckets)
count := len(roots.Buckets)
if count == 0 {
err = errors.New("cannot find matching entry in field " + sidSource + " on index " + plugin.Index)
return
}

fmt.Printf("found %d unique '%s'", count, sidSource)
fmt.Printf("found %d unique '%s'\n", count, sidSource)
nID, err := strconv.Atoi(plugin.Fields.PluginID)
if err != nil {
return
}

for _, lvl1Bucket := range agg.Buckets {
subterm, found := lvl1Bucket.Terms("subterm")
for _, rootBucket := range roots.Buckets {
sidlist, found := rootBucket.Terms("subterm")
if !found {
continue
}
for _, lvl2Bucket := range subterm.Buckets {
sKey := lvl1Bucket.Key.(string)
nKey, err := toInt(lvl2Bucket.Key)

for _, sidBucket := range sidlist.Buckets {
root := rootBucket.Key.(string)
sid, err := toInt(sidBucket.Key)
if err != nil {
return c, fmt.Errorf("invalid sid aggregation key, %s", err.Error())
return c, fmt.Errorf("invalid signature ID, %s", err.Error())
}
// fmt.Println("item1:", sKey, "item2:", nKey)
if shouldCollectCategory {
subSubTerm, found2 := lvl1Bucket.Terms("subSubTerm")
subSubTerm, found2 := rootBucket.Terms("subSubTerm")
if !found2 {
continue
}
for _, lvl3Bucket := range subSubTerm.Buckets {
sCat := lvl3Bucket.Key.(string)
_ = c.upsert(plugin.Name, nID, &nKey, sCat, sKey)
_ = c.upsert(plugin.Name, nID, &sid, sCat, root)
break
}
} else {
_ = c.upsert(plugin.Name, nID, &nKey, categorySource, sKey)
_ = c.upsert(plugin.Name, nID, &sid, categorySource, root)
}
break
}
Expand Down Expand Up @@ -167,7 +168,8 @@ func (es *es6Client) Collect(plugin Plugin, confFile, sidSource, esFilter, categ
err = errors.New("cannot find matching entry in field " + sidSource + " on index " + plugin.Index)
return
}
fmt.Println("Found", count, "uniq "+sidSource+".")

fmt.Printf("found %d unique '%s'\n", count, sidSource)
newSID := 1
nID, err := strconv.Atoi(plugin.Fields.PluginID)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/dpluger/es7client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func (es *es7Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t

for _, sidBucket := range SIDs.Buckets {
rootKey := root.Key.(string)
pluginSID, err := toInt(sidBucket.Key)
sid, err := toInt(sidBucket.Key)
if err != nil {
return ref, fmt.Errorf("invalid SID aggregation key, %s", err.Error())
return ref, fmt.Errorf("invalid signature ID, %s", err.Error())
}

if shouldCollectCategory {
Expand All @@ -117,12 +117,12 @@ func (es *es7Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t

for _, categoryBucket := range categories.Buckets {
category := categoryBucket.Key.(string)
ref.upsert(plugin.Name, pluginID, &pluginSID, category, rootKey)
ref.upsert(plugin.Name, pluginID, &sid, category, rootKey)
break
}

} else {
ref.upsert(plugin.Name, pluginID, &pluginSID, categorySource, rootKey)
ref.upsert(plugin.Name, pluginID, &sid, categorySource, rootKey)
}

break
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/dpluger/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func toInt(v interface{}) (int, error) {
case string:
n, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return 0, err
return 0, fmt.Errorf("expecting numeric value, got '%s'", t)
}

if n >= 0 && n < math.MaxInt32 {
Expand All @@ -265,5 +265,5 @@ func toInt(v interface{}) (int, error) {
return 0, ErrIntValueExceedBoundary
}

return 0, fmt.Errorf("invalid value type, %T", v)
return 0, fmt.Errorf("invalid numeric value type, %T", v)
}

0 comments on commit 6317d7a

Please sign in to comment.