fix: importer 2..
This commit is contained in:
@@ -226,16 +226,27 @@ export async function createSessionsStartEndEvents(
|
|||||||
"name NOT IN ('session_start', 'session_end')",
|
"name NOT IN ('session_start', 'session_end')",
|
||||||
].join(' AND ');
|
].join(' AND ');
|
||||||
|
|
||||||
const sessionBatchSubquery = `
|
|
||||||
(SELECT DISTINCT session_id
|
|
||||||
FROM ${TABLE_NAMES.events_imports}
|
|
||||||
WHERE ${baseWhere}
|
|
||||||
AND session_id > {lastSessionId:String}
|
|
||||||
ORDER BY session_id
|
|
||||||
LIMIT {limit:UInt32})
|
|
||||||
`;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
const idsResult = await ch.query({
|
||||||
|
query: `
|
||||||
|
SELECT DISTINCT session_id
|
||||||
|
FROM ${TABLE_NAMES.events_imports}
|
||||||
|
WHERE ${baseWhere}
|
||||||
|
AND session_id > {lastSessionId:String}
|
||||||
|
ORDER BY session_id
|
||||||
|
LIMIT {limit:UInt32}
|
||||||
|
`,
|
||||||
|
query_params: { importId, lastSessionId, limit: SESSION_BATCH_SIZE },
|
||||||
|
format: 'JSONEachRow',
|
||||||
|
});
|
||||||
|
|
||||||
|
const idRows = (await idsResult.json()) as Array<{ session_id: string }>;
|
||||||
|
if (idRows.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxSessionId = idRows.at(-1)!.session_id;
|
||||||
|
|
||||||
const sessionEventsQuery = `
|
const sessionEventsQuery = `
|
||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
@@ -252,13 +263,14 @@ export async function createSessionsStartEndEvents(
|
|||||||
max(created_at) AS last_timestamp
|
max(created_at) AS last_timestamp
|
||||||
FROM ${TABLE_NAMES.events_imports}
|
FROM ${TABLE_NAMES.events_imports}
|
||||||
WHERE ${baseWhere}
|
WHERE ${baseWhere}
|
||||||
AND session_id IN ${sessionBatchSubquery}
|
AND session_id > {lastSessionId:String}
|
||||||
|
AND session_id <= {maxSessionId:String}
|
||||||
GROUP BY session_id, device_id, project_id
|
GROUP BY session_id, device_id, project_id
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const sessionEventsResult = await ch.query({
|
const sessionEventsResult = await ch.query({
|
||||||
query: sessionEventsQuery,
|
query: sessionEventsQuery,
|
||||||
query_params: { importId, lastSessionId, limit: SESSION_BATCH_SIZE },
|
query_params: { importId, lastSessionId, maxSessionId },
|
||||||
format: 'JSONEachRow',
|
format: 'JSONEachRow',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -411,11 +423,8 @@ export async function createSessionsStartEndEvents(
|
|||||||
await insertImportBatch(sessionEvents, importId);
|
await insertImportBatch(sessionEvents, importId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sessionData.length === 0) {
|
lastSessionId = maxSessionId;
|
||||||
break;
|
if (idRows.length < SESSION_BATCH_SIZE) {
|
||||||
}
|
|
||||||
lastSessionId = sessionData.at(-1)!.session_id;
|
|
||||||
if (sessionData.length < SESSION_BATCH_SIZE) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -476,15 +485,6 @@ export async function backfillSessionsToProduction(
|
|||||||
const SESSION_BATCH_SIZE = 5000;
|
const SESSION_BATCH_SIZE = 5000;
|
||||||
let lastSessionId = '';
|
let lastSessionId = '';
|
||||||
|
|
||||||
const baseWhere = 'import_id = {importId:String} AND session_id > {lastSessionId:String}';
|
|
||||||
const sessionBatchSubquery = `
|
|
||||||
(SELECT DISTINCT session_id
|
|
||||||
FROM ${TABLE_NAMES.events_imports}
|
|
||||||
WHERE ${baseWhere}
|
|
||||||
ORDER BY session_id
|
|
||||||
LIMIT {limit:UInt32})
|
|
||||||
`;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const idsResult = await ch.query({
|
const idsResult = await ch.query({
|
||||||
query: `
|
query: `
|
||||||
@@ -504,6 +504,8 @@ export async function backfillSessionsToProduction(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxSessionId = idRows.at(-1)!.session_id;
|
||||||
|
|
||||||
const sessionsInsertQuery = `
|
const sessionsInsertQuery = `
|
||||||
INSERT INTO ${TABLE_NAMES.sessions} (
|
INSERT INTO ${TABLE_NAMES.sessions} (
|
||||||
id, project_id, profile_id, device_id, created_at, ended_at,
|
id, project_id, profile_id, device_id, created_at, ended_at,
|
||||||
@@ -560,13 +562,14 @@ export async function backfillSessionsToProduction(
|
|||||||
FROM ${TABLE_NAMES.events_imports} e
|
FROM ${TABLE_NAMES.events_imports} e
|
||||||
WHERE
|
WHERE
|
||||||
e.import_id = {importId:String}
|
e.import_id = {importId:String}
|
||||||
AND e.session_id IN ${sessionBatchSubquery}
|
AND e.session_id > {lastSessionId:String}
|
||||||
|
AND e.session_id <= {maxSessionId:String}
|
||||||
GROUP BY e.session_id
|
GROUP BY e.session_id
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await ch.command({
|
await ch.command({
|
||||||
query: sessionsInsertQuery,
|
query: sessionsInsertQuery,
|
||||||
query_params: { importId, lastSessionId, limit: SESSION_BATCH_SIZE },
|
query_params: { importId, lastSessionId, maxSessionId },
|
||||||
clickhouse_settings: {
|
clickhouse_settings: {
|
||||||
wait_end_of_query: 1,
|
wait_end_of_query: 1,
|
||||||
send_progress_in_http_headers: 1,
|
send_progress_in_http_headers: 1,
|
||||||
@@ -574,7 +577,7 @@ export async function backfillSessionsToProduction(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
lastSessionId = idRows.at(-1)!.session_id;
|
lastSessionId = maxSessionId;
|
||||||
if (idRows.length < SESSION_BATCH_SIZE) {
|
if (idRows.length < SESSION_BATCH_SIZE) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user