Skip to content

Commit

Permalink
Merge pull request #206 from bonitoo-io/feat/tasks
Browse files Browse the repository at this point in the history
feat: adding Tasks API
  • Loading branch information
vlastahajek committed Oct 22, 2020
2 parents 1c8cc22 + 195d8e5 commit f8b3f3d
Show file tree
Hide file tree
Showing 17 changed files with 3,688 additions and 2,415 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.2.0 [in progress]
### Features
1. [#206](https://github.com/influxdata/influxdb-client-go/pull/206) Adding TasksAPI for managing tasks and associated logs and runs.

### Bug fixes

Expand Down
4 changes: 3 additions & 1 deletion api/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (b *bucketsAPI) getBuckets(ctx context.Context, params *domain.GetBucketsPa
for _, opt := range pagingOptions {
opt(options)
}
params.Limit = &options.limit
if options.limit > 0 {
params.Limit = &options.limit
}
params.Offset = &options.offset

response, err := b.apiClient.GetBucketsWithResponse(ctx, params)
Expand Down
54 changes: 54 additions & 0 deletions api/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,57 @@ func ExampleDeleteAPI() {
// Close the client
client.Close()
}

func ExampleTasksAPI() {
// Create a new client using an InfluxDB server base URL and an authentication token
client := influxdb2.NewClient("http://localhost:8086", "my-token")

ctx := context.Background()
// Get Delete API client
tasksAPI := client.TasksAPI()
// Get organization that will own task
myorg, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
if err != nil {
panic(err)
}
// task flux script from https://www.influxdata.com/blog/writing-tasks-and-setting-up-alerts-for-influxdb-cloud/
flux := `fruitCollected = from(bucket: “farming”)
|> range(start: -task.every)
|> filter(fn: (r) => (r._measurement == “totalFruitsCollected))
|> filter(fn: (r) => (r._field == “fruits))
|> group(columns: [“farmName”])
|> aggregateWindow(fn: sum, every: task.every)
|> map(fn: (r) => {
return: _time: r._time, _stop: r._stop, _start: r._start, _measurement: “fruitCollectionRate”, _field: “fruits”, _value: r._value, farmName: farmName,
}
})
fruitCollected
|> to(bucket: “farming”)
`
task, err := tasksAPI.CreateTaskWithEvery(ctx, "fruitCollectedRate", flux, "1h", *myorg.Id)
if err != nil {
panic(err)
}
// Force running a task
run, err := tasksAPI.RunManually(ctx, task)
if err != nil {
panic(err)
}

fmt.Println("Forced run completed on ", *run.FinishedAt, " with status ", *run.Status)

// Print logs
logs, err := tasksAPI.FindRunLogs(ctx, run)
if err != nil {
panic(err)
}

fmt.Println("Log:")
for _, logEvent := range logs {
fmt.Println(" Time:", *logEvent.Time, ", Message: ", *logEvent.Message)
}

// Close the client
client.Close()
}
4 changes: 3 additions & 1 deletion api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (o *organizationsAPI) getOrganizations(ctx context.Context, params *domain.
for _, opt := range pagingOptions {
opt(options)
}
params.Limit = &options.limit
if options.limit > 0 {
params.Limit = &options.limit
}
params.Offset = &options.offset
params.Descending = &options.descending
response, err := o.apiClient.GetOrgsWithResponse(ctx, params)
Expand Down
13 changes: 3 additions & 10 deletions api/paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Paging struct {
// Default 0.
offset domain.Offset
// Maximum number of items returned.
// Default 20, minimum 1 and maximum 100.
// Default 0 - not applied
limit domain.Limit
// What field should be used for sorting
sortBy string
Expand All @@ -26,21 +26,14 @@ type Paging struct {
after domain.After
}

// defaultPagingOptions returns default paging options: offset 0, limit 20, default sorting, ascending
// defaultPagingOptions returns default paging options: offset 0, limit 0 (not applied), default sorting, ascending
func defaultPaging() *Paging {
return &Paging{limit: 20, offset: 0, sortBy: "", descending: false, after: ""}
return &Paging{limit: 0, offset: 0, sortBy: "", descending: false, after: ""}
}

// PagingWithLimit sets limit option - maximum number of items returned.
// Default 20, minimum 1 and maximum 100.
func PagingWithLimit(limit int) PagingOption {
return func(p *Paging) {
if limit > 100 {
limit = 100
}
if limit < 1 {
limit = 1
}
p.limit = domain.Limit(limit)
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/paging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestPaging(t *testing.T) {

paging = &Paging{}
PagingWithLimit(0)(paging)
assert.Equal(t, domain.Limit(1), paging.limit)
assert.Equal(t, domain.Limit(0), paging.limit)

paging = &Paging{}
PagingWithLimit(1000)(paging)
assert.Equal(t, domain.Limit(100), paging.limit)
assert.Equal(t, domain.Limit(1000), paging.limit)
}
Loading

0 comments on commit f8b3f3d

Please sign in to comment.