Splunk Search

How to format the duration on weekly summary

auaave
Communicator

Hi,

I have the below Query. I want to have the sum of duration per week / description on time format [h]:mm:ss.
On line 5, I have inserted | eval duration=tostring(eventduration,"duration"), however, the moment I insert line 23 | chart max(eventduration) as sum over DESCRIPTION by weeknumber, the duration is back in seconds.

Thanks!

| dedup IDEVENT 
| where _time>relative_time(now(),"-4w@w") 
| bin _time span=1w 
| stats sum(DURATION) as eventduration by DESCRIPTION _time 
| eval duration=tostring(eventduration,"duration")
| appendpipe 
    [| stats latest(duration) as sortfield latest(_time) as _time by DESCRIPTION 
    | sort - sortfield limit=10 
    | streamstats count as descorder 
    | eval descorder=descorder+100 
    | table DESCRIPTION descorder]
| eventstats max(descorder) as descorder by DESCRIPTION
| where isnotnull(descorder)
| eval DESCRIPTION = descorder." ".DESCRIPTION
| fields - descorder
| appendpipe 
[| stats min(_time) as mintime max(_time) as maxtime 
| eval rollover=if(strftime(mintime,"%Y")!=strftime(maxtime,"%Y"),"Y","N")
| table rollover]
| eventstats max(rollover) as rollover
| eval weeknumber=strftime(_time,"%U")
| eval weeknumber=case(weeknumber<10 AND rollover="Y","Wk_".weeknumber,  true(),"Wk".weeknumber)
| chart max(eventduration) as sum over DESCRIPTION by weeknumber
| eval DESCRIPTION=substr(DESCRIPTION,5,len(DESCRIPTION)-4)
0 Karma

cmerriman
Super Champion

it is because you are using max(eventduration) and not max(duration) in the chart command on line 23.

i'm not sure if it will max the right way, because duration will be a string. you might want something like:

| dedup IDEVENT 
 | where _time>relative_time(now(),"-4w@w") 
 | bin _time span=1w 
 | stats sum(DURATION) as eventduration by DESCRIPTION _time 
 | appendpipe 
     [| stats latest(eventduration) as sortfield latest(_time) as _time by DESCRIPTION 
     | sort - sortfield limit=10 
     | streamstats count as descorder 
     | eval descorder=descorder+100 
     | table DESCRIPTION descorder]
 | eventstats max(descorder) as descorder by DESCRIPTION
 | where isnotnull(descorder)
 | eval DESCRIPTION = descorder." ".DESCRIPTION
 | fields - descorder
 | appendpipe 
 [| stats min(_time) as mintime max(_time) as maxtime 
 | eval rollover=if(strftime(mintime,"%Y")!=strftime(maxtime,"%Y"),"Y","N")
 | table rollover]
 | eventstats max(rollover) as rollover
 | eval weeknumber=strftime(_time,"%U")
 | eval weeknumber=case(weeknumber<10 AND rollover="Y","Wk_".weeknumber,  true(),"Wk".weeknumber)
 | chart max(eventduration) as sum over DESCRIPTION by weeknumber
  | eval duration=tostring(sum,"duration")
  |fields - sum
 | eval DESCRIPTION=substr(DESCRIPTION,5,len(DESCRIPTION)-4)
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi auaave,
you have to maintain duration in seconds for all the search so you can use it for all calculations, at the end, before output, you can format it.
So move line 5 at the end of your search.
Bye.
Giuseppe

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Introducing Splunk 10.0: Smarter, Faster, and More Powerful Than Ever

Now On Demand Whether you're managing complex deployments or looking to future-proof your data ...

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...