Merge pull request #21683 from ankush/on_demand_tags

perf: lazy load sidebar tag stats
This commit is contained in:
Ankush Menat 2023-07-15 13:36:09 +05:30 committed by GitHub
commit b4ea330c86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 22 deletions

View file

@ -525,47 +525,47 @@ def get_stats(stats, doctype, filters=None):
if filters is None:
filters = []
tags = json.loads(stats)
columns = json.loads(stats)
if filters:
filters = json.loads(filters)
stats = {}
results = {}
try:
columns = frappe.db.get_table_columns(doctype)
db_columns = frappe.db.get_table_columns(doctype)
except (frappe.db.InternalError, frappe.db.ProgrammingError):
# raised when _user_tags column is added on the fly
# raised if its a virtual doctype
columns = []
db_columns = []
for tag in tags:
if tag not in columns:
for column in columns:
if column not in db_columns:
continue
try:
tag_count = frappe.get_list(
doctype,
fields=[tag, "count(*)"],
filters=filters + [[tag, "!=", ""]],
group_by=tag,
fields=[column, "count(*)"],
filters=filters + [[column, "!=", ""]],
group_by=column,
as_list=True,
distinct=1,
)
if tag == "_user_tags":
stats[tag] = scrub_user_tags(tag_count)
if column == "_user_tags":
results[column] = scrub_user_tags(tag_count)
no_tag_count = frappe.get_list(
doctype,
fields=[tag, "count(*)"],
filters=filters + [[tag, "in", ("", ",")]],
fields=[column, "count(*)"],
filters=filters + [[column, "in", ("", ",")]],
as_list=True,
group_by=tag,
order_by=tag,
group_by=column,
order_by=column,
)
no_tag_count = no_tag_count[0][1] if no_tag_count else 0
stats[tag].append([_("No Tags"), no_tag_count])
results[column].append([_("No Tags"), no_tag_count])
else:
stats[tag] = tag_count
results[column] = tag_count
except frappe.db.SQLError:
pass
@ -573,7 +573,7 @@ def get_stats(stats, doctype, filters=None):
# raised when _user_tags column is added on the fly
pass
return stats
return results
@frappe.whitelist()

View file

@ -34,7 +34,7 @@ frappe.views.ListSidebar = class ListSidebar {
) {
this.sidebar.find(".list-tags").remove();
} else {
this.sidebar.find(".list-stats").on("click", (e) => {
this.sidebar.find(".list-stats").on("show.bs.dropdown", (e) => {
this.reload_stats();
});
}
@ -187,6 +187,10 @@ frappe.views.ListSidebar = class ListSidebar {
get_stats() {
var me = this;
let dropdown_options = me.sidebar.find(".list-stats-dropdown .stat-result");
this.set_loading_state(dropdown_options);
frappe.call({
method: "frappe.desk.reportview.get_sidebar_stats",
type: "GET",
@ -208,6 +212,14 @@ frappe.views.ListSidebar = class ListSidebar {
});
}
set_loading_state(dropdown) {
dropdown.html(`<li>
<div class="empty-state">
${__("Loading...")}
</div>
</li>`);
}
render_stat(stats) {
let args = {
stats: stats,

View file

@ -587,9 +587,6 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList {
</div>
`);
this.setup_new_doc_event();
if (this.list_view_settings && !this.list_view_settings.disable_sidebar_stats) {
this.list_sidebar && this.list_sidebar.reload_stats();
}
this.toggle_paging && this.$paging_area.toggle(true);
}