根据时间戳显示相对时间



我正在使用 Kibana 根据每个微服务的最新值显示多个微服务的状态。我想将 2019 年 4 月 8 日 14:37:13.173 格式替换为 2 分钟前或 2 分 21 秒前的格式。我知道我可以筛选 Kibana 数据条目的最后 15 分钟,但这并不能显示每个服务更新了多长时间。

我尝试进入索引模式并更改日期模式,但它不需要功能,而只需要格式。我可以添加另一个每分钟发送一次的字段,但这只会浪费资源并使一切复杂化。

一旦我知道脚本字段存在,我就能够解决问题,所以这是我用来显示更新多久前的内容:

if(Math.round((new Date().getTime()-doc['@timestamp'].value)/1000)>7200){
    return Math.round((new Date().getTime()-doc['@timestamp'].value)/3600000) + " hours ago";
}else if(Math.round((new Date().getTime()-doc['@timestamp'].value)/1000)>3600){
    return Math.round((new Date().getTime()-doc['@timestamp'].value)/3600000) + " hour ago";
}else if(Math.round((new Date().getTime()-doc['@timestamp'].value)/1000)>120){
    return Math.round((new Date().getTime()-doc['@timestamp'].value)/60000) + " minutes ago";
} else if(Math.round((new Date().getTime()-doc['@timestamp'].value)/1000)>60){
    return Math.round((new Date().getTime()-doc['@timestamp'].value)/60000) + " minute ago";
}else {
    return Math.round((new Date().getTime()-doc['@timestamp'].value)/1000)  + " seconds ago";
}

最新更新