From 4b05a76e28990bbcaf3f1b8699625d09d4c3f07b Mon Sep 17 00:00:00 2001 From: Fillerino Date: Tue, 26 Sep 2017 19:39:03 +0200 Subject: [PATCH] Added basic status API. Added a basic status API, will be upgraded later on. --- server/controllers/lighthouse.js | 13 +++++++++++-- server/package.json | 5 +++++ server/utils/importer/index.js | 6 +++--- server/yarn.lock | 7 +++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 server/package.json create mode 100644 server/yarn.lock diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js index 6bbdcb8..d93e1f4 100644 --- a/server/controllers/lighthouse.js +++ b/server/controllers/lighthouse.js @@ -3,6 +3,8 @@ import winston from 'winston'; import winstonStream from 'winston-stream'; import { sync, getStats } from '../utils/importer'; import elasticsearch from 'elasticsearch'; +import rp from 'request-promise'; +import pretty from 'prettysize'; const loggerStream = winstonStream(winston, 'info'); @@ -124,7 +126,7 @@ class LighthouseControllers { * @param {ctx} Koa Context */ async info (ctx) { - ctx.body = 'Lighthouse'; + ctx.body = 'Lighthouse search API'; } /** @@ -132,7 +134,14 @@ class LighthouseControllers { * @param {ctx} Koa Context */ async status (ctx) { - ctx.body = eclient.getStats(); + rp('http://localhost:9200/claims/_stats') + .then(function (data) { + data = JSON.parse(data); + ctx.body = {status: getStats().info, spaceUsed: pretty(data._all.total.store.size_in_bytes, true), claimsInIndex: data._all.total.indexing.index_total, totSearches: data._all.total.search.query_total}; + }) + .catch(function (err) { + ctx.body = err; + }); } /* eslint-enable no-param-reassign */ diff --git a/server/package.json b/server/package.json new file mode 100644 index 0000000..9666791 --- /dev/null +++ b/server/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "prettysize": "^1.1.0" + } +} diff --git a/server/utils/importer/index.js b/server/utils/importer/index.js index 0b9ae40..c331cb4 100644 --- a/server/utils/importer/index.js +++ b/server/utils/importer/index.js @@ -67,7 +67,7 @@ let status = {}; export async function sync () { try { - status.info = 'Grabbing the claimTrie...'; + status.info = 'gettingClaimTrie'; let claimTrie = await client.getClaimsInTrie().then(claimtrie => { return claimtrie }).catch(err => { throw err }); let txList = []; let latestClaimTrie = []; @@ -84,7 +84,7 @@ export async function sync () { let oldClaimTrie = await getJSON(path.join(appRoot.path, 'claimTrieCache.json')); // get our old claimTrieCache.... let added = await getAddedClaims(oldClaimTrie, latestClaimTrie); // get all new that should be added let removed = await getRemovedClaims(oldClaimTrie, latestClaimTrie); // get all old that should be removed - status.info = 'Adding/Removing Claims, please wait...'; + status.info = 'calculatingClaimTrie'; for (let claimId of added) { // for all new get their tx info and add to database let tx = txList.find(x => x.claimId === claimId); if (typeof tx !== 'undefined') { @@ -116,7 +116,7 @@ export async function sync () { } // Done adding, update our claimTrie cache to latest and wait a bit... await saveJSON(path.join(appRoot.path, 'claimTrieCache.json'), latestClaimTrie); - status.info = 'Done updating the claimTrieCache, waiting 5 minutes before doing a recheck..'; + status.info = 'upToDate'; await sleep(300000); sync(); } catch (err) { diff --git a/server/yarn.lock b/server/yarn.lock new file mode 100644 index 0000000..45277f1 --- /dev/null +++ b/server/yarn.lock @@ -0,0 +1,7 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +prettysize@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/prettysize/-/prettysize-1.1.0.tgz#c6c52f87161ff172ea435f375f99831dd9a97bb0"