redis cache updated
This commit is contained in:
parent
045636da98
commit
d73b8c1c73
@ -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",
|
||||
|
21
server.ts
21
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 => {
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
});
|
||||
|
@ -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,35 +70,25 @@ 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,
|
||||
letest_block_num = "",
|
||||
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(
|
||||
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 +
|
||||
"'"
|
||||
).then( res2 => {
|
||||
var BlockchainPreviousBlockQuery = res2;
|
||||
);
|
||||
//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);
|
||||
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user