From c901708281d206a22361a8f43015468cb420d0b6 Mon Sep 17 00:00:00 2001 From: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com> Date: Thu, 22 Aug 2024 11:12:25 -0700 Subject: [PATCH] Optimize updating call unread count --- ts/sql/Server.ts | 2 +- .../1160-optimize-calls-unread-count.ts | 36 +++++++++++++++++++ ts/sql/migrations/index.ts | 6 ++-- ts/state/ducks/callHistory.ts | 9 +++-- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 ts/sql/migrations/1160-optimize-calls-unread-count.ts diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 506a63c7f..2043226e1 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -3624,7 +3624,7 @@ const FOUR_HOURS_IN_MS = sqlConstant(4 * 60 * 60 * 1000); function getCallHistoryUnreadCount(db: ReadableDB): number { const [query, params] = sql` SELECT count(*) FROM messages - LEFT JOIN callsHistory ON callsHistory.callId = messages.callId + INNER JOIN callsHistory ON callsHistory.callId = messages.callId WHERE messages.type IS 'call-history' AND messages.seenStatus IS ${SEEN_STATUS_UNSEEN} AND callsHistory.status IS ${CALL_STATUS_MISSED} diff --git a/ts/sql/migrations/1160-optimize-calls-unread-count.ts b/ts/sql/migrations/1160-optimize-calls-unread-count.ts new file mode 100644 index 000000000..4de5947ee --- /dev/null +++ b/ts/sql/migrations/1160-optimize-calls-unread-count.ts @@ -0,0 +1,36 @@ +// Copyright 2024 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only +import type { Database } from '@signalapp/better-sqlite3'; +import type { LoggerType } from '../../types/Logging'; +import { sql, sqlConstant } from '../util'; +import { CallDirection, CallStatusValue } from '../../types/CallDisposition'; + +export const version = 1160; + +const CALL_STATUS_MISSED = sqlConstant(CallStatusValue.Missed); +const CALL_DIRECTION_INCOMING = sqlConstant(CallDirection.Incoming); + +export function updateToSchemaVersion1160( + currentVersion: number, + db: Database, + logger: LoggerType +): void { + if (currentVersion >= 1160) { + return; + } + + db.transaction(() => { + const [query] = sql` + DROP INDEX IF EXISTS callsHistory_incoming_missed; + + CREATE INDEX callsHistory_incoming_missed + ON callsHistory (callId, status, direction) + WHERE status IS ${CALL_STATUS_MISSED} + AND direction IS ${CALL_DIRECTION_INCOMING}; + `; + db.exec(query); + + db.pragma('user_version = 1160'); + })(); + logger.info('updateToSchemaVersion1160: success!'); +} diff --git a/ts/sql/migrations/index.ts b/ts/sql/migrations/index.ts index db2f1cbde..c9323e282 100644 --- a/ts/sql/migrations/index.ts +++ b/ts/sql/migrations/index.ts @@ -91,10 +91,11 @@ import { updateToSchemaVersion1110 } from './1110-sticker-local-key'; import { updateToSchemaVersion1120 } from './1120-messages-foreign-keys-indexes'; import { updateToSchemaVersion1130 } from './1130-isStory-index'; import { updateToSchemaVersion1140 } from './1140-call-links-deleted-column'; +import { updateToSchemaVersion1150 } from './1150-expire-timer-version'; import { - updateToSchemaVersion1150, + updateToSchemaVersion1160, version as MAX_VERSION, -} from './1150-expire-timer-version'; +} from './1160-optimize-calls-unread-count'; function updateToSchemaVersion1( currentVersion: number, @@ -2054,6 +2055,7 @@ export const SCHEMA_VERSIONS = [ updateToSchemaVersion1130, updateToSchemaVersion1140, updateToSchemaVersion1150, + updateToSchemaVersion1160, ]; export class DBVersionFromFutureError extends Error { diff --git a/ts/state/ducks/callHistory.ts b/ts/state/ducks/callHistory.ts index 2f082c260..2cae2adbb 100644 --- a/ts/state/ducks/callHistory.ts +++ b/ts/state/ducks/callHistory.ts @@ -3,7 +3,7 @@ import type { ReadonlyDeep } from 'type-fest'; import type { ThunkAction } from 'redux-thunk'; -import { omit } from 'lodash'; +import { debounce, omit } from 'lodash'; import type { StateType as RootStateType } from '../reducer'; import { clearCallHistoryDataAndSync, @@ -86,7 +86,7 @@ export function getEmptyState(): CallHistoryState { }; } -function updateCallHistoryUnreadCount(): ThunkAction< +function updateCallHistoryUnreadCountInner(): ThunkAction< void, RootStateType, unknown, @@ -105,6 +105,11 @@ function updateCallHistoryUnreadCount(): ThunkAction< }; } +const updateCallHistoryUnreadCount = debounce( + updateCallHistoryUnreadCountInner, + 300 +); + function markCallHistoryRead( conversationId: string, callId: string