2019-09-04 14:39:35 +00:00
|
|
|
import { createAction } from 'redux-actions';
|
|
|
|
|
2019-09-05 09:12:58 +00:00
|
|
|
import apiClient from '../api/Api';
|
2021-04-02 14:30:39 +00:00
|
|
|
import { normalizeLogs } from '../helpers/helpers';
|
2020-07-13 13:06:56 +00:00
|
|
|
import {
|
2020-09-01 13:30:30 +00:00
|
|
|
DEFAULT_LOGS_FILTER, FORM_NAME, QUERY_LOGS_PAGE_LIMIT,
|
2020-07-13 13:06:56 +00:00
|
|
|
} from '../helpers/constants';
|
2020-05-22 14:06:05 +00:00
|
|
|
import { addErrorToast, addSuccessToast } from './toasts';
|
2019-09-04 14:39:35 +00:00
|
|
|
|
2020-01-17 10:27:22 +00:00
|
|
|
const getLogsWithParams = async (config) => {
|
|
|
|
const { older_than, filter, ...values } = config;
|
2020-07-13 13:06:56 +00:00
|
|
|
const rawLogs = await apiClient.getQueryLog({
|
|
|
|
...filter,
|
|
|
|
older_than,
|
|
|
|
});
|
2020-01-17 10:27:22 +00:00
|
|
|
const { data, oldest } = rawLogs;
|
|
|
|
|
|
|
|
return {
|
2021-04-02 14:30:39 +00:00
|
|
|
logs: normalizeLogs(data),
|
2020-07-13 13:06:56 +00:00
|
|
|
oldest,
|
|
|
|
older_than,
|
|
|
|
filter,
|
|
|
|
...values,
|
2019-11-15 07:51:45 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-11-18 11:51:09 +00:00
|
|
|
export const getAdditionalLogsRequest = createAction('GET_ADDITIONAL_LOGS_REQUEST');
|
|
|
|
export const getAdditionalLogsFailure = createAction('GET_ADDITIONAL_LOGS_FAILURE');
|
|
|
|
export const getAdditionalLogsSuccess = createAction('GET_ADDITIONAL_LOGS_SUCCESS');
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
const shortPollQueryLogs = async (data, filter, dispatch, getState, total) => {
|
2019-11-18 11:51:09 +00:00
|
|
|
const { logs, oldest } = data;
|
|
|
|
const totalData = total || { logs };
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
const queryForm = getState().form[FORM_NAME.LOGS_FILTER];
|
|
|
|
const currentQuery = queryForm && queryForm.values.search;
|
|
|
|
const previousQuery = filter?.search;
|
|
|
|
const isQueryTheSame = typeof previousQuery === 'string'
|
|
|
|
&& typeof currentQuery === 'string'
|
|
|
|
&& previousQuery === currentQuery;
|
2019-11-18 11:51:09 +00:00
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
const isShortPollingNeeded = (logs.length < QUERY_LOGS_PAGE_LIMIT
|
|
|
|
|| totalData.logs.length < QUERY_LOGS_PAGE_LIMIT)
|
|
|
|
&& oldest !== '' && isQueryTheSame;
|
|
|
|
|
|
|
|
if (isShortPollingNeeded) {
|
2019-11-18 11:51:09 +00:00
|
|
|
dispatch(getAdditionalLogsRequest());
|
|
|
|
|
|
|
|
try {
|
2020-07-13 13:06:56 +00:00
|
|
|
const additionalLogs = await getLogsWithParams({
|
|
|
|
older_than: oldest,
|
|
|
|
filter,
|
|
|
|
});
|
2020-01-13 14:06:00 +00:00
|
|
|
if (additionalLogs.oldest.length > 0) {
|
2020-09-01 13:30:30 +00:00
|
|
|
return await shortPollQueryLogs(additionalLogs, filter, dispatch, getState, {
|
2019-11-18 11:51:09 +00:00
|
|
|
logs: [...totalData.logs, ...additionalLogs.logs],
|
|
|
|
oldest: additionalLogs.oldest,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dispatch(getAdditionalLogsSuccess());
|
|
|
|
return totalData;
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(getAdditionalLogsFailure(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(getAdditionalLogsSuccess());
|
|
|
|
return totalData;
|
|
|
|
};
|
|
|
|
|
2020-06-17 21:36:19 +00:00
|
|
|
export const toggleDetailedLogs = createAction('TOGGLE_DETAILED_LOGS');
|
2019-09-20 12:05:10 +00:00
|
|
|
|
2019-09-04 14:39:35 +00:00
|
|
|
export const getLogsRequest = createAction('GET_LOGS_REQUEST');
|
|
|
|
export const getLogsFailure = createAction('GET_LOGS_FAILURE');
|
|
|
|
export const getLogsSuccess = createAction('GET_LOGS_SUCCESS');
|
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
export const updateLogs = () => async (dispatch, getState) => {
|
|
|
|
try {
|
|
|
|
const { logs, oldest, older_than } = getState().queryLogs;
|
|
|
|
|
|
|
|
dispatch(getLogsSuccess({
|
2021-04-02 14:30:39 +00:00
|
|
|
logs,
|
2020-09-24 12:48:37 +00:00
|
|
|
oldest,
|
|
|
|
older_than,
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(getLogsFailure(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
export const getLogs = () => async (dispatch, getState) => {
|
2019-09-04 14:39:35 +00:00
|
|
|
dispatch(getLogsRequest());
|
|
|
|
try {
|
2020-09-01 13:30:30 +00:00
|
|
|
const { isFiltered, filter, oldest } = getState().queryLogs;
|
2020-07-13 13:06:56 +00:00
|
|
|
const data = await getLogsWithParams({
|
2020-09-01 13:30:30 +00:00
|
|
|
older_than: oldest,
|
2020-07-13 13:06:56 +00:00
|
|
|
filter,
|
|
|
|
});
|
2019-11-18 11:51:09 +00:00
|
|
|
|
|
|
|
if (isFiltered) {
|
2020-09-01 13:30:30 +00:00
|
|
|
const additionalData = await shortPollQueryLogs(data, filter, dispatch, getState);
|
2019-11-18 11:51:09 +00:00
|
|
|
const updatedData = additionalData.logs ? { ...data, ...additionalData } : data;
|
|
|
|
dispatch(getLogsSuccess(updatedData));
|
|
|
|
} else {
|
|
|
|
dispatch(getLogsSuccess(data));
|
|
|
|
}
|
2019-09-04 14:39:35 +00:00
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(getLogsFailure(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-15 07:51:45 +00:00
|
|
|
export const setLogsFilterRequest = createAction('SET_LOGS_FILTER_REQUEST');
|
|
|
|
|
2020-07-13 13:06:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filter
|
|
|
|
* @param {string} filter.search
|
2020-09-01 13:30:30 +00:00
|
|
|
* @param {string} filter.response_status 'QUERY' field of RESPONSE_FILTER object
|
2020-07-13 13:06:56 +00:00
|
|
|
* @returns function
|
|
|
|
*/
|
|
|
|
export const setLogsFilter = (filter) => setLogsFilterRequest(filter);
|
|
|
|
|
|
|
|
export const setFilteredLogsRequest = createAction('SET_FILTERED_LOGS_REQUEST');
|
|
|
|
export const setFilteredLogsFailure = createAction('SET_FILTERED_LOGS_FAILURE');
|
|
|
|
export const setFilteredLogsSuccess = createAction('SET_FILTERED_LOGS_SUCCESS');
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
export const setFilteredLogs = (filter) => async (dispatch, getState) => {
|
2020-07-13 13:06:56 +00:00
|
|
|
dispatch(setFilteredLogsRequest());
|
2019-11-15 07:51:45 +00:00
|
|
|
try {
|
2020-07-13 13:06:56 +00:00
|
|
|
const data = await getLogsWithParams({
|
|
|
|
older_than: '',
|
|
|
|
filter,
|
|
|
|
});
|
2020-09-01 13:30:30 +00:00
|
|
|
const additionalData = await shortPollQueryLogs(data, filter, dispatch, getState);
|
2019-11-18 11:51:09 +00:00
|
|
|
const updatedData = additionalData.logs ? { ...data, ...additionalData } : data;
|
|
|
|
|
2020-07-13 13:06:56 +00:00
|
|
|
dispatch(setFilteredLogsSuccess({
|
|
|
|
...updatedData,
|
|
|
|
filter,
|
|
|
|
}));
|
2019-11-15 07:51:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
2020-07-13 13:06:56 +00:00
|
|
|
dispatch(setFilteredLogsFailure(error));
|
2019-11-15 07:51:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-13 13:06:56 +00:00
|
|
|
export const resetFilteredLogs = () => setFilteredLogs(DEFAULT_LOGS_FILTER);
|
|
|
|
|
|
|
|
export const refreshFilteredLogs = () => async (dispatch, getState) => {
|
|
|
|
const { filter } = getState().queryLogs;
|
|
|
|
await dispatch(setFilteredLogs(filter));
|
|
|
|
};
|
|
|
|
|
2019-09-04 14:39:35 +00:00
|
|
|
export const clearLogsRequest = createAction('CLEAR_LOGS_REQUEST');
|
|
|
|
export const clearLogsFailure = createAction('CLEAR_LOGS_FAILURE');
|
|
|
|
export const clearLogsSuccess = createAction('CLEAR_LOGS_SUCCESS');
|
|
|
|
|
|
|
|
export const clearLogs = () => async (dispatch) => {
|
|
|
|
dispatch(clearLogsRequest());
|
|
|
|
try {
|
|
|
|
await apiClient.clearQueryLog();
|
|
|
|
dispatch(clearLogsSuccess());
|
|
|
|
dispatch(addSuccessToast('query_log_cleared'));
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(clearLogsFailure(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getLogsConfigRequest = createAction('GET_LOGS_CONFIG_REQUEST');
|
|
|
|
export const getLogsConfigFailure = createAction('GET_LOGS_CONFIG_FAILURE');
|
|
|
|
export const getLogsConfigSuccess = createAction('GET_LOGS_CONFIG_SUCCESS');
|
|
|
|
|
|
|
|
export const getLogsConfig = () => async (dispatch) => {
|
|
|
|
dispatch(getLogsConfigRequest());
|
|
|
|
try {
|
|
|
|
const data = await apiClient.getQueryLogInfo();
|
|
|
|
dispatch(getLogsConfigSuccess(data));
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(getLogsConfigFailure());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setLogsConfigRequest = createAction('SET_LOGS_CONFIG_REQUEST');
|
|
|
|
export const setLogsConfigFailure = createAction('SET_LOGS_CONFIG_FAILURE');
|
|
|
|
export const setLogsConfigSuccess = createAction('SET_LOGS_CONFIG_SUCCESS');
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const setLogsConfig = (config) => async (dispatch) => {
|
2019-09-04 14:39:35 +00:00
|
|
|
dispatch(setLogsConfigRequest());
|
|
|
|
try {
|
|
|
|
await apiClient.setQueryLogConfig(config);
|
|
|
|
dispatch(addSuccessToast('config_successfully_saved'));
|
|
|
|
dispatch(setLogsConfigSuccess(config));
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(setLogsConfigFailure());
|
|
|
|
}
|
|
|
|
};
|