'192.168.2.4', 'db_port' => 3307, 'db_name' => 'task_reporter', 'db_user' => 'task_reporter', 'db_pass' => 'Pass12349ers!', // 报告生成选项 'author' => 'CGNICO Task Reporter', // 报告作者 // 报告配置 'report_days' => 7, // 报告包含的天数(例如:7表示过去一周) 'report_title' => 'CGNICO工具使用报告', // 报告标题 ]; // 根据报告天数设置标题 if (isset($_GET['days'])) { $config['report_days'] = intval($_GET['days']); // 根据天数调整报告标题 if ($config['report_days'] == 7) { $config['report_title'] = 'CGNICO工具使用周报'; } elseif ($config['report_days'] == 30) { $config['report_title'] = 'CGNICO工具使用月报'; } elseif ($config['report_days'] == 90) { $config['report_title'] = 'CGNICO工具使用季报'; } elseif ($config['report_days'] == 365) { $config['report_title'] = 'CGNICO工具使用年报'; } } // 创建数据库连接 try { $pdo = new PDO( "mysql:host={$config['db_host']};port={$config['db_port']};dbname={$config['db_name']}", $config['db_user'], $config['db_pass'] ); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("SET NAMES utf8"); } catch (PDOException $e) { die("数据库连接失败: " . $e->getMessage()); } // 生成报告 function generate_report($pdo, $days) { $report = []; // 获取日期范围 $end_date = date('Y-m-d'); $start_date = date('Y-m-d', strtotime("-{$days} days")); // 1. 总体统计 $stmt = $pdo->prepare(" SELECT COUNT(*) as total_tasks, SUM(time_saved) as total_time_saved, COUNT(DISTINCT username) as total_users, COUNT(DISTINCT tool_name) as total_tools FROM task WHERE timestamp BETWEEN :start_date AND :end_date AND tool_name NOT LIKE '%(Debug)%' "); $stmt->bindParam(':start_date', $start_date); $stmt->bindParam(':end_date', $end_date); $stmt->execute(); $report['summary'] = $stmt->fetch(PDO::FETCH_ASSOC); // 2. 工具使用统计 $stmt = $pdo->prepare(" SELECT tool_name, COUNT(*) as usage_count, SUM(time_saved) as time_saved FROM task WHERE timestamp BETWEEN :start_date AND :end_date AND tool_name NOT LIKE '%(Debug)%' GROUP BY tool_name ORDER BY usage_count DESC LIMIT 10 "); $stmt->bindParam(':start_date', $start_date); $stmt->bindParam(':end_date', $end_date); $stmt->execute(); $report['tools'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // 3. 用户使用统计 $stmt = $pdo->prepare(" SELECT username, COUNT(*) as usage_count, SUM(time_saved) as time_saved FROM task WHERE timestamp BETWEEN :start_date AND :end_date AND tool_name NOT LIKE '%(Debug)%' GROUP BY username ORDER BY usage_count DESC LIMIT 10 "); $stmt->bindParam(':start_date', $start_date); $stmt->bindParam(':end_date', $end_date); $stmt->execute(); $report['users'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // 4. 每日使用趋势 $stmt = $pdo->prepare(" SELECT DATE(timestamp) as date, COUNT(*) as usage_count, SUM(time_saved) as time_saved FROM task WHERE timestamp BETWEEN :start_date AND :end_date AND tool_name NOT LIKE '%(Debug)%' GROUP BY DATE(timestamp) ORDER BY date "); $stmt->bindParam(':start_date', $start_date); $stmt->bindParam(':end_date', $end_date); $stmt->execute(); $report['daily'] = $stmt->fetchAll(PDO::FETCH_ASSOC); return [ 'report' => $report, 'start_date' => $start_date, 'end_date' => $end_date ]; } // 生成HTML报告 function generate_html_report($report_data) { $report = $report_data['report']; $start_date = $report_data['start_date']; $end_date = $report_data['end_date']; $html = '
报告时间范围: ' . $start_date . ' 至 ' . $end_date . '
在过去的 ' . round((strtotime($end_date) - strtotime($start_date)) / 86400) . ' 天中:
排名 | 工具名称 | 使用次数 | 节省时间 (小时) |
---|---|---|---|
' . $rank++ . ' | ' . htmlspecialchars($tool['tool_name']) . ' | ' . number_format($tool['usage_count']) . ' | ' . number_format($tool['time_saved'], 2) . ' |
排名 | 用户名 | 使用次数 | 节省时间 (小时) |
---|---|---|---|
' . $rank++ . ' | ' . htmlspecialchars($user['username']) . ' | ' . number_format($user['usage_count']) . ' | ' . number_format($user['time_saved'], 2) . ' |
日期 | 使用次数 | 节省时间 (小时) |
---|---|---|
' . $day['date'] . ' | ' . number_format($day['usage_count']) . ' | ' . number_format($day['time_saved'], 2) . ' |
此报告由CGNICO Task Reporter自动生成于 ' . date('Y-m-d H:i:s') . '
报告日期 | \n"; echo "报告类型 | \n"; echo "操作 | \n"; echo "
---|---|---|
{$date} | \n"; echo "{$report_type} | \n"; echo "\n"; echo "查看\n"; echo "下载\n"; echo " | \n"; echo "