Skip to content

Commit d4a53ac

Browse files
committed
[ADD] logging
1 parent cd071ae commit d4a53ac

1 file changed

Lines changed: 66 additions & 22 deletions

File tree

spp_base_common/static/src/js/import_model.esm.js

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,84 @@
22
/**
33
* Part of OpenSPP. See LICENSE file for full copyright and licensing details.
44
*
5-
* Override base_import to fix batch import remainder issue in the frontend.
5+
* Override base_import to fix batch import remainder issue.
66
*
7-
* For TEST imports (dryrun), we process ALL records in one go to ensure
8-
* the remainder is included in the test. For ACTUAL imports, we use proper
9-
* batch calculation with Math.ceil to include remainders.
7+
* Ensures proper step calculation and that remainder batches are processed
8+
* automatically without stopping.
109
*/
1110

1211
import {BaseImportModel} from "@base_import/import_model";
1312
import {patch} from "@web/core/utils/patch";
13+
import {_t} from "@web/core/l10n/translation";
1414

1515
patch(BaseImportModel.prototype, {
1616
/**
17-
* Override executeImport to handle test vs actual import differently.
17+
* Override executeImport to ensure all batches including remainder execute.
1818
*/
1919
async executeImport(isTest = false, totalSteps, importProgress) {
20-
if (isTest) {
21-
// For test imports, force single step to test ALL records at once
22-
console.log("[SPP Base Import] Test mode - processing all records in one step");
23-
return super.executeImport(isTest, 1, importProgress);
24-
} else {
25-
// For actual imports, use corrected totalSteps calculation
26-
const correctedSteps = this.totalSteps;
27-
console.log(`[SPP Base Import] Import mode - processing ${correctedSteps} steps`);
28-
return super.executeImport(isTest, correctedSteps, importProgress);
20+
this.handleInterruption = false;
21+
this._updateComments();
22+
this.importMessages = [];
23+
24+
const startRow = this.importOptions.skip;
25+
const importRes = {
26+
ids: [],
27+
fields: this.columns.map((e) => Boolean(e.fieldInfo) && e.fieldInfo.fieldPath),
28+
columns: this.columns.map((e) => e.name.trim().toLowerCase()),
29+
hasError: false,
30+
};
31+
32+
console.log(`[SPP Base Import] Starting import - isTest: ${isTest}, totalSteps: ${totalSteps}, startRow: ${startRow}`);
33+
34+
for (let i = 1; i <= totalSteps; i++) {
35+
if (this.handleInterruption) {
36+
console.log(`[SPP Base Import] Import interrupted at step ${i}`);
37+
if (importRes.hasError || isTest) {
38+
importRes.nextrow = startRow;
39+
this.setOption("skip", startRow);
40+
}
41+
break;
42+
}
43+
44+
console.log(`[SPP Base Import] Executing step ${i} of ${totalSteps}`);
45+
const error = await this._executeImportStep(isTest, importRes);
46+
47+
if (error) {
48+
console.error(`[SPP Base Import] Error at step ${i}:`, error);
49+
const errorData = error.data || {};
50+
const message = errorData.arguments && (errorData.arguments[1] || errorData.arguments[0])
51+
|| _t("An unknown issue occurred during import (possibly lost connection, data limit exceeded or memory limits exceeded). Please retry in case the issue is transient. If the issue still occurs, try to split the file rather than import it at once.");
52+
53+
if (error.message) {
54+
this._addMessage("danger", [error.message, message]);
55+
} else {
56+
this._addMessage("danger", [message]);
57+
}
58+
59+
importRes.hasError = true;
60+
break;
61+
}
62+
63+
// Log nextrow after each step
64+
console.log(`[SPP Base Import] Step ${i} completed. Next row: ${importRes.nextrow || 'complete'}`);
65+
66+
if (importProgress) {
67+
importProgress.step = i;
68+
importProgress.value = Math.round((100 * (i - 1)) / totalSteps);
69+
}
70+
}
71+
72+
if (!importRes.hasError) {
73+
importProgress.value = 100;
2974
}
75+
this._updateComments(importRes);
76+
return {
77+
messages: this.importMessages.map((m) => m.message),
78+
};
3079
},
3180

3281
/**
3382
* Override get totalSteps to ensure proper calculation including remainder.
34-
*
35-
* Uses Math.ceil to properly handle remainder batches:
36-
* - 40100 / 2000 = 20.05 → ceil(20.05) = 21 ✓
37-
* - 40000 / 2000 = 20.0 → ceil(20.0) = 20 ✓
38-
* - 100 / 2000 = 0.05 → ceil(0.05) = 1 ✓
3983
*/
4084
get totalSteps() {
4185
const limit = this.importOptions.limit || 2000;
@@ -44,12 +88,12 @@ patch(BaseImportModel.prototype, {
4488
return 1;
4589
}
4690

47-
// Calculate total steps based on total file length
91+
// Calculate total steps based on file length
4892
const totalSteps = Math.ceil(this.fileLength / limit);
4993

5094
console.log(
51-
`[SPP Base Import] Batch calculation - ` +
52-
`Total file records: ${this.fileLength}, ` +
95+
`[SPP Base Import] Step calculation - ` +
96+
`File records: ${this.fileLength}, ` +
5397
`Batch size: ${limit}, ` +
5498
`Total steps: ${totalSteps}`
5599
);

0 commit comments

Comments
 (0)