繁体   English   中英

在 kibana 仪表板中过滤特定日期

[英]Filter specfic dates in kibana dashboard

我正在使用 ELK – 7.12.1,在 Kibana 仪表板中,我需要使用无痛过滤低于假期日期。

01-Jan-2021
14-Jan-2021
26-Jan-2021
11-Mar-2021
02-Apr-2021
13-Apr-2021
14-May-2021
21-Jul-2021
10-Sep-2021
15-Oct-2021
01-Nov-2021
05-Nov-2021

我有如下脚本无痛。

Language: painless
Type: string
Format: String
Script:
def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
    if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
        return true
    }
    else {
        return false
    }
}

索引模式 > my-index > 脚本字段 > 假期 > 预览结果

[
 {
  "_id": "38009464",
  "@timestamp": "2021-02-26T11:11:39.707Z",
  "holidays": [
   null
  ]
 },
 {
  "_id": "38026158",
  "@timestamp": "2021-02-26T11:11:39.727Z",
  "holidays": [
   null
  ]
 },
 {
  "_id": "38030065",
  "@timestamp": "2021-02-26T11:11:39.735Z",
  "holidays": [
   null
  ]

它返回null 那么我该如何解决这个问题以过滤真(或)假? 因为这将检查时间戳是否在这些日期中的任何一天,如果是则返回 true。 然后只需要在仪表板上过滤holiday = False就是这个想法。

有人可以帮我解决这个问题吗? 这会很有帮助。

您看到的是空值,因为您的脚本在不是一月时不会返回任何内容。 外部 if 没有 else 对应项。 当条件不匹配时会发生什么?

注意:目前,尽管您进行了介绍,但您的脚本正在返回:

  • 以下日期为真:01.01、14.01、26.01、11.01、02.01、13.01、21.01、10.01、05.01
  • 1 月剩余天数为 false
  • 什么都没有(即,null 代表一年中的 rest。

您需要修复脚本以涵盖所有情况,而不仅仅是一月份。 您可以简单地添加一个 else 条件,如下所示。

def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
    if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
        return true
    }
    else {
        return false
    }
} else {
  return false;
}

甚至更好:

def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
    if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
        return true
    }
}
// No previous condition matches, return false
return false;

所以你离你正在寻找的解决方案还很远。

要修复您的脚本以使其正常工作,您应该(正确地)涵盖所有情况。 给定一个较短的列表:

01-Jan-2021 --> 01.01.2021
14-Jan-2021 --> 14.01.2021
26-Jan-2021 --> 26.01.2021
11-Mar-2021 --> 11.03.2021

if(month == 1) {
 // Given is January
 if([01, 14, 26].contains(day)) {
    return true;
 }
} else if (month == 3) {
 // Given is March
 if (day == 11) {
   return true;
 }
}
// If the date is not in the blacklist
return false;

显然,在这里我没有涵盖所有案例(提供了这个答案应该很容易实现)也没有涵盖年份,但也许你应该考虑一下。

亲切的问候,米尔科

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM