From aa4c93b0eba9b77308d28c2914a4299a001e47f2 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Thu, 16 Jul 2020 11:04:25 +0200 Subject: [PATCH] [IMP] kpi_dashboard: Compute on fly, allow to refresh data on fly --- kpi_dashboard/models/kpi_dashboard.py | 16 +++++++++++++- .../static/src/js/dashboard_controller.js | 22 +++++++++++++++++++ .../static/src/js/dashboard_renderer.js | 13 +++++++++++ kpi_dashboard/views/kpi_dashboard.xml | 3 +++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/kpi_dashboard/models/kpi_dashboard.py b/kpi_dashboard/models/kpi_dashboard.py index 09955fef..770a2546 100644 --- a/kpi_dashboard/models/kpi_dashboard.py +++ b/kpi_dashboard/models/kpi_dashboard.py @@ -16,6 +16,10 @@ class KpiDashboard(models.Model): "kpi.dashboard.item", inverse_name="dashboard_id", copy=True, ) number_of_columns = fields.Integer(default=5, required=True) + compute_on_fly_refresh = fields.Integer( + default=0, + help="Seconds to refresh on fly elements" + ) width = fields.Integer(compute="_compute_width") margin_y = fields.Integer(default=10, required=True) margin_x = fields.Integer(default=10, required=True) @@ -43,6 +47,15 @@ class KpiDashboard(models.Model): + rec.widget_dimension_x * rec.number_of_columns ) + def read_dashboard_on_fly(self): + self.ensure_one() + result = [] + for item in self.item_ids: + if not item.kpi_id.compute_on_fly: + continue + result.append(item._read_dashboard()) + return result + def read_dashboard(self): self.ensure_one() result = { @@ -52,6 +65,7 @@ class KpiDashboard(models.Model): "max_cols": self.number_of_columns, "margin_x": self.margin_x, "margin_y": self.margin_y, + "compute_on_fly_refresh": self.compute_on_fly_refresh, "widget_dimension_x": self.widget_dimension_x, "widget_dimension_y": self.widget_dimension_y, "background_color": self.background_color, @@ -167,7 +181,7 @@ class KpiDashboardItem(models.Model): "kpi_id": self.kpi_id.id, "suffix": self.kpi_id.suffix or "", "prefix": self.kpi_id.prefix or "", - "compute_on_fly": self.kpi_id.compute_on_fly or "", + "compute_on_fly": self.kpi_id.compute_on_fly, } ) if self.kpi_id.compute_on_fly: diff --git a/kpi_dashboard/static/src/js/dashboard_controller.js b/kpi_dashboard/static/src/js/dashboard_controller.js index 30f28c0a..952077bd 100644 --- a/kpi_dashboard/static/src/js/dashboard_controller.js +++ b/kpi_dashboard/static/src/js/dashboard_controller.js @@ -10,7 +10,29 @@ odoo.define('kpi_dashboard.DashboardController', function (require) { var DashboardController = BasicController.extend({ custom_events: _.extend({}, BasicController.prototype.custom_events, { addDashboard: '_addDashboard', + refresh_on_fly: '_refreshOnFly', }), + _refreshOnFly: function (event) { + var self = this; + this._rpc({ + model: this.modelName, + method: 'read_dashboard_on_fly', + args: [[this.renderer.state.res_id]], + context: _.extend( + {}, + this.model.get(this.handle, {raw: true}).getContext(), + {bin_size: true} + ), + }).then(function (data) { + _.each(data, function (item) { + // We will follow the same logic used on Bus Notifications + self.renderer._onNotification([[ + "kpi_dashboard_" + self.renderer.state.res_id, + item + ]]) + }); + }); + }, renderPager: function ($node, options) { options = _.extend({}, options, { validate: this.canBeDiscarded.bind(this), diff --git a/kpi_dashboard/static/src/js/dashboard_renderer.js b/kpi_dashboard/static/src/js/dashboard_renderer.js index 6ff43ef5..b2603771 100644 --- a/kpi_dashboard/static/src/js/dashboard_renderer.js +++ b/kpi_dashboard/static/src/js/dashboard_renderer.js @@ -53,8 +53,21 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) { 'bus_service', 'onNotification', this, this._onNotification ); + if (this.state.specialData.compute_on_fly_refresh > 0) { + // Setting the refresh interval + this.on_fly_interval = setInterval(function () { + self.trigger_up('refresh_on_fly'); + }, this.state.specialData.compute_on_fly_refresh *1000); + }; return $.when(); }, + on_detach_callback: function () { + // We want to clear the refresh interval once we exit the view + if (this.on_fly_interval) { + clearInterval(this.on_fly_interval) + } + this._super.apply(this, arguments); + }, _onNotification: function (notifications) { var self = this; _.each(notifications, function (notification) { diff --git a/kpi_dashboard/views/kpi_dashboard.xml b/kpi_dashboard/views/kpi_dashboard.xml index eba04663..e2f841ad 100644 --- a/kpi_dashboard/views/kpi_dashboard.xml +++ b/kpi_dashboard/views/kpi_dashboard.xml @@ -54,6 +54,9 @@ + + +