Skip to content

Commit

Permalink
refactor!: CIRC-10299: Update FindTags() to return whether the result…
Browse files Browse the repository at this point in the history
…s are an estimate (#109)
  • Loading branch information
dhaifley committed May 19, 2023
1 parent 70d7654 commit 0dadc03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
10 changes: 10 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ to [Semantic Versioning](http://semver.org/) rules.

## [Next Release]

## [v1.14.0] - 2023-05-19

* refactor!: Modifies the FindTagsResult values returned by the FindTags()
functions so that they do not include a separate FindCount value, but use the
count and estimate fields on the FindTagsResult when the query is count only.
* feat: Includes accurate values in FindTagsResult.Estimate when results
from a FindTags() query return estimated results from IRONdb. This can be
used to determine that a timeout occurred during processing by IRONdb.

## [v1.13.9] - 2023-03-31

* fix: Ensures PromQLMetadataQuery() only returns metrics with valid PromQL
Expand Down Expand Up @@ -502,6 +511,7 @@ writing to histogram endpoints.
any delay, once started. Created: 2019-03-12. Fixed: 2019-03-13.

[Next Release]: https://github.com/circonus-labs/gosnowth
[v1.14.0]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.14.0
[v1.13.9]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.9
[v1.13.8]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.8
[v1.13.7]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.7
Expand Down
52 changes: 27 additions & 25 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ type FindTagsItem struct {

// FindTagsResult values contain the results of a find tags request.
type FindTagsResult struct {
Items []FindTagsItem
FindCount *FindTagsCount
Count int64
}

// FindTagsCount values represent results from count only requests.
type FindTagsCount struct {
Count int64 `json:"count"`
Estimate bool `json:"estimate"`
Items []FindTagsItem `json:"items,omitempty"`
Count int64 `json:"count"`
Estimate bool `json:"estimate"`
}

// FindTagsOptions values contain optional parameters to be passed to the
Expand Down Expand Up @@ -274,28 +268,36 @@ func (sc *SnowthClient) FindTagsContext(ctx context.Context, accountID int64,
}

if options.CountOnly != 0 {
if err := decodeJSON(body, &r.FindCount); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
} else {
if err := decodeJSON(body, &r.Items); err != nil {
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}

return r, nil
}

if err := decodeJSON(body, &r.Items); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}

// Return a results count and capture it from the header , if provided.
r.Count = int64(len(r.Items))

if header != nil {
c := header.Get("X-Snowth-Search-Result-Count")
if c != "" {
if cv, err := strconv.ParseInt(c, 10, 64); err == nil {
r.Count = cv
}
if header == nil {
return r, nil
}

if v := header.Get("X-Snowth-Search-Result-Count"); v != "" {
if iv, err := strconv.ParseInt(v, 10, 64); err == nil {
r.Count = iv
}
}

if v := header.Get("X-Snowth-Search-Result-Count-Is-Estimate"); v != "" {
if bv, err := strconv.ParseBool(v); err == nil {
r.Estimate = bv
}
}

return r, err
return r, nil
}

// FindTagCats retrieves tag categories that are associated with the
Expand Down Expand Up @@ -337,7 +339,7 @@ func (sc *SnowthClient) FindTagCatsContext(ctx context.Context,
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}

return r, err
return r, nil
}

// FindTagVals retrieves tag values that are associated with the
Expand Down Expand Up @@ -380,7 +382,7 @@ func (sc *SnowthClient) FindTagValsContext(ctx context.Context,
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}

return r, err
return r, nil
}

// CheckTags values contain check tag data from IRONdb.
Expand Down Expand Up @@ -431,7 +433,7 @@ func (sc *SnowthClient) GetCheckTagsContext(ctx context.Context,
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}

return r, err
return r, nil
}

// DeleteCheckTags removes check tags from IRONdb for a specified check.
Expand Down
26 changes: 17 additions & 9 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

const tagsCountTestData = `{"count":22,"estimate":false}`
const tagsCountTestData = `{"count":22,"estimate":true}`

const getCheckTagsTestData = `{
"11223344-5566-7788-9900-aabbccddeeff":["test:test"]
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestFindTagsJSON(t *testing.T) {
}
}

func TestFindTags(t *testing.T) { //nolint:gocyclo
func TestFindTags(t *testing.T) { //nolint:gocyclo,maintidx
t.Parallel()

ms := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,
Expand Down Expand Up @@ -160,8 +160,12 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
t.Fatal(err)
}

if res.Count != 1 {
t.Fatalf("Expected result count: 1, got: %v", res.Count)
if res.Count != 22 {
t.Errorf("Expected result count: 22, got: %v", res.Count)
}

if !res.Estimate {
t.Errorf("Expected result estimate: true, got: %v", res.Estimate)
}

res, err = sc.FindTags(1, "test", &FindTagsOptions{
Expand All @@ -176,8 +180,12 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
t.Fatal(err)
}

if res.Estimate {
t.Errorf("Expected result estimate: false, got: %v", res.Estimate)
}

if res.Count != 1 {
t.Fatalf("Expected result count: 1, got: %v", res.Count)
t.Errorf("Expected result count: 1, got: %v", res.Count)
}

if len(res.Items) != 1 {
Expand All @@ -201,7 +209,7 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
}

if res.Count != 1 {
t.Fatalf("Expected result count: 1, got: %v", res.Count)
t.Errorf("Expected result count: 1, got: %v", res.Count)
}

if len(res.Items) != 1 {
Expand Down Expand Up @@ -233,7 +241,7 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
}

if res.Items[0].Activity[1][1] != 1561848300 {
t.Fatalf("Expected activity timestamp: 1561848300, got %v",
t.Errorf("Expected activity timestamp: 1561848300, got %v",
res.Items[0].Activity[1][1])
}

Expand All @@ -243,7 +251,7 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
}

if res.Count != 1 {
t.Fatalf("Expected result count: 1, got: %v", res.Count)
t.Errorf("Expected result count: 1, got: %v", res.Count)
}

if len(res.Items) != 1 {
Expand Down Expand Up @@ -279,7 +287,7 @@ func TestFindTags(t *testing.T) { //nolint:gocyclo
}

if res.Items[0].Activity[1][1] != 1561848300 {
t.Fatalf("Expected activity timestamp: 1561848300, got %v",
t.Errorf("Expected activity timestamp: 1561848300, got %v",
res.Items[0].Activity[1][1])
}
}
Expand Down

0 comments on commit 0dadc03

Please sign in to comment.