-
Notifications
You must be signed in to change notification settings - Fork 1
/
load-tranf-prod.js
82 lines (71 loc) · 2.14 KB
/
load-tranf-prod.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
const fs = require('fs-extra');
const r = require('rethinkdb');
const config = require("./src/node/config.js");
const Color = require('color');
const {promisify} = require('util');
function transform (buffer) {
let jsonDataBuffered = JSON.parse(buffer.toString());
let pixels = [];
for (let index in jsonDataBuffered) {
let pixel = jsonDataBuffered[index];
let parsedObjColor = Color(pixel.color).object();
for (let i in parsedObjColor) {
pixel[i] = parsedObjColor[i].toString();
}
delete pixel.color; delete pixel.alpha;
pixels.push(pixel);
}
return pixels;
}
function loadAndTransformProcess (filename, sizePart) {
let buf = fs.readFileSync(filename);
let pixels = transform(buf);
let totalPixelsByPart = sizePart;
let totalParts = Math.ceil(pixels.length / totalPixelsByPart);
try {
fs.removeSync('data');
fs.mkdirSync('data');
let initialPartValue = 0,
finalPartValue = totalPixelsByPart - 1;
for (let i = 0; i < totalParts; i++) {
part = pixels.slice(initialPartValue, finalPartValue);
fs.writeFileSync(`data/file_${i}.json`, JSON.stringify(part));
initialPartValue += totalPixelsByPart;
finalPartValue = (totalPixelsByPart * (i + 2)) - 1;
}
} catch (e) {
console.log(e.message);
}
return totalParts;
}
function savePixelInDb(pixel) {
config.rethinkdb.timeout = 3600;
r.connect(config.rethinkdb).then(function(conn) {
r.table('pixels').insert([pixel]).run(conn, (err, result) => {
if (err) throw err;
conn.close({noreplyWait: true}).then(function() {
}).error(function(err) {
})
});
}).catch((err) => {
console.log(err);
});
}
function savePixels(pixels) {
pixels.forEach((pixel) => {
savePixelInDb(pixel);
});
}
const main = (() => {
let sizePart = process.argv[2];
let filename = process.argv[3];
try {
loadAndTransformProcess(filename, parseInt(sizePart));
// for (let i = 0; i < totalParts; i++) {
// pixels = JSON.parse(fs.readFileSync(`data/file_${i}.json`).toString());
// savePixels(pixels);
// }
} catch (err) {
console.log(err.message);
}
})();