Merge pull request #41871 from blaggacao/perf/reduce-critical-path

perf: hot path in page load
This commit is contained in:
David Arnold
2024-09-05 15:50:23 +02:00
committed by GitHub
4 changed files with 90 additions and 64 deletions

View File

@@ -53,18 +53,28 @@ GL_REPOSTING_CHUNK = 100
@frappe.whitelist()
def get_fiscal_year(
date=None, fiscal_year=None, label="Date", verbose=1, company=None, as_dict=False, boolean=False
date=None,
fiscal_year=None,
label="Date",
verbose=1,
company=None,
as_dict=False,
boolean=None,
raise_on_missing=True,
):
if isinstance(raise_on_missing, str):
raise_on_missing = loads(raise_on_missing)
# backwards compat
if isinstance(boolean, str):
boolean = loads(boolean)
if boolean is not None:
raise_on_missing = not boolean
fiscal_years = get_fiscal_years(
date, fiscal_year, label, verbose, company, as_dict=as_dict, boolean=boolean
date, fiscal_year, label, verbose, company, as_dict=as_dict, raise_on_missing=raise_on_missing
)
if boolean:
return fiscal_years
else:
return fiscal_years[0]
return False if not fiscal_years else fiscal_years[0]
def get_fiscal_years(
@@ -74,8 +84,48 @@ def get_fiscal_years(
verbose=1,
company=None,
as_dict=False,
boolean=False,
boolean=None,
raise_on_missing=True,
):
if transaction_date:
transaction_date = getdate(transaction_date)
# backwards compat
if boolean is not None:
raise_on_missing = not boolean
all_fiscal_years = _get_fiscal_years(company=company)
# No restricting selectors
if not transaction_date and not fiscal_year:
return all_fiscal_years
for fy in all_fiscal_years:
if (fiscal_year and fy.name == fiscal_year) or (
transaction_date
and getdate(fy.year_start_date) <= transaction_date
and getdate(fy.year_end_date) >= transaction_date
):
if as_dict:
return (fy,)
else:
return ((fy.name, fy.year_start_date, fy.year_end_date),)
# No match for restricting selectors
if raise_on_missing:
error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(
label, formatdate(transaction_date)
)
if company:
error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company))
if verbose == 1:
frappe.msgprint(error_msg)
raise FiscalYearError(error_msg)
return []
def _get_fiscal_years(company=None):
fiscal_years = frappe.cache().hget("fiscal_years", company) or []
if not fiscal_years:
@@ -86,9 +136,6 @@ def get_fiscal_years(
frappe.qb.from_(FY).select(FY.name, FY.year_start_date, FY.year_end_date).where(FY.disabled == 0)
)
if fiscal_year:
query = query.where(FY.name == fiscal_year)
if company:
FYC = DocType("Fiscal Year Company")
query = query.where(
@@ -105,42 +152,7 @@ def get_fiscal_years(
fiscal_years = query.run(as_dict=True)
frappe.cache().hset("fiscal_years", company, fiscal_years)
if not transaction_date and not fiscal_year:
return fiscal_years
if transaction_date:
transaction_date = getdate(transaction_date)
for fy in fiscal_years:
matched = False
if fiscal_year and fy.name == fiscal_year:
matched = True
if (
transaction_date
and getdate(fy.year_start_date) <= transaction_date
and getdate(fy.year_end_date) >= transaction_date
):
matched = True
if matched:
if as_dict:
return (fy,)
else:
return ((fy.name, fy.year_start_date, fy.year_end_date),)
error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(label, formatdate(transaction_date))
if company:
error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company))
if boolean:
return False
if verbose == 1:
frappe.msgprint(error_msg)
raise FiscalYearError(error_msg)
return fiscal_years
@frappe.whitelist()