Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAC-227: Refactor and update to use Clarify v1.1 (breaking) #30

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
grep -v '^[#;]' "$1" | fold -w 72 -s > "$1".rewrite
mv "$1".rewrite "$1"

4 changes: 4 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

set -ex
golangci-lint run --new-from-rev=origin/main ./...
9 changes: 9 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -ex

command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path.\n"; exit 2; }
git lfs pre-push "$@"

go build -v ./...
go test ./... -failfast
60 changes: 60 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Contributing

This document is mainly aimed at audience within Searis AS, and describes information about how to contribute to this repository.

## Configure git

After first check out, run the following command to use our recommended git hooks:

```sh
git config core.hooksPath .githooks
```

Also ensure that the git author configuration correctly, using your _work email_, or an email (if you contribute on behalf of a company). See the [Git set-up guide](https://docs.github.com/en/get-started/quickstart/set-up-git#setting-up-git) from GitHub for details. We also recommend that you set up [commit verification](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification).

## Updating copyright

Whenever updating a file, ensure that the Copyright notice is up-to-date. I.e. if the notice says "Copyright 2022 Searis AS", and the current year is "2028", then update the notice to "Copyright 2022-2028 Searis AS".

If you are not making the contribution on behalf of Searis AS, you should add a new copyright line instead:

```
// Copyright 2028 <Your company> <contact email for legal issues>
// Copyright 2020 Searis AS
```

The copyright notice containing the current year always needs to be on top.

## Atomic commits

Avoid unrelated changes within a commit, and retain linear history. This requires good knowledge of the Git rebase feature.

## Commit message

Let your commit message follow this format:

```txt
<header line preferably less than 50 chars>
<BLANK LINE>
<Body text, wrap at 72 chars.>
```

With more details following below.

### Commit header format

```txt
<component>: <lower case change summary>
```

Use imperative mode in the header change summary. It should not capitalize the first letter and it should not include a trailing period. This means that if the first word does not naturally start with a large letter (e.g. `change`), the summary should start with a lower-case. Words that always start with an upper-cas (e.g `ID` or `MyClass`), should keep their case, also when used as the first word.

When relevant, the header should also include a _component_ prefix. Generally, a relevant portion of the directory path that's changed is a good candidate as a component name, but make sure it's not too long or you will have less space left to describe your changes. Abstract terms like `docs:` or `ci:` are accepted, but path based alternatives such as `README.md:` and `.github:` are preferred.

### Commit body format

The commit message body should consist of full sentences, including capitalized letters and periods. It may also contain lists.

```txt
<Message containing full sentences and potentially lists.>
```
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This SDK is following [Semantic Versioning][semver]. The SDK is currently in a v

## Features

This SDK is currently written for [Clarify API v1.1beta2][docs-v1.1beta2], and include the following features, based on your integration access configuration:
This SDK is currently written for [Clarify API v1.1][docs-v1.1], and include the following features, based on your integration access configuration:

Always possible:

Expand All @@ -22,18 +22,19 @@ Always possible:

When access to the Admin namespace is granted in Clarify ` (scoped to entire organization):

- Read signal meta-data to Clarify with `client.SelectSignals`. Allows side-loading related items. See [examples/select_signals](examples/select_signals/).
- Publish signals as items directly with `client.PublishSignals`, or more conveniently via the `automation` package. See [examples/publish_signals](examples/publish_signals/) for the latter.
- Read signal meta-data to Clarify with `client.Admin().SelectSignals`. Allows side-loading related items. See [examples/select_signals](examples/select_signals/).
- Publish signals as items directly with `client.Admin().PublishSignals`, or more conveniently via the `automation` package. See [examples/publish_signals](examples/publish_signals/) for the latter.

When access to the Clarify namespace is granted in Clarify (scoped to entire organization):

- Read item meta-data from Clarify via `client.SelectItems`. See [examples/select_items](examples/select_items/).
- Read time-series data from Clarify via `client.DataFrame`. See [examples/data_frame](examples/select_items/).
- Read item meta-data from Clarify via `client.Clarify().SelectItems`. See [examples/select_items](examples/select_items/).
- Read time-series data from Clarify via `client.Clarify().DataFrame`. See [examples/data_frame](examples/select_items/).
- Do calculations with data from Clarify via `client.Clarify().Evaluate`. See [examples/evaluate](examples/evaluate/).

[clarify]: https://clarify.io/
[semver]: https://semver.org/
[docs]: https://docs.clarify.io
[docs-v1.1beta2]: https://docs.clarify.io/1.1beta2
[docs-v1.1]: https://docs.clarify.io/1.1

## Setting up automation routines

Expand Down
29 changes: 10 additions & 19 deletions automation/publish_signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"runtime/debug"

"github.com/clarify/clarify-go"
"github.com/clarify/clarify-go/query"
"github.com/clarify/clarify-go/params"
"github.com/clarify/clarify-go/views"
)

Expand Down Expand Up @@ -100,7 +100,7 @@ type PublishSignals struct {

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

// 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 := params.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 @@ -197,10 +190,8 @@ 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)
func (p PublishSignals) addItems(ctx context.Context, dest map[string]views.ItemSave, c *clarify.Client, integrationID string, q params.ResourceQuery, opts PublishOptions) (bool, error) {
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