Skip to content

Commit

Permalink
[backend/frontend] improve group and users display performance (#7533)
Browse files Browse the repository at this point in the history
  • Loading branch information
SouadHadjiat authored Jul 29, 2024
1 parent daf3d41 commit 493e560
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,12 @@ const groupFragment = graphql`
no_creators
group_confidence_level {
max_confidence
overrides{
overrides {
max_confidence
entity_type
}
}
description
members {
edges {
node {
...UserLine_node
}
}
}
group_confidence_level {
max_confidence
}
default_dashboard {
id
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const subscription = graphql`
subscription RootGroupsSubscription($id: ID!) {
group(id: $id) {
...Group_group
...GroupEditionContainer_group
}
}
`;
Expand All @@ -36,11 +35,6 @@ const groupQuery = graphql`
rolesOrderBy: $rolesOrderBy
rolesOrderMode: $rolesOrderMode
)
...GroupEditionContainer_group
@arguments(
rolesOrderBy: $rolesOrderBy
rolesOrderMode: $rolesOrderMode
)
}
}
`;
Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ export const groupEditDefaultMarking = async (context, user, groupId, defaultMar

export const groupCleanContext = async (context, user, groupId) => {
await delEditContext(user, groupId);
return storeLoadById(context, user, groupId, ENTITY_TYPE_GROUP).then((group) => notify(BUS_TOPICS.Group.EDIT_TOPIC, group, user));
return storeLoadById(context, user, groupId, ENTITY_TYPE_GROUP); // notify removed for performance issues with users cache
};

export const groupEditContext = async (context, user, groupId, input) => {
await setEditContext(user, groupId, input);
return storeLoadById(context, user, groupId, ENTITY_TYPE_GROUP).then((group) => notify(BUS_TOPICS.Group.EDIT_TOPIC, group, user));
return storeLoadById(context, user, groupId, ENTITY_TYPE_GROUP); // notify removed for performance issues with users cache
};
28 changes: 22 additions & 6 deletions opencti-platform/opencti-graphql/src/domain/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1555,21 +1555,37 @@ export const userEditContext = async (context, user, userId, input) => {
};
// endregion

export const getUserEffectiveConfidenceLevel = async (user, context) => {
// we load the user from cache to have the complete user with groupos
const platformUsers = await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_USER);
const cachedUser = platformUsers.get(user.id);
const buildCompleteUserFromCacheOrDb = async (context, user, userToLoad, cachedUsers) => {
const cachedUser = cachedUsers.get(userToLoad.id);
let completeUser;
if (cachedUser) {
// in case we need to resolve user effective confidence level on edit (cache not updated with user edited fields yet)
// we need groups and capabilities to compute user effective confidence level, which are accurate in cache.
completeUser = {
...user,
...userToLoad,
groups: cachedUser.groups,
capabilities: cachedUser.capabilities,
};
} else { // in case we need to resolve user effective confidence level on creation.
completeUser = await findById(context, context.user, user.id);
completeUser = await findById(context, user, userToLoad.id);
}
return completeUser;
};

export const batchUserEffectiveConfidenceLevel = async (context, user, batchUsers) => {
const platformUsers = await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_USER);
const completeUsers = [];
for (let i = 0; i < batchUsers.length; i += 1) {
const batchUser = batchUsers[i];
const completeUser = await buildCompleteUserFromCacheOrDb(context, user, batchUser, platformUsers);
completeUsers.push(completeUser);
}
return completeUsers.map((u) => computeUserEffectiveConfidenceLevel(u));
};

export const getUserEffectiveConfidenceLevel = async (user, context) => {
// we load the user from cache to have the complete user with groupos
const platformUsers = await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_USER);
const completeUser = await buildCompleteUserFromCacheOrDb(context, context.user, user, platformUsers);
return computeUserEffectiveConfidenceLevel(completeUser);
};
4 changes: 3 additions & 1 deletion opencti-platform/opencti-graphql/src/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
authenticateUser,
batchCreator,
batchRolesForUsers,
batchUserEffectiveConfidenceLevel,
bookmarks,
buildCompleteUser,
deleteBookmark,
Expand Down Expand Up @@ -61,6 +62,7 @@ import { executionContext, REDACTED_USER } from '../utils/access';
import { getNotifiers } from '../modules/notifier/notifier-domain';

const rolesUsersLoader = batchLoader(batchRolesForUsers);
const usersConfidenceLoader = batchLoader(batchUserEffectiveConfidenceLevel);
const creatorLoader = batchLoader(batchCreator);

const userResolvers = {
Expand All @@ -85,7 +87,7 @@ const userResolvers = {
objectOrganization: (current, args, context) => userOrganizationsPaginated(context, context.user, current.id, args),
editContext: (current) => fetchEditContext(current.id),
sessions: (current) => findUserSessions(current.id),
effective_confidence_level: (current, args, context) => getUserEffectiveConfidenceLevel(current, context),
effective_confidence_level: (current, args, context) => usersConfidenceLoader.load(current, context, context.user),
personal_notifiers: (current, _, context) => getNotifiers(context, context.user, current.personal_notifiers),
},
Member: {
Expand Down

0 comments on commit 493e560

Please sign in to comment.