mattermost-mobile/app/client/rest/general.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

124 lines
4.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {buildQueryString} from '@utils/helpers';
import {PER_PAGE_DEFAULT} from './constants';
import ClientError from './error';
import type ClientBase from './base';
type PoliciesResponse<T> = {
policies: T[];
total_count: number;
}
export interface ClientGeneralMix {
ping: (deviceId?: string, timeoutInterval?: number, groupLabel?: RequestGroupLabel) => Promise<any>;
logClientError: (message: string, level?: string) => Promise<any>;
getClientConfigOld: (groupLabel?: RequestGroupLabel) => Promise<ClientConfig>;
getClientLicenseOld: (groupLabel?: RequestGroupLabel) => Promise<ClientLicense>;
getTimezones: () => Promise<string[]>;
getGlobalDataRetentionPolicy: (groupLabel?: RequestGroupLabel) => Promise<GlobalDataRetentionPolicy>;
getTeamDataRetentionPolicies: (userId: string, page?: number, perPage?: number, groupLabel?: RequestGroupLabel) => Promise<PoliciesResponse<TeamDataRetentionPolicy>>;
getChannelDataRetentionPolicies: (userId: string, page?: number, perPage?: number, groupLabel?: RequestGroupLabel) => Promise<PoliciesResponse<ChannelDataRetentionPolicy>>;
getRolesByNames: (rolesNames: string[], groupLabel?: RequestGroupLabel) => Promise<Role[]>;
getRedirectLocation: (urlParam: string) => Promise<Record<string, string>>;
sendPerformanceReport: (batch: PerformanceReport) => Promise<{}>;
}
const ClientGeneral = <TBase extends Constructor<ClientBase>>(superclass: TBase) => class extends superclass {
ping = async (deviceId?: string, timeoutInterval?: number, groupLabel?: RequestGroupLabel) => {
let url = `${this.urlVersion}/system/ping?time=${Date.now()}`;
if (deviceId) {
url = `${url}&device_id=${deviceId}`;
}
return this.doFetch(
url,
{method: 'get', timeoutInterval, groupLabel},
false,
);
};
logClientError = async (message: string, level = 'ERROR') => {
const url = `${this.urlVersion}/logs`;
if (!this.enableLogging) {
throw new ClientError(this.apiClient.baseUrl, {
message: 'Logging disabled.',
url,
});
}
return this.doFetch(
url,
{method: 'post', body: {message, level}},
);
};
getClientConfigOld = async (groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.urlVersion}/config/client?format=old`,
{method: 'get', groupLabel},
);
};
getClientLicenseOld = async (groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.urlVersion}/license/client?format=old`,
{method: 'get', groupLabel},
);
};
getTimezones = async () => {
return this.doFetch(
`${this.getTimezonesRoute()}`,
{method: 'get'},
);
};
getGlobalDataRetentionPolicy = (groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.getGlobalDataRetentionRoute()}/policy`,
{method: 'get', groupLabel},
);
};
getTeamDataRetentionPolicies = (userId: string, page = 0, perPage = PER_PAGE_DEFAULT, groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.getGranularDataRetentionRoute(userId)}/team_policies${buildQueryString({page, per_page: perPage})}`,
{method: 'get', groupLabel},
);
};
getChannelDataRetentionPolicies = (userId: string, page = 0, perPage = PER_PAGE_DEFAULT, groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.getGranularDataRetentionRoute(userId)}/channel_policies${buildQueryString({page, per_page: perPage})}`,
{method: 'get', groupLabel},
);
};
getRolesByNames = async (rolesNames: string[], groupLabel?: RequestGroupLabel) => {
return this.doFetch(
`${this.getRolesRoute()}/names`,
{method: 'post', body: rolesNames, groupLabel},
);
};
getRedirectLocation = async (urlParam: string) => {
if (!urlParam.length) {
return Promise.resolve();
}
const url = `${this.getRedirectLocationRoute()}${buildQueryString({url: urlParam})}`;
return this.doFetch(url, {method: 'get'});
};
sendPerformanceReport = async (report: PerformanceReport) => {
return this.doFetch(
this.getPerformanceRoute(),
{method: 'post', body: report},
);
};
};
export default ClientGeneral;