|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2016 LasLabs Inc. |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +from odoo import _, api, fields, models |
| 6 | +from odoo.tools import safe_eval |
| 7 | +from odoo.exceptions import ValidationError, UserError |
| 8 | + |
| 9 | + |
| 10 | +class ClouderMetricType(models.Model): |
| 11 | + """ It provides context for usage metric types """ |
| 12 | + |
| 13 | + _name = 'clouder.metric.type' |
| 14 | + _description = 'Clouder Metric Types' |
| 15 | + |
| 16 | + name = fields.Char() |
| 17 | + code = fields.Char() |
| 18 | + metric_model = fields.Selection( |
| 19 | + selection=lambda s: s._get_metric_models(), |
| 20 | + required=True, |
| 21 | + help='Clouder entity type that this metric is related to.', |
| 22 | + ) |
| 23 | + uom_id = fields.Many2one( |
| 24 | + string='Unit of Measure', |
| 25 | + comodel_name='product.uom', |
| 26 | + required=True, |
| 27 | + ) |
| 28 | + connector_type = fields.Selection( |
| 29 | + selection=lambda s: s.env['base.external.dbsource'].CONNECTORS, |
| 30 | + ) |
| 31 | + metric_code = fields.Text( |
| 32 | + required=True, |
| 33 | + default=lambda s: s._default_query_code(), |
| 34 | + help='Python code to use as query for metric.' |
| 35 | + ) |
| 36 | + |
| 37 | + @api.model |
| 38 | + def _default_query_code(self): |
| 39 | + return _("# Python code. \n" |
| 40 | + "Use `value = my_value` to specify the final calculated " |
| 41 | + " metric value. This is required. \n" |
| 42 | + "Optionally use ``uom = product_uom_record`` to change the " |
| 43 | + "units that the metric is being measured in. \n" |
| 44 | + "You should also add `date_start` and `date_end`, which " |
| 45 | + "are `datetime` values to signify the date of occurrence of " |
| 46 | + "the metric value in question. \n" |
| 47 | + "# You can use the following variables: \n" |
| 48 | + "# - self: browse_record of the current ID Category \n" |
| 49 | + "# - interface: browse_record of the Metrics Interface. \n" |
| 50 | + "# - metric_model: Name of the metric model type. \n") |
| 51 | + |
| 52 | + @api.model |
| 53 | + def _get_metric_models(self): |
| 54 | + """ Returns a selection of available metric models |
| 55 | + Returns: |
| 56 | + list: Additional metric models |
| 57 | + """ |
| 58 | + return [ |
| 59 | + ('clouder.base', 'Base'), |
| 60 | + ('clouder.service', 'Service'), |
| 61 | + ] |
| 62 | + |
| 63 | + @api.multi |
| 64 | + def _get_query_code_context(self, interface): |
| 65 | + """ Returns a query context for use |
| 66 | + Args: |
| 67 | + interface (clouder.metric.interface): The interface to use |
| 68 | + Returns: |
| 69 | + dict: Dict with the context for the given iface and model |
| 70 | + """ |
| 71 | + self.ensure_one() |
| 72 | + return { |
| 73 | + 'interface': interface, |
| 74 | + 'metric_model': self.metric_model, |
| 75 | + 'self': self, |
| 76 | + } |
| 77 | + |
| 78 | + @api.model |
| 79 | + def save_metric_value(self, metric_interfaces): |
| 80 | + """ Saves a metric value from the given interface |
| 81 | + Args: |
| 82 | + metric_interfaces (clouder.metric.interface): The interface to use |
| 83 | + Returns: |
| 84 | + None |
| 85 | + """ |
| 86 | + for iface in metric_interfaces: |
| 87 | + eval_context = iface.type_id._get_query_code_context(iface) |
| 88 | + try: |
| 89 | + safe_eval( |
| 90 | + iface.query_code, |
| 91 | + eval_context, |
| 92 | + mode='exec', |
| 93 | + nocopy=True, |
| 94 | + ) |
| 95 | + except Exception as e: |
| 96 | + raise UserError(_( |
| 97 | + 'Error while evaluating metrics query:' |
| 98 | + '\n %s \n %s' % (iface.name, e), |
| 99 | + )) |
| 100 | + if eval_context.get('value') is None: |
| 101 | + raise ValidationError(_( |
| 102 | + 'Metrics query did not set the `value` variable, which ' |
| 103 | + 'is used to indicate the value that should be saved for ' |
| 104 | + 'the query.', |
| 105 | + )) |
| 106 | + uom = eval_context.get('uom') or iface.uom_id |
| 107 | + iface.write({ |
| 108 | + 'metric_value_ids': [(0, 0, { |
| 109 | + 'value': eval_context['value'], |
| 110 | + 'date_start': eval_context.get('date_start'), |
| 111 | + 'date_end': eval_context.get('date_end'), |
| 112 | + 'uom_id': uom.id, |
| 113 | + })], |
| 114 | + }) |
0 commit comments