Skip to content

Commit

Permalink
lib refactor, switch to API v1.1
Browse files Browse the repository at this point in the history
Switch all method to use API v1.1. Add support for the new clarify.evaluate method. Refactor the library for improved maintenantce and readability.

Package resource (removed):
- Move types that should remain public to the views pacakge.
- Simplify and move request and method types to a new internal/request pacakge.

Package query (breaking changes):
- Query: Make fields private; introduce chainable API.
- Filter: Convert to interface with private method requirements.
- Data: Make fields private; introduce chaniable API.
- DataFilter: Convert to interface with private method requirements.

Root pacakge:
- Client:
  - Remove most of the chainable API for request types. Pass in more parameters during request initialization (breaking).
  - Move admin namespace methods to c.Admin().<method>; refactor to reduce chainable API.
  - Move clarify namespace methods to c.Clarify().<method>; refactor to reduce chainable API.
  - Dupliucate integration namespace methods to c.Integration().<method>.
  • Loading branch information
smyrman committed Oct 17, 2023
1 parent 97fff22 commit 5fc00f9
Show file tree
Hide file tree
Showing 35 changed files with 1,627 additions and 989 deletions.
25 changes: 8 additions & 17 deletions automation/publish_signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type PublishSignals struct {

// SignalsFilter can optionally be specified to limit which signals to
// publish.
SignalsFilter query.FilterType
SignalsFilter query.Filter

// TransformVersion should be changed if you want existing items to be
// republished despite the source signal being unchanged.
Expand Down Expand Up @@ -130,13 +130,7 @@ func (p PublishSignals) Do(ctx context.Context, c *clarify.Client, opts PublishO

// We iterate signals without requesting the total count. This is an
// optimization bet that total % limit == 0 is uncommon.
q := query.Query{
Sort: []string{"id"},
Limit: selectSignalsPageSize,
}
if p.SignalsFilter != nil {
q.Filter = p.SignalsFilter.Filter()
}
q := query.Where(p.SignalsFilter).Sort("id").Limit(selectSignalsPageSize)

items := make(map[string]views.ItemSave)
flush := func(integrationID string) error {
Expand All @@ -150,7 +144,7 @@ func (p PublishSignals) Do(ctx context.Context, c *clarify.Client, opts PublishO
opts.EncodeJSON(items)
}
if !opts.DryRun {
result, err := c.PublishSignals(integrationID, items).Do(ctx)
result, err := c.Admin().PublishSignals(integrationID, items).Do(ctx)
if err != nil {
return fmt.Errorf("failed to publish signals: %w", err)
}
Expand All @@ -166,7 +160,7 @@ func (p PublishSignals) Do(ctx context.Context, c *clarify.Client, opts PublishO
}

for _, id := range p.Integrations {
q.Skip = 0

more := true
for more {
if err := ctx.Err(); err != nil {
Expand All @@ -178,13 +172,12 @@ func (p PublishSignals) Do(ctx context.Context, c *clarify.Client, opts PublishO
if err != nil {
return err
}
q.Skip += q.Limit

if len(items) >= publishSignalsPageSize {
if err := flush(id); err != nil {
return err
}
}
q = q.NextPage()
}

if err := flush(id); err != nil {
Expand All @@ -198,9 +191,7 @@ func (p PublishSignals) Do(ctx context.Context, c *clarify.Client, opts PublishO
// addItems adds items that require update to dest from all signals matching
// the integration ID and query q.
func (p PublishSignals) addItems(ctx context.Context, dest map[string]views.ItemSave, c *clarify.Client, integrationID string, q query.Query, opts PublishOptions) (bool, error) {
results, err := c.SelectSignals(integrationID).
Query(q).
Include("item").Do(ctx)
results, err := c.Admin().SelectSignals(integrationID, q).Include("item").Do(ctx)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -252,12 +243,12 @@ func (p PublishSignals) addItems(ctx context.Context, dest map[string]views.Item
if results.Meta.Total >= 0 {
// More can be calculated exactly when the total count was requested (or
// calculated for free by the backend).
more = q.Skip+len(results.Data) < results.Meta.Total
more = q.GetSkip()+len(results.Data) < results.Meta.Total
} else {
// Fallback to approximate the more value when total was not explicitly
// requested. For large values of q.Limit, this approximation is likely
// faster -- on average -- then to request a total count.
more = (len(results.Data) == q.Limit)
more = (len(results.Data) == q.GetLimit())
}
return more, nil

Expand Down
Loading

0 comments on commit 5fc00f9

Please sign in to comment.