Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
setConfigAppID,
} from '@storage';
import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic';
import { observer as globalObserver } from '@utilities/observer';
import APIMiddleware from './api-middleware';
import { observer as globalObserver } from '@utilities/observer';

const socket_url = `wss://${getServerAddressFallback()}/websockets/v3?app_id=${getAppIdFallback()}&l=${getLanguage().toUpperCase()}&brand=deriv`;

Expand Down
94 changes: 25 additions & 69 deletions src/api-middleware.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,23 @@
import { datadogLogs } from '@datadog/browser-logs';
import { formatDate } from './utilities/utility-functions';

const DATADOG_CLIENT_LOGS_TOKEN = process.env.DATADOG_CLIENT_LOGS_TOKEN ?? '';
const isProduction = process.env.NODE_ENV === 'production';
const isStaging = process.env.NODE_ENV === 'staging';
let dataDogSessionSampleRate = 0;

dataDogSessionSampleRate = process.env.DATADOG_LOGS_SESSION_SAMPLE_RATE
? +process.env.DATADOG_LOGS_SESSION_SAMPLE_RATE
: 1;
let dataDogVersion = '';
let dataDogEnv = '';

if (isProduction) {
dataDogVersion = `binary-bot-${process.env.REF_NAME}`;
dataDogEnv = 'production';
} else if (isStaging) {
dataDogVersion = `binary-bot-staging-v${formatDate(new Date())}`;
dataDogEnv = 'staging';
}

if (DATADOG_CLIENT_LOGS_TOKEN) {
datadogLogs.init({
clientToken: DATADOG_CLIENT_LOGS_TOKEN,
site: 'datadoghq.com',
forwardErrorsToLogs: false,
service: 'BinaryBot',
sessionSampleRate: dataDogSessionSampleRate,
version: dataDogVersion,
env: dataDogEnv,
});
}

export const REQUESTS = [
'active_symbols',
'authorize',
'balance',
'buy',
'proposal',
'proposal_open_contract',
'run-proposal',
'transaction',
'ticks_history',
'history',
'history'
];

class APIMiddleware {
constructor(config) {
this.config = config;
this.debounced_calls = {};
this.addGlobalMethod();
}

/* eslint-disable class-methods-use-this */
getRequestType = request => {
let req_type;
REQUESTS.forEach(type => {
Expand All @@ -60,31 +27,27 @@ class APIMiddleware {
return req_type;
};

/* eslint-disable class-methods-use-this */
log = (measures = [], is_bot_running) => {
if (measures && measures.length) {
measures.forEach(measure => {
datadogLogs.logger.info(measure.name, {
name: measure.name,
startTime: measure.startTimeDate,
duration: measure.duration,
detail: measure.detail,
isBotRunning: is_bot_running,
});
});
}
};

/* eslint-disable class-methods-use-this */
defineMeasure = res_type => {
if (res_type) {
let measure;
if (res_type === 'proposal') {
performance.mark('first_proposal_end');
if (performance.getEntriesByName('bot-start', 'mark').length) {
measure = performance.measure('run-proposal', 'bot-start', 'first_proposal_end');
performance.clearMarks('bot-start');
console.table('bot-first-run', measure.duration)
}
}
if (res_type === 'history') {
performance.mark('ticks_history_end');
measure = performance.measure('ticks_history', 'ticks_history_start', 'ticks_history_end');
console.table('ticks_history', measure.duration)
} else {
performance.mark(`${res_type}_end`);
measure = performance.measure(`${res_type}`, `${res_type}_start`, `${res_type}_end`);
if (res_type === 'proposal') {
console.table('proposal', measure.duration)
}
}
return (measure.startTimeDate = new Date(Date.now() - measure.startTime));
}
Expand All @@ -94,26 +57,19 @@ class APIMiddleware {
sendIsCalled = ({ response_promise, args: [request] }) => {
const req_type = this.getRequestType(request);
if (req_type) performance.mark(`${req_type}_start`);
response_promise
.then(res => {
const res_type = this.getRequestType(res);
if (res_type) {
this.defineMeasure(res_type);
}
})
.catch(() => {});
response_promise.then(res => {
const res_type = this.getRequestType(res);
if (res_type) {
this.defineMeasure(res_type);
}
});
return response_promise;
};

sendRequestsStatistic = is_bot_running => {
REQUESTS.forEach(req_type => {
const measure = performance.getEntriesByName(req_type);
if (measure && measure.length) {
if (DATADOG_CLIENT_LOGS_TOKEN) {
this.log(measure, is_bot_running, req_type);
}
}
});
sendRequestsStatistic = () => {
// REQUESTS.forEach(req_type => {
// const measure = performance.getEntriesByName(req_type);
// });
performance.clearMeasures();
};

Expand Down
5 changes: 4 additions & 1 deletion src/components/ToolBox/ToolBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ const ToolBox = ({
id_container='runButton'
tooltip={translate('Run the bot')}
position='bottom'
onClick={() => globalObserver.emit('blockly.start')}
onClick={() => {
globalObserver.emit("blockly.start")
performance.mark('bot-start');
}}
classes={classNames('toolbox-button icon-run', {
'toolbox-hide': !is_workspace_rendered,
})}
Expand Down