countToIndex.js
1.35 KB
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
const axios = require("axios");
const log = console.log;
const esClient = require('./client');
const fs = require('fs');
async function indexData() {
try {
const aptRaw = await fs.readFileSync('./count.json');
const aptInfo = JSON.parse(aptRaw);
const apt_list = []
for(const item of aptInfo.data){
const tmp_data = {
date : item.time.substring(0,19),
current_count : item.now,
total_count : item.total,
seconds : item.s
}
apt_list.push(tmp_data)
}
const body = apt_list.flatMap(doc => [{ index: { _index: 'count', _type: 'count_info' } }, doc])
const { body: bulkResponse } = await esClient.bulk({ refresh: true, body })
if (bulkResponse.errors) {
const erroredDocuments = []
bulkResponse.items.forEach((action, i) => {
const operation = Object.keys(action)[0]
if (action[operation].error) {
erroredDocuments.push({
status: action[operation].status,
error: action[operation].error,
operation: body[i * 2],
document: body[i * 2 + 1]
})
}
})
console.log(erroredDocuments)
log('bulk error')
}
} catch (error) {
log('에러')
}
}
indexData();