2018-05-13 01:35:56 +02:00
|
|
|
/*
|
|
|
|
* Importer code, handles all the syncing with chainquery into elasticsearch.
|
|
|
|
*/
|
|
|
|
import elasticsearch from 'elasticsearch';
|
|
|
|
import ElasticQueue from 'elastic-queue';
|
|
|
|
import winston from 'winston';
|
|
|
|
import winstonStream from 'winston-stream';
|
|
|
|
import jsonfile from 'jsonfile';
|
|
|
|
import path from 'path';
|
|
|
|
import rp from 'request-promise';
|
|
|
|
import appRoot from 'app-root-path';
|
|
|
|
import fs from 'fs';
|
|
|
|
import fileExists from 'file-exists';
|
2018-05-15 06:03:07 +02:00
|
|
|
import * as util from '../../utils/importer/util';
|
2018-05-13 01:35:56 +02:00
|
|
|
|
2018-05-27 04:49:29 +02:00
|
|
|
const elasticsearchloglevel = 'info';
|
|
|
|
const loggerStream = winstonStream(winston, elasticsearchloglevel);
|
2018-05-13 01:35:56 +02:00
|
|
|
const eclient = new elasticsearch.Client({
|
|
|
|
host: 'http://localhost:9200',
|
|
|
|
|
|
|
|
log: {
|
2018-05-27 04:49:29 +02:00
|
|
|
level : elasticsearchloglevel,
|
2018-05-13 01:35:56 +02:00
|
|
|
type : 'stream',
|
|
|
|
stream: loggerStream,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const queue = new ElasticQueue({elastic: eclient});
|
|
|
|
|
2018-05-27 04:56:09 +02:00
|
|
|
// Check that our syncState file exist.
|
2018-05-13 01:35:56 +02:00
|
|
|
fileExists(path.join(appRoot.path, 'syncState.json'), (err, exists) => {
|
|
|
|
if (err) { throw err }
|
|
|
|
if (!exists) {
|
|
|
|
fs.writeFileSync(path.join(appRoot.path, 'syncState.json'), '{}');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let status = {};
|
|
|
|
|
|
|
|
export async function claimSync () {
|
|
|
|
try {
|
|
|
|
let syncState = await getJSON(path.join(appRoot.path, 'syncState.json')); // get our persisted state
|
|
|
|
if (!syncState.LastSyncTime) {
|
|
|
|
syncState.LastSyncTime = '0001-01-01 00:00:00';
|
|
|
|
}
|
|
|
|
status.info = 'gettingClaimsToUpdate';
|
|
|
|
let claimsResponse = await getClaimsSince(syncState.LastSyncTime);
|
|
|
|
let claims = JSON.parse(claimsResponse).data;
|
|
|
|
status.info = 'addingClaimsToElastic';
|
|
|
|
for (let claim of claims) {
|
|
|
|
claim.value = JSON.parse(claim.value).Claim;
|
|
|
|
if (claim.name && claim.value) {
|
|
|
|
claim.suggest_name = {
|
2018-05-27 04:47:26 +02:00
|
|
|
input : '' + claim.name + '',
|
|
|
|
weight: '30',
|
2018-05-13 01:35:56 +02:00
|
|
|
};
|
|
|
|
}
|
2018-05-27 04:56:09 +02:00
|
|
|
if (claim.bid_state === 'Spent' || claim.bid_state === 'Expired') {
|
|
|
|
deleteFromElastic(claim.claimId);
|
|
|
|
} else {
|
|
|
|
pushElastic(claim);
|
|
|
|
}
|
2018-05-13 01:35:56 +02:00
|
|
|
}
|
2018-05-15 06:03:07 +02:00
|
|
|
winston.log('info', '[Importer] Pushed ' + claims.length + ' claims to elastic search');
|
2018-05-27 04:56:09 +02:00
|
|
|
deleteBlockedClaims();
|
2018-05-13 01:35:56 +02:00
|
|
|
syncState.LastSyncTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
|
|
await saveJSON(path.join(appRoot.path, 'syncState.json'), syncState);
|
|
|
|
status.info = 'upToDate';
|
|
|
|
await sleep(600000);
|
|
|
|
claimSync();
|
|
|
|
} catch (err) {
|
|
|
|
winston.log(err);
|
|
|
|
status.err = err;
|
|
|
|
await sleep(600000);
|
|
|
|
claimSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getStats () {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-05-27 04:56:09 +02:00
|
|
|
async function deleteBlockedClaims () {
|
|
|
|
winston.log('info', '[Importer] Removing blocked claims from search!');
|
|
|
|
let blockedOutputsResponse = await getBlockedOutpoints();
|
|
|
|
let outpointlist = JSON.parse(blockedOutputsResponse);
|
|
|
|
for (let outpoint of outpointlist.data.outpoints) {
|
|
|
|
let claimid = util.OutpointToClaimId(outpoint);
|
|
|
|
deleteFromElastic(claimid);
|
|
|
|
}
|
|
|
|
winston.log('info', '[Importer] Done processing blocked claims!');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteFromElastic (claimid) {
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
queue.push({
|
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claimid,
|
|
|
|
body : {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-13 01:35:56 +02:00
|
|
|
async function pushElastic (claim) {
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
queue.push({
|
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claim.claimId,
|
|
|
|
body : claim,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON (path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
jsonfile.readFile(path, function (err, jsoncontent) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(jsoncontent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function saveJSON (path, obj) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
jsonfile.writeFile(path, obj, function (err, jsoncontent) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sleep (ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBlockedOutpoints () {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-15 06:03:07 +02:00
|
|
|
rp(`https://api.lbry.io/file/list_blocked`)
|
2018-05-13 01:35:56 +02:00
|
|
|
.then(function (htmlString) {
|
|
|
|
resolve(htmlString);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2018-05-27 04:56:09 +02:00
|
|
|
winston.log('error', '[Importer] Error getting blocked outpoints. ' + err);
|
2018-05-13 01:35:56 +02:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClaimsSince (time) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let query = `` +
|
|
|
|
`SELECT ` +
|
|
|
|
`name, ` +
|
|
|
|
`value_as_json as value, ` +
|
|
|
|
`bid_state, ` +
|
|
|
|
`effective_amount, ` +
|
2018-05-15 03:28:14 +02:00
|
|
|
`claim_id as claimId ` +
|
2018-05-15 06:03:07 +02:00
|
|
|
// `,transaction_by_hash_id, ` + // txhash and vout needed to leverage old format for comparison.
|
2018-05-13 01:35:56 +02:00
|
|
|
// `vout ` +
|
|
|
|
`FROM claim ` +
|
|
|
|
`WHERE modified >='` + time + `'`;
|
|
|
|
// Outputs full query to console for copy/paste into chainquery (debugging)
|
|
|
|
// console.log(query);
|
|
|
|
rp(`https://chainquery.lbry.io/api/sql?query=` + query)
|
|
|
|
.then(function (htmlString) {
|
|
|
|
resolve(htmlString);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2018-05-27 04:56:09 +02:00
|
|
|
winston.log('error', '[Importer] Error getting updated claims. ' + err);
|
2018-05-13 01:35:56 +02:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|