Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

91 transactions api performance #92

Merged
merged 4 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rest-api/__tests__/balances.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const request = require('supertest');
const math = require('mathjs');
const config = require('../config.js');
const server = require('../server');
const utils = require('../utils.js');

Expand Down Expand Up @@ -34,7 +35,7 @@ describe('/balances tests', () => {
let response = await request(server).get(apiPrefix + '/balances' + (extraParams === '' ? '' : '?') + extraParams);
expect(response.status).toEqual(200);

const currentSeconds = Math.floor(new Date().getTime() / 1000) - 3600;
const currentSeconds = Math.floor(new Date().getTime() / 1000) - (60 * 60);
expect(utils.secNsToSeconds(JSON.parse(response.text).timestamp)).toBeGreaterThan(currentSeconds);

let balances = JSON.parse(response.text).balances;
Expand All @@ -44,7 +45,7 @@ describe('/balances tests', () => {
const bigPageEntries = balances;
let paginatedEntries = [];
const numPages = 5;
const pageSize = utils.globals.MAX_LIMIT / numPages;
const pageSize = config.limits.RESPONSE_ROWS / numPages;
const firstAcc = orderOptions === 'order=asc' ?
balances[balances.length - 1].account :
balances[0].account;
Expand All @@ -71,7 +72,7 @@ describe('/balances tests', () => {
expect(check).toBeTruthy();

check = true;
for (i = 0; i < utils.globals.MAX_LIMIT; i++) {
for (i = 0; i < config.limits.RESPONSE_ROWS; i++) {
if (bigPageEntries[i].account !== paginatedEntries[i].account ||
bigPageEntries[i].balance !== paginatedEntries[i].balance) {
check = false;
Expand Down
8 changes: 5 additions & 3 deletions rest-api/__tests__/transactions.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const request = require('supertest');
const math = require('mathjs');
const config = require('../config.js');
const server = require('../server');
const utils = require('../utils.js');



beforeAll(async () => {
console.log('Jest starting!');
jest.setTimeout(20000);
Expand Down Expand Up @@ -109,7 +111,7 @@ describe('transaction tests', () => {
let response = await request(server).get(apiPrefix + '/transactions' + (extraParams === '' ? '' : '?') + extraParams);
expect(response.status).toEqual(200);
let transactions = JSON.parse(response.text).transactions;
expect(transactions.length).toEqual(utils.globals.MAX_LIMIT);
expect(transactions.length).toEqual(config.limits.RESPONSE_ROWS);

let check = true;
let prevSeconds = utils.secNsToSeconds(transactions[0].consensus_timestamp);
Expand All @@ -127,7 +129,7 @@ describe('transaction tests', () => {
const bigPageEntries = transactions
let paginatedEntries = [];
const numPages = 5;
const pageSize = utils.globals.MAX_LIMIT / numPages;
const pageSize = config.limits.RESPONSE_ROWS / numPages;
const firstTs = orderOptions === 'order=asc' ?
transactions[transactions.length - 1].consensus_timestamp :
transactions[0].consensus_timestamp;
Expand Down Expand Up @@ -155,7 +157,7 @@ describe('transaction tests', () => {
expect(check).toBeTruthy();

check = true;
for (i = 0; i < utils.globals.MAX_LIMIT; i++) {
for (i = 0; i < config.limits.RESPONSE_ROWS; i++) {
if (bigPageEntries[i].transaction_id !== paginatedEntries[i].transaction_id ||
bigPageEntries[i].consensus_timestamp !== paginatedEntries[i].consensus_timestamp) {
check = false;
Expand Down
115 changes: 49 additions & 66 deletions rest-api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ const transactions = require('./transactions.js');
/**
* Handler function for /accounts API.
* @param {Request} req HTTP request object
* @param {Response} res HTTP response object
* @return {} None.
* @return {Promise} Promise for PostgreSQL query
*/
const getAccounts = function (req, res) {
logger.debug("Client: [" + req.ip + "] URL: " + req.originalUrl);

// Parse the filter parameters for account-numbers, balances, publicKey and pagination (limit/offset)

const getAccounts = function (req) {
// Parse the filter parameters for account-numbers, balances, publicKey and pagination
const [accountQuery, accountParams] =
utils.parseParams(req, 'account.id',
[{ shard: 'entity_shard', realm: 'entity_realm', num: 'entity_num' }],
Expand All @@ -22,19 +18,14 @@ const getAccounts = function (req, res) {
['balance']);

let [pubKeyQuery, pubKeyParams] = utils.parseParams(req, 'account.publickey',
['e.key']);

['e.key'], 'hexstring');
pubKeyQuery = pubKeyQuery === '' ? '' :
"(e.entity_shard = ab.shard \n" +
" and e.entity_realm = ab.realm\n" +
" and e.entity_num = ab.num and " +
pubKeyQuery +
")";

let [anchorQuery, anchorParams] =
utils.parseParams(req, 'pageanchor', ['t.consensus_seconds']);
anchorQuery = anchorQuery.replace('=', '<=');

const { limitQuery, limitParams, order, limit } =
utils.parseLimitAndOrderParams(req, 'asc');

Expand Down Expand Up @@ -72,58 +63,54 @@ const getAccounts = function (req, res) {
pgEntityQuery + JSON.stringify(entityParams));

// Execute query
pool.query(pgEntityQuery, entityParams, (error, results) => {
let ret = {
accounts: [],
links: {
next: null
}
};

if (error) {
logger.error("getAccounts error: " +
JSON.stringify(error, Object.getOwnPropertyNames(error)));
res.json(ret);
return;
}
return (pool
.query(pgEntityQuery, entityParams)
.then(results => {
let ret = {
accounts: [],
links: {
next: null
}
};

for (let row of results.rows) {
row.balance = {};
row.balance.timestamp = '' + row.balance_asof_seconds + '.' +
((row.balance_asof_nanos + '000000000').substring(0, 9));
delete row.balance_asof_seconds;
delete row.balance_asof_nanos;

for (let row of results.rows) {
row.balance = {};
row.balance.timestamp = '' + row.balance_asof_seconds + '.' +
((row.balance_asof_nanos + '000000000').substring(0, 9));
delete row.balance_asof_seconds;
delete row.balance_asof_nanos;

row.expiry_timestamp = utils.nsToSecNs(row.exp_time_ns);
delete row.exp_time_ns;
row.expiry_timestamp = utils.nsToSecNs(row.exp_time_ns);
delete row.exp_time_ns;

row.balance.balance = Number(row.account_balance);
delete row.account_balance;
row.balance.balance = Number(row.account_balance);
delete row.account_balance;

row.auto_renew_period = Number(row.auto_renew_period);
}
row.auto_renew_period = Number(row.auto_renew_period);

let anchorAcc = '0.0.0';
if (results.rows.length > 0) {
anchorAcc = results.rows[results.rows.length - 1].account;
}
row.admin_key = utils.encodeKey(row.admin_key);
row.key = utils.encodeKey(row.key);
}

ret.accounts = results.rows;
let anchorAcc = '0.0.0';
if (results.rows.length > 0) {
anchorAcc = results.rows[results.rows.length - 1].account;
}

logger.debug("ret.accounts.length: " +
ret.accounts.length + ' === limit: ' + limit);
ret.accounts = results.rows;

ret.links = {
next: utils.getPaginationLink(req,
(ret.accounts.length !== limit),
'account.id', anchorAcc, order)
}
ret.links = {
next: utils.getPaginationLink(req,
(ret.accounts.length !== limit),
'account.id', anchorAcc, order)
}

logger.debug("getAccounts returning " +
ret.accounts.length + " entries");
logger.debug("getAccounts returning " +
ret.accounts.length + " entries");

res.json(ret);
});
return (ret);
})
)
}


Expand All @@ -136,7 +123,7 @@ const getAccounts = function (req, res) {
const getOneAccount = function (req, res) {
logger.debug("Client: [" + req.ip + "] URL: " + req.originalUrl);

// Parse the filter parameters for account-numbers, balance, and pagination (limit/offset)
// Parse the filter parameters for account-numbers, balance, and pagination

const acc = utils.parseEntityId(req.params.id);

Expand All @@ -148,16 +135,10 @@ const getOneAccount = function (req, res) {
const [tsQuery, tsParams] =
utils.parseParams(req, 'timestamp', ['t.consensus_seconds']);

let [anchorQuery, anchorParams] =
utils.parseParams(req, 'pageanchor', ['t.consensus_seconds']);
anchorQuery = anchorQuery.replace('=', '<=');

const resultTypeQuery = utils.parseResultParams(req);
const { limitQuery, limitParams, order, limit } =
utils.parseLimitAndOrderParams(req);



let ret = {
balance: {
timestamp: null,
Expand Down Expand Up @@ -192,7 +173,7 @@ const getOneAccount = function (req, res) {
const pgEntityQuery = utils.convertMySqlStyleQueryToPostgress(
entitySql, entityParams);

logger.debug("getOneAccount transactions query: " +
logger.debug("getOneAccount entity query: " +
pgEntityQuery + JSON.stringify(entityParams));
// Execute query & get a promise
const entityPromise = pool.query(pgEntityQuery, entityParams);
Expand Down Expand Up @@ -222,14 +203,12 @@ const getOneAccount = function (req, res) {
' and t.fk_result_id = tr.id\n' +
(accountQuery === '' ? '' : ' and ') + accountQuery + '\n' +
(tsQuery === '' ? '' : ' and ') + tsQuery + '\n' +
(anchorQuery === '' ? '' : ' and ') + anchorQuery + '\n' +
resultTypeQuery + '\n' +
' order by t.consensus_seconds ' + order +
' , t.consensus_nanos ' + order + '\n' +
' ' + limitQuery;
const innerParams = accountParams
.concat(tsParams)
.concat(anchorParams)
.concat(limitParams);

let transactionsQuery =
Expand Down Expand Up @@ -309,6 +288,10 @@ const getOneAccount = function (req, res) {
for (let row of entityResults.rows) {
row.expiry_timestamp = utils.nsToSecNs(row.exp_time_ns);
delete row.exp_time_ns;

row.admin_key = utils.encodeKey(row.admin_key);
row.key = utils.encodeKey(row.key);

}
ret.entity_data = entityResults.rows;

Expand Down
Loading