perf: reduce JS memory heap and maintain namespaces

This commit is contained in:
Gavin D'souza 2020-04-29 10:25:18 +05:30
parent 0d61b35b0b
commit 6b850b7e71

View File

@ -1,6 +1,8 @@
// Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors // Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt // For license information, please see license.txt
frappe.provide("erpnext.tally_migration");
frappe.ui.form.on("Tally Migration", { frappe.ui.form.on("Tally Migration", {
onload: function (frm) { onload: function (frm) {
let reload_status = true; let reload_status = true;
@ -38,10 +40,13 @@ frappe.ui.form.on("Tally Migration", {
refresh: function (frm) { refresh: function (frm) {
frm.trigger("show_logs_preview"); frm.trigger("show_logs_preview");
erpnext.tally_migration.failed_import_log = JSON.parse(frm.doc.failed_import_log);
erpnext.tally_migration.fixed_errors_log = JSON.parse(frm.doc.fixed_errors_log);
["default_round_off_account", "default_warehouse", "default_cost_center"].forEach(account => { ["default_round_off_account", "default_warehouse", "default_cost_center"].forEach(account => {
frm.toggle_reqd(account, frm.doc.is_master_data_imported === 1) frm.toggle_reqd(account, frm.doc.is_master_data_imported === 1)
}) })
if (frm.doc.master_data && !frm.doc.is_master_data_imported) { if (frm.doc.master_data && !frm.doc.is_master_data_imported) {
if (frm.doc.is_master_data_processed) { if (frm.doc.is_master_data_processed) {
if (frm.doc.status != "Importing Master Data") { if (frm.doc.status != "Importing Master Data") {
@ -53,6 +58,7 @@ frappe.ui.form.on("Tally Migration", {
} }
} }
} }
if (frm.doc.day_book_data && !frm.doc.is_day_book_data_imported) { if (frm.doc.day_book_data && !frm.doc.is_day_book_data_imported) {
if (frm.doc.is_day_book_data_processed) { if (frm.doc.is_day_book_data_processed) {
if (frm.doc.status != "Importing Day Book Data") { if (frm.doc.status != "Importing Day Book Data") {
@ -87,7 +93,7 @@ frappe.ui.form.on("Tally Migration", {
frm.toggle_display(field, false); frm.toggle_display(field, false);
return return
} }
let rows = get_html_rows(shown_logs, field); let rows = erpnext.tally_migration.get_html_rows(shown_logs, field);
let rows_head, table_caption; let rows_head, table_caption;
let table_footer = (hidden_logs && (hidden_logs.length > 0)) ? `<tr class="text-muted"> let table_footer = (hidden_logs && (hidden_logs.length > 0)) ? `<tr class="text-muted">
@ -119,7 +125,7 @@ frappe.ui.form.on("Tally Migration", {
}, },
show_error_summary(frm) { show_error_summary(frm) {
let summary = JSON.parse(frm.doc.failed_import_log).reduce((summary, row) => { let summary = erpnext.tally_migration.failed_import_log.reduce((summary, row) => {
if (row.doc) { if (row.doc) {
if (summary[row.doc.doctype]) { if (summary[row.doc.doctype]) {
summary[row.doc.doctype] += 1; summary[row.doc.doctype] += 1;
@ -147,8 +153,7 @@ frappe.ui.form.on("Tally Migration", {
}, },
show_errored_import_log(frm) { show_errored_import_log(frm) {
let import_log = JSON.parse(frm.doc.failed_import_log || "[]"); let import_log = erpnext.tally_migration.failed_import_log;
let logs = import_log.slice(0, 20); let logs = import_log.slice(0, 20);
let hidden_logs = import_log.slice(20); let hidden_logs = import_log.slice(20);
@ -156,7 +161,7 @@ frappe.ui.form.on("Tally Migration", {
}, },
show_fixed_errors_log(frm) { show_fixed_errors_log(frm) {
let completed_log = JSON.parse(frm.doc.fixed_errors_log || "[]"); let completed_log = erpnext.tally_migration.fixed_errors_log;
let logs = completed_log.slice(0, 20); let logs = completed_log.slice(0, 20);
let hidden_logs = completed_log.slice(20); let hidden_logs = completed_log.slice(20);
@ -164,7 +169,7 @@ frappe.ui.form.on("Tally Migration", {
} }
}); });
const getError = (traceback) => { erpnext.tally_migration.getError = (traceback) => {
/* Extracts the Error Message from the Python Traceback or Solved error */ /* Extracts the Error Message from the Python Traceback or Solved error */
let is_multiline = traceback.trim().indexOf("\n") != -1; let is_multiline = traceback.trim().indexOf("\n") != -1;
let message; let message;
@ -181,28 +186,28 @@ const getError = (traceback) => {
return message return message
} }
const cleanDoc = (obj) => { erpnext.tally_migration.cleanDoc = (obj) => {
/* Strips all null and empty values of your JSON object */ /* Strips all null and empty values of your JSON object */
let temp = obj; let temp = obj;
$.each(temp, function(key, value){ $.each(temp, function(key, value){
if (value === "" || value === null){ if (value === "" || value === null){
delete obj[key]; delete obj[key];
} else if (Object.prototype.toString.call(value) === '[object Object]') { } else if (Object.prototype.toString.call(value) === '[object Object]') {
cleanDoc(value); erpnext.tally_migration.cleanDoc(value);
} else if ($.isArray(value)) { } else if ($.isArray(value)) {
$.each(value, function (k,v) { cleanDoc(v); }); $.each(value, function (k,v) { erpnext.tally_migration.cleanDoc(v); });
} }
}); });
return temp; return temp;
} }
window.unresolve = (document) => { erpnext.tally_migration.unresolve = (document) => {
let frm = cur_frm; let frm = cur_frm;
let failed_log = JSON.parse(frm.doc.failed_import_log); let failed_log = erpnext.tally_migration.failed_import_log;
let fixed_log = JSON.parse(frm.doc.fixed_errors_log); let fixed_log = erpnext.tally_migration.fixed_errors_log;
let modified_fixed_log = fixed_log.filter(row => { let modified_fixed_log = fixed_log.filter(row => {
if (!frappe.utils.deep_equal(cleanDoc(row.doc), document)) { if (!frappe.utils.deep_equal(erpnext.tally_migration.cleanDoc(row.doc), document)) {
return row return row
} }
}); });
@ -217,13 +222,13 @@ window.unresolve = (document) => {
frm.save(); frm.save();
} }
window.create_new_doc = (doctype, document) => { erpnext.tally_migration.resolve = (document) => {
let frm = cur_frm; let frm = cur_frm;
let failed_log = JSON.parse(frm.doc.failed_import_log); let failed_log = erpnext.tally_migration.failed_import_log;
let fixed_log = JSON.parse(frm.doc.fixed_errors_log); let fixed_log = erpnext.tally_migration.fixed_errors_log;
let modified_failed_log = failed_log.filter(row => { let modified_failed_log = failed_log.filter(row => {
if (!frappe.utils.deep_equal(cleanDoc(row.doc), document)) { if (!frappe.utils.deep_equal(erpnext.tally_migration.cleanDoc(row.doc), document)) {
return row return row
} }
}); });
@ -235,17 +240,21 @@ window.create_new_doc = (doctype, document) => {
// frm.trigger('show_logs_preview') // frm.trigger('show_logs_preview')
frm.dirty(); frm.dirty();
frm.save(); frm.save();
}
erpnext.tally_migration.create_new_doc = (doctype, document) => {
erpnext.tally_migration.resolve(document);
frappe.new_doc(doctype, document); frappe.new_doc(doctype, document);
} }
const get_html_rows = (logs, field) => { erpnext.tally_migration.get_html_rows = (logs, field) => {
let index = 0; let index = 0;
let rows = logs let rows = logs
.map(({ doc, exc }) => { .map(({ doc, exc }) => {
let id = frappe.dom.get_unique_id(); let id = frappe.dom.get_unique_id();
let traceback = exc; let traceback = exc;
let error_message = getError(traceback); let error_message = erpnext.tally_migration.getError(traceback);
index++; index++;
let show_traceback = ` let show_traceback = `
@ -264,17 +273,17 @@ const get_html_rows = (logs, field) => {
</button> </button>
<div class="collapse margin-top" id="${id}-doc"> <div class="collapse margin-top" id="${id}-doc">
<div class="well"> <div class="well">
<pre style="font-size: smaller;">${JSON.stringify(cleanDoc(doc), null, 1)}</pre> <pre style="font-size: smaller;">${JSON.stringify(erpnext.tally_migration.cleanDoc(doc), null, 1)}</pre>
</div> </div>
</div>`; </div>`;
let create_button = ` let create_button = `
<button class='btn btn-default btn-xs m-3' type='button' onclick='create_new_doc("${doc.doctype}", ${JSON.stringify(doc)})'> <button class='btn btn-default btn-xs m-3' type='button' onclick='erpnext.tally_migration.create_new_doc("${doc.doctype}", ${JSON.stringify(doc)})'>
${__("Create Document")} ${__("Create Document")}
</button>` </button>`
let mark_as_unresolved = ` let mark_as_unresolved = `
<button class='btn btn-default btn-xs m-3' type='button' onclick='unresolve(${JSON.stringify(doc)})'> <button class='btn btn-default btn-xs m-3' type='button' onclick='erpnext.tally_migration.unresolve(${JSON.stringify(doc)})'>
${__("Mark as unresolved")} ${__("Mark as unresolved")}
</button>` </button>`