From fb8d23f51baa785a1ea50b0fe67cadc0f7f140a8 Mon Sep 17 00:00:00 2001 From: Artem Baskal Date: Mon, 13 Jan 2020 17:41:59 +0300 Subject: [PATCH 1/2] - client: fix logic of top clients normalization --- .../Settings/Clients/AutoClients.js | 2 +- .../Settings/Clients/ClientsTable.js | 5 +++- .../src/components/Settings/Clients/index.js | 2 ++ client/src/helpers/helpers.js | 23 +++++++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/client/src/components/Settings/Clients/AutoClients.js b/client/src/components/Settings/Clients/AutoClients.js index 4348c62b..295c0466 100644 --- a/client/src/components/Settings/Clients/AutoClients.js +++ b/client/src/components/Settings/Clients/AutoClients.js @@ -38,7 +38,7 @@ class AutoClients extends Component { }, { Header: this.props.t('requests_count'), - accessor: row => this.props.normalizedTopClients[row.ip] || 0, + accessor: row => this.props.normalizedTopClients.auto[row.ip] || 0, sortMethod: (a, b) => b - a, id: 'statistics', minWidth: COLUMN_MIN_WIDTH, diff --git a/client/src/components/Settings/Clients/ClientsTable.js b/client/src/components/Settings/Clients/ClientsTable.js index dc1c4c39..b1a806f4 100644 --- a/client/src/components/Settings/Clients/ClientsTable.js +++ b/client/src/components/Settings/Clients/ClientsTable.js @@ -40,6 +40,7 @@ class ClientsTable extends Component { } else { this.handleFormAdd(config); } + this.props.getStats(); }; getClient = (name, clients) => { @@ -64,6 +65,7 @@ class ClientsTable extends Component { // eslint-disable-next-line no-alert if (window.confirm(this.props.t('client_confirm_delete', { key: data.name }))) { this.props.deleteClient(data); + this.props.getStats(); } }; @@ -161,7 +163,7 @@ class ClientsTable extends Component { { Header: this.props.t('requests_count'), id: 'statistics', - accessor: row => this.props.normalizedTopClients[row.name] || 0, + accessor: row => this.props.normalizedTopClients.configured[row.name] || 0, sortMethod: (a, b) => b - a, minWidth: 120, Cell: (row) => { @@ -305,6 +307,7 @@ ClientsTable.propTypes = { processingAdding: PropTypes.bool.isRequired, processingDeleting: PropTypes.bool.isRequired, processingUpdating: PropTypes.bool.isRequired, + getStats: PropTypes.func.isRequired, }; export default withNamespaces()(ClientsTable); diff --git a/client/src/components/Settings/Clients/index.js b/client/src/components/Settings/Clients/index.js index 667441ef..f422a7ef 100644 --- a/client/src/components/Settings/Clients/index.js +++ b/client/src/components/Settings/Clients/index.js @@ -23,6 +23,7 @@ class Clients extends Component { updateClient, deleteClient, toggleClientModal, + getStats, } = this.props; return ( @@ -44,6 +45,7 @@ class Clients extends Component { processingAdding={clients.processingAdding} processingDeleting={clients.processingDeleting} processingUpdating={clients.processingUpdating} + getStats={getStats} /> { export const normalizeTextarea = text => text && text.replace(/[;, ]/g, '\n').split('\n').filter(n => n); -export const normalizeTopClients = clients => clients.reduce((accumulator, clientObj) => { - const { name, count } = clientObj; +/** + * Normalizes the topClients array + * + * @param {Object[]} topClients + * @param {string} topClients.name + * @param {number} topClients.count + * @param {Object} topClients.info + * @param {string} topClients.info.name + * @returns {Object} normalizedTopClients + * @returns {Object.} normalizedTopClients.auto - auto clients + * @returns {Object.} normalizedTopClients.configured - configured clients + */ + +export const normalizeTopClients = topClients => topClients.reduce((nameToCountMap, clientObj) => { + const { name, count, info: { name: infoName } } = clientObj; // eslint-disable-next-line no-param-reassign - accumulator[name] = count; - return accumulator; + nameToCountMap.auto = { ...nameToCountMap.auto, [name]: count }; + // eslint-disable-next-line no-param-reassign + nameToCountMap.configured = { ...nameToCountMap.configured, [infoName]: count }; + return nameToCountMap; }, {}); export const getClientInfo = (clients, ip) => { From 392535ce3aad89fad52074399ec7b6986de8c4e9 Mon Sep 17 00:00:00 2001 From: Artem Baskal Date: Tue, 14 Jan 2020 11:09:17 +0300 Subject: [PATCH 2/2] - client: change code style of top clients normalization helper --- client/src/helpers/helpers.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/helpers/helpers.js b/client/src/helpers/helpers.js index 54fcfa75..3f9511bc 100644 --- a/client/src/helpers/helpers.js +++ b/client/src/helpers/helpers.js @@ -277,11 +277,11 @@ export const normalizeTextarea = text => text && text.replace(/[;, ]/g, '\n').sp export const normalizeTopClients = topClients => topClients.reduce((nameToCountMap, clientObj) => { const { name, count, info: { name: infoName } } = clientObj; // eslint-disable-next-line no-param-reassign - nameToCountMap.auto = { ...nameToCountMap.auto, [name]: count }; + nameToCountMap.auto[name] = count; // eslint-disable-next-line no-param-reassign - nameToCountMap.configured = { ...nameToCountMap.configured, [infoName]: count }; + nameToCountMap.configured[infoName] = count; return nameToCountMap; -}, {}); +}, { auto: {}, configured: {} }); export const getClientInfo = (clients, ip) => { const client = clients