Add query optimization and reliability improvements
- Consolidate StatsModel queries from 12 to 3 using conditional aggregation - Add input validation to DashboardController (sort columns, dates, priorities) - Combine getCategories/getTypes into single query - Add transaction support to BulkOperationsModel with atomic mode option - Add depth limit (20) to dependency cycle detection to prevent DoS - Add caching to UserModel.getAllGroups() with 5-minute TTL - Improve ticket ID generation with 50 attempts, exponential backoff, and fallback Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,38 +9,99 @@ class DashboardController {
|
|||||||
private $statsModel;
|
private $statsModel;
|
||||||
private $conn;
|
private $conn;
|
||||||
|
|
||||||
|
/** Valid sort columns (whitelist) */
|
||||||
|
private const VALID_SORT_COLUMNS = [
|
||||||
|
'ticket_id', 'title', 'status', 'priority', 'category', 'type',
|
||||||
|
'created_at', 'updated_at', 'assigned_to', 'created_by'
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Valid statuses */
|
||||||
|
private const VALID_STATUSES = ['Open', 'Pending', 'In Progress', 'Closed'];
|
||||||
|
|
||||||
public function __construct($conn) {
|
public function __construct($conn) {
|
||||||
$this->conn = $conn;
|
$this->conn = $conn;
|
||||||
$this->ticketModel = new TicketModel($conn);
|
$this->ticketModel = new TicketModel($conn);
|
||||||
$this->prefsModel = new UserPreferencesModel($conn);
|
$this->prefsModel = new UserPreferencesModel($conn);
|
||||||
$this->statsModel = new StatsModel($conn);
|
$this->statsModel = new StatsModel($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate and sanitize a date string
|
||||||
|
*/
|
||||||
|
private function validateDate(?string $date): ?string {
|
||||||
|
if (empty($date)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Check if it's a valid date format (YYYY-MM-DD)
|
||||||
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) && strtotime($date) !== false) {
|
||||||
|
return $date;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate priority value (1-5)
|
||||||
|
*/
|
||||||
|
private function validatePriority($priority): ?int {
|
||||||
|
if ($priority === null || $priority === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$val = (int)$priority;
|
||||||
|
return ($val >= 1 && $val <= 5) ? $val : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate user ID
|
||||||
|
*/
|
||||||
|
private function validateUserId($userId): ?int {
|
||||||
|
if ($userId === null || $userId === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$val = (int)$userId;
|
||||||
|
return ($val > 0) ? $val : null;
|
||||||
|
}
|
||||||
|
|
||||||
public function index() {
|
public function index() {
|
||||||
// Get user ID for preferences
|
// Get user ID for preferences
|
||||||
$userId = isset($_SESSION['user']['user_id']) ? $_SESSION['user']['user_id'] : null;
|
$userId = isset($_SESSION['user']['user_id']) ? $_SESSION['user']['user_id'] : null;
|
||||||
|
|
||||||
// Get query parameters
|
// Validate and sanitize page parameter
|
||||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
|
||||||
|
|
||||||
// Get rows per page from user preferences, fallback to cookie, then default
|
// Get rows per page from user preferences, fallback to cookie, then default
|
||||||
|
// Clamp to reasonable range (1-100)
|
||||||
$limit = 15;
|
$limit = 15;
|
||||||
if ($userId) {
|
if ($userId) {
|
||||||
$limit = (int)$this->prefsModel->getPreference($userId, 'rows_per_page', 15);
|
$limit = (int)$this->prefsModel->getPreference($userId, 'rows_per_page', 15);
|
||||||
} else if (isset($_COOKIE['ticketsPerPage'])) {
|
} else if (isset($_COOKIE['ticketsPerPage'])) {
|
||||||
$limit = (int)$_COOKIE['ticketsPerPage'];
|
$limit = (int)$_COOKIE['ticketsPerPage'];
|
||||||
}
|
}
|
||||||
|
$limit = max(1, min(100, $limit));
|
||||||
|
|
||||||
$sortColumn = isset($_GET['sort']) ? $_GET['sort'] : 'ticket_id';
|
// Validate sort column against whitelist
|
||||||
$sortDirection = isset($_GET['dir']) ? $_GET['dir'] : 'desc';
|
$sortColumn = isset($_GET['sort']) && in_array($_GET['sort'], self::VALID_SORT_COLUMNS, true)
|
||||||
$category = isset($_GET['category']) ? $_GET['category'] : null;
|
? $_GET['sort']
|
||||||
$type = isset($_GET['type']) ? $_GET['type'] : null;
|
: 'ticket_id';
|
||||||
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
|
|
||||||
|
// Validate sort direction
|
||||||
|
$sortDirection = isset($_GET['dir']) && strtolower($_GET['dir']) === 'asc' ? 'asc' : 'desc';
|
||||||
|
|
||||||
|
// Category and type are validated by the model (uses prepared statements)
|
||||||
|
$category = isset($_GET['category']) ? trim($_GET['category']) : null;
|
||||||
|
$type = isset($_GET['type']) ? trim($_GET['type']) : null;
|
||||||
|
|
||||||
|
// Sanitize search - limit length to prevent abuse
|
||||||
|
$search = isset($_GET['search']) ? substr(trim($_GET['search']), 0, 255) : null;
|
||||||
|
|
||||||
// Handle status filtering with user preferences
|
// Handle status filtering with user preferences
|
||||||
$status = null;
|
$status = null;
|
||||||
if (isset($_GET['status']) && !empty($_GET['status'])) {
|
if (isset($_GET['status']) && !empty($_GET['status'])) {
|
||||||
$status = $_GET['status'];
|
// Validate each status in the comma-separated list
|
||||||
|
$requestedStatuses = array_map('trim', explode(',', $_GET['status']));
|
||||||
|
$validStatuses = array_filter($requestedStatuses, function($s) {
|
||||||
|
return in_array($s, self::VALID_STATUSES, true);
|
||||||
|
});
|
||||||
|
$status = !empty($validStatuses) ? implode(',', $validStatuses) : null;
|
||||||
} else if (!isset($_GET['show_all'])) {
|
} else if (!isset($_GET['show_all'])) {
|
||||||
// Get default status filters from user preferences
|
// Get default status filters from user preferences
|
||||||
if ($userId) {
|
if ($userId) {
|
||||||
@@ -52,23 +113,41 @@ class DashboardController {
|
|||||||
}
|
}
|
||||||
// If $_GET['show_all'] exists or no status param with show_all, show all tickets (status = null)
|
// If $_GET['show_all'] exists or no status param with show_all, show all tickets (status = null)
|
||||||
|
|
||||||
// Build advanced search filters array
|
// Build and validate advanced search filters
|
||||||
$filters = [];
|
$filters = [];
|
||||||
if (isset($_GET['created_from'])) $filters['created_from'] = $_GET['created_from'];
|
|
||||||
if (isset($_GET['created_to'])) $filters['created_to'] = $_GET['created_to'];
|
// Validate date filters
|
||||||
if (isset($_GET['updated_from'])) $filters['updated_from'] = $_GET['updated_from'];
|
$createdFrom = $this->validateDate($_GET['created_from'] ?? null);
|
||||||
if (isset($_GET['updated_to'])) $filters['updated_to'] = $_GET['updated_to'];
|
$createdTo = $this->validateDate($_GET['created_to'] ?? null);
|
||||||
if (isset($_GET['priority_min'])) $filters['priority_min'] = $_GET['priority_min'];
|
$updatedFrom = $this->validateDate($_GET['updated_from'] ?? null);
|
||||||
if (isset($_GET['priority_max'])) $filters['priority_max'] = $_GET['priority_max'];
|
$updatedTo = $this->validateDate($_GET['updated_to'] ?? null);
|
||||||
if (isset($_GET['created_by'])) $filters['created_by'] = $_GET['created_by'];
|
|
||||||
if (isset($_GET['assigned_to'])) $filters['assigned_to'] = $_GET['assigned_to'];
|
if ($createdFrom) $filters['created_from'] = $createdFrom;
|
||||||
|
if ($createdTo) $filters['created_to'] = $createdTo;
|
||||||
|
if ($updatedFrom) $filters['updated_from'] = $updatedFrom;
|
||||||
|
if ($updatedTo) $filters['updated_to'] = $updatedTo;
|
||||||
|
|
||||||
|
// Validate priority filters
|
||||||
|
$priorityMin = $this->validatePriority($_GET['priority_min'] ?? null);
|
||||||
|
$priorityMax = $this->validatePriority($_GET['priority_max'] ?? null);
|
||||||
|
|
||||||
|
if ($priorityMin !== null) $filters['priority_min'] = $priorityMin;
|
||||||
|
if ($priorityMax !== null) $filters['priority_max'] = $priorityMax;
|
||||||
|
|
||||||
|
// Validate user ID filters
|
||||||
|
$createdBy = $this->validateUserId($_GET['created_by'] ?? null);
|
||||||
|
$assignedTo = $this->validateUserId($_GET['assigned_to'] ?? null);
|
||||||
|
|
||||||
|
if ($createdBy !== null) $filters['created_by'] = $createdBy;
|
||||||
|
if ($assignedTo !== null) $filters['assigned_to'] = $assignedTo;
|
||||||
|
|
||||||
// Get tickets with pagination, sorting, search, and advanced filters
|
// Get tickets with pagination, sorting, search, and advanced filters
|
||||||
$result = $this->ticketModel->getAllTickets($page, $limit, $status, $sortColumn, $sortDirection, $category, $type, $search, $filters);
|
$result = $this->ticketModel->getAllTickets($page, $limit, $status, $sortColumn, $sortDirection, $category, $type, $search, $filters);
|
||||||
|
|
||||||
// Get categories and types for filters
|
// Get categories and types for filters (single query)
|
||||||
$categories = $this->getCategories();
|
$filterOptions = $this->getCategoriesAndTypes();
|
||||||
$types = $this->getTypes();
|
$categories = $filterOptions['categories'];
|
||||||
|
$types = $filterOptions['types'];
|
||||||
|
|
||||||
// Extract data for the view
|
// Extract data for the view
|
||||||
$tickets = $result['tickets'];
|
$tickets = $result['tickets'];
|
||||||
@@ -82,30 +161,38 @@ class DashboardController {
|
|||||||
include 'views/DashboardView.php';
|
include 'views/DashboardView.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCategories() {
|
/**
|
||||||
$sql = "SELECT DISTINCT category FROM tickets WHERE category IS NOT NULL ORDER BY category";
|
* Get categories and types in a single query
|
||||||
$stmt = $this->conn->prepare($sql);
|
*
|
||||||
$stmt->execute();
|
* @return array ['categories' => [...], 'types' => [...]]
|
||||||
$result = $stmt->get_result();
|
*/
|
||||||
|
private function getCategoriesAndTypes(): array {
|
||||||
|
$sql = "SELECT 'category' as field, category as value FROM tickets WHERE category IS NOT NULL
|
||||||
|
UNION
|
||||||
|
SELECT 'type' as field, type as value FROM tickets WHERE type IS NOT NULL
|
||||||
|
ORDER BY field, value";
|
||||||
|
|
||||||
|
$result = $this->conn->query($sql);
|
||||||
$categories = [];
|
$categories = [];
|
||||||
|
$types = [];
|
||||||
|
|
||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
$categories[] = $row['category'];
|
if ($row['field'] === 'category' && !in_array($row['value'], $categories, true)) {
|
||||||
|
$categories[] = $row['value'];
|
||||||
|
} elseif ($row['field'] === 'type' && !in_array($row['value'], $types, true)) {
|
||||||
|
$types[] = $row['value'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$stmt->close();
|
|
||||||
return $categories;
|
return ['categories' => $categories, 'types' => $types];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTypes() {
|
private function getCategories(): array {
|
||||||
$sql = "SELECT DISTINCT type FROM tickets WHERE type IS NOT NULL ORDER BY type";
|
return $this->getCategoriesAndTypes()['categories'];
|
||||||
$stmt = $this->conn->prepare($sql);
|
}
|
||||||
$stmt->execute();
|
|
||||||
$result = $stmt->get_result();
|
private function getTypes(): array {
|
||||||
$types = [];
|
return $this->getCategoriesAndTypes()['types'];
|
||||||
while ($row = $result->fetch_assoc()) {
|
|
||||||
$types[] = $row['type'];
|
|
||||||
}
|
|
||||||
$stmt->close();
|
|
||||||
return $types;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -41,10 +41,14 @@ class BulkOperationsModel {
|
|||||||
/**
|
/**
|
||||||
* Process a bulk operation
|
* Process a bulk operation
|
||||||
*
|
*
|
||||||
|
* Uses database transaction to ensure atomicity - either all tickets
|
||||||
|
* are updated or none are (on failure, changes are rolled back).
|
||||||
|
*
|
||||||
* @param int $operationId Operation ID
|
* @param int $operationId Operation ID
|
||||||
|
* @param bool $atomic If true, rollback all changes on any failure
|
||||||
* @return array Result with processed and failed counts
|
* @return array Result with processed and failed counts
|
||||||
*/
|
*/
|
||||||
public function processBulkOperation($operationId) {
|
public function processBulkOperation($operationId, bool $atomic = false) {
|
||||||
// Get operation details
|
// Get operation details
|
||||||
$sql = "SELECT * FROM bulk_operations WHERE operation_id = ?";
|
$sql = "SELECT * FROM bulk_operations WHERE operation_id = ?";
|
||||||
$stmt = $this->conn->prepare($sql);
|
$stmt = $this->conn->prepare($sql);
|
||||||
@@ -62,6 +66,7 @@ class BulkOperationsModel {
|
|||||||
$parameters = $operation['parameters'] ? json_decode($operation['parameters'], true) : [];
|
$parameters = $operation['parameters'] ? json_decode($operation['parameters'], true) : [];
|
||||||
$processed = 0;
|
$processed = 0;
|
||||||
$failed = 0;
|
$failed = 0;
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
// Load required models
|
// Load required models
|
||||||
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
||||||
@@ -73,7 +78,11 @@ class BulkOperationsModel {
|
|||||||
// Batch load all tickets in one query to eliminate N+1 problem
|
// Batch load all tickets in one query to eliminate N+1 problem
|
||||||
$ticketsById = $ticketModel->getTicketsByIds($ticketIds);
|
$ticketsById = $ticketModel->getTicketsByIds($ticketIds);
|
||||||
|
|
||||||
foreach ($ticketIds as $ticketId) {
|
// Start transaction for data consistency
|
||||||
|
$this->conn->begin_transaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
foreach ($ticketIds as $ticketId) {
|
||||||
$ticketId = trim($ticketId);
|
$ticketId = trim($ticketId);
|
||||||
$success = false;
|
$success = false;
|
||||||
|
|
||||||
@@ -162,22 +171,66 @@ class BulkOperationsModel {
|
|||||||
$processed++;
|
$processed++;
|
||||||
} else {
|
} else {
|
||||||
$failed++;
|
$failed++;
|
||||||
|
$errors[] = "Ticket $ticketId: Update failed";
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$failed++;
|
$failed++;
|
||||||
|
$errors[] = "Ticket $ticketId: " . $e->getMessage();
|
||||||
error_log("Bulk operation error for ticket $ticketId: " . $e->getMessage());
|
error_log("Bulk operation error for ticket $ticketId: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If atomic mode and any failures, rollback everything
|
||||||
|
if ($atomic && $failed > 0) {
|
||||||
|
$this->conn->rollback();
|
||||||
|
error_log("Bulk operation $operationId rolled back due to $failed failures");
|
||||||
|
|
||||||
|
// Update operation status as failed
|
||||||
|
$sql = "UPDATE bulk_operations SET status = 'failed', processed_tickets = 0, failed_tickets = ?,
|
||||||
|
completed_at = NOW() WHERE operation_id = ?";
|
||||||
|
$stmt = $this->conn->prepare($sql);
|
||||||
|
$stmt->bind_param("ii", $failed, $operationId);
|
||||||
|
$stmt->execute();
|
||||||
|
$stmt->close();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'processed' => 0,
|
||||||
|
'failed' => $failed,
|
||||||
|
'rolled_back' => true,
|
||||||
|
'errors' => $errors
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit the transaction
|
||||||
|
$this->conn->commit();
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Rollback on any unexpected error
|
||||||
|
$this->conn->rollback();
|
||||||
|
error_log("Bulk operation $operationId failed with exception: " . $e->getMessage());
|
||||||
|
|
||||||
|
return [
|
||||||
|
'processed' => 0,
|
||||||
|
'failed' => count($ticketIds),
|
||||||
|
'error' => 'Transaction failed: ' . $e->getMessage(),
|
||||||
|
'rolled_back' => true
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update operation status
|
// Update operation status
|
||||||
$sql = "UPDATE bulk_operations SET status = 'completed', processed_tickets = ?, failed_tickets = ?,
|
$status = $failed > 0 ? 'completed_with_errors' : 'completed';
|
||||||
|
$sql = "UPDATE bulk_operations SET status = ?, processed_tickets = ?, failed_tickets = ?,
|
||||||
completed_at = NOW() WHERE operation_id = ?";
|
completed_at = NOW() WHERE operation_id = ?";
|
||||||
$stmt = $this->conn->prepare($sql);
|
$stmt = $this->conn->prepare($sql);
|
||||||
$stmt->bind_param("iii", $processed, $failed, $operationId);
|
$stmt->bind_param("siii", $status, $processed, $failed, $operationId);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
|
|
||||||
return ['processed' => $processed, 'failed' => $failed];
|
$result = ['processed' => $processed, 'failed' => $failed];
|
||||||
|
if (!empty($errors)) {
|
||||||
|
$result['errors'] = $errors;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -169,6 +169,9 @@ class DependencyModel {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Maximum depth for cycle detection to prevent DoS */
|
||||||
|
private const MAX_DEPENDENCY_DEPTH = 20;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if adding a dependency would create a cycle
|
* Check if adding a dependency would create a cycle
|
||||||
*
|
*
|
||||||
@@ -177,7 +180,7 @@ class DependencyModel {
|
|||||||
* @param string $type Dependency type
|
* @param string $type Dependency type
|
||||||
* @return bool True if it would create a cycle
|
* @return bool True if it would create a cycle
|
||||||
*/
|
*/
|
||||||
private function wouldCreateCycle($ticketId, $dependsOnId, $type) {
|
private function wouldCreateCycle($ticketId, $dependsOnId, $type): bool {
|
||||||
// Only check for cycles in blocking relationships
|
// Only check for cycles in blocking relationships
|
||||||
if (!in_array($type, ['blocks', 'blocked_by'])) {
|
if (!in_array($type, ['blocks', 'blocked_by'])) {
|
||||||
return false;
|
return false;
|
||||||
@@ -185,23 +188,39 @@ class DependencyModel {
|
|||||||
|
|
||||||
// Check if dependsOnId already has ticketId in its dependency chain
|
// Check if dependsOnId already has ticketId in its dependency chain
|
||||||
$visited = [];
|
$visited = [];
|
||||||
return $this->hasDependencyPath($dependsOnId, $ticketId, $visited);
|
return $this->hasDependencyPath($dependsOnId, $ticketId, $visited, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if there's a dependency path from source to target
|
* Check if there's a dependency path from source to target
|
||||||
*
|
*
|
||||||
|
* Uses iterative BFS approach with depth limit to prevent stack overflow
|
||||||
|
* and DoS attacks from deeply nested or circular dependencies.
|
||||||
|
*
|
||||||
* @param string $source Source ticket ID
|
* @param string $source Source ticket ID
|
||||||
* @param string $target Target ticket ID
|
* @param string $target Target ticket ID
|
||||||
* @param array $visited Already visited tickets
|
* @param array $visited Already visited tickets (passed by reference for efficiency)
|
||||||
|
* @param int $depth Current recursion depth
|
||||||
* @return bool True if path exists
|
* @return bool True if path exists
|
||||||
*/
|
*/
|
||||||
private function hasDependencyPath($source, $target, &$visited) {
|
private function hasDependencyPath($source, $target, array &$visited, int $depth): bool {
|
||||||
|
// Depth limit to prevent DoS and stack overflow
|
||||||
|
if ($depth >= self::MAX_DEPENDENCY_DEPTH) {
|
||||||
|
error_log("Dependency cycle detection hit max depth ({$depth}) from {$source} to {$target}");
|
||||||
|
return false; // Assume no cycle to avoid blocking legitimate operations
|
||||||
|
}
|
||||||
|
|
||||||
if ($source === $target) {
|
if ($source === $target) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_array($source, $visited)) {
|
if (in_array($source, $visited, true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limit visited array size to prevent memory exhaustion
|
||||||
|
if (count($visited) > 100) {
|
||||||
|
error_log("Dependency cycle detection visited too many nodes from {$source} to {$target}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +234,7 @@ class DependencyModel {
|
|||||||
$result = $stmt->get_result();
|
$result = $stmt->get_result();
|
||||||
|
|
||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
if ($this->hasDependencyPath($row['depends_on_id'], $target, $visited)) {
|
if ($this->hasDependencyPath($row['depends_on_id'], $target, $visited, $depth + 1)) {
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,22 +200,76 @@ class StatsModel {
|
|||||||
/**
|
/**
|
||||||
* Fetch all stats from database (uncached)
|
* Fetch all stats from database (uncached)
|
||||||
*
|
*
|
||||||
|
* Uses consolidated queries to reduce database round-trips from 12 to 4.
|
||||||
|
*
|
||||||
* @return array All dashboard statistics
|
* @return array All dashboard statistics
|
||||||
*/
|
*/
|
||||||
private function fetchAllStats(): array {
|
private function fetchAllStats(): array {
|
||||||
|
// Query 1: Get all simple counts in one query using conditional aggregation
|
||||||
|
$countsSql = "SELECT
|
||||||
|
SUM(CASE WHEN status IN ('Open', 'Pending', 'In Progress') THEN 1 ELSE 0 END) as open_tickets,
|
||||||
|
SUM(CASE WHEN status = 'Closed' THEN 1 ELSE 0 END) as closed_tickets,
|
||||||
|
SUM(CASE WHEN DATE(created_at) = CURDATE() THEN 1 ELSE 0 END) as created_today,
|
||||||
|
SUM(CASE WHEN YEARWEEK(created_at, 1) = YEARWEEK(CURDATE(), 1) THEN 1 ELSE 0 END) as created_this_week,
|
||||||
|
SUM(CASE WHEN status = 'Closed' AND DATE(updated_at) = CURDATE() THEN 1 ELSE 0 END) as closed_today,
|
||||||
|
SUM(CASE WHEN assigned_to IS NULL AND status != 'Closed' THEN 1 ELSE 0 END) as unassigned,
|
||||||
|
SUM(CASE WHEN priority = 1 AND status != 'Closed' THEN 1 ELSE 0 END) as critical,
|
||||||
|
AVG(CASE WHEN status = 'Closed' AND updated_at > created_at
|
||||||
|
THEN TIMESTAMPDIFF(HOUR, created_at, updated_at) ELSE NULL END) as avg_resolution
|
||||||
|
FROM tickets";
|
||||||
|
|
||||||
|
$countsResult = $this->conn->query($countsSql);
|
||||||
|
$counts = $countsResult->fetch_assoc();
|
||||||
|
|
||||||
|
// Query 2: Get priority, status, and category breakdowns in one query
|
||||||
|
$breakdownSql = "SELECT
|
||||||
|
'priority' as type, CONCAT('P', priority) as label, COUNT(*) as count
|
||||||
|
FROM tickets WHERE status != 'Closed' GROUP BY priority
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'status' as type, status as label, COUNT(*) as count
|
||||||
|
FROM tickets GROUP BY status
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'category' as type, category as label, COUNT(*) as count
|
||||||
|
FROM tickets WHERE status != 'Closed' GROUP BY category";
|
||||||
|
|
||||||
|
$breakdownResult = $this->conn->query($breakdownSql);
|
||||||
|
$byPriority = [];
|
||||||
|
$byStatus = [];
|
||||||
|
$byCategory = [];
|
||||||
|
|
||||||
|
while ($row = $breakdownResult->fetch_assoc()) {
|
||||||
|
switch ($row['type']) {
|
||||||
|
case 'priority':
|
||||||
|
$byPriority[$row['label']] = (int)$row['count'];
|
||||||
|
break;
|
||||||
|
case 'status':
|
||||||
|
$byStatus[$row['label']] = (int)$row['count'];
|
||||||
|
break;
|
||||||
|
case 'category':
|
||||||
|
$byCategory[$row['label']] = (int)$row['count'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort priority keys
|
||||||
|
ksort($byPriority);
|
||||||
|
|
||||||
|
// Query 3: Get assignee stats (requires JOIN, kept separate)
|
||||||
|
$byAssignee = $this->getTicketsByAssignee();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'open_tickets' => $this->getOpenTicketCount(),
|
'open_tickets' => (int)($counts['open_tickets'] ?? 0),
|
||||||
'closed_tickets' => $this->getClosedTicketCount(),
|
'closed_tickets' => (int)($counts['closed_tickets'] ?? 0),
|
||||||
'created_today' => $this->getTicketsCreatedToday(),
|
'created_today' => (int)($counts['created_today'] ?? 0),
|
||||||
'created_this_week' => $this->getTicketsCreatedThisWeek(),
|
'created_this_week' => (int)($counts['created_this_week'] ?? 0),
|
||||||
'closed_today' => $this->getTicketsClosedToday(),
|
'closed_today' => (int)($counts['closed_today'] ?? 0),
|
||||||
'unassigned' => $this->getUnassignedTicketCount(),
|
'unassigned' => (int)($counts['unassigned'] ?? 0),
|
||||||
'critical' => $this->getCriticalTicketCount(),
|
'critical' => (int)($counts['critical'] ?? 0),
|
||||||
'avg_resolution_hours' => $this->getAverageResolutionTime(),
|
'avg_resolution_hours' => $counts['avg_resolution'] ? round((float)$counts['avg_resolution'], 1) : 0.0,
|
||||||
'by_priority' => $this->getTicketsByPriority(),
|
'by_priority' => $byPriority,
|
||||||
'by_status' => $this->getTicketsByStatus(),
|
'by_status' => $byStatus,
|
||||||
'by_category' => $this->getTicketsByCategory(),
|
'by_category' => $byCategory,
|
||||||
'by_assignee' => $this->getTicketsByAssignee()
|
'by_assignee' => $byAssignee
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -319,13 +319,20 @@ class TicketModel {
|
|||||||
|
|
||||||
public function createTicket(array $ticketData, ?int $createdBy = null): array {
|
public function createTicket(array $ticketData, ?int $createdBy = null): array {
|
||||||
// Generate unique ticket ID (9-digit format with leading zeros)
|
// Generate unique ticket ID (9-digit format with leading zeros)
|
||||||
// Loop until we find an ID that doesn't exist to prevent collisions
|
// Uses cryptographically secure random numbers for better distribution
|
||||||
$maxAttempts = 10;
|
// Includes exponential backoff and fallback for reliability under high load
|
||||||
|
$maxAttempts = 50;
|
||||||
$attempts = 0;
|
$attempts = 0;
|
||||||
$ticket_id = null;
|
$ticket_id = null;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$candidate_id = sprintf('%09d', mt_rand(100000000, 999999999));
|
// Use random_int for cryptographically secure random number
|
||||||
|
try {
|
||||||
|
$candidate_id = sprintf('%09d', random_int(100000000, 999999999));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Fallback to mt_rand if random_int fails (shouldn't happen)
|
||||||
|
$candidate_id = sprintf('%09d', mt_rand(100000000, 999999999));
|
||||||
|
}
|
||||||
|
|
||||||
// Check if this ID already exists
|
// Check if this ID already exists
|
||||||
$checkSql = "SELECT ticket_id FROM tickets WHERE ticket_id = ? LIMIT 1";
|
$checkSql = "SELECT ticket_id FROM tickets WHERE ticket_id = ? LIMIT 1";
|
||||||
@@ -339,13 +346,34 @@ class TicketModel {
|
|||||||
}
|
}
|
||||||
$checkStmt->close();
|
$checkStmt->close();
|
||||||
$attempts++;
|
$attempts++;
|
||||||
|
|
||||||
|
// Exponential backoff: sleep longer as attempts increase
|
||||||
|
// This helps reduce contention under high load
|
||||||
|
if ($ticket_id === null && $attempts < $maxAttempts) {
|
||||||
|
usleep(min($attempts * 1000, 10000)); // Max 10ms delay
|
||||||
|
}
|
||||||
} while ($ticket_id === null && $attempts < $maxAttempts);
|
} while ($ticket_id === null && $attempts < $maxAttempts);
|
||||||
|
|
||||||
|
// Fallback: use timestamp-based ID if random generation fails
|
||||||
if ($ticket_id === null) {
|
if ($ticket_id === null) {
|
||||||
return [
|
// Generate ID from timestamp + random suffix for uniqueness
|
||||||
'success' => false,
|
$timestamp = (int)(microtime(true) * 1000) % 1000000000;
|
||||||
'error' => 'Failed to generate unique ticket ID after ' . $maxAttempts . ' attempts'
|
$ticket_id = sprintf('%09d', $timestamp);
|
||||||
];
|
|
||||||
|
// Verify this fallback ID is unique
|
||||||
|
$checkStmt = $this->conn->prepare("SELECT ticket_id FROM tickets WHERE ticket_id = ? LIMIT 1");
|
||||||
|
$checkStmt->bind_param("s", $ticket_id);
|
||||||
|
$checkStmt->execute();
|
||||||
|
if ($checkStmt->get_result()->num_rows > 0) {
|
||||||
|
$checkStmt->close();
|
||||||
|
error_log("Ticket ID generation failed after {$maxAttempts} attempts + fallback");
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'error' => 'Failed to generate unique ticket ID. Please try again.'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$checkStmt->close();
|
||||||
|
error_log("Ticket ID generation used fallback after {$maxAttempts} random attempts");
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = "INSERT INTO tickets (ticket_id, title, description, status, priority, category, type, created_by, visibility, visibility_groups)
|
$sql = "INSERT INTO tickets (ticket_id, title, description, status, priority, category, type, created_by, visibility, visibility_groups)
|
||||||
|
|||||||
@@ -271,9 +271,20 @@ class UserModel {
|
|||||||
* Get all distinct groups from all users
|
* Get all distinct groups from all users
|
||||||
* Used for visibility group selection UI
|
* Used for visibility group selection UI
|
||||||
*
|
*
|
||||||
|
* Results are cached for 5 minutes to reduce database load
|
||||||
|
* since group changes are infrequent.
|
||||||
|
*
|
||||||
* @return array Array of unique group names
|
* @return array Array of unique group names
|
||||||
*/
|
*/
|
||||||
public function getAllGroups(): array {
|
public function getAllGroups(): array {
|
||||||
|
$cacheKey = 'all_groups';
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
$cached = self::getCached($cacheKey);
|
||||||
|
if ($cached !== null) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $this->conn->prepare("SELECT DISTINCT groups FROM users WHERE groups IS NOT NULL AND groups != ''");
|
$stmt = $this->conn->prepare("SELECT DISTINCT groups FROM users WHERE groups IS NOT NULL AND groups != ''");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$result = $stmt->get_result();
|
$result = $stmt->get_result();
|
||||||
@@ -289,6 +300,18 @@ class UserModel {
|
|||||||
// Return unique groups sorted alphabetically
|
// Return unique groups sorted alphabetically
|
||||||
$uniqueGroups = array_unique($allGroups);
|
$uniqueGroups = array_unique($allGroups);
|
||||||
sort($uniqueGroups);
|
sort($uniqueGroups);
|
||||||
|
|
||||||
|
// Cache the result
|
||||||
|
self::setCache($cacheKey, $uniqueGroups);
|
||||||
|
|
||||||
return $uniqueGroups;
|
return $uniqueGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidate the groups cache
|
||||||
|
* Call this when user groups are modified
|
||||||
|
*/
|
||||||
|
public static function invalidateGroupsCache(): void {
|
||||||
|
unset(self::$userCache['all_groups']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user