diff --git a/__manifest__.py b/__manifest__.py index e4716d60f30774088d41f8e82f4bf126a5b6371d..ab2b29862001c576a2ddd5ab621faa87174cf39a 100644 --- a/__manifest__.py +++ b/__manifest__.py @@ -5,7 +5,7 @@ "website": "https://www.le-filament.com", "version": "14.0.1.0.1", "license": "AGPL-3", - "depends": ["acc_operation"], + "depends": ["acc_operation", "queue_job_batch"], "data": [ "security/ir.model.access.csv", # datas diff --git a/models/acc_operation.py b/models/acc_operation.py index c6d89c349fbcdde7ce2e9659fefa76d2383b5f44..6d10f9c7fb6bfc46aaaff15ad6ee6a6df1397c90 100644 --- a/models/acc_operation.py +++ b/models/acc_operation.py @@ -1,7 +1,7 @@ # Copyright 2021 Le Filament (<http://www.le-filament.com>) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from datetime import date, datetime +from datetime import date from dateutil.relativedelta import relativedelta @@ -43,25 +43,20 @@ class AccOperation(models.Model): """This method is called from a cron job. It is used to get data from Enedis with API. """ + # TODO: Optimiser fonction CRON + # day = (date.today() - relativedelta(days=10)).day + # search by day (integer computed) + day_birthday = date.today() - relativedelta(months=1, days=10) records = self.search( [ - ("date_start_contract", "<", date.today()), + ("birthday_date", "=", day_birthday.day), ("client_id", "!=", False), ("secret_id", "!=", False), - ("active", "=", True), ] ) for rec in records: - d = rec.date_start_contract + relativedelta(days=10) - if d.day == date.today().day: - date_today_10 = date.today() - relativedelta(days=10) - first_date = datetime( - date_today_10.year, - date_today_10.month - 1, - rec.date_start_contract.day, - ) - last_date = first_date + relativedelta(months=1, days=-1) - rec.get_curves(first_date.date(), last_date.date()) + last_date = day_birthday + relativedelta(months=1, days=-1) + rec.get_curves(day_birthday, last_date.date()) # ------------------------------------------------------ # Actions @@ -70,17 +65,31 @@ class AccOperation(models.Model): # Ask token to API token = self.access_token() + # Création du lot + batch_name = ( + "Courbes du " + + str(date_start) + + " au " + + str(date_end) + + " - Opération :" + + str(self.name) + ) + batch = self.env["queue.job.batch"].get_new_batch(batch_name) # Load consommation data by PRM for delivery_counter_id in self.acc_delivery_ids: - self.definitive_load_curves( + # Mise en file d'attente des appels API consommateur + self.with_context(job_batch=batch).with_delay().definitive_load_curves( date_start, date_end, delivery_counter_id, token=token ) # Load production data by PRM for injection_counter_id in self.acc_injection_ids: - self.definitive_load_curves( + # Mise en file d'attente des appels API producteur + self.with_context(job_batch=batch).with_delay().definitive_load_curves( date_start, date_end, injection_counter_id, token=token ) + # lancement de la file d'attente + batch.enqueue() def get_perimeter(self): for operation in self: