From 6567f1ec53ff2fe484f6d7c98fa907e4e7fe0e19 Mon Sep 17 00:00:00 2001 From: benjamin <benjamin@le-filament.com> Date: Wed, 17 May 2023 18:12:19 +0200 Subject: [PATCH] [update] queue management --- __manifest__.py | 1 + data/queue_data.xml | 26 +++++++++++++++++++++ models/hall_flow_log.py | 6 ++--- models/hall_flow_sensor.py | 4 ++-- models/hall_flow_zone.py | 46 +++++++++++++++++++++----------------- 5 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 data/queue_data.xml diff --git a/__manifest__.py b/__manifest__.py index 84eb634..4978c10 100644 --- a/__manifest__.py +++ b/__manifest__.py @@ -11,6 +11,7 @@ "security/ir.model.access.csv", # datas "data/cron_import_data_wts.xml", + "data/queue_data.xml", # views "views/hall.xml", "views/hall_flow.xml", diff --git a/data/queue_data.xml b/data/queue_data.xml new file mode 100644 index 0000000..6d03a6f --- /dev/null +++ b/data/queue_data.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <record id="channel_wts" model="queue.job.channel"> + <field name="name">wts_channel</field> + <field name="parent_id" ref="queue_job.channel_root" /> + </record> + + <record id="job_function_wts_create_visitor" model="queue.job.function"> + <field name="model_id" ref="hall_flow_whattheshop.model_hall_flow_zone" /> + <field name="method">_wts_create_visitor</field> + <field name="channel_id" ref="channel_wts" /> + <field name="retry_pattern" eval="{1: 10, 5: 20, 10: 60,}" /> + </record> + <record id="job_function_wts_wts_create_passerby" model="queue.job.function"> + <field name="model_id" ref="hall_flow_whattheshop.model_hall_flow_zone" /> + <field name="method">_wts_create_passerby</field> + <field name="channel_id" ref="channel_wts" /> + <field name="retry_pattern" eval="{1: 10, 5: 20, 10: 60,}" /> + </record> + <record id="job_function_wts_create_inout" model="queue.job.function"> + <field name="model_id" ref="hall_flow_whattheshop.model_hall_flow_sensor" /> + <field name="method">_wts_create_inout</field> + <field name="channel_id" ref="channel_wts" /> + <field name="retry_pattern" eval="{1: 10, 5: 20, 10: 60,}" /> + </record> +</odoo> diff --git a/models/hall_flow_log.py b/models/hall_flow_log.py index b3ad3dd..755ac53 100644 --- a/models/hall_flow_log.py +++ b/models/hall_flow_log.py @@ -71,18 +71,18 @@ class HallFlowLog(models.Model): if delta_days > 0: start_passerby = start_date for day in range(0, delta_days): - zone.with_delay(description=pass_desc)._wts_create_passerby( + zone.with_delay(description=pass_desc, max_retries=10)._wts_create_passerby( start_passerby, end_date, log_id ) start_passerby = start_passerby + relativedelta(days=1, hours=1) else: - zone.with_delay(description=pass_desc)._wts_create_passerby( + zone.with_delay(description=pass_desc, max_retries=10)._wts_create_passerby( start_date, end_date, log_id ) # Retrieve In/Out datas for sensor in zone.sensor_ids.filtered(lambda s: s.sensor_type == "sensor"): description = log_id.hall_id.name + " - " + sensor.name - sensor.with_delay(description=description)._wts_create_inout( + sensor.with_delay(description=description, max_retries=10)._wts_create_inout( start_date, end_date, log_id ) diff --git a/models/hall_flow_sensor.py b/models/hall_flow_sensor.py index 5b5f3e4..6a86e5a 100644 --- a/models/hall_flow_sensor.py +++ b/models/hall_flow_sensor.py @@ -7,9 +7,9 @@ from odoo import fields, models from odoo.addons.api_connector.tools.date_utils import local_to_utc -class HallFlowZone(models.Model): +class HallFlowSensor(models.Model): _name = "hall.flow.sensor" - _description = "Hall Zone Management" + _description = "Hall Sensor Management" # ------------------------------------------------------ # Fields declaration diff --git a/models/hall_flow_zone.py b/models/hall_flow_zone.py index 506b549..d39faf9 100644 --- a/models/hall_flow_zone.py +++ b/models/hall_flow_zone.py @@ -5,6 +5,7 @@ from datetime import datetime from odoo import fields, models from odoo.addons.api_connector.tools.date_utils import local_to_utc +from odoo.addons.queue_job.exception import FailedJobError class HallFlowZone(models.Model): @@ -100,27 +101,30 @@ class HallFlowZone(models.Model): flow_ids = self.env["hall.flow"] backend_id = log_id.hall_id.backend_id for zone in self: - visitor_datas = backend_id._get_wifi_datas( - url="/visitor/counthourdetails", - zone_ids=zone.mapped("wts_id"), - start_date=start_date, - end_date=end_date, - opening_time=opening_time, - ) - if visitor_datas: - for date, data in visitor_datas.items(): - flow_ids += flow_ids.sudo().create( - { - "date": local_to_utc( - datetime.strptime(date, "%Y-%m-%d %H:%M:%S"), zone.timezone - ), - "flow_type": "visitor", - "count": data, - "log_id": log_id.id, - "hall_id": log_id.hall_id.id, - "zone_id": zone.id, - } - ) + try: + visitor_datas = backend_id._get_wifi_datas( + url="/visitor/counthourdetails", + zone_ids=zone.mapped("wts_id"), + start_date=start_date, + end_date=end_date, + opening_time=opening_time, + ) + if visitor_datas: + for date, data in visitor_datas.items(): + flow_ids += flow_ids.sudo().create( + { + "date": local_to_utc( + datetime.strptime(date, "%Y-%m-%d %H:%M:%S"), zone.timezone + ), + "flow_type": "visitor", + "count": data, + "log_id": log_id.id, + "hall_id": log_id.hall_id.id, + "zone_id": zone.id, + } + ) + except Exception as e: + raise FailedJobError(e.__str__()) return flow_ids -- GitLab