Release: automated reports reopen closed tickets (#128) #129

Merged
jared merged 2 commits from development into main 2026-11-28 03:22:29 -05:00
3 changed files with 16 additions and 53 deletions

No files matched your search

+1 -1
View File
@@ -229,7 +229,7 @@ Content-Type: application/json
- **Source**: the API key's name (`gandalf`, `hwmonDaemon`, `thor`) is stored in `tickets.source` and shown as the creator instead of "System"; the dashboard has a Source filter. Comments this endpoint writes (escalation, recurrence) are authored by the key name too. Tickets from before #123: `php scripts/backfill_ticket_source.php --dry-run`, then without - **Source**: the API key's name (`gandalf`, `hwmonDaemon`, `thor`) is stored in `tickets.source` and shown as the creator instead of "System"; the dashboard has a Source filter. Comments this endpoint writes (escalation, recurrence) are authored by the key name too. Tickets from before #123: `php scripts/backfill_ticket_source.php --dry-run`, then without
- **`dedup_key` (preferred, #123)**: the reporter decides what "the same problem" is by sending `dedup_key` (`scope:subject:kind`, 1-128 of `A-Za-z0-9_.:-`, case-sensitive, e.g. `host:compute-storage-01:oom`, `cluster:ceph:BLUESTORE_SLOW_OP_ALERT`). The hash is then `sha256("key:" + dedup_key)` and the title is not looked at; an invalid key is a 400. When an app starts sending keys, give its existing tickets theirs with `php scripts/backfill_dedup_keys.php --map=file.json [--dry-run]` (the app supplies `{ticket_id: dedup_key}`), or its next reports open duplicates - **`dedup_key` (preferred, #123)**: the reporter decides what "the same problem" is by sending `dedup_key` (`scope:subject:kind`, 1-128 of `A-Za-z0-9_.:-`, case-sensitive, e.g. `host:compute-storage-01:oom`, `cluster:ceph:BLUESTORE_SLOW_OP_ALERT`). The hash is then `sha256("key:" + dedup_key)` and the title is not looked at; an invalid key is a 400. When an app starts sending keys, give its existing tickets theirs with `php scripts/backfill_dedup_keys.php --map=file.json [--dry-run]` (the app supplies `{ticket_id: dedup_key}`), or its next reports open duplicates
- **Deduplication without a key**: Generates a SHA-256 hash from the issue category, hostname, and device (no time window). A repeat alert matching an existing **open** ticket updates its title/description and escalates the priority if the condition worsened - **Deduplication without a key**: Generates a SHA-256 hash from the issue category, hostname, and device (no time window). A repeat alert matching an existing **open** ticket updates its title/description and escalates the priority if the condition worsened
- **Recurrence (every reporter, keyed or not)**: a report matching a **Closed** ticket that closed less than `AUTO_REOPEN_WINDOW_DAYS` (default 7) ago reopens it, if the Workflow Designer allows an unattended reopen (no admin, no comment); otherwise the recurrence is noted on it and it stays Closed (`recurrence_noted`). A ticket closed longer ago is left alone: a **new ticket** takes over the dedup slot, carries the same `dedup_key`, records the old one in `recurrence_of`, is linked `relates_to` it, and gets a history comment (times seen in the last 7 and 30 days, when it last cleared). Response `action`: `created`, `updated`, `deduplicated`, `reopened`, `recurrence_noted` or `new_recurrence` (with `previous_ticket_id`) - **Recurrence (every reporter, keyed or not)**: a report matching a **Closed** ticket that closed less than `AUTO_REOPEN_WINDOW_DAYS` (default 7) ago reopens it to Open. Automated reopens skip the Workflow Designer (its Closed → Open rule is for people; prod requires admin + comment there, which no reporter can give); the audit entry says so. A ticket closed longer ago is left alone: a **new ticket** takes over the dedup slot, carries the same `dedup_key`, records the old one in `recurrence_of`, is linked `relates_to` it, and gets a history comment (times seen in the last 7 and 30 days, when it last cleared). Response `action`: `created`, `updated`, `deduplicated`, `reopened` or `new_recurrence` (with `previous_ticket_id`). (`recurrence_noted` is no longer returned.)
- Cluster-wide issues (Ceph health, etc.) deduplicate across all nodes (hostname excluded from hash) - Cluster-wide issues (Ceph health, etc.) deduplicate across all nodes (hostname excluded from hash)
- Automated network tickets are keyed per problem, not just per host: on the optional `check_code` payload field when sent (it must name the instance, e.g. `iface_down:enp3s0`), otherwise on the title text outside the `[tags]`. After deploying this change (#116), run `php scripts/rehash_network_tickets.php --dry-run`, then without `--dry-run`, so open network tickets keep matching their alerts. The hash logic lives in `helpers/TicketHash.php` - Automated network tickets are keyed per problem, not just per host: on the optional `check_code` payload field when sent (it must name the instance, e.g. `iface_down:enp3s0`), otherwise on the title text outside the `[tags]`. After deploying this change (#116), run `php scripts/rehash_network_tickets.php --dry-run`, then without `--dry-run`, so open network tickets keep matching their alerts. The hash logic lives in `helpers/TicketHash.php`
- `description` is rendered as markdown on the ticket page, the same as markdown comments (#114). Reporters should send markdown: `##` sections, tables for key/value data and fenced code blocks for raw command output. Fixed-width box layouts lose their alignment. - `description` is rendered as markdown on the ticket page, the same as markdown comments (#114). Reporters should send markdown: `##` sections, tables for key/value data and fenced code blocks for raw command output. Fixed-width box layouts lose their alignment.
+13 -50
View File
@@ -43,7 +43,6 @@ require_once __DIR__ . '/middleware/ApiKeyAuth.php';
require_once __DIR__ . '/models/AuditLogModel.php'; require_once __DIR__ . '/models/AuditLogModel.php';
require_once __DIR__ . '/models/StatsModel.php'; require_once __DIR__ . '/models/StatsModel.php';
require_once __DIR__ . '/models/TicketModel.php'; require_once __DIR__ . '/models/TicketModel.php';
require_once __DIR__ . '/models/WorkflowModel.php';
require_once __DIR__ . '/helpers/UrlHelper.php'; require_once __DIR__ . '/helpers/UrlHelper.php';
require_once __DIR__ . '/helpers/TicketHash.php'; require_once __DIR__ . '/helpers/TicketHash.php';
require_once __DIR__ . '/services/AutoTicketService.php'; require_once __DIR__ . '/services/AutoTicketService.php';
@@ -260,28 +259,12 @@ for ($dedupAttempt = 1; $dedupAttempt <= $maxDedupAttempts; $dedupAttempt++) {
exit; exit;
} }
// Ticket closed inside the reopen window: reopen it and add a recurrence comment. Route // Ticket closed inside the reopen window: reopen it and add a recurrence
// through the Workflow Designer like every other status-write path in // comment. Automated reporters reopen regardless of the Workflow
// the app, rather than forcing status='Open' via raw SQL regardless of // Designer (operator decision, 2026-11-28): prod's Closed -> Open needs
// configured transition rules. // admin + comment, which no unattended reporter can give, so the rule
$workflowModel = new WorkflowModel($conn); // only ever produced a "could not be reopened" note on every report
$reopenStatus = 'Open'; // (2,110 of them before #123). People still go through the workflow.
if (!$workflowModel->isTransitionAllowed('Closed', 'Open', false)) {
// Direct Closed->Open isn't configured — fall back to any transition
// the Workflow Designer does allow from Closed that this unattended,
// non-admin automation can actually satisfy (no comment prompt, no
// admin elevation). If even that doesn't exist, leave the ticket
// Closed rather than force an unconfigured state.
$reopenStatus = null;
foreach ($workflowModel->getAllowedTransitions('Closed') as $transition) {
if (!$transition['requires_comment'] && !$transition['requires_admin']) {
$reopenStatus = $transition['to_status'];
break;
}
}
}
if ($reopenStatus !== null) {
$ticketModel = new TicketModel($conn); $ticketModel = new TicketModel($conn);
$ticketModel->updateTicket([ $ticketModel->updateTicket([
'ticket_id' => $existingId, 'ticket_id' => $existingId,
@@ -289,23 +272,13 @@ for ($dedupAttempt = 1; $dedupAttempt <= $maxDedupAttempts; $dedupAttempt++) {
'description' => $description, 'description' => $description,
'category' => $category, 'category' => $category,
'type' => $type, 'type' => $type,
'status' => $reopenStatus, 'status' => 'Open',
'priority' => $priority, 'priority' => $priority,
], $userId); ], $userId);
} else {
error_log("create_ticket_api: {$source} recurrence for ticket $existingId: "
. "no admin-free, comment-free transition from Closed is configured; leaving ticket Closed");
}
$commentText = "**Issue recurred, ticket reopened automatically.**\n\n" $commentText = "**Issue recurred, ticket reopened automatically.**\n\n"
. "{$source} detected this condition again. The ticket description reflects the " . "{$source} detected this condition again. The ticket description reflects the "
. "latest report; see this comment's timestamp for when the issue recurred."; . "latest report; see this comment's timestamp for when the issue recurred.";
if ($reopenStatus === null) {
$commentText = "**Issue recurred, but the ticket could not be reopened automatically.**\n\n"
. "{$source} detected this condition again. No Workflow Designer transition from "
. "Closed is configured that this automation can perform unattended (no comment/admin "
. "requirement); the ticket remains Closed. Please review and reopen manually if appropriate.";
}
$commentStmt = $conn->prepare( $commentStmt = $conn->prepare(
"INSERT INTO ticket_comments (ticket_id, user_id, user_name, comment_text, markdown_enabled) VALUES (?, ?, ?, ?, 1)" "INSERT INTO ticket_comments (ticket_id, user_id, user_name, comment_text, markdown_enabled) VALUES (?, ?, ?, ?, 1)"
); );
@@ -313,41 +286,31 @@ for ($dedupAttempt = 1; $dedupAttempt <= $maxDedupAttempts; $dedupAttempt++) {
$commentStmt->execute(); $commentStmt->execute();
$commentStmt->close(); $commentStmt->close();
if ($reopenStatus !== null) {
$auditLog->log($userId, 'update', 'ticket', $existingId, [ $auditLog->log($userId, 'update', 'ticket', $existingId, [
'status' => ['from' => 'Closed', 'to' => $reopenStatus], 'status' => ['from' => 'Closed', 'to' => 'Open'],
'reason' => "auto-reopened by {$source} (issue recurred)", 'reason' => "auto-reopened by {$source} (issue recurred; automated reopen skips the Workflow Designer)",
]); ]);
// Ticket reopened — refresh dashboard stats. // Ticket reopened: refresh dashboard stats.
(new StatsModel($conn))->invalidateCache(); (new StatsModel($conn))->invalidateCache();
} else {
$auditLog->log($userId, 'update', 'ticket', $existingId, [
'reason' => "{$source} recurrence detected but no valid reopen transition configured; ticket left Closed",
]);
}
$conn->commit(); $conn->commit();
Database::close(); Database::close();
if ($reopenStatus !== null) {
require_once __DIR__ . '/helpers/NotificationHelper.php'; require_once __DIR__ . '/helpers/NotificationHelper.php';
NotificationHelper::sendTicketNotification($existingId, [ NotificationHelper::sendTicketNotification($existingId, [
'title' => $title, 'title' => $title,
'priority' => $priority, 'priority' => $priority,
'category' => $category, 'category' => $category,
'type' => $type, 'type' => $type,
'status' => $reopenStatus, 'status' => 'Open',
], 'automated'); ], 'automated');
}
echo json_encode([ echo json_encode([
'success' => true, 'success' => true,
'ticket_id' => $existingId, 'ticket_id' => $existingId,
'message' => $reopenStatus !== null 'message' => 'Existing closed ticket reopened',
? 'Existing closed ticket reopened' 'action' => 'reopened',
: 'Recurrence noted; ticket left Closed (no valid workflow transition configured)',
'action' => $reopenStatus !== null ? 'reopened' : 'recurrence_noted',
]); ]);
exit; exit;
} }
+2 -2
View File
@@ -9,8 +9,8 @@
* transaction; nothing here commits or rolls back. * transaction; nothing here commits or rolls back.
* *
* Recurrence rule, the same for every reporter: a report whose dedup hash * Recurrence rule, the same for every reporter: a report whose dedup hash
* matches a Closed ticket reopens it when it closed less than * matches a Closed ticket reopens it (skipping the Workflow Designer) when it
* AUTO_REOPEN_WINDOW_DAYS ago. Otherwise a new ticket takes over the hash, is * closed less than AUTO_REOPEN_WINDOW_DAYS ago. Otherwise a new ticket takes over the hash, is
* linked relates_to the old one, records it in recurrence_of, and gets a * linked relates_to the old one, records it in recurrence_of, and gets a
* history comment. tickets.hash is UNIQUE, so the old ticket gives up its hash * history comment. tickets.hash is UNIQUE, so the old ticket gives up its hash
* (set to NULL) and keeps its dedup_key. * (set to NULL) and keeps its dedup_key.