This repository has been archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes2couchdb.js
186 lines (156 loc) · 3.89 KB
/
nodes2couchdb.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
177
178
179
180
181
182
183
184
185
186
var cradle = require('cradle');
var rest = require('restler');
var async = require('async');
cradle.setup({
host: '95.85.40.145',
auth: { username: 'root', password: '*******' },
cache: false,
raw: false,
retries: 3,
retryTimeout: 10 * 1000
});
var nodes2couchdb = function() {
//Private
var connection = new(cradle.Connection);
var db = connection.database('libremap-dev');
function showError(msg) {
console.log("ERROR in nodes2couchdb: ", msg);
return msg;
}
//Public
return({
/**
* TODO
*/
init: function(callback) {
db.exists(function (err, exists) {
if (err) {
showError(err);
callback(false);
} else if (exists) {
//console.log('Yeah! The database exists');
callback(true);
} else {
showError('database does not exists.');
//db.create();
/*TODO populate design documents */
callback(false);
}
});
},
/**
* TODO
*/
getRoutersFromNetmon: function(callback) {
// var routers = [];
// routers = require('./nodes.json');
// callback(routers);
rest.get('http://localhost:9001/nodes?status=online').once('success', function(data, response) {
callback(data);
});
},
/**
* TODO
*/
createTempView: function(callback) {
db.temporaryView({
map: function (doc) {
if (doc.type === "router") {
emit(doc.hostname, {
_rev: doc._rev,
ctime: doc.ctime
})
}
}
}, function (err, res) {
if (err) {
console.log(err);
return
}
//console.log(res);
callback();
});
},
/**
* TODO
*/
getNodesByHostname: function(hostname, callback) {
db.view('libremap-api/routers_by_site', { key: hostname }, function (err, doc) {
if (err) return showError(err);
callback(doc.rows);
});
},
/**
* TODO
*/
getNodesByNetmonId: function(netmonId, callback) {
db.view('libremap-api/routers_by_netmonid', { key: netmonId }, function (err, doc) {
if (err) return showError(err);
callback(doc.rows);
});
},
/**
* TODO
*/
saveDocument: function(doc, callback) {
db.save(doc,
function (err, res) {
if (err) return showError(err);
callback(res.id);
});
},
/**
* TODO
*/
updateDocument: function(id, doc, callback) {
db.merge(id, doc, function (err, res) {
if (err) return showError(err);
callback(res._rev);
});
}
});
};
function updateNodes2couchdb() {
var n2c = nodes2couchdb();
n2c.init(function(exists) {
if (!exists) return;
n2c.getRoutersFromNetmon(function(routers) {
if (!routers || routers.length <= 0)
return console.log("ERROR: netmon API returned no routers");
function updateRouter(router) {
if (!router.hostname || router.hostname.length <= 0) return;
//n2c.getNodesByHostname(router.hostname, function(doc) {
n2c.getNodesByNetmonId(router.attributes.netmon.id, function(doc) {
if (doc.length === 1) {
//router already exists in the DB
//console.log(doc[0]);
console.log("router \"" + router.hostname + "\" already exists in the DB");
router._rev = doc[0]._rev;
delete router.ctime;
delete router.id;
n2c.updateDocument(doc[0].id, router, function(rev) {
console.log("document updated: ", rev);
});
} else if (doc.length > 1){
console.log("WARN: multible routers with the same name found: ", docs);
} else {
//router does not yet exis in the DB
console.log("router \"" + router.hostname + "\" not found");
n2c.saveDocument(router, function(id) {
console.log("document creaded: ", id);
});
}
});
}
// updateRouter(routers[0]);
routers.forEach(function(router) {
updateRouter(router);
});
});
});
}
updateNodes2couchdb();
//update every 15n minutes
setInterval(function(){
updateNodes2couchdb();
}, 15 * 60000);