Publish only things who were read 10seconds ago from now

十年热恋 提交于 2020-01-07 02:36:19

问题


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:

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()} },
);

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.

[EDIT] 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()}}]
    })
});

回答1:


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.

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.



来源:https://stackoverflow.com/questions/41937167/publish-only-things-who-were-read-10seconds-ago-from-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!