~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to dm/dm_engine.py

  • Committer: Mitesh Soni
  • Date: 2010-02-23 10:13:53 UTC
  • mfrom: (4287.1.66)
  • Revision ID: mso@tinyerp.com-20100223101353-kehqa77hcxeilji8
[MERGE]:Merging from same branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
from osv import fields
25
25
from osv import osv
26
 
import time
27
26
import sys
28
27
import datetime
29
28
import netsvc
30
29
import traceback
31
30
import tools
 
31
import pooler
 
32
 
 
33
# Enable/Disable performance monitoring
 
34
enable_monitor = False
 
35
 
 
36
class dm_perf_monitor(osv.osv): # {{{
 
37
    _name = "dm.perf_monitor"
 
38
    _descitption = "DM Performance Monitor"
 
39
 
 
40
    _columns = {
 
41
        'name' : fields.char('Function Name', size=64, required=True),
 
42
        'model' : fields.char('Model Name', size=64, required=True),
 
43
        'duration' : fields.float('Duration'),
 
44
        'date' : fields.datetime('Date'),
 
45
    }
 
46
 
 
47
    _defaults = {
 
48
        'date' : lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
 
49
    }
 
50
dm_perf_monitor() # }}}
 
51
 
 
52
def monitor(func): # {{{
 
53
    if enable_monitor:
 
54
        def callf(*args, **kwargs):
 
55
            time_start = time.time()                
 
56
            r = func(*args, **kwargs)
 
57
            time_stop = time.time()
 
58
            duration = time_stop - time_start
 
59
            vals = {
 
60
                'name' : func.__name__,
 
61
                'duration' : duration,
 
62
                'model' : args[0],
 
63
            }
 
64
            pooler.get_pool(args[1].dbname).get('dm.perf_monitor').create(args[1], args[2], vals)
 
65
            return r
 
66
        return callf
 
67
    else:
 
68
        return func # }}}
 
69
    
 
70
class report_dm_performance_monitor(osv.osv):
 
71
    _name = "report.dm.performance.monitor"
 
72
    _description = "Performance monitor"
 
73
    _auto = False
 
74
    _columns = {
 
75
        'name' : fields.char('Function Name', size=64, required=True),
 
76
        'duration' : fields.float('Duration'),
 
77
    }
 
78
    
 
79
    def init(self, cr):
 
80
        tools.drop_view_if_exists(cr, 'report_dm_performance_monitor')
 
81
        cr.execute("""
 
82
            create or replace view report_dm_performance_monitor as (
 
83
                select
 
84
                    min(p.id) as id,
 
85
                    p.name as name,
 
86
                    avg(p.duration) as duration
 
87
                from
 
88
                    dm_perf_monitor p
 
89
                group by p.name
 
90
            )""")
 
91
    
 
92
report_dm_performance_monitor()
32
93
 
33
94
class dm_workitem(osv.osv): # {{{
34
95
    _name = "dm.workitem"
55
116
            res[wi.id] = wi.address_id and wi.address_id.email or ''
56
117
        return res
57
118
 
 
119
 
58
120
    _columns = {
59
121
        'step_id': fields.many2one('dm.offer.step', 'Offer Step',
60
122
                                    ondelete="cascade", select="1"),
65
127
                                      'Customer Address',
66
128
                                       select="1",
67
129
                                       ondelete="cascade"),
68
 
        'customer_id': fields.function(_customer_id, method=True, type='char', string='Customer Id'),
69
 
        'customer_email': fields.function(_customer_email, method=True, type='char', string='Customer Email'),
 
130
        'customer_id': fields.function(_customer_id, method=True, type='char',
 
131
                                        string='Customer Id'),
 
132
        'customer_email': fields.function(_customer_email, method=True,
 
133
                                        type='char', string='Customer Email'),
70
134
        'action_time': fields.datetime('Action Time', select="1"),
71
135
        'source': fields.selection(_SOURCES, 'Source', required=True),
72
136
        'error_msg': fields.text('System Message'),
73
 
#        'is_global': fields.boolean('Global Workitem'),
74
137
        'is_preview': fields.boolean('Document Preview Workitem'),
