diff --git a/package.json b/package.json index 4efc90f..8f13b69 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "moment": "^2.24.0", "morgan": "^1.9.1", "ngx-cookie-service": "^2.2.0", + "node-cron": "^2.0.3", "pg": "^7.11.0", "pg-native": "^3.0.0", "plotly.js": "^1.48.3", diff --git a/server.ts b/server.ts index ad591c6..95ae8e0 100644 --- a/server.ts +++ b/server.ts @@ -63,6 +63,7 @@ config({ path: resolve(__dirname, "../.env") }); // const connection = connectionManager.create(dbConfig); import { join } from "path"; +var cron = require('node-cron'); // Faster server renders w/ Prod mode (dev mode never needed) enableProdMode(); @@ -111,6 +112,8 @@ app.get("/swagger.json", function(req, res) { ); }); + + controllers.forEach(controller => { app.use("/epic_explorer/v1", controller.router); }); @@ -185,12 +188,22 @@ console.log(__dirname); const server = app.listen(PORT, () => { console.log(`Node Express server listening on http://localhost:${PORT}`); }); + + cron.schedule('* * * * * *', () => { + universalGetLatestBlockDetails('Testnet'); + universalGetLatestBlockDetails('Floonet'); + }); const io = require("socket.io").listen(server); io.sockets.on("connection", socket => { - //setTimeout(function() { - //universalGetLatestBlockDetails(socket); - //},1000); - socket.on("disconnect", () => console.log("Client disconnected")); + //console.log(socket.handshake.query.network); + //var network = "Testnet"; + let key = process.env.REDIS_KEY + socket.handshake.query.network + 'Latest_Block_details' + setInterval(function() { + Global.client.get(key, function(err, reply){ + socket.emit("latestblockdetail", JSON.parse(reply) ); + }); + },1000); + //socket.on("disconnect", () => console.log("Client disconnected")); }); }) .catch(error => { diff --git a/server/global.ts b/server/global.ts index 177d22f..a1014f2 100644 --- a/server/global.ts +++ b/server/global.ts @@ -1,3 +1,7 @@ +import * as redis from 'redis'; +var REDIS_URL = process.env.REDIS_URL; export namespace Global { export var network: string = 'Floonet'; + // connect to Redis + export var client = redis.createClient(REDIS_URL); } \ No newline at end of file diff --git a/server/middlewares/redis.middleware.ts b/server/middlewares/redis.middleware.ts index 8f77d74..fb01d2d 100644 --- a/server/middlewares/redis.middleware.ts +++ b/server/middlewares/redis.middleware.ts @@ -5,35 +5,35 @@ import { Global } from "../global"; import { Duration } from 'moment'; // connect to Redis -const REDIS_URL = process.env.REDIS_URL; -const client = redis.createClient(REDIS_URL); +// const REDIS_URL = process.env.REDIS_URL; +// const client = redis.createClient(REDIS_URL); -client.on('connect', () => { +Global.client.on('connect', () => { console.log(`connected to redis`); }); -client.on('error', err => { +Global.client.on('error', err => { console.log(`Error: ${err}`); }); export function redisMiddleware( duration: any, ) { - console.log(duration); return (request, response, next) => { + //console.log(request.originalUrl); // Global.network = request.headers.network; let key = process.env.REDIS_KEY + Global.network + request.originalUrl || request.url - client.get(key, function(err, reply){ + Global.client.get(key, function(err, reply){ if(reply && duration!=0){ - console.log("key : ", key); - console.log("reply : ", reply); - console.log("---------------------------------------------------------------------------------------"); - console.log(`enabled`); - response.send(reply); + //console.log("key : ", key); + //console.log("reply : ", reply); + //console.log("---------------------------------------------------------------------------------------"); + //console.log(`enabled`); + response.send(JSON.parse(reply)); }else{ - console.log(`raw`); + //console.log(`raw`); response.sendResponse = response.send; response.send = (body) => { - client.set(key, JSON.stringify(body), 'EX', duration, function(err){ + Global.client.set(key, JSON.stringify(body), 'EX', duration, function(err){ //client.set(key, JSON.stringify(body)); response.sendResponse(body); }); diff --git a/server/socket/block.ts b/server/socket/block.ts index 249ed01..b5c2317 100644 --- a/server/socket/block.ts +++ b/server/socket/block.ts @@ -1,6 +1,5 @@ import { getConnection } from "typeorm"; import { Global } from "../global"; -import {createConnection, Connection} from "typeorm"; var moment = require("moment"); moment.updateLocale('en', { @@ -71,8 +70,10 @@ function convertMinsToHrmin(millseconds, insec) { } -export async function universalGetLatestBlockDetails(socket) { +export async function universalGetLatestBlockDetails(current_network) { + + let key = process.env.REDIS_KEY + current_network + 'Latest_Block_details' let block_height = "", letest_block, @@ -80,26 +81,14 @@ export async function universalGetLatestBlockDetails(socket) { letest_block_duration = ""; - // const BlockchainLatestBlockQuery = await getConnection("Testnet").query( - // "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1"); - // const BlockchainPreviousBlockQuery = await getConnection("Testnet").query( - // "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + - // "'" + - // BlockchainLatestBlockQuery[0].previous_id + - // "'" - // ); - - getConnection("Testnet").query( - "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1").then(res => { - var BlockchainLatestBlockQuery = res; - //console.log(res); - getConnection("Testnet").query( - "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + - "'" + - BlockchainLatestBlockQuery[0].previous_id + - "'" - ).then( res2 => { - var BlockchainPreviousBlockQuery = res2; + const BlockchainLatestBlockQuery = await getConnection(current_network).query( + "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1"); + const BlockchainPreviousBlockQuery = await getConnection(current_network).query( + "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + + "'" + + BlockchainLatestBlockQuery[0].previous_id + + "'" + ); //console.log(BlockchainPreviousBlockQuery); let height = BlockchainLatestBlockQuery[0].height; @@ -279,7 +268,7 @@ export async function universalGetLatestBlockDetails(socket) { let arr = balance.match(/.{1,6}/g); var hasharray = arr.map(i => '#' + i); - socket.emit("latestblockdetail", { + var final_result = { block_height, letest_block, letest_block_num, @@ -309,14 +298,12 @@ export async function universalGetLatestBlockDetails(socket) { BlockchainLatestBlockQuery[0].total_difficulty_progpow, TotalDifficultyRandomx: BlockchainLatestBlockQuery[0].total_difficulty_randomx - }); - setTimeout(function() { - universalGetLatestBlockDetails(socket); - },1000); + } - }); - }); + Global.client.set(key, JSON.stringify(final_result), 'EX', parseInt(process.env.REDIS_EXPIRY), function(err){ + //client.set(key, JSON.stringify(body)); + }); - - + //console.log(final_result); + } diff --git a/src/app/shared/services/chart.service.ts b/src/app/shared/services/chart.service.ts index 5d4e7e2..f1e3377 100644 --- a/src/app/shared/services/chart.service.ts +++ b/src/app/shared/services/chart.service.ts @@ -15,9 +15,15 @@ import * as io from "socket.io-client"; export class ChartService { private server = environment.domain; private socket; + private socketnetwork: any; constructor(public http: HttpClient) { - this.socket = io.connect(this.server); + if(localStorage.getItem('network') == null){ + this.socketnetwork = "Testnet" + }else{ + this.socketnetwork = localStorage.getItem('network') + } + this.socket = io.connect(this.server, {query: 'network='+this.socketnetwork}); } // public createSocketConnection() {