Skip to content

Commit

Permalink
[IMP] report_async: Add schedule time to decide queue eta when async …
Browse files Browse the repository at this point in the history
…reporting
  • Loading branch information
HviorForgeFlow committed Mar 16, 2023
1 parent 36772dd commit 8ad731d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion report_async/models/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def report_action(self, docids, data=None, config=True):
rpt_async_id = res['context']['active_id']
report_async = self.env['report.async'].browse(rpt_async_id)
if res['report_type'] in REPORT_TYPES:
report_async.with_delay().run_report(
report_async.with_delay(eta=res['context'].get('eta', False)).run_report(
res['context'].get('active_ids', []), data,
self.id, self._uid)
return {}
Expand Down
13 changes: 13 additions & 0 deletions report_async/models/report_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

import base64
from datetime import datetime, timedelta
from odoo import api, fields, models, _
from odoo.tools.safe_eval import safe_eval
from odoo.exceptions import UserError
Expand Down Expand Up @@ -70,6 +71,8 @@ class ReportAsync(models.Model):
help="List all files created by this report background process",
)

schedule_time = fields.Char(string='Schedule time')

@api.multi
def _compute_job(self):
for rec in self:
Expand Down Expand Up @@ -110,6 +113,8 @@ def run_async(self):
result = action.read()[0]
ctx = safe_eval(result.get('context', {}))
ctx.update({'async_process': True})
if self.schedule_time:
ctx.update({'eta': self._get_next_schedule_time()})
result['context'] = ctx
return result

Expand Down Expand Up @@ -160,3 +165,11 @@ def _send_email(self, attachment):
template.send_mail(attachment.id,
notif_layout='mail.mail_notification_light',
force_send=False)

def _get_next_schedule_time(self):
target_time = datetime.strptime(self.schedule_time, "%H:%M").time()
now = datetime.now()
target_datetime = datetime.combine(now.date(), target_time)
if now.time() > target_time:
target_datetime += datetime.timedelta(days=1)
return target_datetime
2 changes: 2 additions & 0 deletions report_async/views/report_async.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
<field name="allow_async"/>
<field name="email_notify"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
<field name="schedule_time" placeholder="23:30"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
</group>
<group>
<field name="job_status"
Expand Down

0 comments on commit 8ad731d

Please sign in to comment.