2023-01-31

jq to report rolling differences between array element values

I have an input like below which just has stageIds, along with their submit and completion time in unix time seconds

[
  {
    "stageId": 1,
    "submitTime_epoch_secs": 5,
    "completionTime_epoch_secs": 10
  },
  {
    "stageId": 2,
    "submitTime_epoch_secs": 15,
    "completionTime_epoch_secs": 17
  },
  {
    "stageId": 3,
    "submitTime_epoch_secs": 29,
    "completionTime_epoch_secs": 30
  }
]

desired output is below, where each stageId, submit, and completion times are compared with previous and next and the delay is added as another key/val per element.

[
  {
    "stageId": 1,
    "submitTime_epoch_secs": 5,
    "completionTime_epoch_secs": 10,
    "delayTillNextStageSubmit",5
    "delayFromPrevStageComplete",null
  },
  {
    "stageId": 2,
    "submitTime_epoch_secs": 15,
    "completionTime_epoch_secs": 17,
    "delayTillNextStageSubmit",12
    "delayFromPrevStageComplete",5
  },
  {
    "stageId": 3,
    "submitTime_epoch_secs": 29,
    "completionTime_epoch_secs": 30,
    "delayTillNextStageSubmit",null
    "delayFromPrevStageComplete",12
  }
]

here the stageId 1 delayTillNextStageSubmit is difference between stageId 2 submitTime and stageId 1 completion time, 15 - 10 = 5.

is this possible with jq?

I am new to jq, so don't know how to solve this



No comments:

Post a Comment