reubenmiller / go-c8y-cli

go c8y cli tool for interacting with the Cumulocity IoT API
https://goc8ycli.netlify.app/
MIT License
36 stars 7 forks source link

irregular timestamp evaluation in templates #311

Open reubenmiller opened 1 year ago

reubenmiller commented 1 year ago

There is a strange behaviour where the timestamp is not being evaluated too early for the first 3 elements, then afterwards the timestamp works as expected.

This behaviour is reproducible via the following snippet:

c8y util repeat 5 \
| c8y events create --device 12345 --type "myType" --template "{c8y_Winding:{temperature:{value: 25.0,unit:'°C'}}}" --dry --delay 5s --text testme --dryFormat json \
| c8y util show --select body.time -o csv

The expected outcome is that the time property should be offset by ~5 seconds for each entry, where as the following clearly shows that the time is not uniformly distributed:

2023-09-12T16:42:16.983+10:00
2023-09-12T16:42:17.313+10:00
2023-09-12T16:42:17.814+10:00
2023-09-12T16:42:22.321+10:00
2023-09-12T16:42:27.328+10:00
reubenmiller commented 1 year ago

Investigating the issue and the root cause is that the body template is evaluated when the job is added to the queue and not when the request is sent. This leads to the timestamps being unpredictable when using with delay and delayBefore flags.

There might be a way to delay the evaluation of the body though this will likely be a more complex change (which will take longer).

Until this is fixed, then c8y util repeat --delay 5s before the c8y events create (or creating any body using a template)

c8y util repeat 5 \
| c8y util repeat --delay 5s \
| c8y events create --device 12345 --type "myType" --template "{c8y_Winding:{temperature:{value: 25.0,unit:'°C'}}}" --dry --text testme --dryFormat json \
| c8y util show --select body.time -o csv
2023-09-12T20:25:25.541+10:00
2023-09-12T20:25:30.397+10:00
2023-09-12T20:25:35.397+10:00
2023-09-12T20:25:40.398+10:00
2023-09-12T20:25:45.400+10:00