Skip to content

Commit

Permalink
Merge pull request #887 from bitholla/stream-data
Browse files Browse the repository at this point in the history
Stream data
  • Loading branch information
abeikverdi authored Jul 29, 2021
2 parents 1153b13 + fde563f commit 5560ed7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
108 changes: 99 additions & 9 deletions server/api/controllers/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,62 @@ const getUserTrades = (req, res) => {
);

const user_id = req.auth.sub.id;
const { limit, page, order_by, order, start_date, end_date, format } = req.swagger.params;
const {
limit,
page,
order_by,
order,
start_date,
end_date,
format
} = req.swagger.params;
const symbol = req.swagger.params.symbol.value;

loggerTrades.info(
req.uuid,
'controllers/trade/getUserTrades params',
'user_id',
user_id,
'symbol',
symbol,
'limit',
limit.value,
'page',
page.value,
'order_by',
order_by.value,
'order',
order.value,
'start_date',
start_date.value,
'end_date',
end_date.value,
'format',
format.value
);

if (symbol && !toolsLib.subscribedToPair(symbol)) {
loggerTrades.error(req.uuid, 'controllers/trade/getUserTrades', 'Invalid symbol');
return res.status(400).json({ message: 'Invalid symbol' });
}

toolsLib.order.getAllUserTradesByKitId(user_id, symbol, limit.value, page.value, order_by.value, order.value, start_date.value, end_date.value, format.value)
toolsLib.order.getAllUserTradesByKitId(
user_id,
symbol,
limit.value,
page.value,
order_by.value,
order.value,
start_date.value,
end_date.value,
format.value
)
.then((data) => {
if (format.value) {
if (format.value == 'csv') {
res.setHeader('Content-disposition', `attachment; filename=${toolsLib.getKitConfig().api_name}-trades.csv`);
res.set('Content-Type', 'text/csv');
return res.status(202).send(data);
res.status(202);
return res.send(data);
} else {
return res.json(data);
}
Expand All @@ -41,20 +83,68 @@ const getAdminTrades = (req, res) => {

const { user_id, symbol, limit, page, order_by, order, start_date, end_date, format } = req.swagger.params;

loggerTrades.info(
req.uuid,
'controllers/trade/getAdminTrades params',
'user_id',
user_id.value,
'symbol',
symbol.value,
'limit',
limit.value,
'page',
page.value,
'order_by',
order_by.value,
'order',
order.value,
'start_date',
start_date.value,
'end_date',
end_date.value,
'format',
format.value
);

if (symbol.value && !toolsLib.subscribedToPair(symbol.value)) {
loggerTrades.error(req.uuid, 'controllers/trade/getUserTrades', 'Invalid symbol');
return res.status(400).json({ message: 'Invalid symbol' });
}

let promiseQuery;

if (user_id.value) {
promiseQuery = toolsLib.order.getAllUserTradesByKitId(user_id.value, symbol.value, limit.value, page.value, order_by.value, order.value, start_date.value, end_date.value, format.value);
promiseQuery = toolsLib.order.getAllUserTradesByKitId(
user_id.value,
symbol.value,
limit.value,
page.value,
order_by.value,
order.value,
start_date.value,
end_date.value,
format.value
);
} else {
promiseQuery = toolsLib.order.getAllTradesNetwork(symbol.value, limit.value, page.value, order_by.value, order.value, start_date.value, end_date.value, format.value);
promiseQuery = toolsLib.order.getAllTradesNetwork(
symbol.value,
limit.value,
page.value,
order_by.value,
order.value,
start_date.value,
end_date.value,
format.value
);
}

promiseQuery
.then((data) => {
if (format.value) {
res.setHeader('Content-disposition', `attachment; filename=${toolsLib.getKitConfig().api_name}-users-trades.csv`);
if (format.value === 'csv') {
res.setHeader('Content-disposition', `attachment; filename=${user_id.value ? `user-${user_id.value}-` : ''}trades.csv`);
res.set('Content-Type', 'text/csv');
return res.status(202).send(data);
res.status(202);
return res.send(data);
} else {
return res.json(data);
}
Expand Down
7 changes: 3 additions & 4 deletions server/api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ consumes:
produces:
- application/json
- text/csv
- text/plain

securityDefinitions:
Bearer:
Expand Down Expand Up @@ -1572,14 +1573,12 @@ paths:
required: false
type: number
format: int32
default: 50
- in: query
name: page
description: Page of data to retrieve
required: false
type: number
format: int32
default: 1
- in: query
name: order_by
description: Field to order data
Expand Down Expand Up @@ -1607,7 +1606,7 @@ paths:
name: format
description: Specify data format
required: false
enum: ['csv']
enum: ['csv', 'all']
type: string
responses:
200:
Expand Down Expand Up @@ -2988,7 +2987,7 @@ paths:
name: format
description: Specify data format
required: false
enum: ['csv']
enum: ['csv', 'all']
type: string
tags:
- Admin
Expand Down
6 changes: 4 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "bitHolla Inc.",
"main": "app.js",
"dependencies": {
"JSONStream": "1.3.5",
"bcryptjs": "2.4.3",
"bluebird": "3.5.3",
"body-parser": "1.19.0",
Expand All @@ -28,8 +29,8 @@
"flat": "5.0.0",
"geoip-lite": "1.4.1",
"helmet": "3.12.0",
"hollaex-node-lib": "github:bitholla/hollaex-node-lib#2.5",
"hollaex-tools-lib": "github:bitholla/hollaex-tools-lib#2.8",
"hollaex-node-lib": "github:bitholla/hollaex-node-lib#2.6",
"hollaex-tools-lib": "github:bitholla/hollaex-tools-lib#2.9",
"http": "0.0.0",
"install": "0.10.4",
"json2csv": "4.5.4",
Expand All @@ -48,6 +49,7 @@
"otp": "0.1.3",
"pg": "6.4.2",
"pg-hstore": "2.3.2",
"pg-query-stream": "4.1.0",
"pm2": "2.10.1",
"pmx": "1.6.4",
"random-string": "0.2.0",
Expand Down

0 comments on commit 5560ed7

Please sign in to comment.