forked from OCA/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_sla_control.py
305 lines (276 loc) · 12.4 KB
/
project_sla_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2013 Daniel Reis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import fields, orm
from openerp.tools.safe_eval import safe_eval
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT as DT_FMT
from openerp import SUPERUSER_ID
from datetime import datetime as dt
from . import m2m
import logging
_logger = logging.getLogger(__name__)
SLA_STATES = [('5', 'Failed'), ('4', 'Will Fail'), ('3', 'Warning'),
('2', 'Watching'), ('1', 'Achieved')]
def safe_getattr(obj, dotattr, default=False):
"""
Follow an object attribute dot-notation chain to find the leaf value.
If any attribute doesn't exist or has no value, just return False.
Checks hasattr ahead, to avoid ORM Browse log warnings.
"""
attrs = dotattr.split('.')
while attrs:
attr = attrs.pop(0)
if attr in obj._model._columns:
try:
obj = getattr(obj, attr)
except AttributeError:
return default
if not obj:
return default
else:
return default
return obj
class SLAControl(orm.Model):
"""
SLA Control Registry
Each controlled document (Issue, Claim, ...) will have a record here.
This model concentrates all the logic for Service Level calculation.
"""
_name = 'project.sla.control'
_description = 'SLA Control Registry'
_columns = {
'doc_id': fields.integer('Document ID', readonly=True),
'doc_model': fields.char('Document Model', size=128, readonly=True),
'sla_line_id': fields.many2one(
'project.sla.line', 'Service Agreement'),
'sla_warn_date': fields.datetime('Warning Date'),
'sla_limit_date': fields.datetime('Limit Date'),
'sla_start_date': fields.datetime('Start Date'),
'sla_close_date': fields.datetime('Close Date'),
'sla_achieved': fields.integer('Achieved?'),
'sla_state': fields.selection(SLA_STATES, string="SLA Status"),
'locked': fields.boolean(
'Recalculation disabled',
help="Safeguard manual changes from future automatic "
"recomputations."),
# Future: perfect SLA manual handling
}
def write(self, cr, uid, ids, vals, context=None):
"""
Update the related Document's SLA State when any of the SLA Control
lines changes state
"""
res = super(SLAControl, self).write(
cr, uid, ids, vals, context=context)
new_state = vals.get('sla_state')
if new_state:
# just update sla_state without recomputing the whole thing
ctx = dict(context) if context else {}
ctx['__sla_stored__'] = 1
for sla in self.browse(cr, uid, ids, context=ctx):
doc = self.pool.get(sla.doc_model).browse(
cr, uid, sla.doc_id, context=ctx)
if doc.sla_state < new_state:
doc.write({'sla_state': new_state})
return res
def update_sla_states(self, cr, uid, context=None):
"""
Updates SLA States, given the current datetime:
Only works on "open" sla states (watching, warning and will fail):
- exceeded limit date are set to "will fail"
- exceeded warning dates are set to "warning"
To be used by a scheduled job.
"""
now = dt.strftime(dt.now(), DT_FMT)
# SLAs to mark as "will fail"
control_ids = self.search(
cr, uid,
[('sla_state', 'in', ['2', '3']), ('sla_limit_date', '<', now)],
context=context)
self.write(cr, uid, control_ids, {'sla_state': '4'}, context=context)
# SLAs to mark as "warning"
control_ids = self.search(
cr, uid,
[('sla_state', 'in', ['2']), ('sla_warn_date', '<', now)],
context=context)
self.write(cr, uid, control_ids, {'sla_state': '3'}, context=context)
return True
def _compute_sla_date(self, cr, uid, calendar_id, resource_id,
start_date, hours, context=None):
"""
Return a limit datetime by adding hours to a start_date, honoring
a working_time calendar and a resource's (res_uid) timezone and
availability (leaves)
"""
assert isinstance(start_date, dt)
assert isinstance(hours, int) and hours >= 0
cal_obj = self.pool.get('resource.calendar')
periods = cal_obj._schedule_hours(
cr, uid, calendar_id,
hours,
day_dt=start_date,
compute_leaves=True,
resource_id=resource_id,
default_interval=(8, 16),
context=context)
end_date = periods[-1][1]
return end_date
def _get_computed_slas(self, cr, uid, doc, context=None):
"""
Returns a dict with the computed data for SLAs, given a browse record
for the target document.
* The SLA used is either from a related analytic_account_id or
project_id, whatever is found first.
* The work calendar is taken from the Project's definitions ("Other
Info" tab -> Working Time).
* The timezone used for the working time calculations are from the
document's responsible User (user_id) or from the current User (uid).
For the SLA Achieved calculation:
* Creation date is used to start counting time
* Control date, used to calculate SLA achievement, is defined in the
SLA Definition rules.
"""
def datetime2str(dt_value, fmt): # tolerant datetime to string
return dt_value and dt.strftime(dt_value, fmt) or None
res = []
sla_ids = (safe_getattr(doc, 'analytic_account_id.sla_ids') or
safe_getattr(doc, 'project_id.analytic_account_id.sla_ids'))
if not sla_ids:
return res
for sla in sla_ids:
if sla.control_model != doc._name:
continue # SLA not for this model; skip
for l in sla.sla_line_ids:
eval_context = {'o': doc, 'obj': doc, 'object': doc}
if not l.condition or safe_eval(l.condition, eval_context):
start_date = dt.strptime(doc.create_date, DT_FMT)
res_uid = doc.user_id.id or uid
cal = safe_getattr(
doc, 'project_id.resource_calendar_id.id')
warn_date = self._compute_sla_date(
cr, uid, cal, res_uid,
start_date, l.warn_qty,
context=context)
lim_date = self._compute_sla_date(
cr, uid, cal, res_uid,
warn_date, l.limit_qty - l.warn_qty,
context=context)
# evaluate sla state
control_val = getattr(doc, sla.control_field_id.name)
if control_val:
control_date = dt.strptime(control_val, DT_FMT)
if control_date > lim_date:
sla_val, sla_state = 0, '5' # failed
else:
sla_val, sla_state = 1, '1' # achieved
else:
control_date = None
now = dt.now()
if now > lim_date:
sla_val, sla_state = 0, '4' # will fail
elif now > warn_date:
sla_val, sla_state = 0, '3' # warning
else:
sla_val, sla_state = 0, '2' # watching
res.append(
{'sla_line_id': l.id,
'sla_achieved': sla_val,
'sla_state': sla_state,
'sla_warn_date': datetime2str(warn_date, DT_FMT),
'sla_limit_date': datetime2str(lim_date, DT_FMT),
'sla_start_date': datetime2str(start_date, DT_FMT),
'sla_close_date': datetime2str(control_date, DT_FMT),
'doc_id': doc.id,
'doc_model': sla.control_model})
break
if sla_ids and not res:
_logger.warning("No valid SLA rule foun for %d, SLA Ids %s"
% (doc.id, repr([x.id for x in sla_ids])))
return res
def store_sla_control(self, cr, uid, docs, context=None):
"""
Used by controlled documents to ask for SLA calculation and storage.
``docs`` is a Browse object
"""
# context flag to avoid infinite loops on further writes
context = context or {}
if '__sla_stored__' in context:
return False
else:
ctx = dict(context)
ctx['__sla_stored__'] = 1
res = []
for ix, doc in enumerate(docs):
if ix and ix % 50 == 0:
_logger.info('...%d SLAs recomputed for %s' % (ix, doc._name))
control = {x.sla_line_id.id: x
for x in doc.sla_control_ids}
sla_recs = self._get_computed_slas(cr, uid, doc, context=ctx)
# calc sla control lines
if sla_recs:
slas = []
for sla_rec in sla_recs:
sla_line_id = sla_rec.get('sla_line_id')
if sla_line_id in control:
control_rec = control.get(sla_line_id)
if not control_rec.locked:
slas += m2m.write(control_rec.id, sla_rec)
else:
slas += m2m.add(sla_rec)
global_sla = max([sla[2].get('sla_state') for sla in slas])
else:
slas = m2m.clear()
global_sla = None
# calc sla control summary and store
vals = {'sla_state': global_sla, 'sla_control_ids': slas}
doc._model.write( # regular users can't write on SLA Control
cr, SUPERUSER_ID, [doc.id], vals, context=ctx)
return res
class SLAControlled(orm.AbstractModel):
"""
SLA Controlled documents: AbstractModel to apply SLA control on Models
"""
_name = 'project.sla.controlled'
_description = 'SLA Controlled Document'
_columns = {
'sla_control_ids': fields.many2many(
'project.sla.control', string="SLA Control", ondelete='cascade'),
'sla_state': fields.selection(
SLA_STATES, string="SLA Status", readonly=True),
}
def create(self, cr, uid, vals, context=None):
res = super(SLAControlled, self).create(cr, uid, vals, context=context)
docs = self.browse(cr, uid, [res], context=context)
self.pool.get('project.sla.control').store_sla_control(
cr, uid, docs, context=context)
return res
def write(self, cr, uid, ids, vals, context=None):
res = super(SLAControlled, self).write(
cr, uid, ids, vals, context=context)
docs = [x for x in self.browse(cr, uid, ids, context=context)
if (not x.stage_id.fold or x.sla_state not in ['1', '5'])]
self.pool.get('project.sla.control').store_sla_control(
cr, uid, docs, context=context)
return res
def unlink(self, cr, uid, ids, context=None):
# Unlink and delete all related Control records
for doc in self.browse(cr, uid, ids, context=context):
vals = [m2m.remove(x.id)[0] for x in doc.sla_control_ids]
doc.write({'sla_control_ids': vals})
return super(SLAControlled, self).unlink(cr, uid, ids, context=context)