Skip to content

Commit

Permalink
fix: sort prepared report filters (#19267) (#19270)
Browse files Browse the repository at this point in the history
backport of #19265

(cherry picked from commit b3e16b8)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Dec 13, 2022
1 parent 94b5e8d commit 0d645f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 12 additions & 1 deletion frappe/core/doctype/prepared_report/prepared_report.py
Expand Up @@ -68,7 +68,7 @@ def get_reports_in_queued_state(report_name, filters):
"Prepared Report",
filters={
"report_name": report_name,
"filters": json.dumps(json.loads(filters)),
"filters": process_filters_for_prepared_report(filters),
"status": "Queued",
},
)
Expand Down Expand Up @@ -102,6 +102,17 @@ def delete_prepared_reports(reports):
)


def process_filters_for_prepared_report(filters):
if isinstance(filters, str):
filters = json.loads(filters)

# This looks like an insanity but, without this it'd be very hard to find Prepared Reports matching given condition
# We're ensuring that spacing is consistent. e.g. JS seems to put no spaces after ":", Python on the other hand does.
# We are also ensuring that order of keys is same so generated JSON string will be identical too.
# PS: frappe.as_json sorts keys
return frappe.as_json(filters, indent=None, separators=(",", ":"))


def create_json_gz_file(data, dt, dn):
# Storing data in CSV file causes information loss
# Reports like P&L Statement were completely unsuable because of this
Expand Down
14 changes: 10 additions & 4 deletions frappe/desk/query_report.py
Expand Up @@ -147,16 +147,18 @@ def normalize_result(result, columns):
@frappe.whitelist()
def background_enqueue_run(report_name, filters=None, user=None):
"""run reports in background"""
from frappe.core.doctype.prepared_report.prepared_report import (
process_filters_for_prepared_report,
)

if not user:
user = frappe.session.user
report = get_report_doc(report_name)
track_instance = frappe.get_doc(
{
"doctype": "Prepared Report",
"report_name": report_name,
# This looks like an insanity but, without this it'd be very hard to find Prepared Reports matching given condition
# We're ensuring that spacing is consistent. e.g. JS seems to put no spaces after ":", Python on the other hand does.
"filters": json.dumps(json.loads(filters)),
"filters": process_filters_for_prepared_report(filters),
"ref_report_doctype": report_name,
"report_type": report.report_type,
"query": report.query,
Expand Down Expand Up @@ -271,6 +273,10 @@ def add_custom_column_data(custom_columns, result):


def get_prepared_report_result(report, filters, dn="", user=None):
from frappe.core.doctype.prepared_report.prepared_report import (
process_filters_for_prepared_report,
)

latest_report_data = {}
doc = None
if dn:
Expand All @@ -282,7 +288,7 @@ def get_prepared_report_result(report, filters, dn="", user=None):
"Prepared Report",
filters={
"status": "Completed",
"filters": json.dumps(filters),
"filters": process_filters_for_prepared_report(filters),
"owner": user,
"report_name": report.get("custom_report") or report.get("report_name"),
},
Expand Down

0 comments on commit 0d645f2

Please sign in to comment.