epic_explorer/server/controllers/BlockchainKernel.ts

942 lines
30 KiB
TypeScript
Raw Permalink Normal View History

2019-07-09 09:22:36 +00:00
import express from 'express';
import { Request, Response, NextFunction } from 'express';
import { getRepository, getConnection } from 'typeorm';
2019-08-21 06:16:14 +00:00
import { validationMiddleware, redisMiddleware } from '../middlewares';
2019-08-14 10:55:23 +00:00
import { Global } from "../global";
2019-07-19 14:05:06 +00:00
import * as path from 'path';
2019-07-09 09:22:36 +00:00
import {
InternalServerErrorException,
NoDataFoundException,
} from '../exceptions';
import { BlockchainKernel } from '../entities';
import {
BlockchainKernelCreateDto,
BlockchainKernelSingleViewDto,
BlockchainKernelUpdateDto,
BlockchainKernelPaginationDto,
TransactionFeeDto,
} from '../dtos';
import { Paginate } from '../utils';
2019-08-02 12:54:58 +00:00
const http = require('http');
2019-09-03 08:03:01 +00:00
const request_promise = require('request-promise');
2019-07-09 09:22:36 +00:00
var moment = require('moment');
export class BlockchainKernelController {
public path = '/blockchain_kernel';
public router = express.Router();
constructor() {
this.IntializeRoutes();
}
2019-08-09 06:30:06 +00:00
IsJsonString(str) {
try {
var dataJson = JSON.parse(str);
} catch (e) {
return [];
}
return dataJson;
}
2019-07-09 09:22:36 +00:00
public IntializeRoutes() {
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel:
* post:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: create a blockchain_kernel
* description: create a blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: BlockchainKernel
* description: create a blockchain_kernel
* in: body
* required: true
* schema:
* $ref: '#/definitions/BlockchainKernelDto'
* responses:
* 200:
* description: blockchain_kernel created successfully
* definitions:
* BlockchainKernelDto:
* description: Dto
* properties:
* Id:
* type: integer
* Features:
* type: string
* Fee:
* type: integer
* LockHeight:
* type: integer
* Excess:
* type: string
* ExcessSig:
* type: string
* Block:
* type: string
*/
this.router.post(
`${this.path}`,
validationMiddleware(BlockchainKernelCreateDto),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.BlockchainKernelCreate,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/transactionfee:
* get:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: create a blockchain_kernel
* description: create a blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: FromDate
* description: Enter the From date
* in: query
* type: string
* - name: ToDate
* description: Enter the To date
* in: query
* type: string
* - name: Interval
* description: Try to give Intevals such as 1 week/ 15 days/ 30 days/ 60 days/ 3 months
* in: query
* type: string
* responses:
* 200:
* description: Transaction fee chart fetched successfully
*/
this.router.get(
`${this.path}/transactionfee`,
validationMiddleware(TransactionFeeDto, true),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.TransactionFee,
);
2019-07-19 09:46:52 +00:00
/**
* @swagger
* /epic_explorer/v1/translator:
* get:
* tags:
* - name: Translator | Translator CONTROLLER
* summary: create a translator
* description: create a translator
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: lang
* responses:
* 200:
* description: Transaction fee chart fetched successfully
*/
this.router.get(
`${this.path}/translator`,
this.Translator,
);
2019-08-14 10:55:23 +00:00
/**
* @swagger
* /epic_explorer/v1/network:
* get:
* tags:
* - name: Network | Network CONTROLLER
* summary: change a network
* description: change a network
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: network
* responses:
* 200:
* description: Network Changed successfully
*/
this.router.get(
`${this.path}/network`,
this.changeNetwok,
);
2019-08-02 10:24:24 +00:00
/**
* @swagger
* /epic_explorer/v1/getpeers:
* get:
* tags:
* - name: Translator | Translator CONTROLLER
* summary: create a translator
* description: create a translator
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: lang
* responses:
* 200:
* description: Transaction fee chart fetched successfully
*/
this.router.get(
`${this.path}/getpeers`,
2019-09-03 08:03:01 +00:00
redisMiddleware(60),
2019-08-02 10:24:24 +00:00
this.getPeers,
);
2019-07-09 09:22:36 +00:00
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/transactionheatmap:
* get:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: Transaction Heatmap
* description: Transaction Heatmap (Input/output/kernal)
* consumes:
* - application/json
* produces:
* - application/json
* responses:
* 200:
* description: Transaction heatmap chart fetched successfully
*/
this.router.get(
`${this.path}/transactionheatmap`,
validationMiddleware(TransactionFeeDto, true),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.HeatmapChart,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/transactionlinechart:
* get:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: Transaction line chart
* description: Transaction line chart
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: FromDate
* description: Enter the From date
* in: query
* type: string
* - name: ToDate
* description: Enter the To date
* in: query
* type: string
* - name: Interval
* description: Try to give Intevals such as 1 week/ 15 days/ 30 days/ 60 days/ 3 months
* in: query
* type: string
* responses:
* 200:
* description: Transaction line chart fetched successfully
*/
this.router.get(
`${this.path}/transactionlinechart`,
validationMiddleware(TransactionFeeDto, true),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.TransactionChart,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/list:
* get:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* description: pagination blockchain_kernel
* summary: pagination blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: current_page
* description: current page specification
* in: query
* type: integer
* required: true
* - name: page_size
* description: page size specification
* in: query
* type: integer
* required: true
* - name: max_pages
* description: max pages specification
* in: query
* type: integer
* required: true
* responses:
* 200:
* description: blockchain_kernel list fetched successfully
*/
this.router.get(
`${this.path}/list`,
validationMiddleware(BlockchainKernelPaginationDto),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.BlockchainKernelPagination,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/id:
* get:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: get single blockchain_kernel
* description: get single blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: Id
* in: path
* description: blockchain_kernel id
* required: true
* type: string
* responses:
* 200:
* description: blockchain_kernel successfully fetched for given id..
*/
this.router.get(
`${this.path}/:id`,
validationMiddleware(BlockchainKernelSingleViewDto),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.BlockchainKernelFetch,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel:
* patch:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: update a blockchain_kernel
* description: update a blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: blockchain_kernel
* description:
* in: body
* required: true
* schema:
* $ref: '#/definitions/BlockchainKernelDto'
* responses:
* 200:
* description: blockchain_kernel updated successfully
*/
this.router.patch(
`${this.path}`,
validationMiddleware(BlockchainKernelUpdateDto),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.BlockchainKernelUpdate,
);
/**
* @swagger
* /epic_explorer/v1/blockchain_kernel/id:
* delete:
* tags:
* - name: BLOCKCHAIN_KERNEL | BLOCKCHAIN_KERNEL CONTROLLER
* summary: delete a blockchain_kernel
* description: delete a blockchain_kernel
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - name: Id
* in: path
* description: blockchain_kernel id
* required: true
* type: string
* responses:
* 200:
* description: blockchain_kernel successfully deleted for given id..
*/
this.router.delete(
`${this.path}/:id`,
validationMiddleware(BlockchainKernelSingleViewDto),
2019-08-21 06:16:14 +00:00
redisMiddleware(process.env.REDIS_EXPIRY),
2019-07-09 09:22:36 +00:00
this.BlockchainKernelDelete,
);
}
private BlockchainKernelCreate = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const BlockchainKernelRequestData: BlockchainKernelCreateDto =
request.body;
2019-08-14 10:55:23 +00:00
const BlockchainKernelCreateQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).save(BlockchainKernelRequestData);
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'blockchain_kernel created successfully',
response: BlockchainKernelCreateQuery,
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private BlockchainKernelFetch = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
2019-08-14 10:55:23 +00:00
const BlockchainKernelFetchQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).findOne({
where: { id: request.params.id },
});
BlockchainKernelFetchQuery
? response.status(200).json({
2019-08-02 12:54:58 +00:00
status: 200,
timestamp: Date.now(),
message: 'blockchain_kernelsuccessfully fetched for given id.',
response: { ...BlockchainKernelFetchQuery },
})
2019-07-09 09:22:36 +00:00
: next(new NoDataFoundException());
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private BlockchainKernelUpdate = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const BlockchainKernelRequestData: BlockchainKernelUpdateDto =
request.body;
2019-08-14 10:55:23 +00:00
const BlockchainKernelUpdateQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).update(BlockchainKernelRequestData.Id, BlockchainKernelRequestData);
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'blockchain_kernel updated succesfully',
response: { ...BlockchainKernelUpdateQuery },
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private BlockchainKernelDelete = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
2019-08-14 10:55:23 +00:00
const BlockchainKernelDeleteQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).delete(request.params.Id);
BlockchainKernelDeleteQuery
? response.status(200).json({
2019-08-02 12:54:58 +00:00
status: 200,
timestamp: Date.now(),
message: 'blockchain_kernel successfully deleted for given id.',
response: { ...BlockchainKernelDeleteQuery },
})
2019-07-09 09:22:36 +00:00
: next(new NoDataFoundException());
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private BlockchainKernelPagination = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const BlockchainKernelRequestData: BlockchainKernelPaginationDto =
request.query;
2019-08-14 10:55:23 +00:00
const BlockchainKernelCountQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).findAndCount({});
if (BlockchainKernelCountQuery[1]) {
const PaginationReponseData = Paginate(
BlockchainKernelCountQuery[1],
BlockchainKernelRequestData.CurrentPage,
BlockchainKernelRequestData.PageSize,
BlockchainKernelRequestData.MaxPages,
);
2019-08-14 10:55:23 +00:00
const BlockchainKernelPaginationQuery = await getConnection(Global.network).getRepository(
2019-07-09 09:22:36 +00:00
BlockchainKernel,
).find({
skip: PaginationReponseData.startIndex,
take: PaginationReponseData.pageSize,
order: {
Id: 'DESC',
},
});
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'blockchain_kernel list fetched successfully',
response: {
...PaginationReponseData,
...BlockchainKernelPaginationQuery,
},
});
} else {
next(new NoDataFoundException());
}
} catch (error) {
next(new InternalServerErrorException(error));
}
};
2019-08-02 12:54:58 +00:00
2019-08-14 10:55:23 +00:00
private changeNetwok = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
Global.network = request.query.network;
console.log(Global.network);
//const network = request.query.network;
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'Network Changed successfully',
response: request.query.network,
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
2019-07-19 09:46:52 +00:00
private Translator = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
2019-07-19 14:05:06 +00:00
const lang = request.query.lang;
2019-08-02 12:54:58 +00:00
/* response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'Transaction fee chart fetched successfully',
response: {
lang: lang
},
}); */
console.log(path.resolve(__dirname + '/../i18n/' + request.query.lang + '.json'));
console.log("Without :", path.resolve('/../i18n/' + request.query.lang + '.json'));
response.header("Content-Type", 'application/json');
response.sendFile(path.resolve(__dirname + '/../i18n/' + request.query.lang + '.json'));
2019-07-19 09:46:52 +00:00
} catch (error) {
next(new InternalServerErrorException(error));
}
};
2019-07-09 09:22:36 +00:00
2019-08-02 10:24:24 +00:00
private getPeers = async (
request: Request,
response: Response,
next: NextFunction,
) => {
2019-08-09 06:30:06 +00:00
var self = this;
2019-08-02 10:24:24 +00:00
try {
2019-08-16 13:06:07 +00:00
if(Global.network == "Floonet"){
var peer_url = process.env.FLOONET_PEER_URL;
}else{
var peer_url = process.env.TESTNET_PEER_URL;
}
2019-09-03 08:03:01 +00:00
let finalresult = await request_promise(peer_url);
if(finalresult){
var jsonresponse = JSON.parse(finalresult);
}
let result = jsonresponse.map(function (value, i) {
value['id'] = i;
return value;
2019-08-09 10:15:17 +00:00
});
response.status(200).json({
2019-09-03 08:03:01 +00:00
status: 200,
timestamp: Date.now(),
message: 'Peers list fetched successfully',
response: {
dataJson: result
},
2019-08-02 12:54:58 +00:00
});
2019-09-03 08:03:01 +00:00
// http.get(peer_url,
// async (resp) => {
// // console.log('resp resp respresp',resp);
// let data = '';
// let result ;
// // A chunk of data has been recieved.
// await new Promise((resolve) => {
// resp.on('data', function (chunk) {
// data += chunk;
// let dataJson = self.IsJsonString(data);
// if(dataJson.length > 0){
// result = dataJson.map(function (value, i) {
// value['id'] = i;
// return value;
// });
// }
// resolve();
// });
// });
// response.status(200).json({
// status: 200,
// timestamp: Date.now(),
// message: 'Peers list fetched successfully',
// response: {
// dataJson: result
// },
// });
// });
2019-08-02 10:24:24 +00:00
} catch (error) {
2019-08-02 12:54:58 +00:00
console.log('error 3###########', error);
2019-08-02 10:24:24 +00:00
next(new InternalServerErrorException(error));
}
};
2019-08-09 06:30:06 +00:00
2019-07-09 09:22:36 +00:00
private TransactionFee = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const TransactionFeeRequestData: TransactionFeeDto = request.query;
if (TransactionFeeRequestData.Interval) {
var timeIntervalQry =
"timestamp > current_date - interval '" +
TransactionFeeRequestData.Interval +
"'";
} else if (
TransactionFeeRequestData.FromDate ||
TransactionFeeRequestData.ToDate
) {
let fromdate = moment(TransactionFeeRequestData.FromDate)
.utc()
.format('YYYY-MM-DD');
let todate = moment(TransactionFeeRequestData.ToDate)
.utc()
.format('YYYY-MM-DD');
2020-01-07 12:28:19 +00:00
var timeIntervalQry =
"timestamp at time zone '" +
process.env.TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
fromdate +
"' AND '" +
todate +
"'";
2019-07-09 09:22:36 +00:00
} else {
var timeIntervalQry = "timestamp > current_date - interval '30 days'";
}
2019-08-14 10:55:23 +00:00
const TransactionFeeQuery = await getConnection(Global.network)
2019-07-09 09:22:36 +00:00
.query(
2019-09-05 13:26:32 +00:00
"select 1 as hash, date(DATE_TRUNC('day', timestamp)) as date, avg(fee)/1000000000 as fee \
from blockchain_block t1 join blockchain_kernel t2 on t2.block_id=t1.hash where fee > 0 and " +
2019-08-02 12:54:58 +00:00
timeIntervalQry +
"group by DATE_TRUNC('day', timestamp) order by date",
2019-07-09 09:22:36 +00:00
)
.catch(err_msg => {
next(err_msg);
});
let date = [],
Fee = [];
TransactionFeeQuery.forEach(e => {
date.push(moment(e.date).format('YYYY-MM-DD'));
2019-09-05 13:26:32 +00:00
Fee.push(e.fee);
2019-07-09 09:22:36 +00:00
});
2019-08-19 08:03:05 +00:00
if(date.length == 0){
date = [moment(Date.now()).format('YYYY-MM-DD')];
Fee = [0];
}
2019-07-09 09:22:36 +00:00
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'Transaction fee chart fetched successfully',
response: {
Date: date,
Fee: Fee,
},
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private HeatmapChart = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
// const TransactionFeeRequestData: TransactionFeeDto = request.query;
// if (TransactionFeeRequestData.Interval) {
// var timeIntervalQry =
// "timestamp > current_date - interval '" +
// TransactionFeeRequestData.Interval +
// "'";
// } else if (
// TransactionFeeRequestData.FromDate ||
// TransactionFeeRequestData.ToDate
// ) {
let fromdate = moment()
.subtract(7, 'd')
.format('YYYY-MM-DD');
let todate = moment()
.subtract(1, 'd')
.format('YYYY-MM-DD');
// var timeIntervalQry =
// 'timestamp BETWEEN SYMMETRIC ' + fromdate + ' AND ' + todate;
// } else {
// var timeIntervalQry = "timestamp > current_date - interval '30 days'";
// }
2019-08-14 10:55:23 +00:00
const TransactionHeatmapChartQuery = await getConnection(Global.network)
2019-07-09 09:22:36 +00:00
.query(
"with hours as ( \
SELECT generate_series('" +
2019-08-02 12:54:58 +00:00
fromdate +
" 00:00:00'::timestamp, '" +
todate +
" 23:00:00', '1 hours') as hour ) select hours.hour, t1.totalinput, t1.totalkernal, t1.totaloutput \
2019-07-09 09:22:36 +00:00
from hours left join(select to_char(x.timestamp,'YYYY-MM-DD HH24:00:00') as hour_of_day ,\
SUM(x.input_count) as totalinput, SUM(x.kernal_count) as totalkernal, SUM(x.output_count) as totaloutput\
from ( SELECT blockchain_block.hash, \
blockchain_block.timestamp, \
sum(bi.block_id_count) AS input_count,\
sum(bk.block_id_count) AS kernal_count, \
sum(bo.block_id_count) AS output_count \
FROM blockchain_block \
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_input group by block_id) as bi \
ON blockchain_block.hash = \
bi.block_id \
2019-08-28 05:50:16 +00:00
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_kernel where features != 'Coinbase' group by block_id) as bk \
2019-07-09 09:22:36 +00:00
ON blockchain_block.hash = \
bk.block_id \
2019-08-28 06:21:53 +00:00
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_output where output_type != 'Coinbase' group by block_id) as bo \
2019-07-09 09:22:36 +00:00
ON blockchain_block.hash = \
bo.block_id WHERE blockchain_block.timestamp >= '" +
2019-08-02 12:54:58 +00:00
fromdate +
" 00:00:00' \
2019-07-09 09:22:36 +00:00
AND blockchain_block.timestamp <= '" +
2019-08-02 12:54:58 +00:00
todate +
" 23:59:59' \
2019-07-09 09:22:36 +00:00
GROUP BY blockchain_block.hash \
ORDER BY blockchain_block.timestamp DESC ) as x \
group by to_char(x.timestamp,'YYYY-MM-DD HH24:00:00') \
order by hour_of_day asc) as t1 on to_timestamp(t1.hour_of_day, 'YYYY-MM-DD HH24:00:00') = hours.hour",
)
.catch(err_msg => {
next(err_msg);
});
let date = [],
hour = [],
totalinput = [],
totalkernal = [],
totaloutput = [],
innerhour = [],
innertotalinput = [],
innertotalkernal = [],
innertotaloutput = [],
prev_date = '';
// for(var i=0 ; i < 7 ; i++){
// date.push(moment().subtract(7, 'days').add(i, 'days').format('YYYY-MM-DD'));
// }
TransactionHeatmapChartQuery.forEach(e => {
var dateformat = moment(e.hour)
.format('YYYY-MM-DD HH')
.split(' ');
if (date.indexOf(dateformat[0]) >= 0 && prev_date == dateformat[0]) {
innerhour.push(dateformat[1]);
innertotalinput.push(e.totalinput != null ? e.totalinput : 0);
innertotalkernal.push(e.totalkernal != null ? e.totalkernal : 0);
innertotaloutput.push(e.totaloutput != null ? e.totaloutput : 0);
} else {
date.push(dateformat[0]);
innerhour.length > 0 ? hour.push(innerhour) : '';
innertotalinput.length > 0 ? totalinput.push(innertotalinput) : '';
innertotalkernal.length > 0 ? totalkernal.push(innertotalkernal) : '';
innertotaloutput.length > 0 ? totaloutput.push(innertotaloutput) : '';
(innerhour = []),
(innertotalinput = []),
(innertotalkernal = []),
(innertotaloutput = []);
innerhour.push(dateformat[1]);
innertotalinput.push((e.totalinput! = null ? e.totalinput : 0));
innertotalkernal.push(e.totalkernal != null ? e.totalkernal : 0);
innertotaloutput.push(e.totaloutput != null ? e.totaloutput : 0);
prev_date = dateformat[0];
}
});
hour.push(innerhour);
totalinput.push(innertotalinput);
totalkernal.push(innertotalkernal);
totaloutput.push(innertotaloutput);
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'Transaction heatmap chart fetched successfully',
response: {
date,
hour,
totalinput,
totalkernal,
totaloutput,
},
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
private TransactionChart = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const TransactionFeeRequestData: TransactionFeeDto = request.query;
2019-10-17 05:51:52 +00:00
if (TransactionFeeRequestData.Interval) {
2019-07-09 09:22:36 +00:00
var timeIntervalQry =
"blockchain_block.timestamp > current_date - interval '" +
TransactionFeeRequestData.Interval +
"'";
var seriesquery =
"now() - interval '" +
TransactionFeeRequestData.Interval +
"', now()";
} else if (
TransactionFeeRequestData.FromDate ||
TransactionFeeRequestData.ToDate
) {
2020-01-07 12:28:19 +00:00
// var timeIntervalQry =
// 'blockchain_block.timestamp BETWEEN SYMMETRIC ' +
// TransactionFeeRequestData.FromDate +
// ' AND ' +
// TransactionFeeRequestData.ToDate;
var timeIntervalQry =
"blockchain_block.timestamp at time zone '" +
process.env.TIME_ZONE +
"' BETWEEN SYMMETRIC '" +
2019-07-09 09:22:36 +00:00
TransactionFeeRequestData.FromDate +
2020-01-07 12:28:19 +00:00
"' AND '" +
TransactionFeeRequestData.ToDate +
"'";
2019-07-09 09:22:36 +00:00
var seriesquery =
"'" +
TransactionFeeRequestData.FromDate +
"'::timestamp, '" +
TransactionFeeRequestData.ToDate +
"'";
} else {
var timeIntervalQry =
"blockchain_block.timestamp > current_date - interval '30 days'";
var seriesquery = "now() - interval '30 days', now()";
}
2019-08-14 10:55:23 +00:00
const TransactionHeatmapChartQuery = await getConnection(Global.network)
2019-07-09 09:22:36 +00:00
.query(
'with hours as ( SELECT hour::date from generate_series(' +
2019-08-02 12:54:58 +00:00
seriesquery +
", '1 day') as hour) select hours.hour, \
2019-07-09 09:22:36 +00:00
t1.totalinput, \
t1.totalkernal, \
t1.totaloutput \
from hours left join(select to_char(x.timestamp,'YYYY-MM-DD') as hour_of_day , \
SUM(x.input_count) as totalinput, SUM(x.kernal_count) as totalkernal, SUM(x.output_count) as totaloutput \
from ( SELECT blockchain_block.hash, \
blockchain_block.timestamp, \
sum(bi.block_id_count) AS input_count,\
sum(bk.block_id_count) AS kernal_count, \
sum(bo.block_id_count) AS output_count \
FROM blockchain_block \
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_input group by block_id) as bi \
ON blockchain_block.hash = \
bi.block_id \
2019-08-28 05:50:16 +00:00
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_kernel where features != 'Coinbase' group by block_id) as bk \
2019-07-09 09:22:36 +00:00
ON blockchain_block.hash = \
bk.block_id \
2019-08-28 06:21:53 +00:00
LEFT JOIN (select block_id, count(block_id) as block_id_count from blockchain_output where output_type != 'Coinbase' group by block_id) as bo \
2019-07-09 09:22:36 +00:00
ON blockchain_block.hash = \
2019-10-17 06:06:42 +00:00
bo.block_id WHERE " +
2019-08-02 12:54:58 +00:00
timeIntervalQry +
" \
2019-07-09 09:22:36 +00:00
GROUP BY blockchain_block.hash \
ORDER BY blockchain_block.timestamp DESC) as x \
group by to_char(x.timestamp,'YYYY-MM-DD') \
order by hour_of_day asc) as t1 on to_timestamp(t1.hour_of_day, 'YYYY-MM-DD') = hours.hour",
)
.catch(err_msg => {
next(err_msg);
});
let date = [],
totalinput = [],
totalkernal = [],
totaloutput = [];
TransactionHeatmapChartQuery.forEach(e => {
2019-10-18 07:33:50 +00:00
if(moment(e.hour).format('YYYY-MM-DD') >= moment('2019-09-03').format('YYYY-MM-DD'))
2019-10-17 05:51:52 +00:00
{
date.push(moment(e.hour).format('YYYY-MM-DD'));
totalinput.push(e.totalinput != null ? e.totalinput : 0);
totalkernal.push(e.totalkernal != null ? e.totalkernal : 0);
totaloutput.push(e.totaloutput != null ? e.totaloutput : 0);
}
2019-07-09 09:22:36 +00:00
});
response.status(200).json({
status: 200,
timestamp: Date.now(),
message: 'Transaction chart fetched successfully',
response: {
date,
totalinput,
totalkernal,
totaloutput,
},
});
} catch (error) {
next(new InternalServerErrorException(error));
}
};
}