remove unused replay snapshot path before merge
This commit is contained in:
1 parent
462be82782
commit
b5d56a2a29
3 files changed
+31
-177
No files matched your search
@@ -3,11 +3,6 @@ import { json, unauthorized } from '@/lib/response';
|
||||
import { canViewWebsite } from '@/permissions';
|
||||
import { getReplayChunks } from '@/queries/sql';
|
||||
|
||||
const RRWEB_TYPE_FULL_SNAPSHOT = 2;
|
||||
const RRWEB_TYPE_META = 4;
|
||||
const SNAPSHOT_WINDOW_CHUNK_LIMIT = 6;
|
||||
const SNAPSHOT_WINDOW_MAX_CHUNKS = 96;
|
||||
|
||||
function getEventTimestamp(event: any): number | null {
|
||||
const timestamp = Number(event?.timestamp);
|
||||
|
||||
@@ -24,102 +19,6 @@ function parseOptionalInteger(value: string | null): number | undefined {
|
||||
return Number.isInteger(parsed) ? parsed : undefined;
|
||||
}
|
||||
|
||||
function trimChunksToCheckpoint(
|
||||
chunks: Awaited<ReturnType<typeof getReplayChunks>>,
|
||||
{ endChunkIndex, endEventIndex }: { endChunkIndex?: number; endEventIndex?: number },
|
||||
) {
|
||||
let lastMetaChunkIndex: number | null = null;
|
||||
let checkpointStartChunkIndex: number | null = null;
|
||||
let hasMeta = false;
|
||||
|
||||
for (const chunk of chunks) {
|
||||
if (endChunkIndex !== undefined && chunk.chunkIndex > endChunkIndex) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (let chunkEventIndex = 0; chunkEventIndex < chunk.events.length; chunkEventIndex++) {
|
||||
if (
|
||||
chunk.chunkIndex === endChunkIndex &&
|
||||
endEventIndex !== undefined &&
|
||||
chunkEventIndex > endEventIndex
|
||||
) {
|
||||
break;
|
||||
}
|
||||
|
||||
const event = chunk.events[chunkEventIndex];
|
||||
|
||||
if (event?.type === RRWEB_TYPE_META) {
|
||||
lastMetaChunkIndex = chunk.chunkIndex;
|
||||
hasMeta = true;
|
||||
}
|
||||
|
||||
if (event?.type === RRWEB_TYPE_FULL_SNAPSHOT) {
|
||||
checkpointStartChunkIndex = lastMetaChunkIndex ?? chunk.chunkIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (checkpointStartChunkIndex === null) {
|
||||
return { chunks, foundCheckpoint: false, hasMeta };
|
||||
}
|
||||
|
||||
return {
|
||||
chunks: chunks.filter(chunk => chunk.chunkIndex >= checkpointStartChunkIndex),
|
||||
foundCheckpoint: true,
|
||||
hasMeta,
|
||||
};
|
||||
}
|
||||
|
||||
async function getReplaySnapshotChunks(
|
||||
websiteId: string,
|
||||
replayId: string,
|
||||
{ endAt, endChunkIndex, endEventIndex }: { endAt?: Date; endChunkIndex: number; endEventIndex?: number },
|
||||
) {
|
||||
let limit = SNAPSHOT_WINDOW_CHUNK_LIMIT;
|
||||
|
||||
while (limit <= SNAPSHOT_WINDOW_MAX_CHUNKS) {
|
||||
const descChunks = await getReplayChunks(websiteId, replayId, {
|
||||
endAt,
|
||||
endChunkIndex,
|
||||
limit,
|
||||
order: 'desc',
|
||||
});
|
||||
|
||||
const chunks = [...descChunks].reverse();
|
||||
const { chunks: trimmedChunks, foundCheckpoint, hasMeta } = trimChunksToCheckpoint(chunks, {
|
||||
endChunkIndex,
|
||||
endEventIndex,
|
||||
});
|
||||
|
||||
if (foundCheckpoint || descChunks.length < limit) {
|
||||
if (hasMeta || trimmedChunks.length === 0) {
|
||||
return trimmedChunks;
|
||||
}
|
||||
|
||||
const initialChunks = await getReplayChunks(websiteId, replayId, {
|
||||
limit: 1,
|
||||
order: 'asc',
|
||||
});
|
||||
|
||||
if (!initialChunks.length) {
|
||||
return trimmedChunks;
|
||||
}
|
||||
|
||||
const initialChunk = initialChunks[0];
|
||||
|
||||
if (trimmedChunks.some(chunk => chunk.chunkIndex === initialChunk.chunkIndex)) {
|
||||
return trimmedChunks;
|
||||
}
|
||||
|
||||
return [initialChunk, ...trimmedChunks];
|
||||
}
|
||||
|
||||
limit *= 2;
|
||||
}
|
||||
|
||||
return getReplayChunks(websiteId, replayId, { endAt, endChunkIndex });
|
||||
}
|
||||
|
||||
function mergeReplayEvents(
|
||||
chunks: Awaited<ReturnType<typeof getReplayChunks>>,
|
||||
{
|
||||
@@ -191,14 +90,7 @@ export async function GET(
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const chunks =
|
||||
endChunkIndex !== undefined
|
||||
? await getReplaySnapshotChunks(websiteId, replayId, {
|
||||
endAt,
|
||||
endChunkIndex,
|
||||
endEventIndex,
|
||||
})
|
||||
: await getReplayChunks(websiteId, replayId, { endAt, endChunkIndex });
|
||||
const chunks = await getReplayChunks(websiteId, replayId, { endAt, endChunkIndex });
|
||||
const allEvents = mergeReplayEvents(chunks, { until, endChunkIndex, endEventIndex });
|
||||
const sessionId = chunks.length > 0 ? chunks[0].sessionId : null;
|
||||
const startedAt = chunks.length > 0 ? chunks[0].startedAt : null;
|
||||
|
||||
Reference in new issue
Block a user