Merge: + client: get rid of the clients caching

Close #1218

* commit '7812ee1861be0eea89bd419ee82f6cb45b24f043':
  + client: get rid of clients caching
This commit is contained in:
Artem Baskal 2020-01-17 18:57:30 +03:00
commit 1947f1232e
2 changed files with 30 additions and 46 deletions

View File

@ -5,28 +5,20 @@ import { addErrorToast, addSuccessToast } from './index';
import { normalizeLogs, getParamsForClientsSearch, addClientInfo } from '../helpers/helpers'; import { normalizeLogs, getParamsForClientsSearch, addClientInfo } from '../helpers/helpers';
import { TABLE_DEFAULT_PAGE_SIZE } from '../helpers/constants'; import { TABLE_DEFAULT_PAGE_SIZE } from '../helpers/constants';
// Cache clients in closure const getLogsWithParams = async (config) => {
const getLogsWithParamsWrapper = () => { const { older_than, filter, ...values } = config;
let clients = {}; const rawLogs = await apiClient.getQueryLog({ ...filter, older_than });
return async (config) => { const { data, oldest } = rawLogs;
const { older_than, filter, ...values } = config; const logs = normalizeLogs(data);
const rawLogs = await apiClient.getQueryLog({ ...filter, older_than }); const clientsParams = getParamsForClientsSearch(logs, 'client');
const { data, oldest } = rawLogs; const clients = await apiClient.findClients(clientsParams);
const logs = normalizeLogs(data); const logsWithClientInfo = addClientInfo(logs, clients, 'client');
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');
return { return {
logs: logsWithClientInfo, oldest, older_than, filter, ...values, logs: logsWithClientInfo, oldest, older_than, filter, ...values,
};
}; };
}; };
const getLogsWithParams = getLogsWithParamsWrapper();
export const getAdditionalLogsRequest = createAction('GET_ADDITIONAL_LOGS_REQUEST'); export const getAdditionalLogsRequest = createAction('GET_ADDITIONAL_LOGS_REQUEST');
export const getAdditionalLogsFailure = createAction('GET_ADDITIONAL_LOGS_FAILURE'); export const getAdditionalLogsFailure = createAction('GET_ADDITIONAL_LOGS_FAILURE');
export const getAdditionalLogsSuccess = createAction('GET_ADDITIONAL_LOGS_SUCCESS'); export const getAdditionalLogsSuccess = createAction('GET_ADDITIONAL_LOGS_SUCCESS');

View File

@ -39,38 +39,30 @@ export const getStatsRequest = createAction('GET_STATS_REQUEST');
export const getStatsFailure = createAction('GET_STATS_FAILURE'); export const getStatsFailure = createAction('GET_STATS_FAILURE');
export const getStatsSuccess = createAction('GET_STATS_SUCCESS'); export const getStatsSuccess = createAction('GET_STATS_SUCCESS');
// Cache clients in closure export const getStats = () => async (dispatch) => {
const getStatsWrapper = () => { dispatch(getStatsRequest());
let clients = {}; try {
return () => async (dispatch) => { const stats = await apiClient.getStats();
dispatch(getStatsRequest()); const normalizedTopClients = normalizeTopStats(stats.top_clients);
try { const clientsParams = getParamsForClientsSearch(normalizedTopClients, 'name');
const stats = await apiClient.getStats(); const clients = await apiClient.findClients(clientsParams);
const normalizedTopClients = normalizeTopStats(stats.top_clients); const topClientsWithInfo = addClientInfo(normalizedTopClients, clients, 'name');
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');
const normalizedStats = { const normalizedStats = {
...stats, ...stats,
top_blocked_domains: normalizeTopStats(stats.top_blocked_domains), top_blocked_domains: normalizeTopStats(stats.top_blocked_domains),
top_clients: topClientsWithInfo, top_clients: topClientsWithInfo,
top_queried_domains: normalizeTopStats(stats.top_queried_domains), top_queried_domains: normalizeTopStats(stats.top_queried_domains),
avg_processing_time: secondsToMilliseconds(stats.avg_processing_time), avg_processing_time: secondsToMilliseconds(stats.avg_processing_time),
}; };
dispatch(getStatsSuccess(normalizedStats)); dispatch(getStatsSuccess(normalizedStats));
} catch (error) { } catch (error) {
dispatch(addErrorToast({ error })); dispatch(addErrorToast({ error }));
dispatch(getStatsFailure()); dispatch(getStatsFailure());
} }
};
}; };
export const getStats = getStatsWrapper();
export const resetStatsRequest = createAction('RESET_STATS_REQUEST'); export const resetStatsRequest = createAction('RESET_STATS_REQUEST');
export const resetStatsFailure = createAction('RESET_STATS_FAILURE'); export const resetStatsFailure = createAction('RESET_STATS_FAILURE');
export const resetStatsSuccess = createAction('RESET_STATS_SUCCESS'); export const resetStatsSuccess = createAction('RESET_STATS_SUCCESS');