UNPKG

3.84 kBJavaScriptView Raw
1var _ = require('lodash');
2var AmpersandModel = require('ampersand-model');
3var AmpersandCollection = require('ampersand-rest-collection');
4var AmpersandState = require('ampersand-state');
5var DatabaseCollection = require('mongodb-database-model').Collection;
6var CollectionCollection = require('mongodb-collection-model').Collection;
7var hostname = require('os').hostname();
8
9var HostInfo = AmpersandState.extend({
10 props: {
11 system_time: 'date',
12 hostname: 'string',
13 os: 'string',
14 os_family: 'string',
15 kernel_version: 'string',
16 kernel_version_string: 'string',
17 memory_bits: 'number',
18 memory_page_size: 'number',
19 arch: 'string',
20 cpu_cores: 'number',
21 cpu_cores_physical: 'number',
22 cpu_scheduler: 'string',
23 cpu_frequency: 'number',
24 cpu_string: 'string',
25 cpu_bits: 'number',
26 machine_model: 'string',
27 feature_numa: 'boolean',
28 feature_always_full_sync: 'number',
29 feature_nfs_async: 'number'
30 }
31});
32
33var BuildInfo = AmpersandState.extend({
34 props: {
35 version: 'string',
36 commit: 'string',
37 commit_url: 'string',
38 flags_loader: 'string',
39 flags_compiler: 'string',
40 allocator: 'string',
41 javascript_engine: 'string',
42 debug: 'boolean',
43 for_bits: 'number',
44 max_bson_object_size: 'number',
45 enterprise_module: 'boolean'
46 }
47});
48
49var genuineMongoDB = AmpersandState.extend({
50 props: {
51 isGenuine: 'boolean',
52 dbType: 'string'
53 }
54});
55
56var dataLake = AmpersandState.extend({
57 props: {
58 isDataLake: 'boolean',
59 version: 'string'
60 }
61});
62
63var Instance = AmpersandModel.extend({
64 modelType: 'Instance',
65 idAttribute: '_id',
66 props: {
67 _id: {
68 type: 'string',
69 required: true
70 },
71 name: {
72 type: 'string'
73 },
74 /**
75 * @todo (imlucas) Stabilize and cleanup
76 * `replicaset`, `state`, and `aliases`.
77 *
78 * See: http://npm.im/mongodb-replicaset
79 */
80 replicaset: 'string',
81 state: 'string',
82 aliases: {
83 type: 'array',
84 default: function() {
85 return [];
86 }
87 }
88 },
89 derived: {
90 hostname: {
91 deps: ['_id'],
92 fn: function() {
93 if (!this._id) {
94 return undefined;
95 }
96 return this._id.split(':')[0];
97 }
98 },
99 port: {
100 deps: ['_id'],
101 fn: function() {
102 if (!this._id) {
103 return undefined;
104 }
105 return parseInt(this._id.split(':')[1], 10);
106 }
107 }
108 },
109 collections: {
110 databases: DatabaseCollection,
111 collections: CollectionCollection
112 },
113 children: {
114 host: HostInfo,
115 build: BuildInfo,
116 genuineMongoDB: genuineMongoDB,
117 dataLake: dataLake
118 },
119 /**
120 * Override `AmpersandModel.serialize()` to
121 * make logging less chatty and problems easier
122 * to debug.
123 *
124 * @return {Object}
125 */
126 serialize: function() {
127 var res = this.getAttributes({
128 props: true,
129 derived: true
130 }, true);
131
132 var model = this;
133 if (this.databases.length > 0) {
134 _.each(model._children, function(value, key) {
135 res[key] = model[key].serialize();
136 });
137 _.each(model._collections, function(value, key) {
138 res[key] = model[key].serialize();
139 });
140 }
141 return res;
142 }
143});
144
145var InstanceCollection = AmpersandCollection.extend({
146 comparator: '_id',
147 model: Instance,
148 modelType: 'InstanceCollection'
149});
150
151module.exports = Instance;
152module.exports.Collection = InstanceCollection;
153module.exports.BuildInfo = BuildInfo;
154module.exports.HostInfo = HostInfo;
155
156module.exports.getId = function(id) {
157 if (typeof id === 'number') {
158 id = 'localhost:' + id;
159 }
160 return id.toLowerCase()
161 .replace(hostname.toLowerCase(), 'localhost')
162 .replace('127.0.0.1', 'localhost')
163 .replace('mongodb://', '')
164 .replace(/^[^@]+@/, '')
165 .replace(/\/?\?(?:\w+=[^&]+&)*\w+=[^&]+$/, '');
166};