Skip to content

Commit

Permalink
fix: creating Tasks with import statements (#352)
Browse files Browse the repository at this point in the history
* fix: creating `Tasks` with `import` statements

* docs: update CHANGELOG.md
  • Loading branch information
bednar authored May 18, 2022
1 parent 19cf00f commit 06ac268
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bug Fixes
1. [#339](https://github.com/influxdata/influxdb-client-java/pull/339): Evaluation of connection string
1. [#352](https://github.com/influxdata/influxdb-client-java/pull/352): Creating `Tasks` with `import` statements

## 6.0.0 [2022-04-19]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,11 @@ private Task createTask(@Nonnull final String name,
repetition += "cron: ";
repetition += "\"" + cron + "\"";
}
String fluxWithOptions = String.format("option task = {name: \"%s\", %s} \n %s", name, repetition, flux);

int fromBegin = flux.indexOf("from");
String fluxWithOptions = flux.substring(0, fromBegin)
+ String.format("option task = {name: \"%s\", %s}\n\n", name, repetition)
+ flux.substring(fromBegin);

task.setFlux(fluxWithOptions);

Expand Down
48 changes: 48 additions & 0 deletions client/src/test/java/com/influxdb/client/ITTasksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.influxdb.client.domain.Run;
import com.influxdb.client.domain.RunManually;
import com.influxdb.client.domain.Task;
import com.influxdb.client.domain.TaskCreateRequest;
import com.influxdb.client.domain.TaskStatusType;
import com.influxdb.client.domain.User;
import com.influxdb.exceptions.NotFoundException;
Expand Down Expand Up @@ -183,6 +184,53 @@ void createTaskCron() {
Assertions.assertThat(task.getLinks().getLabels()).isEqualTo("/api/v2/tasks/" + task.getId() + "/labels");
}

@Test
void createTaskByTaskCreateRequest() {
String taskName = generateName("it task");
String taskFlux = "import \"experimental\"\n"
+ "\n"
+ "option task = {name: \""+taskName+"\", cron: \"12 0 * * * *\"}"
+ "\n"
+ "from(bucket: \"my-bucket\")\n"
+ " |> range(start: experimental.addDuration(d: -1d, to: today()), stop: today())\n"
+ " |> max()";

TaskCreateRequest taskCreateRequest = new TaskCreateRequest()
.orgID(organization.getId())
.flux(taskFlux)
.status(TaskStatusType.ACTIVE);

Task task = tasksApi.createTask(taskCreateRequest);

Assertions.assertThat(task).isNotNull();
Assertions.assertThat(task.getId()).isNotBlank();
Assertions.assertThat(task.getName()).isEqualTo(taskName);
Assertions.assertThat(task.getOrgID()).isEqualTo(organization.getId());
Assertions.assertThat(task.getStatus()).isEqualTo(TaskStatusType.ACTIVE);
Assertions.assertThat(task.getCron()).isEqualTo("12 0 * * * *");
Assertions.assertThat(task.getFlux()).isEqualToIgnoringWhitespace(taskFlux);
}

@Test
void createTaskWithImport() {
String taskName = generateName("it task");
String taskFlux = "import \"experimental\"\n"
+ "\n"
+ "from(bucket: \"my-bucket\")\n"
+ " |> range(start: experimental.addDuration(d: -1d, to: today()), stop: today())\n"
+ " |> max()";

Task task = tasksApi.createTaskCron(taskName, taskFlux, "10 0 * * * *", organization.getId());

Assertions.assertThat(task).isNotNull();
Assertions.assertThat(task.getId()).isNotBlank();
Assertions.assertThat(task.getName()).isEqualTo(taskName);
Assertions.assertThat(task.getOrgID()).isEqualTo(organization.getId());
Assertions.assertThat(task.getStatus()).isEqualTo(TaskStatusType.ACTIVE);
Assertions.assertThat(task.getCron()).isEqualTo("10 0 * * * *");
Assertions.assertThat(task.getFlux()).contains("import \"experimental\"\n");
}

@Test
void findTaskByID() {

Expand Down

0 comments on commit 06ac268

Please sign in to comment.