Dismissile
Dismissile

Reputation: 33071

Formatting milliseconds smartly

I have a query that summarizes some data that is in milliseconds:

requests
| summarize avg(duration) by name

When I display this data, I would like to format it so that it shows the amount of time in a smart way:

Examples:

I know that the built in graphs for Application Insights does this in the Performance tab, but I can't find a built in function that does it. Does anyone know if there is a function that does this?

Upvotes: 2

Views: 1423

Answers (1)

Yoni L.
Yoni L.

Reputation: 25895

below is one option, using timespan arithmetics and some string manipulation

alternatively - if it suits your requirements - you can use format_timespan(): https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/format-timespanfunction

let format_ms_as_pretty_string = (t:long)
{
    case(t < 1s / 1ms, strcat(t, " ms"),
         t < 1m / 1ms, strcat(round(1.0 * t * 1ms / 1s, 3), " secs"),
         t < 1h / 1ms, strcat(round(1.0 * t * 1ms / 1m, 3), " mins"),
         strcat(round(1.0 * t * 1ms / 1h, 3), " hours"))
}
;
let input = datatable(t:long) [1500,300,120000,8481600,360100] // replace this with your input
;
input
| extend output = format_ms_as_pretty_string(t)
t output
1500 1.5 secs
300 300 ms
120000 2.0 mins
8481600 2.356 hours
360100 6.002 mins

Upvotes: 4

Related Questions