influxdata / influxdata/kapacitor

Inconsistent handling of ID when defining templated task with -file option

Open
#1,743 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.4k
Forks
479
Avg merge
4d 16h
Merged PRs (30d)
4

Description

When initializing a templated task with a JSON file such as the following, the ID supplied on the command line is ignored and a UUID is used instead.

`chlorine_low_limit.json`
```
{
"template-id": "conditionAndDuration",
"dbrps": [{"db": "northern_utility_services_org", "rp": "autogen"}],
"vars": {
"measurement": {
"type": "string",
"value": "creekwood"
},
"warnState": {
"type": "lambda",
"value": "\"chlorine_level\" < .29"
},
"warnDuration": {
"type": "lambda",
"value": "\"duration\" > 200"
}
}
}
```
```
$ ## No tasks yet
$ kapacitor list tasks
ID Type Status Executing Databases and Retention Policies
$ ## Define the task from the JSON file
$ kapacitor define chlorine_low_limit -file ./chlorine_low_limit.json
$ ## Observe that it was assigned a UUID for its ID
$ kapacitor list tasks
ID Type Status Executing Databases and Retention Policies
c6a5c58d-aaa0-4c33-af7c-f250175545a5 stream disabled false ["northern_utility_services_org"."autogen"]
$ ## Verify that the task was created correctly (it was)
$ kapacitor show c6a5c58d-aaa0-4c33-af7c-f250175545a5
ID: c6a5c58d-aaa0-4c33-af7c-f250175545a5
Error:
Template: conditionAndDuration
Type: stream
Status: disabled
Executing: false
Created: 23 Dec 17 14:33 AKST
Modified: 23 Dec 17 14:33 AKST
LastEnabled: 01 Jan 01 00:00 UTC
Databases Retention Policies: ["northern_utility_services_org"."autogen"]
TICKscript:
var db = 'northern_utility_services_org'

var rp = 'autogen'

var measurement string

var alertTopic = measurement

var timeUnit = 1m

var warnState lambda

var warnDuration lambda

var site = stream
|from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.groupBy(*)

var stateDuration = site
|stateDuration(warnState)
.unit(timeUnit)
.as('duration')

stateDuration
|alert()
.warn(warnDuration)
.topic(alertTopic)

Vars:
Name Type Value
measurement string creekwood
warnDuration lambda "duration" > 200
warnState lambda "chlorine_level" < .29
DOT:
digraph c6a5c58d-aaa0-4c33-af7c-f250175545a5 {
stream0 -> from1;
from1 -> state_duration2;
state_duration2 -> alert3;
}
```
Clearly the task is being created properly but a UUID is being assigned instead of the supplied command line argument.

I can get the task ID to be correct if I specify the "ID" field in the JSON. In this case, the command line argument is ignored. This is a minor inconvenience given that the ID is normally inherited from the file name when placed in the `tasks` load directory. It is certainly a point of confusion.

It seems to me that the command line argument should be optional when the `-file` option is specified with `kapacitor define`. In my opinion the order of name overrides should be as follows:
1. Command line argument if present
2. "ID" field value in JSON or YAML if present
3. file name (without extension)
4. file name + UUID if task with file name already exists
5. UUID if a new `-uuid` option is supplied, maybe this gets printed back to stdout for convenient use in scripts

In some of these cases you might prefer to just fail instead of actually creating the task, like in the 4th case.

Alternatively the simplest fix is to just default to the command line argument and ignore the "ID" field and fail otherwise.

I am happy to write the fix for this and submit a PR if someone from the dev team signs off on the specification. Ultimately how I think it should work is just my opinion and I don't want to write code that isn't inline with the project's goals.

Thank you

Contributor guide

Open the contributing guide

Research direction

Start with the `kapacitor define -file` command path and reproduce how the command-line name, JSON or YAML `ID`, and file name are currently handled. Clarify the intended precedence before changing behavior; done means the selected ID is consistent with the agreed rule and the file-based definition still creates the expected task.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.