-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrail_monitor-client.js
122 lines (97 loc) · 4.49 KB
/
trail_monitor-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if (Trail.Settings.Config.monitor) {
var monitorPublishName = Trail.Settings.Config.monitorPublishName;
if (Meteor.isClient) {
var latestMessagesCollection = new Meteor.Collection('trail');
Trail.MonitorManager = (function() {
function MonitorManager() {
this._collection = new Meteor.Collection(null);
this._monitors = {};
}
MonitorManager.prototype.addMonitor = function(monitor) {
var name = monitor.getName();
this._monitors[name] = monitor;
this._collection.insert({name: name});
};
MonitorManager.prototype.removeMonitor = function(monitor) {
var name = monitor.getName();
delete this._monitors[name];
};
MonitorManager.prototype.getMonitorNamesReactive = function() {
return this._collection.find().fetch();
};
MonitorManager.prototype.getMonitorByName = function(name) {
return this._monitors[name];
};
return MonitorManager;
})();
Trail.Instance.monitorManager = new Trail.MonitorManager();
Trail.Monitor = (function() {
function Monitor(name, filter, options) {
filter = typeof filter === 'object' ? filter : {};
options = typeof options === 'object' ? options : {};
this._name = name;
this._subscriptionHandle = Meteor.subscribe(monitorPublishName, filter, Trail.Origin.id);
this._options = {
unmanaged: !!options.unmanaged,
limit: typeof options.limit === 'number' ? options.limit : Trail.Settings.Config.monitorDefaultLimit
};
this._collection = new Meteor.Collection(null);
this._latestRemoteItemId = 0;
this._localItemId = 0;
var self = this;
this._computation = Deps.autorun(function() {
// Construct selector:
var selector = Trail.Util.constructSelectorFromFilter(filter, Trail.Origin.id);
selector._monitorItemId = {$gt: self._latestRemoteItemId};
// Perform query:
var newItems = latestMessagesCollection.find(selector);
// Add new items to the local collection of items:
newItems.forEach(function(item) {
if (item._monitorItemId > self._latestRemoteItemId) {
self._latestRemoteItemId = item._monitorItemId;
}
self._collection.insert({
_itemId: ++self._localItemId,
timestamp: item.timestamp,
level: item.level,
levelValue: item.levelValue,
message: item.message,
meta: item.meta
});
});
// Remove local items that don't fit within the limit:
self._collection.remove({
_itemId: {$lte: self._localItemId - self._options.limit}
});
});
if (!this._options.unmanaged) {
Trail.Instance.monitorManager.addMonitor(this);
}
}
Monitor.prototype.getName = function() {
return this._name;
};
Monitor.prototype.getItemsReactive = function() {
return this._collection.find();
};
Monitor.prototype.destroy = function() {
this._computation.stop();
this._subscriptionHandle.stop();
if (!this._options.unmanaged) {
Trail.Instance.monitorManager.removeMonitor(this);
}
};
return Monitor;
})();
// Create all monitors defined in the config settings:
var monitors = Trail.Settings.Config.monitors;
if (monitors instanceof Array) {
for (var i = 0; i < monitors.length; ++i) {
var monitorDef = monitors[i];
if (!monitorDef.disable && typeof monitorDef.name === 'string') {
var monitor = new Trail.Monitor(monitorDef.name, monitorDef.filter, monitorDef.options);
}
}
}
}
}