Splunk Search

chart sorting by last field

aaronkorn
Splunk Employee
Splunk Employee

Hello,

We have the following chart which displays current ticket counts over the last 7 days for different groups but need to be able to sort on the count for the last day. In this case we would need to sort on Jul 30 but it would need to be dynamic and always sort on the most recent date in the chart. Here is the search we have:

 index=reporter | dedup TKT_NUMBER | eval time=strptime(LASTOCCURRENCE, "%b %d %Y %I:%M%p") | bucket time span=1d | convert timeformat="%b %d" ctime("time") | chart count by NOTIFY_GROUP, time | rename NOTIFY_GROUP AS Group

Is this possible?
alt text

Gilberto_Castil
Splunk Employee
Splunk Employee

This is tricky because of the dynamic nature of your field. Here is one easy way to change this but there is a beautification aspect to note. Notice how you've lost the precision on your field names as those have now become part of the table itself.

Assuming that you consistently have ten (10) rows in your table and seven (7) columns, this will create the desired effect:

| transpose 10 | transpose 10 | fields - column | sort - "row 7"

This will change the format of your original table, like the one above, to something like this:

alt text

The other side effect is that by using the transpose command you've lost the ability to drill down.


[07-31-2130] Update:

While thinking about this, it occurred to me that renaming the last field is simple as long as it is understood that the dynamic "time" variable that you have is actually a list.

alt text

The key here is to perform a comparison between the value of the "time" list and the "today" list. If the comparison fits, then we should assign the value of the latest ("today") variable to the appropriate value in the "time" list.

The comparison I used was all using the ephoc date. It is just easier for me to deal with numeric values, rather than strings

| eval time=if(round((today-time)/60/60/24)=0,"Today",time)

Then we convert to the string desired:

| eval time=if(time="Today",time,strftime(time, "%b %d"))

alt text


And, finally, you can apply the chart transformation and sort:

| chart count over NOTIFY_GROUP by time 
| rename NOTIFY_GROUP AS Group
| sort - Today

alt text


This will not change your format and it will keep click precision.

aaronkorn
Splunk Employee
Splunk Employee

I tried sending an email but it came back undeliverable

0 Karma

aaronkorn
Splunk Employee
Splunk Employee

Also, now the dates are in alphabetical order. Anyway to get around that? Thanks!

0 Karma

aaronkorn
Splunk Employee
Splunk Employee

Still didnt seem to do it. I think adding the new eval helped but the issue is probably still with the LASOCCURRENCE not being epoch

0 Karma

Gilberto_Castil
Splunk Employee
Splunk Employee

I think I see the problem. You need to instantiate a value for the field "today":

| eval today=round(time())

-- Your search might work with this:

index=reporter
| dedup TKT_NUMBER
| eval time=strptime(LASTOCCURRENCE, "%b %d %Y %I:%M%p")
| bucket time span=1d
| eval today=round(time())
| eval time=if(round((today-time)/60/60/24)=0,"Today",time)
| eval time=if(time="Today",time,strftime(time, "%b %d"))
| chart count by NOTIFY_GROUP, time
| rename NOTIFY_GROUP AS Group
| sort - Today

0 Karma

aaronkorn
Splunk Employee
Splunk Employee

Its in the following format: Jul 31 2013 1:49PM

Is there a way we can convert to epoch then eval on that?

0 Karma

Gilberto_Castil
Splunk Employee
Splunk Employee

Correct. If LASTOCCURRENCE is not epoch, the calculation suggested does not work. What is the original format for LASTOCCURRENCE?

0 Karma

aaronkorn
Splunk Employee
Splunk Employee

Thanks for the detailed explanation! I tried adding it to my previous search but it still doesn't appear to be working. I wonder since LASTOCCURRENCE isnt epoch that why its throwing it off...

index=reporter | dedup TKT_NUMBER | eval time=strptime(LASTOCCURRENCE, "%b %d %Y %I:%M%p") | bucket time span=1d | eval time=if(round((today-time)/60/60/24)=0,"Today",time) | eval time=if(time="Today",time,strftime(time, "%b %d")) | chart count by NOTIFY_GROUP, time | rename NOTIFY_GROUP AS Group

0 Karma

Gilberto_Castil
Splunk Employee
Splunk Employee

I did some testing with this and found a solution. They key is compare the dynamic "time" variable to the "today" variable.

time=if((today-time)<24hours,"Today",time)

That will assign the value of "Today" to the dynamic "time" variable and then you can perform the sort by the "Today" column. I was challenged with the string comparisons so defaulted using pure epoch times for the comparison and it worked.

| eval time=if(round((today-time)/60/60/24)=0,"Today",time)

--

I will post more if needed.

0 Karma

aaronkorn
Splunk Employee
Splunk Employee

Thanks for the response. Couldnt we do something like this?

index=reporter | dedup TKT_NUMBER | eval time=strptime(LASTOCCURRENCE, "%b %d %Y %I:%M%p") | bucket time span=1d | eval today=time() | convert timeformat="%b %d" ctime("time") ctime("today") | chart count by NOTIFY_GROUP, time | rename NOTIFY_GROUP AS Group | sort - "today"

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...