-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance-dump(yield).js
98 lines (89 loc) · 3.42 KB
/
performance-dump(yield).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
//npm: co, colors, commander, mongodb
#!/usr/bin/env node
const MongoClient = require('mongodb').MongoClient,
assert = require('assert'),
fs = require('fs'),
co = require('co'),
colors = require('colors'),
program = require('commander');
program
.version('1.0.0')
.option('-i, --integer <n>', 'The number of performances you want to dump', parseInt)
.option('-t, --time <time>', '[required] The start time of dumping')
.option('-u, --url <url>', '[required] The url of the MongoDB to connect to')
.option('-f, --file <file>', '[required] The name of output file')
.parse(process.argv);
function make_red(txt) {
return colors.red(txt); //display the help text in red on the console
}
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);
}
});
});
}
function getResult(db, performanceList) {
let promiseList = [];
for(let i of performanceList)
promiseList.push(getSensorDataPromise(db, i));
return Promise.all(promiseList);
}
// url = 'mongodb://localhost:3001/meteor';
function connectMongoClient(url) {
return new Promise(function (resolve, reject) {
MongoClient.connect(url, function (err, db) {
if (err)
reject(err);
else
resolve(db);
})
})
}
function *makePerformances() {
let performanceNumbers = program.integer || null,
startTime = new Date(program.time),
outputFile = program.file,
url = program.url,
db = undefined;
try {
if (outputFile === undefined || outputFile === null || url === null || program.time === undefined) {
program.outputHelp(colors.red);
throw new Error("Please read Options and input the required items!");
}
if (isNaN((startTime).getTime()))
throw new Error("Check the format of the start time you've just inputted!");
db = yield connectMongoClient(url);
let performanceList = yield getPerformancePromise(db, startTime, performanceNumbers),
result = yield getResult(db, performanceList);
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 (e) {
console.error(e);
} finally {
if(db)
db.close();
}
}
co(makePerformances());