Process definition statistics based on time spent at each stage in a process

I need to create an API which gives me data sorted upon total historical time taken at each activity within a process definition. Is there any Rest API or some other way this data can be made available.
I am on Camunda 7.17 currently.

similar link but based on count - Get Historic Activity Statistics |

Hi @aasthana4 ,

for statistics only including tasks, have a look on this page: Get Task Report (Historic) | This API supports the reportType “duration”. You would probably want to filter by process definition key and group by e.g. task name. In case you care about different versions of your process definitions you could take the process definition id instead.

In case you need statistics on durations of any activity instance, I do not know of any pre-built option. Someone else might prove me wrong tho. (I would be glad to read about it!)
My approach would be to use the ACT_HI_ACTINST table. It contains the necessary data including start/end time. Since you also have the process definition key available you could do per process definition analysis. If you need to differentiate between different versions you could make use of the process definition id.
While you could aggregate data live using code or data base queries, it could have a performance impact on large amount of data. Then, you might go for custom history event handler which listens for process ends, calculates durations for the finished process and finally stores them somewhere. In that case you would need a mechanism that aggregates data for already finished processes before you implemented such an event handler.

I hope it helps!

Thanks Adagatiya, that helps.
Will give a try to align my requirement to taskReports.
Even I was thinking of last option as to query directly.