Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion queue_job/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Job Queue",
"version": "13.0.3.6.0",
"version": "13.0.3.7.0",
"author": "Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/queue/queue_job",
"license": "LGPL-3",
Expand Down
9 changes: 9 additions & 0 deletions queue_job/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def store(self):
"date_enqueued": False,
"date_started": False,
"date_done": False,
"exec_time": False,
"eta": False,
"identity_key": False,
"worker_pid": self.worker_pid,
Expand All @@ -540,6 +541,8 @@ def store(self):
vals["date_started"] = self.date_started
if self.date_done:
vals["date_done"] = self.date_done
if self.exec_time:
vals["exec_time"] = self.exec_time
if self.eta:
vals["eta"] = self.eta
if self.identity_key:
Expand Down Expand Up @@ -661,6 +664,12 @@ def channel(self):
def channel(self, value):
self._channel = value

@property
def exec_time(self):
if self.date_done and self.date_started:
return (self.date_done - self.date_started).total_seconds()
return None

def set_pending(self, result=None, reset_retry=True):
self.state = PENDING
self.date_enqueued = None
Expand Down
35 changes: 35 additions & 0 deletions queue_job/migrations/13.0.3.7.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

import logging

from odoo.tools.sql import column_exists

_logger = logging.getLogger(__name__)


def migrate(cr, version):
if not column_exists(cr, "queue_job", "exec_time"):
# Disable trigger otherwise the update takes ages.
cr.execute(
"""
ALTER TABLE queue_job DISABLE TRIGGER queue_job_notify;
"""
)
cr.execute(
"""
ALTER TABLE queue_job ADD COLUMN exec_time double precision DEFAULT 0;
"""
)
cr.execute(
"""
UPDATE
queue_job
SET
exec_time = EXTRACT(EPOCH FROM (date_done - date_started));
"""
)
cr.execute(
"""
ALTER TABLE queue_job ENABLE TRIGGER queue_job_notify;
"""
)
5 changes: 5 additions & 0 deletions queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class QueueJob(models.Model):
date_started = fields.Datetime(string="Start Date", readonly=True)
date_enqueued = fields.Datetime(string="Enqueue Time", readonly=True)
date_done = fields.Datetime(readonly=True)
exec_time = fields.Float(
string="Execution Time (avg)",
group_operator="avg",
help="Time required to execute this job in seconds. Average when grouped.",
)

eta = fields.Datetime(string="Execute only after")
retry = fields.Integer(string="Current try")
Expand Down
26 changes: 25 additions & 1 deletion queue_job/views/queue_job_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<field name="date_enqueued" />
<field name="date_started" />
<field name="date_done" />
<!-- Do not use float_time as it does not work properly -->
<field name="exec_time" string="Time (s)" />
</group>
</group>
<group colspan="4">
Expand Down Expand Up @@ -107,12 +109,34 @@
<field name="eta" />
<field name="date_created" />
<field name="date_done" />
<field name="exec_time" widget="float_time" />
<field name="uuid" />
<field name="channel" />
<field name="company_id" groups="base.group_multi_company" />
</tree>
</field>
</record>
<record id="view_queue_job_pivot" model="ir.ui.view">
<field name="name">queue.job.pivot</field>
<field name="model">queue.job</field>
<field name="arch" type="xml">
<pivot string="Jobs">
<field name="exec_time" type="measure" />
<field name="job_function_id" type="row" />
<field name="date_done" type="col" interval="week" />
</pivot>
</field>
</record>
<record id="view_queue_job_graph" model="ir.ui.view">
<field name="name">queue.job.graph</field>
<field name="model">queue.job</field>
<field name="arch" type="xml">
<graph string="Jobs">
<field name="job_function_id" type="row" />
<field name="exec_time" type="measure" />
</graph>
</field>
</record>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a pivot view gives better information:

<pivot string="Queue Job">
    <field name="exec_time" type="measure"/>
    <field name="job_function_id" type="row"/>
    <field name="date_done" type="col" interval="week"/>
</pivot>

Example:
Example of pivot view

Note: in this example, I used group_operator="avg" on the field, which I'm not sure we want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The average is really cool on this view as we can see if it grows over time...

Copy link
Contributor Author

@simahawk simahawk Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. At least is a great start!

<record id="view_queue_job_search" model="ir.ui.view">
<field name="name">queue.job.search</field>
<field name="model">queue.job</field>
Expand Down Expand Up @@ -178,7 +202,7 @@
<record id="action_queue_job" model="ir.actions.act_window">
<field name="name">Jobs</field>
<field name="res_model">queue.job</field>
<field name="view_mode">tree,form</field>
<field name="view_mode">tree,form,pivot,graph</field>
<field name="context">{'search_default_pending': 1,
'search_default_enqueued': 1,
'search_default_started': 1,
Expand Down
3 changes: 3 additions & 0 deletions test_queue_job/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def test_worker_pid(self):

def test_set_done(self):
job_a = Job(self.method)
job_a.date_started = datetime(2015, 3, 15, 16, 40, 0)
datetime_path = "odoo.addons.queue_job.job.datetime"
with mock.patch(datetime_path, autospec=True) as mock_datetime:
mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0)
Expand All @@ -157,6 +158,7 @@ def test_set_done(self):
self.assertEquals(job_a.state, DONE)
self.assertEquals(job_a.result, "test")
self.assertEquals(job_a.date_done, datetime(2015, 3, 15, 16, 41, 0))
self.assertEquals(job_a.exec_time, 60.0)
self.assertFalse(job_a.exc_info)

def test_set_failed(self):
Expand Down Expand Up @@ -233,6 +235,7 @@ def test_read(self):
self.assertAlmostEqual(job_read.date_started, test_date, delta=delta)
self.assertAlmostEqual(job_read.date_enqueued, test_date, delta=delta)
self.assertAlmostEqual(job_read.date_done, test_date, delta=delta)
self.assertAlmostEqual(job_read.exec_time, 0.0)

def test_job_unlinked(self):
test_job = Job(self.method, args=("o", "k"), kwargs={"c": "!"})
Expand Down