Files
archived-chatwoot/app/javascript/dashboard/store/modules/conversationStats.js
Shivam Mishra 3fd585f40b feat: Throttle meta request for large chat size (#10518)
For large accounts with huge volumes of messages, it can be very
wasteful to make the meta request so often. It also puts un-necessary
load on the DB bombarding it with so many requests. This PR fixes it by
throttling the requests to 5 seconds for all users with more than 1000
accessible chats.

### Why not cache this value in the backend?

Well, it's a bit tricky, since a user can have different permissions
over inboxes and can see different chats, maintaining a cache for each
of them is not effective, besides the requests will reach the server
anyway.
2024-12-05 22:35:30 -08:00

65 lines
1.4 KiB
JavaScript

import types from '../mutation-types';
import ConversationApi from '../../api/inbox/conversation';
const state = {
mineCount: 0,
unAssignedCount: 0,
allCount: 0,
updatedOn: null,
};
export const getters = {
getStats: $state => $state,
};
export const actions = {
get: async ({ commit, state: $state }, params) => {
const currentTime = new Date();
const lastUpdatedTime = new Date($state.updatedOn);
// Skip large accounts from making too many requests
if (currentTime - lastUpdatedTime < 10000 && $state.allCount > 1000) {
// eslint-disable-next-line no-console
console.warn('Skipping conversation meta fetch');
return;
}
try {
const response = await ConversationApi.meta(params);
const {
data: { meta },
} = response;
commit(types.SET_CONV_TAB_META, meta);
} catch (error) {
// Ignore error
}
},
set({ commit }, meta) {
commit(types.SET_CONV_TAB_META, meta);
},
};
export const mutations = {
[types.SET_CONV_TAB_META](
$state,
{
mine_count: mineCount,
unassigned_count: unAssignedCount,
all_count: allCount,
} = {}
) {
$state.mineCount = mineCount;
$state.allCount = allCount;
$state.unAssignedCount = unAssignedCount;
$state.updatedOn = new Date();
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};