This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.js
176 lines (164 loc) · 5.22 KB
/
fetch.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* pass in a a driver database handle and get index details back.
* @param {Mongo.db} db database handle from the node driver
* @param {Function} done callback
*/
var _ = require('lodash');
var async = require('async');
var mongodbNS = require('mongodb-ns');
var isNotAuthorizedError = require('mongodb-js-errors').isNotAuthorized;
var debug = require('debug')('mongodb-index-model:fetch');
/**
* helper function to attach objects to the async.auto task structure.
* @param {any} anything pass in any variable to attach it to the name
* @param {Function} done callback function.
*/
function attach(anything, done) {
done(null, anything);
}
/**
* get basic index information via `db.collection.indexes()`
* @param {object} results results from async.auto
* @param {Function} done callback
*/
function getIndexes(results, done) {
var client = results.client;
var ns = mongodbNS(results.namespace);
client
.db(ns.database)
.collection(ns.collection)
.indexes(function(err, indexes) {
if (err) {
debug('getIndexes failed!', err);
done(err);
return;
}
// add ns field to each index
_.each(indexes, function(idx) {
idx.ns = ns.ns;
});
done(null, indexes);
});
}
/**
* get index statistics via `db.collection.aggregate({$indexStats: {}})`
* @param {object} results results from async.auto
* @param {Function} done callback
*/
function getIndexStats(results, done) {
var client = results.client;
var ns = mongodbNS(results.namespace);
var pipeline = [
{ $indexStats: {} },
{
$project: {
name: 1,
usageHost: '$host',
usageCount: '$accesses.ops',
usageSince: '$accesses.since'
}
}
];
debug('Getting $indexStats for %s', results.namespace);
var collection = client.db(ns.database).collection(ns.collection);
collection.aggregate(pipeline, { cursor: {} }).toArray(function(err, res) {
if (err) {
if (isNotAuthorizedError(err)) {
debug('Not authorized to get index stats', err);
/**
* In the 3.2 server, `readWriteAnyDatabase@admin` does not grant sufficient privileges for $indexStats.
* The `clusterMonitor` role is required to run $indexStats.
* @see https://jira.mongodb.org/browse/INT-1520
*/
return done(null, {});
}
if (err.message.match(/Unrecognized pipeline stage name/)) {
debug('$indexStats not yet supported, return empty document', err);
return done(null, {});
}
debug('Unknown error while getting index stats!', err);
return done(err);
}
res = _.mapKeys(res, function(stat) {
return stat.name;
});
done(null, res);
});
}
/**
* get index sizes via `db.collection.stats()` (`indexSizes` field)
* @param {object} results results from async.auto
* @param {Function} done callback
*/
function getIndexSizes(results, done) {
var client = results.client;
var ns = mongodbNS(results.namespace);
debug('Getting index sizes for %s', results.namespace);
client
.db(ns.database)
.collection(ns.collection)
.stats(function(err, res) {
if (err) {
if (isNotAuthorizedError(err)) {
debug(
'Not authorized to get collection stats. Returning default for indexSizes {}.'
);
return done(null, {});
}
debug('Error getting index sizes for %s', results.namespace, err);
return done(err);
}
res = _.mapValues(res.indexSizes, function(size) {
return { size: size };
});
debug('Got index sizes for %s', results.namespace, res);
done(null, res);
});
}
/**
* merge all information together for each index
* @param {object} results results from async.auto
* @param {Function} done callback
*/
function combineStatsAndIndexes(results, done) {
var indexes = results.getIndexes;
var stats = results.getIndexStats;
var sizes = results.getIndexSizes;
_.each(indexes, function(idx, i) {
_.assign(indexes[i], stats[idx.name]);
_.assign(indexes[i], sizes[idx.name]);
});
done(null, indexes);
}
/**
* get basic index information via `db.collection.indexes()`
* @param {MongoClient} client handle from mongodb driver
* @param {String} namespace namespace for which to get indexes
* @param {Function} done callback
*/
function getIndexDetails(client, namespace, done) {
var tasks = {
client: attach.bind(null, client),
namespace: attach.bind(null, namespace),
getIndexes: ['client', 'namespace', getIndexes],
getIndexStats: ['client', 'namespace', getIndexStats],
getIndexSizes: ['client', 'namespace', getIndexSizes],
indexes: [
'getIndexes',
'getIndexStats',
'getIndexSizes',
combineStatsAndIndexes
]
};
debug('Getting index details for namespace %s', namespace);
async.auto(tasks, function(err, results) {
if (err) {
debug('Failed to get index details for namespace %s', namespace, err);
return done(err);
}
debug('Index details for namespace %s', namespace, results.indexes);
// all info was collected in indexes
return done(null, results.indexes);
});
}
module.exports = getIndexDetails;