-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathesData.js
101 lines (93 loc) · 2.35 KB
/
esData.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
var elasticsearch = require('elasticsearch');
var async = require('async');
// var allCoins = require('./lib/coin-names').all;
global.fetch = require('node-fetch')
var cc = require('cryptocompare')
var client = new elasticsearch.Client({
host: 'search-coins-jtghe5d7qp3qk245pxfvez73hi.us-east-1.es.amazonaws.com',
log: 'info'
});
// docker run -d -p 9200:9200 -p 9300:9300 -v ~/esdata:/usr/share/elasticsearch/data --name elasticsearch elasticsearch
// docker run -d --link elasticsearch:elasticsearch -p 5601:5601 --name kibana kibana
var allCoins = require('./lib/all-coins.json');
// var allCoins = require('./lib/my_coins.json');
var allCoinsData = [];
allCoins.forEach(function(info) {
allCoinsData.push(function(callback) {
getCoinPrices(info.coin, info.currency, callback);
});
});
async.series(allCoinsData, function(err) {
if (err) {
console.log(err);
} else {
console.log('DONE');
}
});
function getCoinPrices(coinName, currency, callback) {
console.log(coinName + '/' + currency);
cc.histoMinute(coinName, currency, {limit: 1440})
// cc.histoDay(coinName, currency, {limit: 'none'})
.then(function(data) {
console.log(data.length);
processData(coinName, currency, data, callback);
})
.catch((error) => {
console.error(error);
callback(error);
});
}
function processData(coinName, currency, hits, callback) {
var elasticSearchBody = [];
hits.forEach(function(hit) {
hit.time = hit.time * 1000;
hit.coinName = coinName;
hit.currency = currency;
elasticSearchBody.push({
index: {
_id: coinName + '-' + currency + '-' + hit.time,
_index: 'coins',
_type: 'price'
}
});
elasticSearchBody.push(hit);
});
client.bulk({
body: elasticSearchBody
})
.then(function() {
callback();
})
.catch(function(err) {
callback(err);
});
}
// PUT _template/coins
// {
// "template": "coins",
// "settings": {
// "index.number_of_shards": 1,
// "index.number_of_replicas": 1
// },
// "mappings": {
// "fluentd": {
// "_source": {
// "enabled": true
// },
// "properties": {
// "close": {
// "type": "double"
// },
// "time": {
// "type": "date"
// },
// "coinName": {
// "type": "keyword"
// },
// "currency": {
// "type": "keyword"
// }
// }
// }
// }
// }