-
Notifications
You must be signed in to change notification settings - Fork 0
/
delSensor.js
64 lines (58 loc) · 1.21 KB
/
delSensor.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
// usage node delsensor.js F3 -force
const config = require("./config.json");
const nano = require("nano")(config.writer);
const weatherdb = nano.use("weatherdb");
const bulk = {
docs: [],
};
var sensor, force;
force = false;
if (process.argv.length > 2) {
if (process.argv.length > 3) {
force = process.argv[3] == "-force";
sensor = process.argv[2];
} else {
if (process.argv[2] == "-force") {
force = true;
} else {
sensor = process.argv[2];
}
}
}
function bulkDelete() {
weatherdb.bulk(bulk, function (err, body) {
console.log(err, body);
});
}
function processRow(row) {
if (typeof sensor != "string" || row.doc.sensorid == sensor) {
bulk.docs.push({
_id: row.doc._id,
_rev: row.doc._rev,
_deleted: true,
});
}
}
weatherdb.view(
"data",
"unknownSensors",
{
reduce: false,
include_docs: true,
},
function (err, body) {
if (!err) {
console.log("Total number of unknown sensors:", body.total_rows);
body.rows.forEach(processRow);
console.log("rows to delete:", bulk.docs.length);
if (force) {
console.log("starting delete");
bulkDelete();
} else {
console.log("use: -force to delete records");
}
} else {
console.log("err:", err.reason);
}
},
);