Skip to content

Commit f122299

Browse files
authored
fix: use lastActionId for matomo_log_action (#9)
1 parent f6121e2 commit f122299

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/lib.mjs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ export const copyTableBatch = async ({
9595
* Fetch valid action IDs for a site
9696
* @param {SourceDB} sourceDB
9797
* @param {number} idsite
98+
* @param {number} [lastActionId]
9899
* @param {(msg: string) => void} [log]
99100
* @returns {Promise<Set<number>>}
100101
*/
101102
export const fetchValidActionIds = async (
102103
sourceDB,
103104
idsite,
105+
lastActionId = 0,
104106
log = console.log,
105107
) => {
106108
const validActions = new Set();
@@ -115,15 +117,23 @@ export const fetchValidActionIds = async (
115117
.distinct()
116118
.where("idsite", "=", idsite)
117119
.where("server_time", ">=", new Date("2025-01-01"))
120+
.where(({ eb }) =>
121+
eb.or([
122+
eb("idaction_name", ">", lastActionId),
123+
eb("idaction_url", ">", lastActionId),
124+
]),
125+
)
118126
.limit(ACTION_BATCH)
119127
.offset(actionOffset)
120128
.execute();
121129

122130
if (batch.length === 0) break;
123131

124132
for (const r of batch) {
125-
if (r.idaction_name !== null) validActions.add(r.idaction_name);
126-
if (r.idaction_url !== null) validActions.add(r.idaction_url);
133+
if (r.idaction_name !== null && r.idaction_name > lastActionId)
134+
validActions.add(r.idaction_name);
135+
if (r.idaction_url !== null && r.idaction_url > lastActionId)
136+
validActions.add(r.idaction_url);
127137
}
128138
actionOffset += ACTION_BATCH;
129139
log(` - fetched ${actionOffset} rows, ${validActions.size} unique actions`);
@@ -192,7 +202,7 @@ export const copyActions = async ({
192202
.values(sourceRows)
193203
.executeTakeFirst();
194204
log(
195-
` - inserted ${insertion.numInsertedOrUpdatedRows} (processed ${chunkIndex}/${validActionsArray.length})`,
205+
` - inserted ${insertion.numInsertedOrUpdatedRows} (processed ${Math.min(chunkIndex, validActionsArray.length)}/${validActionsArray.length})`,
196206
);
197207
}
198208

@@ -278,7 +288,7 @@ export const importSite = async ({
278288

279289
// Copy actions
280290
log(`copy matomo_log_action from ${lastActionId}...`);
281-
const validActions = await fetchValidActionIds(sourceDB, idsite, log);
291+
const validActions = await fetchValidActionIds(sourceDB, idsite, lastActionId, log);
282292
await copyActions({
283293
sourceDB,
284294
targetDB,

src/lib.test.mjs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ describe("copyTableBatch", () => {
240240
describe("fetchValidActionIds", () => {
241241
it("should return empty set when no actions found", async () => {
242242
const sourceDB = createMockDB({ selectResults: [[]] });
243-
const result = await fetchValidActionIds(sourceDB, 1, () => {});
243+
const result = await fetchValidActionIds(sourceDB, 1, 0, () => {});
244244

245245
assert.strictEqual(result.size, 0);
246246
});
247247

248248
it("should query matomo_log_link_visit_action with correct columns", async () => {
249249
const sourceDB = createMockDB({ selectResults: [[]] });
250-
await fetchValidActionIds(sourceDB, 1, () => {});
250+
await fetchValidActionIds(sourceDB, 1, 0, () => {});
251251

252252
assert.deepStrictEqual(sourceDB._calls.selectFrom, [
253253
"matomo_log_link_visit_action",
@@ -262,7 +262,7 @@ describe("fetchValidActionIds", () => {
262262

263263
it("should apply idsite and date filters", async () => {
264264
const sourceDB = createMockDB({ selectResults: [[]] });
265-
await fetchValidActionIds(sourceDB, 42, () => {});
265+
await fetchValidActionIds(sourceDB, 42, 0, () => {});
266266

267267
// Check idsite filter
268268
assert.deepStrictEqual(sourceDB._calls.where[0], ["idsite", "=", 42]);
@@ -273,7 +273,7 @@ describe("fetchValidActionIds", () => {
273273

274274
it("should use pagination with limit and offset", async () => {
275275
const sourceDB = createMockDB({ selectResults: [[]] });
276-
await fetchValidActionIds(sourceDB, 1, () => {});
276+
await fetchValidActionIds(sourceDB, 1, 0, () => {});
277277

278278
assert.deepStrictEqual(sourceDB._calls.limit, [1000]);
279279
assert.deepStrictEqual(sourceDB._calls.offset, [0]);
@@ -291,14 +291,34 @@ describe("fetchValidActionIds", () => {
291291
],
292292
});
293293

294-
const result = await fetchValidActionIds(sourceDB, 1, () => {});
294+
const result = await fetchValidActionIds(sourceDB, 1, 0, () => {});
295295

296296
assert.strictEqual(result.size, 4);
297297
assert.ok(result.has(1));
298298
assert.ok(result.has(2));
299299
assert.ok(result.has(3));
300300
assert.ok(result.has(4));
301301
});
302+
303+
it("should only collect action IDs greater than lastActionId", async () => {
304+
const sourceDB = createMockDB({
305+
selectResults: [
306+
[
307+
{ idaction_name: 5, idaction_url: 10 },
308+
{ idaction_name: 3, idaction_url: 15 },
309+
],
310+
[],
311+
],
312+
});
313+
314+
const result = await fetchValidActionIds(sourceDB, 1, 8, () => {});
315+
316+
assert.strictEqual(result.size, 2);
317+
assert.ok(result.has(10));
318+
assert.ok(result.has(15));
319+
assert.ok(!result.has(5));
320+
assert.ok(!result.has(3));
321+
});
302322
});
303323

304324
describe("copyActions", () => {

0 commit comments

Comments
 (0)