dynamic loading of grid reports

This commit is contained in:
Rushabh Mehta 2013-02-07 22:19:46 +05:30
parent 69dbc96963
commit 2f2f1ec9b7
2 changed files with 96 additions and 57 deletions

View file

@ -5,36 +5,26 @@ wn.provide("wn.report_dump");
$.extend(wn.report_dump, {
data: {},
last_modified: {},
with_data: function(doctypes, callback, progress_bar) {
var missing = [];
$.each(doctypes, function(i, v) {
if(!wn.report_dump.data[v]) missing.push(v);
})
if(missing.length) {
wn.call({
method: "webnotes.widgets.report_dump.get_data",
args: {
doctypes: doctypes,
missing: missing
},
callback: function(r) {
// creating map of data from a list
$.each(r.message, function(doctype, doctype_data) {
var data = [];
$.each(doctype_data.data, function(i, d) {
var row = {};
$.each(doctype_data.columns, function(idx, col) {
row[col] = d[idx];
});
row.id = row.name || doctype + "-" + i;
row.doctype = doctype;
data.push(row);
});
wn.report_dump.data[doctype] = data;
});
// reverse map names
$.each(r.message, function(doctype, doctype_data) {
var pre_loaded = keys(wn.report_dump.last_modified);
wn.call({
method: "webnotes.widgets.report_dump.get_data",
type: "GET",
args: {
doctypes: doctypes,
last_modified: wn.report_dump.last_modified
},
callback: function(r) {
// creating map of data from a list
$.each(r.message, function(doctype, doctype_data) {
wn.report_dump.set_data(doctype, doctype_data);
});
// reverse map names
$.each(r.message, function(doctype, doctype_data) {
// only if not pre-loaded
if(!in_list(pre_loaded, doctype)) {
if(doctype_data.links) {
$.each(wn.report_dump.data[doctype], function(row_idx, row) {
$.each(doctype_data.links, function(link_key, link) {
@ -46,16 +36,57 @@ $.extend(wn.report_dump, {
})
})
}
});
callback();
},
progress_bar: progress_bar
}
});
callback();
},
progress_bar: progress_bar
})
},
set_data: function(doctype, doctype_data) {
var data = [];
var replace_dict = {};
var make_row = function(d) {
var row = {};
$.each(doctype_data.columns, function(idx, col) {
row[col] = d[idx];
});
row.id = row.name;
row.doctype = doctype;
return row;
}
if(wn.report_dump.last_modified[doctype]) {
// partial loading, make a name dict
$.each(doctype_data.data, function(i, d) {
var row = make_row(d);
replace_dict[row.name] = row;
});
// replace old data
$.each(wn.report_dump.data[doctype], function(i, d) {
if(replace_dict[d.name]) {
data.push(replace_dict[d.name]);
delete replace_dict[d.name];
} else {
data.push(d);
}
});
// add new records
$.each(replace_dict, function(name, d) {
data.push(d);
})
} else {
callback();
// first loading
$.each(doctype_data.data, function(i, d) {
data.push(make_row(d));
});
}
},
wn.report_dump.last_modified[doctype] = doctype_data.last_modified;
wn.report_dump.data[doctype] = data;
}
});
wn.provide("wn.views");
@ -87,19 +118,24 @@ wn.views.GridReport = Class.extend({
$(this.page).bind('show', function() {
// reapply filters on show
wn.cur_grid_report = me;
me.apply_filters_from_route();
me.refresh();
me.get_data()
});
},
get_data: function() {
var me = this;
var progress_bar = null;
if(!this.setup_filters_done)
progress_bar = this.wrapper.find(".progress .bar");
wn.report_dump.with_data(this.doctypes, function() {
// setup filters
me.setup_filters();
if(!me.setup_filters_done) {
me.setup_filters();
me.setup_filters_done = true;
}
me.apply_filters_from_route();
me.refresh();
}, this.wrapper.find(".progress .bar"));
}, progress_bar);
},
setup_filters: function() {
var me = this;
@ -132,7 +168,9 @@ wn.views.GridReport = Class.extend({
});
// plot check
if(this.setup_plot_check) this.setup_plot_check();
if(this.setup_plot_check)
this.setup_plot_check();
},
set_autocomplete: function($filter, list) {
var me = this;

View file

@ -25,16 +25,21 @@ import webnotes
import json
@webnotes.whitelist()
def get_data():
def get_data(doctypes, last_modified):
from startup.report_data_map import data_map
import datetime
doctypes = json.loads(webnotes.form_dict.get("doctypes"))
out = {}
doctypes = json.loads(doctypes)
last_modified = json.loads(last_modified)
start = datetime.datetime.now()
for d in doctypes:
args = data_map[d]
dt = d.find("[") != -1 and d[:d.find("[")] or d
if d in last_modified:
args['conditions'].append("modified > '" + last_modified[d] + "'")
conditions = order_by = ""
if args.get("force_index"):
@ -43,13 +48,16 @@ def get_data():
conditions += " where " + " and ".join(args["conditions"])
if args.get("order_by"):
order_by = " order by " + args["order_by"]
table = args.get("from") or ("`tab%s`" % dt)
table = args.get("from") or ("`tab%s`" % dt)
out[dt] = {}
start = datetime.datetime.now()
out[dt]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from %s %s %s""" \
% (",".join(args["columns"]), table, conditions, order_by))]
out[dt]["time"] = str(datetime.datetime.now() - start)
# last modified
tmp = webnotes.conn.sql("""select `modified`
from %s order by modified desc limit 1""" % table)
out[dt]["last_modified"] = tmp and tmp[0][0] or ""
out[dt]["columns"] = map(lambda c: c.split(" as ")[-1], args["columns"])
if args.get("links"):
@ -57,7 +65,8 @@ def get_data():
for d in out:
unused_links = []
if out[d].get("links"):
# only compress full dumps (not partial)
if out[d].get("links") and (not d in last_modified):
for link_key in out[d]["links"]:
link = out[d]["links"][link_key]
if link[0] in out:
@ -82,12 +91,4 @@ def get_data():
for link in unused_links:
del out[d]["links"][link]
missing = {}
# don't send everything
# send only missing!
# (but we need to load all to make links)
for d in out:
if d in webnotes.form_dict.get("missing",[]):
missing[d] = out[d]
return missing
return out