Creating day of week filter on my dashboard

Hello there,

I am trying to create Day of Week filter that would enable me to exclude day of the week which I do not want to see in the reports on my Dashboard.

What I did is created a new script:

doc['createdDate'].value.getDayOfWeek()

preview of the script is:

so, when I created a filter on dashboard I got this:

image

Instead of numbers I would like to see the names of the days: Monday, Tuesday, etc. How could I do this?

Hi @Valerija

What version are you on?

If you are on a newer version 8.x you should use a runtime field instead of a scripted field.

You can add them to a mapping or Data View / Index pattern

Here is my quick code...

int day = doc['@timestamp'].value.getDayOfWeek().getValue();
String dayOfWeek = "unkown";

if (day == DayOfWeek.MONDAY.value) {
  dayOfWeek = "Monday";
} else if (day == DayOfWeek.TUESDAY.value) {
  dayOfWeek = "Tuesday";
} else if (day == DayOfWeek.WEDNESDAY.value) {
  dayOfWeek = "Wednesday";
} else if (day == DayOfWeek.THURSDAY.value) {
  dayOfWeek = "Thursday";
} else if (day == DayOfWeek.FRIDAY.value) {
  dayOfWeek = "Friday";
} else if (day == DayOfWeek.SATURDAY.value) {
  dayOfWeek = "Saturday";
} else if (day == DayOfWeek.SUNDAY.value) {
  dayOfWeek = "Sunday";
} else {
  dayOfWeek = "unkown";
}

emit(dayOfWeek);

Now in my control drop down

if you want to have it available for all filters searches etc you need to add it to the mapping / template

Hello @stephenb

Thank you very much for the explanation.

I am on 7.10 version. What you have described as a solution is available in this version as well or not?

I should add the script you provided as a new scripted field or?

I would like this filter to apply to all reports. When you say "you need to add it to the mapping / template" what do you mean?

Sorry, I am totally new to this tool so I am probably asking a lot of questions about basic things.

Thank you in advance.
Valerija

Hi @Valerija '

7.10 is very very very old you should upgrade as a matter of urgency.

Make sure you look at the correct docs for scripting :

Runtime fields do not exist in 7.10 so you will need to continue with scripted fields which are at search time not mapping

So you will need to write a script similar to mine but with the correct syntax The script I showed should be pretty close but at the end instead of emit I think it will be.

return dayOfWeek;

Hi @stephenb

I am also surprised we are using such an old version. I will check that internally.

I wrote a script as you suggested...I am sending printscreen.

Script is created but there are not results.

Regards.
Valerija

Not sure but at the top it looks like you're trying to return a number but the script returns, text or keyword

Hello @stephenb ,

I am not sure what is wrong.

I was googling some solutions and run into discussion from 2021 - Exclude weekends(Saturdays and Sundays) from my dashboard view.

This filter would solve all of my problems. I did read through the discussion but am not sure how to write the scrip in the end.

I tried like this but no success...

That's an invalid script You just show it about..

The script is a program it needs to be defined and then logic and then returned...

What I showed you earlier is closer but you're going to have to debug it. No other way around it.

And apologies I don't have it but old 7.10 to test it on....

I brought a 7.10 docker realy quick

This looks like it works my script above showed the exact error when I tried

I had to change
int day = doc['@timestamp'].value.getDayOfWeek().getValue();
to this
int day = doc['@timestamp'].value.getDayOfWeek();

and add the return statement

Then it worked fine.

int day = doc['@timestamp'].value.getDayOfWeek();
String dayOfWeek = "unkown";

if (day == DayOfWeek.MONDAY.value) {
  dayOfWeek = "Monday";
} else if (day == DayOfWeek.TUESDAY.value) {
  dayOfWeek = "Tuesday";
} else if (day == DayOfWeek.WEDNESDAY.value) {
  dayOfWeek = "Wednesday";
} else if (day == DayOfWeek.THURSDAY.value) {
  dayOfWeek = "Thursday";
} else if (day == DayOfWeek.FRIDAY.value) {
  dayOfWeek = "Friday";
} else if (day == DayOfWeek.SATURDAY.value) {
  dayOfWeek = "Saturday";
} else if (day == DayOfWeek.SUNDAY.value) {
  dayOfWeek = "Sunday";
} else {
  dayOfWeek = "unkown";
}

return dayOfWeek;

Added it to the index pattern

Then made a control and it worked fine, note I only have 1 day so far...

Hello @stephenb

Thank you so much for extra effort and going back to 7.10 ver.

I did as you explained but now I have another issue. I assume that this is because some actualStart fields are empty.

Google is your friend :slight_smile:
I just searched

painless script check if field exists

Found several answers

So you could use code like that to check if the field exists and if it does not set a default value or return a default value

You're basically writing a small program so all the normal programming things apply.

Hello @stephenb

The scrip works now! :slight_smile: Thank you so so much for all your help and your patience. I learned a lot from converstions with you about Kibana.

Thanks once again!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.