[IMP] maintenance_service_http_monitoring: rework maintenance.request creation
Previously, a single ``maintenance.request`` was created per equipment,
regardless of how many services were down on that equipment. The name
was ``[HTTP KO] {equipment.name}`` and deduplication relied on a
name+date+equipment search that was fragile (manual clear of the field
would lose the reference to an existing open request).
This commit reworks the whole creation logic:
- **1 request per KO service** instead of 1 per equipment. Each failing
``service.instance`` gets its own ``maintenance.request``, allowing
fine-grained tracking and independent resolution.
- **Request name** is now ``[HTTP KO] {service_url}``, making it
immediately identifiable without opening the record.
- **Description** includes the error detail: ``HTTP {status_code}`` for
HTTP errors, or a human-readable network error label when
``last_http_status_code == -1`` (timeout / DNS / SSL failures).
- **Deduplication** is now based solely on whether an open (non-done)
``maintenance.request`` already exists on the ``service.instance``
via the new ``http_maintenance_request`` field. No date boundary —
as long as the request is open, no new one is created.
- **``http_maintenance_request``** field moved from
``maintenance.equipment`` to ``service.instance``, where it belongs
given the 1-request-per-service model. It is exposed as an optional
hidden column in the service instance list view.
- ``_build_ko_services_description()`` is removed (no longer needed).
- ``create_http_maintenance_request()`` now receives a single
``service.instance`` recordset instead of a list.
Tests updated accordingly (14 tests total):
- Tests 2, 4, 9, 10, 12, 13 now assert on
``service_instance.http_maintenance_request``.
- Test 2 also verifies the request name contains the service URL and
the description contains the HTTP status code.
- New test 14 asserts that two KO services on the same equipment
produce two distinct requests with the correct names.
This commit is contained in:
@@ -27,11 +27,6 @@ class MaintenanceEquipment(models.Model):
|
||||
readonly=True,
|
||||
help="Computed from start + configured duration",
|
||||
)
|
||||
http_maintenance_request = fields.Many2one(
|
||||
"maintenance.request",
|
||||
string="HTTP Maintenance Request",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
def action_activate_maintenance_mode(self):
|
||||
for rec in self:
|
||||
@@ -72,31 +67,30 @@ class MaintenanceEquipment(models.Model):
|
||||
)
|
||||
expired.action_deactivate_maintenance_mode()
|
||||
|
||||
def create_http_maintenance_request(self, ko_services):
|
||||
def create_http_maintenance_request(self, ko_service):
|
||||
"""
|
||||
Create or return the open maintenance.request for a single KO service.
|
||||
|
||||
Deduplication: if ko_service already has an open (non-done) request,
|
||||
return it without creating a new one.
|
||||
"""
|
||||
self.ensure_one()
|
||||
today = fields.Date.context_today(self)
|
||||
name = f"[HTTP KO] {self.name}"
|
||||
domain = [
|
||||
("name", "=", name),
|
||||
("equipment_id", "=", self.id),
|
||||
("maintenance_type", "=", "corrective"),
|
||||
("create_date", ">=", f"{today} 00:00:00"),
|
||||
("create_date", "<=", f"{today} 23:59:59"),
|
||||
]
|
||||
existing = self.env["maintenance.request"].search(domain, limit=1)
|
||||
# Check if a task with same name already exist for the day, if it’s the case : skip
|
||||
if existing:
|
||||
self.http_maintenance_request = existing.id
|
||||
existing = ko_service.http_maintenance_request
|
||||
if existing and not existing.stage_id.done:
|
||||
return existing
|
||||
request = self.http_maintenance_request
|
||||
if request and not request.stage_id.done:
|
||||
return request
|
||||
status_code = ko_service.last_http_status_code
|
||||
if status_code == -1:
|
||||
error_detail = "Erreur réseau (timeout / DNS / SSL)"
|
||||
else:
|
||||
error_detail = f"HTTP {status_code}"
|
||||
name = f"[HTTP KO] {ko_service.service_url}"
|
||||
description = f"Service KO: {ko_service.service_url}\n{error_detail}"
|
||||
vals = {
|
||||
"name": name,
|
||||
"equipment_id": self.id,
|
||||
"priority": "2",
|
||||
"maintenance_type": "corrective",
|
||||
"description": self._build_ko_services_description(ko_services),
|
||||
"description": description,
|
||||
}
|
||||
if self.employee_id:
|
||||
vals["employee_id"] = self.employee_id.id
|
||||
@@ -109,8 +103,8 @@ class MaintenanceEquipment(models.Model):
|
||||
if team:
|
||||
vals["maintenance_team_id"] = team.id
|
||||
request = self.env["maintenance.request"].create(vals)
|
||||
self.http_maintenance_request = request.id
|
||||
self._notify_webhook(request, ko_services)
|
||||
ko_service.http_maintenance_request = request.id
|
||||
self._notify_webhook(request, ko_service)
|
||||
return request
|
||||
|
||||
def _notify_webhook(self, request, ko_service):
|
||||
@@ -156,7 +150,3 @@ class MaintenanceEquipment(models.Model):
|
||||
request.id,
|
||||
e,
|
||||
)
|
||||
|
||||
def _build_ko_services_description(self, ko_services):
|
||||
lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services]
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -31,6 +31,11 @@ class ServiceInstance(models.Model):
|
||||
readonly=True,
|
||||
default=True,
|
||||
)
|
||||
http_maintenance_request = fields.Many2one(
|
||||
"maintenance.request",
|
||||
string="HTTP Maintenance Request",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
def check_http_status(self):
|
||||
"""
|
||||
@@ -98,4 +103,4 @@ class ServiceInstance(models.Model):
|
||||
ko_confirmed = ko_after_pass1.check_http_status()
|
||||
|
||||
for service in ko_confirmed:
|
||||
service.equipment_id.create_http_maintenance_request([service])
|
||||
service.equipment_id.create_http_maintenance_request(service)
|
||||
|
||||
Reference in New Issue
Block a user