Skip to content

Commit

Permalink
[REF] kpi: module reformed and adapted to new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasperalta1 committed Aug 27, 2024
1 parent 6e2b57c commit a087f91
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 526 deletions.
4 changes: 0 additions & 4 deletions kpi/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
"security/ir.model.access.csv",
"views/kpi_category_views.xml",
"views/kpi_history_views.xml",
"views/kpi_threshold_range_views.xml",
"views/kpi_threshold_views.xml",
"views/kpi_views.xml",
"views/menu.xml",
"data/kpi_data.xml",
],
"images": [
"images/kpi_definition.png",
"images/kpi_computation.png",
"images/kpi_threshold.png",
"images/kpi_range.png",
],
"installable": True,
}
2 changes: 0 additions & 2 deletions kpi/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import kpi_category
from . import kpi_threshold_range
from . import kpi_threshold
from . import kpi_history
from . import kpi
27 changes: 5 additions & 22 deletions kpi/models/kpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ class KPI(models.Model):
name = fields.Char(required=True)
description = fields.Text()
category_id = fields.Many2one("kpi.category", required=True)
threshold_id = fields.Many2one("kpi.threshold", required=True)
periodicity = fields.Integer(default=1)

periodicity_uom = fields.Selection(
[
("minute", "Minute"),
# ("minute", "Minute"), Comentamos opción minuto por tema performance
("hour", "Hour"),
("day", "Day"),
("week", "Week"),
Expand All @@ -74,18 +73,15 @@ class KPI(models.Model):

next_execution_date = fields.Datetime(readonly=True)
value = fields.Float(compute="_compute_display_last_kpi_value")
color = fields.Text(compute="_compute_display_last_kpi_value")
last_execution = fields.Datetime(compute="_compute_display_last_kpi_value")
kpi_type = fields.Selection(
[
("python", "Python"),
("local", "SQL - Local DB"),
("external", "SQL - External DB"),
("sql", "SQL - Local DB"),
],
"KPI Computation Type",
)

dbsource_id = fields.Many2one("base.external.dbsource", "External DB Source")
kpi_code = fields.Text(
help=(
"SQL code must return the result as 'value' " "(i.e. 'SELECT 5 AS value')."
Expand All @@ -110,40 +106,27 @@ def _compute_display_last_kpi_value(self):
if history_ids:
his = obj.history_ids[0]
obj.value = his.value
obj.color = his.color
obj.last_execution = his.date
else:
obj.value = 0
obj.color = "#FFFFFF"
obj.last_execution = False

def _get_kpi_value(self):
self.ensure_one()
kpi_value = 0
if self.kpi_code:
if self.kpi_type == "local" and is_sql_or_ddl_statement(self.kpi_code):
if self.kpi_type == "sql" and is_sql_or_ddl_statement(self.kpi_code):
self.env.cr.execute(self.kpi_code)
dic = self.env.cr.dictfetchall()
if is_one_value(dic):
kpi_value = dic[0]["value"]
elif (
self.kpi_type == "external"
and self.dbsource_id.id
and is_sql_or_ddl_statement(self.kpi_code)
):
dbsrc_obj = self.dbsource_id
res = dbsrc_obj.execute(self.kpi_code)
if is_one_value(res):
kpi_value = res[0]["value"]
elif self.kpi_type == "python":
kpi_value = safe_eval(self.kpi_code, {"self": self})
if isinstance(kpi_value, dict):
res = kpi_value
else:
threshold_obj = self.threshold_id
res = {
"value": kpi_value,
"color": threshold_obj.get_color(kpi_value),
}
res.update({"kpi_id": self.id})
return res
Expand All @@ -159,8 +142,8 @@ def update_next_execution_date(self):
for obj in self:
if obj.periodicity_uom == "hour":
delta = relativedelta(hours=obj.periodicity)
elif obj.periodicity_uom == "minute":
delta = relativedelta(minutes=obj.periodicity)
# elif obj.periodicity_uom == "minute":
# delta = relativedelta(minutes=obj.periodicity)
elif obj.periodicity_uom == "day":
delta = relativedelta(days=obj.periodicity)
elif obj.periodicity_uom == "week":
Expand Down
1 change: 0 additions & 1 deletion kpi/models/kpi_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class KPIHistory(models.Model):
default=lambda r: fields.Datetime.now(),
)
value = fields.Float(required=True, readonly=True)
color = fields.Text(required=True, readonly=True, default="#FFFFFF")
company_id = fields.Many2one(
"res.company", "Company", default=lambda self: self.env.company
)
92 changes: 0 additions & 92 deletions kpi/models/kpi_threshold.py

This file was deleted.

174 changes: 0 additions & 174 deletions kpi/models/kpi_threshold_range.py

This file was deleted.

Loading

0 comments on commit a087f91

Please sign in to comment.