[report] make it a prepared if query times out

This commit is contained in:
Prateeksha Singh 2018-06-29 15:55:28 +05:30
parent 0a7a4988b1
commit afa3836748
2 changed files with 19 additions and 6 deletions

View file

@ -23,21 +23,24 @@ class PreparedReport(Document):
self.report_start_time = frappe.utils.now()
def after_insert(self):
# enqueue(
# run_background,
# instance=self, timeout=6000
# )
run_background(self)
enqueue(
run_background,
instance=self, timeout=6000
)
def run_background(instance):
report = frappe.get_doc("Report", instance.ref_report_doctype)
result = generate_report_result(report, filters=json.loads(instance.filters), user=instance.owner)
create_csv_file(result['columns'], result['result'], 'Prepared Report', instance.name)
instance.status = "Completed"
instance.report_end_time = frappe.utils.now()
instance.save()
# TODO:
# Show Alert
# Send Email to user
def remove_header_meta(columns):
column_list = []

View file

@ -58,7 +58,17 @@ def generate_report_result(report, filters=None, user=None):
module = report.module or frappe.db.get_value("DocType", report.ref_doctype, "module")
if report.is_standard == "Yes":
method_name = get_report_module_dotted_path(module, report.name) + ".execute"
res = frappe.get_attr(method_name)(frappe._dict(filters))
res = []
# The JOB:
try:
res = frappe.get_attr(method_name)(frappe._dict(filters))
except:
report.prepared_report = 1
report.save()
frappe.throw("The report to too long to load. Please reload the page to generate it in background.")
columns, result = res[0], res[1]
if len(res) > 2:
message = res[2]