187 lines
6.0 KiB
PHP
187 lines
6.0 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* StatsModel - Dashboard statistics and metrics
|
||
|
|
*
|
||
|
|
* Provides various ticket statistics for dashboard widgets
|
||
|
|
*/
|
||
|
|
|
||
|
|
class StatsModel {
|
||
|
|
private $conn;
|
||
|
|
|
||
|
|
public function __construct($conn) {
|
||
|
|
$this->conn = $conn;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get count of open tickets
|
||
|
|
*/
|
||
|
|
public function getOpenTicketCount() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE status IN ('Open', 'Pending', 'In Progress')";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get count of closed tickets
|
||
|
|
*/
|
||
|
|
public function getClosedTicketCount() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE status = 'Closed'";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tickets grouped by priority
|
||
|
|
*/
|
||
|
|
public function getTicketsByPriority() {
|
||
|
|
$sql = "SELECT priority, COUNT(*) as count FROM tickets WHERE status != 'Closed' GROUP BY priority ORDER BY priority";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$data = [];
|
||
|
|
while ($row = $result->fetch_assoc()) {
|
||
|
|
$data['P' . $row['priority']] = (int)$row['count'];
|
||
|
|
}
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tickets grouped by status
|
||
|
|
*/
|
||
|
|
public function getTicketsByStatus() {
|
||
|
|
$sql = "SELECT status, COUNT(*) as count FROM tickets GROUP BY status ORDER BY FIELD(status, 'Open', 'Pending', 'In Progress', 'Closed')";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$data = [];
|
||
|
|
while ($row = $result->fetch_assoc()) {
|
||
|
|
$data[$row['status']] = (int)$row['count'];
|
||
|
|
}
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tickets grouped by category
|
||
|
|
*/
|
||
|
|
public function getTicketsByCategory() {
|
||
|
|
$sql = "SELECT category, COUNT(*) as count FROM tickets WHERE status != 'Closed' GROUP BY category ORDER BY count DESC";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$data = [];
|
||
|
|
while ($row = $result->fetch_assoc()) {
|
||
|
|
$data[$row['category']] = (int)$row['count'];
|
||
|
|
}
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get average resolution time in hours
|
||
|
|
*/
|
||
|
|
public function getAverageResolutionTime() {
|
||
|
|
$sql = "SELECT AVG(TIMESTAMPDIFF(HOUR, created_at, updated_at)) as avg_hours
|
||
|
|
FROM tickets
|
||
|
|
WHERE status = 'Closed'
|
||
|
|
AND created_at IS NOT NULL
|
||
|
|
AND updated_at IS NOT NULL
|
||
|
|
AND updated_at > created_at";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return $row['avg_hours'] ? round($row['avg_hours'], 1) : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get count of tickets created today
|
||
|
|
*/
|
||
|
|
public function getTicketsCreatedToday() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE DATE(created_at) = CURDATE()";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get count of tickets created this week
|
||
|
|
*/
|
||
|
|
public function getTicketsCreatedThisWeek() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE YEARWEEK(created_at, 1) = YEARWEEK(CURDATE(), 1)";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get count of tickets closed today
|
||
|
|
*/
|
||
|
|
public function getTicketsClosedToday() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE status = 'Closed' AND DATE(updated_at) = CURDATE()";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tickets by assignee (top 5)
|
||
|
|
*/
|
||
|
|
public function getTicketsByAssignee($limit = 5) {
|
||
|
|
$sql = "SELECT
|
||
|
|
u.display_name,
|
||
|
|
u.username,
|
||
|
|
COUNT(t.ticket_id) as ticket_count
|
||
|
|
FROM tickets t
|
||
|
|
JOIN users u ON t.assigned_to = u.user_id
|
||
|
|
WHERE t.status != 'Closed'
|
||
|
|
GROUP BY t.assigned_to
|
||
|
|
ORDER BY ticket_count DESC
|
||
|
|
LIMIT ?";
|
||
|
|
$stmt = $this->conn->prepare($sql);
|
||
|
|
$stmt->bind_param('i', $limit);
|
||
|
|
$stmt->execute();
|
||
|
|
$result = $stmt->get_result();
|
||
|
|
$data = [];
|
||
|
|
while ($row = $result->fetch_assoc()) {
|
||
|
|
$name = $row['display_name'] ?: $row['username'];
|
||
|
|
$data[$name] = (int)$row['ticket_count'];
|
||
|
|
}
|
||
|
|
$stmt->close();
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get unassigned ticket count
|
||
|
|
*/
|
||
|
|
public function getUnassignedTicketCount() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE assigned_to IS NULL AND status != 'Closed'";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get critical (P1) ticket count
|
||
|
|
*/
|
||
|
|
public function getCriticalTicketCount() {
|
||
|
|
$sql = "SELECT COUNT(*) as count FROM tickets WHERE priority = 1 AND status != 'Closed'";
|
||
|
|
$result = $this->conn->query($sql);
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
return (int)$row['count'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all stats as a single array
|
||
|
|
*/
|
||
|
|
public function getAllStats() {
|
||
|
|
return [
|
||
|
|
'open_tickets' => $this->getOpenTicketCount(),
|
||
|
|
'closed_tickets' => $this->getClosedTicketCount(),
|
||
|
|
'created_today' => $this->getTicketsCreatedToday(),
|
||
|
|
'created_this_week' => $this->getTicketsCreatedThisWeek(),
|
||
|
|
'closed_today' => $this->getTicketsClosedToday(),
|
||
|
|
'unassigned' => $this->getUnassignedTicketCount(),
|
||
|
|
'critical' => $this->getCriticalTicketCount(),
|
||
|
|
'avg_resolution_hours' => $this->getAverageResolutionTime(),
|
||
|
|
'by_priority' => $this->getTicketsByPriority(),
|
||
|
|
'by_status' => $this->getTicketsByStatus(),
|
||
|
|
'by_category' => $this->getTicketsByCategory(),
|
||
|
|
'by_assignee' => $this->getTicketsByAssignee()
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|