mattermost-mobile/app/actions/remote/role.ts
Elias Nahum d1d3fda83a
Request metrics improvements (#8420)
* Network metrics

* update network-client ref

* fix unit tests

* missing catch error parsing

* Replace API's to fetch all teams and channels for a user
Update groupLabel to use a type definition
Include effective Latency and Average Speed in the metrics
Include parallel and sequential request counts

* update network library and fix tests

* feat(MM-61865): send network request telemetry data to the server (#8436)

* feat(MM-61865): send network info telemetry data

* unit testing

* fix latency vs effectiveLatency

* cleanup test

* fix failing tests

* fix spelling error

* fix failing test

* more cleanup

* fix: improve parallel requests calculation (#8439)

* fix: improve parallel requests calculation

* fix test issue

* multiple parallelGroups testing

* calculateAverageSpeedWithCategories test

* categorizedRequests in it's own describe

* clean up duplicate tests

* check for when groupLabel is non-existent

* a case when groupLabel is non-existent

* more testing with effective latency.

* resolveAndFlattenModels with a capital F

* add try..catch within resolveAndFlattenModels

* update groupLabel to fix failing lint

* fix linting issue due to unknown group label

* forgot to push changes

* fix the indentation problem again

* add env var option for COLLECT_NETWORK_METRICS

---------

Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
2025-01-16 13:55:29 -07:00

100 lines
3.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {General} from '@constants';
import DatabaseManager from '@database/manager';
import NetworkManager from '@managers/network_manager';
import {queryRoles} from '@queries/servers/role';
import {getFullErrorMessage} from '@utils/errors';
import {logDebug} from '@utils/log';
import {forceLogoutIfNecessary} from './session';
export type RolesRequest = {
error?: unknown;
roles?: Role[];
}
export const fetchRolesIfNeeded = async (
serverUrl: string, updatedRoles: string[],
fetchOnly = false, force = false, groupLabel?: RequestGroupLabel,
): Promise<RolesRequest> => {
if (!updatedRoles.length) {
return {roles: []};
}
try {
const client = NetworkManager.getClient(serverUrl);
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
let newRoles;
if (force) {
newRoles = updatedRoles;
} else {
const existingRoles = await queryRoles(database).fetch();
const roleNames = new Set(existingRoles.map((role) => {
return role.name;
}));
newRoles = updatedRoles.filter((newRole) => {
return !roleNames.has(newRole);
});
}
if (!newRoles.length) {
return {roles: []};
}
const getRolesRequests = [];
for (let i = 0; i < newRoles.length; i += General.MAX_GET_ROLES_BY_NAMES) {
const chunk = newRoles.slice(i, i + General.MAX_GET_ROLES_BY_NAMES);
getRolesRequests.push(client.getRolesByNames(chunk, groupLabel));
}
const roles = (await Promise.all(getRolesRequests)).flat();
if (!fetchOnly) {
await operator.handleRole({
roles,
prepareRecordsOnly: false,
});
}
return {roles};
} catch (error) {
logDebug('error on fetchRolesIfNeeded', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
};
export const fetchRoles = async (
serverUrl: string, teamMembership?: TeamMembership[], channelMembership?: ChannelMembership[],
user?: UserProfile, fetchOnly = false, force = false, groupLabel?: RequestGroupLabel,
) => {
const rolesToFetch = new Set<string>(user?.roles.split(' ') || []);
if (teamMembership?.length) {
const teamRoles: string[] = [];
teamMembership?.forEach((tm) => {
teamRoles.push(...tm.roles.split(' '));
});
teamRoles.forEach(rolesToFetch.add, rolesToFetch);
}
if (channelMembership?.length) {
for (let i = 0; i < channelMembership!.length; i++) {
const member = channelMembership[i];
member.roles.split(' ').forEach(rolesToFetch.add, rolesToFetch);
}
}
rolesToFetch.delete('');
if (rolesToFetch.size > 0) {
return fetchRolesIfNeeded(serverUrl, Array.from(rolesToFetch), fetchOnly, force, groupLabel);
}
return {roles: []};
};