-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
107 lines (100 loc) · 3.51 KB
/
query.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
var sqlite3 = require('sqlite3').verbose();
var path = require("path");
var chalk = require("chalk");
var fs = require("fs");
var queue = require("queue");
var stompit = require("stompit");
var dbpath = path.join(__dirname, "db", "devtest.sqlite3");
var connectOptions = {
"host": "localhost",
"port": "61613",
"connectionHeaders": {
"host": "/",
"login": "admin",
"passcode": "admin"
}
}
var db = new sqlite3.Database(dbpath, (err) => {
if (err) console.log(chalk.red(err));
else console.log(chalk.green("Connection to the sqlite DB located at " + dbpath));
});
var csvHeaders = "one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve"
var directory = path.join(__dirname, "output")
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory)
}
db.serialize(() => {
db.all("SELECT * from csvEntries", function (err, rows) {
if (err) console.log(chalk.red(err))
else {
var lines = []
lines[0] = csvHeaders;
rows.forEach((row) => {
lines.push(`${row.one}|${row.two}|${row.three}|${row.four}|${row.five}|${row.six}|${row.seven}|${row.eight}|${row.nine}|${row.ten}|${row.eleven}|${row.twelve}`)
})
}
var csvData = lines.join("\n");
fs.open(path.join(directory, "fromDb.csv"), "w", (err) => {
if (err) throw new Error(err);
else {
fs.writeFile(path.join(directory, "fromDb.csv"), csvData, (err) => {
if (err) throw new Error(err);
else console.log("Data from Sqlite table csvEntries has been entered into " + directory + "/fromDb.csv")
})
}
})
})
})
db.close();
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
var msgbuffer = [];
function readAMQ() {
stompit.connect(connectOptions, function (error, client) {
if (error) {
console.log('Unable to connect: ' + error.message);
return;
}
client.subscribe({ "destination": "/queue/dev", "ack": "client-individual" }, function (error, message) {
if (error) {
console.log('subscribe error ' + error.message);
return;
}
message.readString('utf-8', function (error, body) {
if (error) {
console.log('read message error ' + error.message);
return;
}
msgbuffer.push(body);
client.ack(message);
client.disconnect();
});
});
});
}
for (let i = 0; i < 80; i++) {
readAMQ();
sleep(1);
}
setTimeout(() => {
var lines2 = []
console.log(lines2)
lines2[0] = csvHeaders;
console.log(msgbuffer.length)
msgbuffer.forEach((elem) => {
var row = JSON.parse(elem)
lines2.push(`${row.one}|${row.two}|${row.three}|${row.four}|${row.five}|${row.six}|${row.seven}|${row.eight}|${row.nine}|${row.ten}|${row.eleven}|${row.twelve}`)
})
var csvData2 = lines2.join("\n");
fs.open(path.join(directory, "fromActiveMQ.csv"), "w", (err) => {
if (err) throw new Error(err);
else {
fs.writeFile(path.join(directory, "fromActiveMQ.csv"), csvData2, (err) => {
if (err) throw new Error(err);
else console.log("Data from ActiveMQ has been entered into " + directory + "/fromActiveMq.csv")
})
}
})
}, 1000)