Files
tinker_tickets/scripts/rehash_network_tickets.php
T

79 lines
2.6 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* Re-key open automated network tickets after #116.
*
* Network tickets used to hash on host alone, so every network alert for a
* host shared one ticket. They now hash on host + the problem's heading (see
* helpers/TicketHash.php). Without this script, each open network ticket
* keeps its old hash and its next alert opens a duplicate instead of
* updating it. Run once after deploying #116:
*
* php scripts/rehash_network_tickets.php --dry-run # report only
* php scripts/rehash_network_tickets.php
*
* Closed tickets keep their old hash on purpose: they stop catching
* unrelated alerts. A ticket whose new hash already belongs to another ticket
* is skipped and reported (resolve it by hand). Safe to re-run.
*/
if (php_sapi_name() !== 'cli') {
http_response_code(403);
exit('CLI access only');
}
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/helpers/Database.php';
require_once dirname(__DIR__) . '/helpers/TicketHash.php';
$dryRun = in_array('--dry-run', $argv, true);
$conn = Database::getConnection();
$rows = $conn->query("SELECT ticket_id, title, hash FROM tickets WHERE status <> 'Closed' ORDER BY ticket_id")
->fetch_all(MYSQLI_ASSOC);
$counts = ['updated' => 0, 'unchanged' => 0, 'conflict' => 0];
$lookup = $conn->prepare("SELECT ticket_id FROM tickets WHERE hash = ? AND ticket_id <> ? LIMIT 1");
$update = $conn->prepare("UPDATE tickets SET hash = ? WHERE ticket_id = ?");
foreach ($rows as $row) {
$data = ['title' => (string)$row['title']];
$components = TicketHash::components($data);
if ($components['issue_category'] !== 'network' || $components['source_type'] !== 'auto') {
continue;
}
$newHash = TicketHash::generate($data);
$id = $row['ticket_id'];
if ($newHash === $row['hash']) {
$counts['unchanged']++;
echo "unchanged $id {$row['title']}\n";
continue;
}
$lookup->bind_param('ss', $newHash, $id);
$lookup->execute();
$other = $lookup->get_result()->fetch_assoc();
if ($other) {
$counts['conflict']++;
echo "CONFLICT $id {$row['title']} (new hash already on ticket {$other['ticket_id']})\n";
continue;
}
if (!$dryRun) {
$update->bind_param('ss', $newHash, $id);
$update->execute();
}
$counts['updated']++;
echo ($dryRun ? 'would update' : 'updated ') . " $id {$row['title']}\n";
}
printf(
"%s%d updated, %d already current, %d conflicts\n",
$dryRun ? '[dry run] ' : '',
$counts['updated'],
$counts['unchanged'],
$counts['conflict']
);