Skip to content

Commit

Permalink
Fix/notification account limit bugs OK-33938 (#6223)
Browse files Browse the repository at this point in the history
* fix: getAllNetworks perf log

* fix: notification account limit
  • Loading branch information
sidmorizon authored Nov 19, 2024
1 parent e872cbc commit 1569058
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export class SimpleDbEntityNotificationSettings extends SimpleDbEntityBase<ISimp
const settings = await this.getRawData();
let count = 0;
Object.values(settings?.accountActivity || {}).forEach((wallet) => {
if (wallet.enabled) {
Object.values(wallet.accounts || {}).forEach((account) => {
if (account.enabled) {
if (wallet?.enabled) {
Object.values(wallet?.accounts || {}).forEach((account) => {
if (account?.enabled) {
count += 1;
}
});
Expand Down
37 changes: 36 additions & 1 deletion packages/kit-bg/src/services/ServiceNetwork/ServiceNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { appLocale } from '@onekeyhq/shared/src/locale/appLocale';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';
import networkUtils from '@onekeyhq/shared/src/utils/networkUtils';
import perfUtils, {
EPerformanceTimerLogNames,
} from '@onekeyhq/shared/src/utils/perfUtils';
import type { IServerNetwork } from '@onekeyhq/shared/types';

import { vaultFactory } from '../../vaults/factory';
Expand Down Expand Up @@ -57,21 +60,28 @@ class ServiceNetwork extends ServiceBase {
uniqByImpl?: boolean;
} = {},
): Promise<{ networks: IServerNetwork[] }> {
const perf = perfUtils.createPerf(
EPerformanceTimerLogNames.serviceNetwork__getAllNetworks,
);

perf.markStart('getPresetNetworks');
// TODO save to simpleDB
const excludeTestNetwork = params?.excludeTestNetwork ?? false;
const uniqByImpl = params?.uniqByImpl ?? false;
const excludeNetworkIds = params?.excludeNetworkIds ?? [];
if (params.excludeAllNetworkItem) {
excludeNetworkIds.push(getNetworkIdsMap().onekeyall);
}

const presetNetworks = getPresetNetworks();
perf.markEnd('getPresetNetworks');

perf.markStart('getServerNetworks-and-getAllCustomNetworks');
// Fetch server and custom networks
const [serverNetworks, customNetworks] = await Promise.all([
this.backgroundApi.serviceCustomRpc.getServerNetworks(),
this.backgroundApi.serviceCustomRpc.getAllCustomNetworks(),
]);
perf.markEnd('getServerNetworks-and-getAllCustomNetworks');

// Create a Map to store unique networks by id
// Priority: serverNetworks > presetNetworks > customNetworks
Expand All @@ -86,25 +96,50 @@ class ServiceNetwork extends ServiceBase {
});
};

perf.markStart('addNetworks-presetNetworks');
// Add networks in order of priority
addNetworks(presetNetworks);
perf.markEnd('addNetworks-presetNetworks');

perf.markStart('addNetworks-serverNetworks');
addNetworks(serverNetworks);
perf.markEnd('addNetworks-serverNetworks');

perf.markStart('addNetworks-customNetworks');
addNetworks(customNetworks);
perf.markEnd('addNetworks-customNetworks');

perf.markStart('convertMapToArray');
// Convert Map back to array
let networks = Array.from(networkMap.values());
perf.markEnd('convertMapToArray');

perf.markStart('filterNetworks-excludeCustomNetwork');
if (params.excludeCustomNetwork) {
excludeNetworkIds.push(...customNetworks.map((n) => n.id));
}
perf.markEnd('filterNetworks-excludeCustomNetwork');

perf.markStart('filterNetworks-uniqByImpl');
if (uniqByImpl) {
networks = uniqBy(networks, (n) => n.impl);
}
perf.markEnd('filterNetworks-uniqByImpl');

perf.markStart('filterNetworks-excludeTestNetwork');
if (excludeTestNetwork) {
networks = networks.filter((n) => !n.isTestnet);
}
perf.markEnd('filterNetworks-excludeTestNetwork');

perf.markStart('filterNetworks-excludeNetworkIds');
if (excludeNetworkIds?.length) {
networks = networks.filter((n) => !excludeNetworkIds.includes(n.id));
}
perf.markEnd('filterNetworks-excludeNetworkIds');

perf.done();

return Promise.resolve({ networks });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,10 @@ export default class ServiceNotification extends ServiceBase {
const { wallets } = await this.getNotificationWalletsAndAccounts();
const oldAccountActivity = cloneDeep(settings?.accountActivity ?? {});
const accountActivity: IAccountActivityNotificationSettings = {};

const currentEnabledAccountCount =
await this.backgroundApi.simpleDb.notificationSettings.getEnabledAccountCount();

let totalEnabledCount = 0;
const isInit = !settings?.accountActivity;
const updateWalletAccountActivity = (wallet: IDBWallet) => {
Expand Down Expand Up @@ -674,14 +678,22 @@ export default class ServiceNotification extends ServiceBase {
const isWalletEnabled =
oldAccountActivity?.[wallet.id]?.enabled === true ||
oldAccountActivity?.[wallet.id]?.enabled === undefined;
const isAccountEnabled =
const isAccountEnabledUndefined =
oldAccountActivity?.[wallet.id]?.accounts?.[account.id]?.enabled ===
true ||
undefined;
const isAccountEnabled =
oldAccountActivity?.[wallet.id]?.accounts?.[account.id]?.enabled ===
undefined;
true || isAccountEnabledUndefined;

if (isWalletEnabled && isAccountEnabled) {
enableAccount(account);
if (
isAccountEnabledUndefined &&
currentEnabledAccountCount >= maxAccountCount
) {
disableAccount(account);
} else {
enableAccount(account);
}
} else {
disableAccount(account);
}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/utils/perfUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum EPerformanceTimerLogNames {
allNetwork__getAccountLocalTokens = 'allNetwork__getAccountLocalTokens',
allNetwork__useAllNetworkRequests = 'allNetwork__useAllNetworkRequests',
allNetwork__handleAllNetworkCacheRequests = 'allNetwork__handleAllNetworkCacheRequests',
serviceNetwork__getAllNetworks = 'serviceNetwork__getAllNetworks',
}

function getPerformanceTimerLogConfigMap() {
Expand Down

0 comments on commit 1569058

Please sign in to comment.