-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
119 lines (104 loc) · 2.98 KB
/
main.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
import { Meteor } from 'meteor/meteor'
import { EJSON } from 'meteor/ejson'
import { _ } from 'meteor/underscore'
const SUBSCRIPTIONS = Meteor.connection._subscriptions
function _getSubscriptionByIndex (index) {
const idx = +index
const key = idx >= 0
? _.keys(SUBSCRIPTIONS)[idx]
: idx
return _.isString(key)
? SUBSCRIPTIONS[key]
: null
}
export class SA {
static list () {
const output = _.map(SUBSCRIPTIONS, sub => ({
'id': sub.id,
'name': sub.name,
'params': JSON.stringify(sub.params),
'inactive': sub.inactive,
'ready': sub.ready,
'ddp session': sub.connection._lastSessionId
}))
console.info(new Date().toLocaleString())
console.table(output)
}
static show (
index,
fields = ['id', 'name', 'params', 'inactive', 'ready'],
isFormattedOutput = false
) {
const sub = _getSubscriptionByIndex(index)
if (!sub) {
console.error('Subscription %s not found', index)
return
}
const output = Array.isArray(fields)
? _.pick(sub, fields)
: (fields === '*' ? sub : null)
if (isFormattedOutput) {
console.log(JSON.stringify(output, null, 2))
} else {
console.dir(output)
}
}
static showDetails (index) {
this.show(index, '*')
}
static showParams (index) {
this.show(index, ['params'], true)
}
static stop (index) {
const sub = _getSubscriptionByIndex(index)
if (sub) {
sub.stop()
console.info('Subscription %s was stopped', sub.id)
} else {
console.error('Subscription %s not found', index)
}
}
static collList () {
const collections = Meteor.connection._mongo_livedata_collections
const output = _.map(collections, (collection, name) => ({
collection: name,
size: collection.find().count()
}))
console.info(new Date().toLocaleString())
console.table(output)
}
static showDups () {
const idxSubsByName = _.groupBy(_.values(SUBSCRIPTIONS), 'name')
const subsGroups = _.filter(idxSubsByName, group => group.length > 1)
_.each(subsGroups, subsGroup => {
const params = _.pluck(subsGroup, 'params')
const dups = {}
_.each(subsGroup, sub => {
const subscriptionId = sub.id
_.each(params, param => {
if (EJSON.equals(sub.params, param)) {
dups[subscriptionId] = _.has(dups, subscriptionId)
? dups[subscriptionId] + 1
: 0
}
})
})
const subIds = _.filter(_.keys(dups), subId => dups[subId] > 0)
if (subIds.length === 0) return
const idxSubsById = _.indexBy(SUBSCRIPTIONS, 'id')
const output = {}
_.each(subIds, subId => {
const sub = idxSubsById[subId]
if (sub) {
output[subId] = {
'params': JSON.stringify(sub.params),
'ddp session': sub.connection._lastSessionId
}
}
})
console.group(subsGroup[0].name)
console.table(output)
console.groupEnd()
})
}
}