75
138
        'tr_from_id': fields.many2one('dm.offer.step.transition', 
76
139
                                      'Source Transition',
82
145
    _defaults = {
83
146
        'source': lambda *a: 'address_id',
84
147
        'state': lambda *a: 'pending',
85
 
#        'is_global': lambda *a: False,
86
148
        'is_preview': lambda *a: False,
87
149
        'is_realtime': lambda *a: True,
88
150
        'action_time': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
95
157
            wi_res = self.run(cr, uid, obj, context=context)
96
158
        return id
97
159
 
98
 
    @tools.cache()
 
160
#    @tools.cache()
 
161
    @monitor
99
162
    def _check_sysmsg(self, cr, uid, code, context=None):
100
163
        """ Check action message code and set workitem log """
101
164
        sysmsg_id  = self.pool.get('dm.sysmsg').search(cr, uid, 
111
174
                    'msg': "An unknown error has occured : %s" % code,
112
175
                    'result': False}
113
176
 
114
 
 
 
177
    @monitor
115
178
    def run(self, cr, uid, wi, context={}):
116
179
        logger = netsvc.Logger()
117
180
        context['active_id'] = wi.id
149
212
                    break
150
213
 
151
214
            if tr_res:
152
 
                """ Execute server action """
 
215
                "Execute server action"
153
216
                wi_res = server_obj.run(cr, uid, [wi.step_id.action_id.id], context)
154
217
                """ Check returned code and set wi status """
155
218
                wi_status = self._check_sysmsg(cr, uid, wi_res['code'],
156
219
                                                context.copy())
157
220
 
158
 
                """ Set workitem state and message """
 
221
                "Set workitem state and message"
159
222
                wr = self.write(cr, uid, [wi.id], {'state': wi_status['state'],
160
223
                                                'error_msg':wi_status['msg']})
161
224
                done = wi_status['result']
162
 
                """ If workitem done then execute mail service action """
 
225
                "If workitem done then execute mail service action"
163
226
                if done:
164
227
                    doc_context = {}
165
228
                    doc_obj = self.pool.get(wi_res['model'])
180
243
                                                'error_msg':ms_status['msg']})
181
244
 
182
245
            else:
183
 
                """ Dont Execute Action if workitem is not to be processed """
 
246
                "Dont Execute Action if workitem is not to be processed"
184
247
                self.write(cr, uid, [wi.id], {'state': 'cancel',
185
248
                                    'error_msg': 'Cancelled by : %s'% act_step})
186
249
 
257
320
 
258
321
                """ Get workitems to process and run action """
259
322
                ids = self.search(cr, uid, [('state', '=', 'pending'),
260
 
                                            ('action_time',
261
 
                                '<=', time.strftime('%Y-%m-%d %H:%M:%S')),
 
323
                    ('action_time','<=', time.strftime('%Y-%m-%d %H:%M:%S')),
262
324
                    ('is_realtime', '=', False)])
263
325
                for wi in self.browse(cr, uid, ids[:MAX_SIZE], context=context):
264
326
                    wi_res = self.run(cr, uid, wi, context=context)
294
356
        'source': lambda *a: 'address_id',
295
357
    }
296
358
 
 
359
    @monitor
297
360
    def create(self, cr, uid, vals, context={}):
298
361
        id = super(dm_event,self).create(cr, uid, vals, context)
299
362
        obj = self.browse(cr, uid, id)
302
365
                               [('step_from_id', '=', obj.step_id.id),
303
366
                                ('condition_id', '=', obj.trigger_type_id.id)])
304
367
        if not tr_ids:
305
 
            netsvc.Logger().notifyChannel('dm event case', netsvc.LOG_WARNING, "There is no transition %s at this step : %s"% (obj.trigger_type_id.name, obj.step_id.code))
306
 
            raise osv.except_osv('Warning', "There is no outgoing transition %s at this step : %s"% (obj.trigger_type_id.name, obj.step_id.code))
 
368
            netsvc.Logger().notifyChannel('dm event case', netsvc.LOG_WARNING,
 
369
                "There is no transition %s at this step : %s"% (obj.trigger_type_id.name,
 
370
                    obj.step_id.code))
 
371
            raise osv.except_osv('Warning',
 
372
                "There is no outgoing transition %s at this step : %s"% (obj.trigger_type_id.name,
 
373
                    obj.step_id.code))
307
374
            return False
 
375
 
308
376
        for tr in self.pool.get('dm.offer.step.transition').browse(cr, uid,
309
377
                                                                   tr_ids):
310
378
            if obj.action_time: