From 7812ee1861be0eea89bd419ee82f6cb45b24f043 Mon Sep 17 00:00:00 2001
From: Artem Baskal <a.baskal@adguard.com>
Date: Fri, 17 Jan 2020 13:27:22 +0300
Subject: [PATCH] + client: get rid of clients caching

---
 client/src/actions/queryLogs.js | 28 +++++++------------
 client/src/actions/stats.js     | 48 ++++++++++++++-------------------
 2 files changed, 30 insertions(+), 46 deletions(-)

diff --git a/client/src/actions/queryLogs.js b/client/src/actions/queryLogs.js
index b28e45d1..c3f6fdea 100644
--- a/client/src/actions/queryLogs.js
+++ b/client/src/actions/queryLogs.js
@@ -5,28 +5,20 @@ import { addErrorToast, addSuccessToast } from './index';
 import { normalizeLogs, getParamsForClientsSearch, addClientInfo } from '../helpers/helpers';
 import { TABLE_DEFAULT_PAGE_SIZE } from '../helpers/constants';
 
-// Cache clients in closure
-const getLogsWithParamsWrapper = () => {
-    let clients = {};
-    return async (config) => {
-        const { older_than, filter, ...values } = config;
-        const rawLogs = await apiClient.getQueryLog({ ...filter, older_than });
-        const { data, oldest } = rawLogs;
-        const logs = normalizeLogs(data);
-        const clientsParams = getParamsForClientsSearch(logs, 'client');
-        if (!Object.values(clientsParams).every(client => client in clients)) {
-            clients = await apiClient.findClients(clientsParams);
-        }
-        const logsWithClientInfo = addClientInfo(logs, clients, 'client');
+const getLogsWithParams = async (config) => {
+    const { older_than, filter, ...values } = config;
+    const rawLogs = await apiClient.getQueryLog({ ...filter, older_than });
+    const { data, oldest } = rawLogs;
+    const logs = normalizeLogs(data);
+    const clientsParams = getParamsForClientsSearch(logs, 'client');
+    const clients = await apiClient.findClients(clientsParams);
+    const logsWithClientInfo = addClientInfo(logs, clients, 'client');
 
-        return {
-            logs: logsWithClientInfo, oldest, older_than, filter, ...values,
-        };
+    return {
+        logs: logsWithClientInfo, oldest, older_than, filter, ...values,
     };
 };
 
-const getLogsWithParams = getLogsWithParamsWrapper();
-
 export const getAdditionalLogsRequest = createAction('GET_ADDITIONAL_LOGS_REQUEST');
 export const getAdditionalLogsFailure = createAction('GET_ADDITIONAL_LOGS_FAILURE');
 export const getAdditionalLogsSuccess = createAction('GET_ADDITIONAL_LOGS_SUCCESS');
diff --git a/client/src/actions/stats.js b/client/src/actions/stats.js
index b928b85a..25897aab 100644
--- a/client/src/actions/stats.js
+++ b/client/src/actions/stats.js
@@ -39,38 +39,30 @@ export const getStatsRequest = createAction('GET_STATS_REQUEST');
 export const getStatsFailure = createAction('GET_STATS_FAILURE');
 export const getStatsSuccess = createAction('GET_STATS_SUCCESS');
 
-// Cache clients in closure
-const getStatsWrapper = () => {
-    let clients = {};
-    return () => async (dispatch) => {
-        dispatch(getStatsRequest());
-        try {
-            const stats = await apiClient.getStats();
-            const normalizedTopClients = normalizeTopStats(stats.top_clients);
-            const clientsParams = getParamsForClientsSearch(normalizedTopClients, 'name');
-            if (!Object.values(clientsParams).every(client => client in clients)) {
-                clients = await apiClient.findClients(clientsParams);
-            }
-            const topClientsWithInfo = addClientInfo(normalizedTopClients, clients, 'name');
+export const getStats = () => async (dispatch) => {
+    dispatch(getStatsRequest());
+    try {
+        const stats = await apiClient.getStats();
+        const normalizedTopClients = normalizeTopStats(stats.top_clients);
+        const clientsParams = getParamsForClientsSearch(normalizedTopClients, 'name');
+        const clients = await apiClient.findClients(clientsParams);
+        const topClientsWithInfo = addClientInfo(normalizedTopClients, clients, 'name');
 
-            const normalizedStats = {
-                ...stats,
-                top_blocked_domains: normalizeTopStats(stats.top_blocked_domains),
-                top_clients: topClientsWithInfo,
-                top_queried_domains: normalizeTopStats(stats.top_queried_domains),
-                avg_processing_time: secondsToMilliseconds(stats.avg_processing_time),
-            };
+        const normalizedStats = {
+            ...stats,
+            top_blocked_domains: normalizeTopStats(stats.top_blocked_domains),
+            top_clients: topClientsWithInfo,
+            top_queried_domains: normalizeTopStats(stats.top_queried_domains),
+            avg_processing_time: secondsToMilliseconds(stats.avg_processing_time),
+        };
 
-            dispatch(getStatsSuccess(normalizedStats));
-        } catch (error) {
-            dispatch(addErrorToast({ error }));
-            dispatch(getStatsFailure());
-        }
-    };
+        dispatch(getStatsSuccess(normalizedStats));
+    } catch (error) {
+        dispatch(addErrorToast({ error }));
+        dispatch(getStatsFailure());
+    }
 };
 
-export const getStats = getStatsWrapper();
-
 export const resetStatsRequest = createAction('RESET_STATS_REQUEST');
 export const resetStatsFailure = createAction('RESET_STATS_FAILURE');
 export const resetStatsSuccess = createAction('RESET_STATS_SUCCESS');