-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-dump(promise).js
79 lines (71 loc) · 3.18 KB
/
performance-dump(promise).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
//npm: mongodb, commander
let MongoClient = require('mongodb').MongoClient,
assert = require('assert'),
fs = require('fs');
// url = 'mongodb://localhost:3001/meteor';
let program = require('commander');
program
.version('0.0.1')
.option('-i, --integer <n>', 'An integer argument', parseInt)
.option('-t, --time <time>', 'The start time of dumping')
.option('-u, --url <url>', 'The url of the MongoDB to connect to')
.option('-f, --file <file>', 'The name of output file')
.parse(process.argv);
function getPerformancePromise(db, startTime, performanceNumbers) {
return new Promise(function (resolve, reject) {
db.collection('Performances').find({"updatedAt": {$gt: startTime}}, {limit: performanceNumbers}).sort({"updatedAt": 1}).toArray((err, performanceList) => {
if (err) {
reject(err);
} else {
resolve(performanceList);
}
});
});
}
function getSensorDataPromise(db, performance) {
return new Promise(function (resolve, reject) {
db.collection('SensorDatas').findOne({_id: performance.sensorDataId}, {}, function(err, sensorData) {
if (err) {
reject(err);
} else {
delete performance.sensorDataId;
performance.sensorData = sensorData;
resolve(performance);
}
});
});
}
let makePerformances = function() {
let performanceNumbers = program.integer || null,
startTime = new Date(program.time),
outputFile = program.file,
url = program.url;
if (outputFile === undefined || outputFile === null)
throw new Error("Please use -f to input the name of the output file!");
if (url === null)
throw new Error("Please use -u to input the url of MongoDB!");
if (program.time === undefined)
throw new Error("Please use -t to input the start time of dumping!");
if (isNaN((startTime).getTime()))
throw new Error("Check the format of the start time you've just inputted!");
MongoClient.connect(url, function (err, db) {
if (err) {
throw new Error("MongoClient connect Error!");
} else {
getPerformancePromise(db, startTime, performanceNumbers).then(function (performanceList) {
let performancePromises = [];
for (let i of performanceList)
performancePromises.push(getSensorDataPromise(db, i));
Promise.all(performancePromises).then(function (result) {
// console.log(result);
db.close();
let resultJSON = JSON.stringify(result);
fs.writeFileSync(outputFile, resultJSON);
// 生成一个日志,记录操作时间、实际转换数量、本次转换开始时间、下次转换开始时间
console.log(new Date() + ': ' + 'dumped number: ' + result.length + ' startTime: ' + startTime + ' next_startTime: ' + result[result.length - 1].updatedAt.toISOString());
}).catch(error => console.log(error));
}).catch(error => console.log(error));
}
});
};
makePerformances();