仅发布从现在起10秒钟前阅读的内容 [英] Publish only things who were read 10seconds ago from now
问题描述
我需要从我的CPU,内存和其他存储在集合中的东西发布统计信息.事情是,它只需要从现在开始发布最后10秒钟.
我加入了一张图片以帮助对此进行理解:
I need to publish the stats from my CPU, Memory and other things that are stored in a collection. The things is it need to publish only the 10 last seconds from now.
I join a picture to help the understanding of this:
我编写了一个函数,但问题是它不会删除比现在早11秒,12秒的信息.
And I coded a function but the problem is that it doesn't remove the infos that are 11sec,12sec... older than now.
Meteor.publish('dockerStats', function readInfosDockerStats(timeLimitSecond) {
console.log(moment(new Date()).subtract(timeLimitSecond, 'second').toDate())
return DockerStats.find(
{ read: { "$gte": moment(new Date()).subtract(timeLimitSecond, 'second').unix()} },
);
我认为我需要使用$and
并查找
I think I need to use $and
and to find infos that are
>now-10sec && <now
但是我不知道该怎么做,所以我正在寻求您的帮助.
But I don't know how to make it so I'm asking for your help.
我添加了$and
的唯一问题是它仅发布1次(信息一直在集合中写入,但没有发布):
I added the $and
the only problem is that it publish only 1time (the infos keep being writing in the collection but it doesn't publish them):
Meteor.publish('dockerStats', function readInfosDockerStats(timeLimitSecond) {
return DockerStats.find(
{ $and: [{ read:{"$gte": moment(new Date()).subtract(timeLimitSecond, 'second').unix()}},{read:{"$lte": moment(new Date()).unix()}}]
})
});
推荐答案
流星出版物以导致您出现此问题的方式...
Meteor publications work in a way that is causing you this problem...
有选择地从服务器发布数据.但是,一旦发布,它就不会从客户端集合中删除.因此,随着越来越多的数据发布,您的客户端数据会随着时间增长.
Data is published selectively from the server. But once published, it is not removed from the client side collection. So your client side data is growing over time as more and more data is published.
因此,您还需要在客户端代码(即您的订阅)中进行过滤.这样可以解决问题.但是,它将依赖于客户端和服务器之间的准确时间同步.
So you will need to do a filter in your client code (ie your subscription) as well. That will fix the problem. It will, however, rely on exact time synchronisation between client and server.
随着时间的流逝,您的客户端收集也会增加,最终会导致一些内存/性能问题.因此,您可能需要考虑另一种方法.您可以创建一个由服务器管理的单独集合,以仅包含最后10个数据.删除较旧的数据,并且客户端订阅很简单,无需在客户端和服务器之间进行精确的时钟同步.
Over time your client side collection will also grow, and eventually cause you some memory/performance problems. So you might want to consider a different approach. You could create a separate collection which is managed by the server, to just contain the last 10s of data. Older data is deleted, and the client subscription is simple, with no need for precise clock sync between client and server.
这篇关于仅发布从现在起10秒钟前阅读的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!