time - 根据时间戳显示相对时间
问题描述
我正在使用 Kibana 根据每个微服务的最新值显示多个微服务的状态。我想用 2 分钟前或 2 分钟 21 秒前的格式替换 2019 年 4 月 8 日 14:37:13.173 格式。我知道我可以过滤过去 15 分钟的 Kibana 数据条目,但这并不能显示每个服务更新的时间。
我已经尝试进入索引模式并更改日期模式,但它不包含函数,而只包含格式。我可以添加另一个每分钟发送一次的字段,但这只会浪费资源并使一切复杂化。
解决方案
一旦我知道脚本字段存在,我就能够解决这个问题,所以这就是我用来显示更新多久之前的内容:
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";
}
推荐阅读
- android - 将活动传递给另一个对象是一种好习惯吗?
- php - 在 DB2 中进行合并时值不兼容
- cron - 复杂的 Cron 调度器
- javascript - 创建新用户帐户并发送电子邮件以创建密码 - Firebase
- c# - 如何将日期 dd/mm/yyyy hh:mm:ss AM 转换为 mm/dd/yyyy hh:mm:ss: am throwing invalid cast 异常
- python-3.x - 在另一列熊猫数据框中获取布尔列的值
- r - 在 dplyr 中使用“first”来选择组的第一个不是 NA 的值
- c# - 如何在内存映射文件中写入冗长的字符串?
- json - Kotlinx 序列化无法识别 Json.parse() 或 Data.serializer()
- c# - 如何为我的 UserControl 绑定不同的 DataContext?