Nested Aggregation On Date Differance

Hello,
i have data each document in it have history array like following

......
"history" : [
{
"user" : {
"id" : "14",
"firstName" : "user14",
"lastName" : "user14",
"username" : "user14",
"principalType" : "user"
},
"time" : "2018-06-25T01:05:20Z",
"action" : "create",
"newStatus" : "in progress",
"isEdit" : false,
"data" : { }
},
{
"user" : {
"id" : "5b2d98a1c4fae4a5ab10e841",
"firstName" : "Ramy",
"lastName" : "Ebrahim",
"username" : "Ramy",
"principalType" : "ministry-employee"
},
"time" : "2018-06-30T18:37:21Z",
"action" : "patchAttributes",
"status" : "start",
"newStatus" : "review",
"isEdit" : false,
"data" : { }
},
{
"user" : {
"id" : "5b2d98a1c4fdfda45cb10e841",
"firstName" : "Yasser",
"lastName" : "Mohamed",
"username" : "Yasser",
"principalType" : "ministry-employee"
},
"time" : "2018-07-01T23:28:27Z",
"action" : "patchAttributes",
"status" : "review",
"newStatus" : "confirm1",
"isEdit" : false,
"data" : { }
}
]

i want to aggregate on the newStatus to get the average response time of each status depending on time diff , each object have newStatus and time attributes what i want to do for each status subtract the time of the previous status from current status time then get the average response of of each status within bucket like this

result
"buckets": [
{
"key": "in progress",
"doc_count": 100,
"responseTime": {
"value": 1.48
}
},
{
"key": "review",
"doc_count": 88,
"responseTime": {
"value": 1.5454545454545454
}
},
{
"key": "confirm1",
"doc_count": 47,
"responseTime": {
"value": 2.021276595744681
}
},
{
"key": "rejected",
"doc_count": 28,
"responseTime": {
"value": 1.4285714285714286
}
},
{
"key": "confirm2",
"doc_count": 23,
"responseTime": {
"value": 1
}
},
{
"key": "completed",
"doc_count": 15,
"responseTime": {
"value": 1
}
}
]