Integrate LN stats importer into the main process
This commit is contained in:
parent
4ea1e98547
commit
91ada9ce75
@ -29,11 +29,11 @@ import channelsRoutes from './api/explorer/channels.routes';
|
|||||||
import generalLightningRoutes from './api/explorer/general.routes';
|
import generalLightningRoutes from './api/explorer/general.routes';
|
||||||
import lightningStatsUpdater from './tasks/lightning/stats-updater.service';
|
import lightningStatsUpdater from './tasks/lightning/stats-updater.service';
|
||||||
import nodeSyncService from './tasks/lightning/node-sync.service';
|
import nodeSyncService from './tasks/lightning/node-sync.service';
|
||||||
import statisticsRoutes from "./api/statistics/statistics.routes";
|
import statisticsRoutes from './api/statistics/statistics.routes';
|
||||||
import miningRoutes from "./api/mining/mining-routes";
|
import miningRoutes from './api/mining/mining-routes';
|
||||||
import bisqRoutes from "./api/bisq/bisq.routes";
|
import bisqRoutes from './api/bisq/bisq.routes';
|
||||||
import liquidRoutes from "./api/liquid/liquid.routes";
|
import liquidRoutes from './api/liquid/liquid.routes';
|
||||||
import bitcoinRoutes from "./api/bitcoin/bitcoin.routes";
|
import bitcoinRoutes from './api/bitcoin/bitcoin.routes';
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
private wss: WebSocket.Server | undefined;
|
private wss: WebSocket.Server | undefined;
|
||||||
|
@ -4,11 +4,12 @@ import logger from '../../logger';
|
|||||||
import lightningApi from '../../api/lightning/lightning-api-factory';
|
import lightningApi from '../../api/lightning/lightning-api-factory';
|
||||||
import channelsApi from '../../api/explorer/channels.api';
|
import channelsApi from '../../api/explorer/channels.api';
|
||||||
import { isIP } from 'net';
|
import { isIP } from 'net';
|
||||||
|
import LightningStatsImporter from './sync-tasks/stats-importer';
|
||||||
|
|
||||||
class LightningStatsUpdater {
|
class LightningStatsUpdater {
|
||||||
hardCodedStartTime = '2018-01-12';
|
hardCodedStartTime = '2018-01-12';
|
||||||
|
|
||||||
public async $startService() {
|
public async $startService(): Promise<void> {
|
||||||
logger.info('Starting Lightning Stats service');
|
logger.info('Starting Lightning Stats service');
|
||||||
let isInSync = false;
|
let isInSync = false;
|
||||||
let error: any;
|
let error: any;
|
||||||
@ -28,6 +29,8 @@ class LightningStatsUpdater {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LightningStatsImporter.$run();
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.$runTasks();
|
this.$runTasks();
|
||||||
}, this.timeUntilMidnight());
|
}, this.timeUntilMidnight());
|
||||||
|
@ -29,24 +29,25 @@ interface Channel {
|
|||||||
htlc_maximum_msat: number;
|
htlc_maximum_msat: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const topologiesFolder = config.LIGHTNING.TOPOLOGY_FOLDER;
|
class LightningStatsImporter {
|
||||||
const parser = new XMLParser();
|
topologiesFolder = config.LIGHTNING.TOPOLOGY_FOLDER;
|
||||||
|
parser = new XMLParser();
|
||||||
|
|
||||||
let latestNodeCount = 1; // Ignore gap in the data
|
latestNodeCount = 1; // Ignore gap in the data
|
||||||
|
|
||||||
async function $run(): Promise<void> {
|
async $run(): Promise<void> {
|
||||||
// const [channels]: any[] = await DB.query('SELECT short_id from channels;');
|
// const [channels]: any[] = await DB.query('SELECT short_id from channels;');
|
||||||
// logger.info('Caching funding txs for currently existing channels');
|
// logger.info('Caching funding txs for currently existing channels');
|
||||||
// await fundingTxFetcher.$fetchChannelsFundingTxs(channels.map(channel => channel.short_id));
|
// await fundingTxFetcher.$fetchChannelsFundingTxs(channels.map(channel => channel.short_id));
|
||||||
|
|
||||||
await $importHistoricalLightningStats();
|
await this.$importHistoricalLightningStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the file content into XML, and return a list of nodes and channels
|
* Parse the file content into XML, and return a list of nodes and channels
|
||||||
*/
|
*/
|
||||||
function parseFile(fileContent): any {
|
parseFile(fileContent): any {
|
||||||
const graph = parser.parse(fileContent);
|
const graph = this.parser.parse(fileContent);
|
||||||
if (Object.keys(graph).length === 0) {
|
if (Object.keys(graph).length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -105,7 +106,7 @@ function parseFile(fileContent): any {
|
|||||||
/**
|
/**
|
||||||
* Generate LN network stats for one day
|
* Generate LN network stats for one day
|
||||||
*/
|
*/
|
||||||
async function computeNetworkStats(timestamp: number, networkGraph): Promise<void> {
|
async computeNetworkStats(timestamp: number, networkGraph): Promise<void> {
|
||||||
// Node counts and network shares
|
// Node counts and network shares
|
||||||
let clearnetNodes = 0;
|
let clearnetNodes = 0;
|
||||||
let torNodes = 0;
|
let torNodes = 0;
|
||||||
@ -237,8 +238,8 @@ async function computeNetworkStats(timestamp: number, networkGraph): Promise<voi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function $importHistoricalLightningStats(): Promise<void> {
|
async $importHistoricalLightningStats(): Promise<void> {
|
||||||
const fileList = readdirSync(topologiesFolder);
|
const fileList = readdirSync(this.topologiesFolder);
|
||||||
fileList.sort().reverse();
|
fileList.sort().reverse();
|
||||||
|
|
||||||
const [rows]: any[] = await DB.query('SELECT UNIX_TIMESTAMP(added) as added FROM lightning_stats');
|
const [rows]: any[] = await DB.query('SELECT UNIX_TIMESTAMP(added) as added FROM lightning_stats');
|
||||||
@ -249,26 +250,26 @@ export async function $importHistoricalLightningStats(): Promise<void> {
|
|||||||
|
|
||||||
for (const filename of fileList) {
|
for (const filename of fileList) {
|
||||||
const timestamp = parseInt(filename.split('_')[1], 10);
|
const timestamp = parseInt(filename.split('_')[1], 10);
|
||||||
const fileContent = readFileSync(`${topologiesFolder}/${filename}`, 'utf8');
|
const fileContent = readFileSync(`${this.topologiesFolder}/${filename}`, 'utf8');
|
||||||
|
|
||||||
const graph = parseFile(fileContent);
|
const graph = this.parseFile(fileContent);
|
||||||
if (!graph) {
|
if (!graph) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore drop of more than 90% of the node count as it's probably a missing data point
|
// Ignore drop of more than 90% of the node count as it's probably a missing data point
|
||||||
const diffRatio = graph.nodes.length / latestNodeCount;
|
const diffRatio = graph.nodes.length / this.latestNodeCount;
|
||||||
if (diffRatio < 0.90) {
|
if (diffRatio < 0.90) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
latestNodeCount = graph.nodes.length;
|
this.latestNodeCount = graph.nodes.length;
|
||||||
|
|
||||||
// Stats exist already, don't calculate/insert them
|
// Stats exist already, don't calculate/insert them
|
||||||
if (existingStatsTimestamps[timestamp] === true) {
|
if (existingStatsTimestamps[timestamp] === true) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(`Processing ${topologiesFolder}/${filename}`);
|
logger.debug(`Processing ${this.topologiesFolder}/${filename}`);
|
||||||
|
|
||||||
const datestr = `${new Date(timestamp * 1000).toUTCString()} (${timestamp})`;
|
const datestr = `${new Date(timestamp * 1000).toUTCString()} (${timestamp})`;
|
||||||
logger.debug(`${datestr}: Found ${graph.nodes.length} nodes and ${graph.channels.length} channels`);
|
logger.debug(`${datestr}: Found ${graph.nodes.length} nodes and ${graph.channels.length} channels`);
|
||||||
@ -278,10 +279,11 @@ export async function $importHistoricalLightningStats(): Promise<void> {
|
|||||||
await fundingTxFetcher.$fetchChannelsFundingTxs(graph.channels.map(channel => channel.scid.slice(0, -2)));
|
await fundingTxFetcher.$fetchChannelsFundingTxs(graph.channels.map(channel => channel.scid.slice(0, -2)));
|
||||||
|
|
||||||
logger.debug(`Generating LN network stats for ${datestr}`);
|
logger.debug(`Generating LN network stats for ${datestr}`);
|
||||||
await computeNetworkStats(timestamp, graph);
|
await this.computeNetworkStats(timestamp, graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Lightning network stats historical import completed`);
|
logger.info(`Lightning network stats historical import completed`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$run().then(() => process.exit(0));
|
export default new LightningStatsImporter;
|
Loading…
x
Reference in New Issue
Block a user