Skip to content

Commit dc90a63

Browse files
committed
Merge branch 'aminmarashi-separate-total-info-per-account' into dev
2 parents 1b0d8e1 + 884e81e commit dc90a63

File tree

15 files changed

+213
-105
lines changed

15 files changed

+213
-105
lines changed

src/botPage/bot/TradeEngine/Balance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default Engine =>
2222
balanceStr = `${balance} ${currency}`;
2323

2424
this.balancePromise();
25-
info({ balance: balanceStr });
25+
info({ accountID: this.accountInfo.loginid, balance: balanceStr });
2626
});
2727
}
2828
// eslint-disable-next-line class-methods-use-this

src/botPage/bot/TradeEngine/OpenContract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default Engine =>
2121

2222
this.data = this.data.set('contract', contract);
2323

24-
broadcastContract(contract);
24+
broadcastContract({ accountID: this.accountInfo.loginid, ...contract });
2525

2626
if (this.isSold) {
2727
this.contractId = '';

src/botPage/bot/TradeEngine/Purchase.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default Engine =>
2525
delayIndex = 0;
2626
notify('info', `${translate('Bought')}: ${buy.longcode}`);
2727
info({
28+
accountID : this.accountInfo.loginid,
2829
totalRuns : this.updateAndReturnTotalRuns(),
2930
transaction_ids: { buy: buy.transaction_id },
3031
contract_type : contractType,

src/botPage/bot/TradeEngine/Total.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { roundBalance } from '../../common/tools';
33
import { info, notify } from '../broadcast';
44
import createError from '../../common/error';
55

6-
let totalProfit = 0;
7-
let totalWins = 0;
8-
let totalLosses = 0;
9-
let totalStake = 0;
10-
let totalPayout = 0;
11-
let totalRuns = 0;
6+
const skeleton = {
7+
totalProfit: 0,
8+
totalWins : 0,
9+
totalLosses: 0,
10+
totalStake : 0,
11+
totalPayout: 0,
12+
totalRuns : 0,
13+
};
14+
15+
const globalStat = {};
1216

1317
export default Engine =>
1418
class Total extends Engine {
@@ -24,24 +28,36 @@ export default Engine =>
2428

2529
const win = profit > 0;
2630

27-
totalWins += win ? 1 : 0;
31+
const accountStat = this.getAccountStat();
32+
33+
accountStat.totalWins += win ? 1 : 0;
2834

29-
totalLosses += !win ? 1 : 0;
35+
accountStat.totalLosses += !win ? 1 : 0;
3036

3137
this.sessionProfit = roundBalance({ currency, balance: Number(this.sessionProfit) - Number(profit) });
3238

33-
totalProfit = roundBalance({ currency, balance: Number(totalProfit) + Number(profit) });
34-
totalStake = roundBalance({ currency, balance: Number(totalStake) + Number(buyPrice) });
35-
totalPayout = roundBalance({ currency, balance: Number(totalPayout) + Number(sellPrice) });
39+
accountStat.totalProfit = roundBalance({
40+
currency,
41+
balance: Number(accountStat.totalProfit) + Number(profit),
42+
});
43+
accountStat.totalStake = roundBalance({
44+
currency,
45+
balance: Number(accountStat.totalStake) + Number(buyPrice),
46+
});
47+
accountStat.totalPayout = roundBalance({
48+
currency,
49+
balance: Number(accountStat.totalPayout) + Number(sellPrice),
50+
});
3651

3752
info({
3853
profit,
3954
contract,
40-
totalProfit,
41-
totalWins,
42-
totalLosses,
43-
totalStake,
44-
totalPayout,
55+
accountID : this.accountInfo.loginid,
56+
totalProfit: accountStat.totalProfit,
57+
totalWins : accountStat.totalWins,
58+
totalLosses: accountStat.totalLosses,
59+
totalStake : accountStat.totalStake,
60+
totalPayout: accountStat.totalPayout,
4561
});
4662

4763
if (win) {
@@ -52,14 +68,18 @@ export default Engine =>
5268
}
5369
updateAndReturnTotalRuns() {
5470
this.sessionRuns++;
55-
return ++totalRuns;
71+
const accountStat = this.getAccountStat();
72+
73+
return ++accountStat.totalRuns;
5674
}
5775
/* eslint-disable class-methods-use-this */
5876
getTotalRuns() {
59-
return totalRuns;
77+
const accountStat = this.getAccountStat();
78+
return accountStat.totalRuns;
6079
}
6180
getTotalProfit() {
62-
return totalProfit;
81+
const accountStat = this.getAccountStat();
82+
return accountStat.totalProfit;
6383
}
6484
/* eslint-enable */
6585
checkLimits(tradeOption) {
@@ -78,4 +98,13 @@ export default Engine =>
7898
}
7999
}
80100
}
101+
getAccountStat() {
102+
const { loginid: accountID } = this.accountInfo;
103+
104+
if (!(accountID in globalStat)) {
105+
globalStat[accountID] = { ...skeleton };
106+
}
107+
108+
return globalStat[accountID];
109+
}
81110
};

src/botPage/bot/TradeEngine/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop
9696
doUntilDone(() => this.api.authorize(token)).catch(e => this.$scope.observer.emit('Error', e));
9797

9898
return new Promise(resolve =>
99-
this.listen('authorize', () => {
99+
this.listen('authorize', ({ authorize }) => {
100+
this.accountInfo = authorize;
100101
this.token = token;
101102
resolve();
102103
})

src/botPage/view/updateSummary/Summary.js renamed to src/botPage/view/TradeInfoPanel/Summary.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import React, { Component } from 'react';
2+
import { observer as globalObserver } from 'binary-common-utils/lib/observer';
23
import { translate } from '../../../common/i18n';
34
import * as style from '../style';
45

56
export default class Summary extends Component {
6-
componentWillReceiveProps({ tradeInfo }) {
7-
this.setState({ ...this.state, ...tradeInfo });
7+
componentWillMount() {
8+
globalObserver.register('bot.info', info => {
9+
const { accountID } = info;
10+
this.setState({ [accountID]: { ...this.state[accountID], ...info } });
11+
});
812
}
9-
constructor() {
13+
constructor({ accountID }) {
1014
super();
11-
this.state = {};
15+
this.state = { [accountID]: {} };
1216
}
1317
render() {
14-
const { totalRuns, totalStake, totalPayout, totalWins, totalLosses, totalProfit, balance } = this.state;
18+
const { accountID } = this.props;
19+
20+
const { totalRuns, totalStake, totalPayout, totalWins, totalLosses, totalProfit, balance } =
21+
accountID in this.state ? this.state[accountID] : {};
1522

1623
const profitColor = {
1724
color: totalProfit > 0 ? 'green' : 'red',
@@ -20,6 +27,9 @@ export default class Summary extends Component {
2027
<table>
2128
<thead>
2229
<tr>
30+
<th>
31+
{translate('Account')}
32+
</th>
2333
<th>
2434
{translate('No. of runs')}
2535
</th>
@@ -45,6 +55,9 @@ export default class Summary extends Component {
4555
</thead>
4656
<tbody>
4757
<tr>
58+
<td className="accountID">
59+
{accountID}
60+
</td>
4861
<td className="totalRuns">
4962
{totalRuns}
5063
</td>

src/botPage/view/updateTradeTable/TradeTable.js renamed to src/botPage/view/TradeInfoPanel/TradeTable.js

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json2csv from 'json2csv';
22
import React, { Component } from 'react';
33
import ReactDataGrid from 'react-data-grid';
4+
import { observer as globalObserver } from 'binary-common-utils/lib/observer';
45
import { appendRow, updateRow, saveAs } from '../shared';
56
import { translate } from '../../../common/i18n';
67
import { roundBalance } from '../../common/tools';
@@ -27,11 +28,17 @@ const ProfitColor = ({ value }) =>
2728
</div>;
2829

2930
export default class TradeTable extends Component {
30-
constructor() {
31+
constructor({ accountID }) {
3132
super();
3233
this.state = {
33-
id : 0,
34-
rows: [],
34+
initial: {
35+
id : 0,
36+
rows: [],
37+
},
38+
[accountID]: {
39+
id : 0,
40+
rows: [],
41+
},
3542
};
3643
this.columns = [
3744
{ key: 'id', width: 70, resizable: true, name: translate('Number') },
@@ -44,28 +51,40 @@ export default class TradeTable extends Component {
4451
{ key: 'profit', width: 80, resizable: true, name: translate('Profit/Loss'), formatter: ProfitColor },
4552
];
4653
}
47-
componentWillReceiveProps(nextProps) {
48-
const { trade: tradeObj } = nextProps;
49-
if (!Object.keys(tradeObj).length) {
50-
return;
51-
}
52-
const trade = {
53-
...tradeObj,
54-
profit: getProfit(tradeObj),
55-
};
56-
const prevRowIndex = this.state.rows.findIndex(t => t.reference === trade.reference);
57-
if (prevRowIndex >= 0) {
58-
this.setState(updateRow(prevRowIndex, trade, this.state));
59-
} else {
60-
this.setState(appendRow(trade, this.state));
61-
}
54+
componentWillMount() {
55+
globalObserver.register('bot.contract', info => {
56+
if (!info) {
57+
return;
58+
}
59+
const tradeObj = { reference: info.transaction_ids.buy, ...info };
60+
const { accountID } = tradeObj;
61+
62+
const trade = {
63+
...tradeObj,
64+
profit: getProfit(tradeObj),
65+
};
66+
67+
const accountStat = this.getAccountStat(accountID);
68+
69+
const rows = accountStat.rows;
70+
const prevRowIndex = rows.findIndex(t => t.reference === trade.reference);
71+
72+
if (prevRowIndex >= 0) {
73+
this.setState({ [accountID]: updateRow(prevRowIndex, trade, accountStat) });
74+
} else {
75+
this.setState({ [accountID]: appendRow(trade, accountStat) });
76+
}
77+
});
6278
}
6379
rowGetter(i) {
64-
return this.state.rows[i];
80+
const { accountID } = this.props;
81+
return this.state[accountID].rows[i];
6582
}
6683
export() {
84+
const { accountID } = this.props;
85+
6786
const data = json2csv({
68-
data : this.state.rows,
87+
data : this.state[accountID].rows,
6988
fields: [
7089
'id',
7190
'reference',
@@ -79,17 +98,25 @@ export default class TradeTable extends Component {
7998
});
8099
saveAs({ data, filename: 'logs.csv', type: 'text/csv;charset=utf-8' });
81100
}
82-
render() {
83-
if (!$('#tradeInfo:visible').length) {
84-
return <div style={{ height: minHeight }} />;
101+
getAccountStat(accountID) {
102+
if (!(accountID in this.state)) {
103+
const initialInfo = this.state.initial;
104+
this.setState({ [accountID]: { ...initialInfo } });
105+
return initialInfo;
85106
}
107+
return this.state[accountID];
108+
}
109+
render() {
110+
const { accountID } = this.props;
111+
const rows = accountID in this.state ? this.state[accountID].rows : [];
112+
86113
return (
87114
<div>
88115
<ExportButton onClick={() => this.export()} customStyle={style.tradeTableExport} />
89116
<ReactDataGrid
90117
columns={this.columns}
91118
rowGetter={this.rowGetter.bind(this)}
92-
rowsCount={this.state.rows.length}
119+
rowsCount={rows.length}
93120
minHeight={minHeight}
94121
/>
95122
</div>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React, { Component } from 'react';
2+
import { observer as globalObserver } from 'binary-common-utils/lib/observer';
3+
import { translate } from '../../../common/i18n';
4+
import { tradePanelAccount } from '../style';
5+
import Summary from './Summary';
6+
import TradeTable from './TradeTable';
7+
8+
export default class TradeInfoPanel extends Component {
9+
constructor() {
10+
super();
11+
this.state = { accountID: '', accountIDList: [], currentAccountID: '' };
12+
}
13+
componentWillMount() {
14+
globalObserver.register('bot.info', ({ accountID }) => {
15+
const { accountIDList } = this.state;
16+
if (!accountIDList.includes(accountID)) {
17+
this.setState({ accountIDList: [...accountIDList, accountID] });
18+
}
19+
if (!this.state.accountID) {
20+
this.setState({ accountID });
21+
}
22+
this.setState({ currentAccountID: accountID });
23+
});
24+
}
25+
render() {
26+
const { accountID, currentAccountID } = this.state;
27+
28+
return (
29+
<div>
30+
<div className="content">
31+
<div>
32+
<label style={tradePanelAccount}>
33+
{`${translate('Account')}: `}
34+
<select
35+
value={accountID}
36+
rel={el => (this.accountIDDropdown = el)}
37+
onChange={e => this.setState({ accountID: e.target.value })}
38+
>
39+
{this.state.accountIDList.map(account =>
40+
<option value={account}>
41+
{`${account}${account !== currentAccountID
42+
? ` - ${translate('Stopped')}`
43+
: ''}`}
44+
</option>
45+
)}
46+
</select>
47+
</label>
48+
<h3>
49+
<span>
50+
{translate('Summary')}
51+
</span>
52+
</h3>
53+
54+
<Summary accountID={accountID} />
55+
</div>
56+
<div>
57+
<h3>
58+
<span>
59+
{translate('Trades')}
60+
</span>
61+
</h3>
62+
63+
<TradeTable accountID={accountID} />
64+
</div>
65+
</div>
66+
</div>
67+
);
68+
}
69+
}

0 commit comments

Comments
 (0)