From 0d19881a228b33fba5cdf90ce22ed9f00510de67 Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 28 Jul 2026 00:03:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(nginx-log):=20=E6=96=B0=E5=A2=9E=206=20?= =?UTF-8?q?=E5=BC=A0=E8=A1=A8=20phinx=20migration=20+=20Scheme=20+=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/model/NginxAccessLog.php | 34 ++++++++ app/admin/model/NginxLogPosition.php | 24 ++++++ app/admin/model/NginxStatHour.php | 26 +++++++ app/admin/model/NginxStatReferer.php | 19 +++++ app/admin/model/NginxStatUa.php | 20 +++++ app/admin/model/NginxStatUrl.php | 21 +++++ app/admin/scheme/NginxAccessLog.php | 77 +++++++++++++++++++ app/admin/scheme/NginxLogPosition.php | 43 +++++++++++ app/admin/scheme/NginxStatHour.php | 50 ++++++++++++ app/admin/scheme/NginxStatReferer.php | 29 +++++++ app/admin/scheme/NginxStatUa.php | 32 ++++++++ app/admin/scheme/NginxStatUrl.php | 35 +++++++++ .../20260726100001_nginx_log_position.php | 28 +++++++ .../20260726100002_nginx_access_log.php | 44 +++++++++++ .../20260726100003_nginx_stat_hour.php | 32 ++++++++ .../20260726100004_nginx_stat_url.php | 26 +++++++ .../20260726100005_nginx_stat_referer.php | 24 ++++++ .../20260726100006_nginx_stat_ua.php | 26 +++++++ 18 files changed, 590 insertions(+) create mode 100644 app/admin/model/NginxAccessLog.php create mode 100644 app/admin/model/NginxLogPosition.php create mode 100644 app/admin/model/NginxStatHour.php create mode 100644 app/admin/model/NginxStatReferer.php create mode 100644 app/admin/model/NginxStatUa.php create mode 100644 app/admin/model/NginxStatUrl.php create mode 100644 app/admin/scheme/NginxAccessLog.php create mode 100644 app/admin/scheme/NginxLogPosition.php create mode 100644 app/admin/scheme/NginxStatHour.php create mode 100644 app/admin/scheme/NginxStatReferer.php create mode 100644 app/admin/scheme/NginxStatUa.php create mode 100644 app/admin/scheme/NginxStatUrl.php create mode 100644 database/migrations/20260726100001_nginx_log_position.php create mode 100644 database/migrations/20260726100002_nginx_access_log.php create mode 100644 database/migrations/20260726100003_nginx_stat_hour.php create mode 100644 database/migrations/20260726100004_nginx_stat_url.php create mode 100644 database/migrations/20260726100005_nginx_stat_referer.php create mode 100644 database/migrations/20260726100006_nginx_stat_ua.php diff --git a/app/admin/model/NginxAccessLog.php b/app/admin/model/NginxAccessLog.php new file mode 100644 index 0000000..7b63152 --- /dev/null +++ b/app/admin/model/NginxAccessLog.php @@ -0,0 +1,34 @@ +table('nginx_log_position') + ->setComment('nginx 日志文件读取位置(增量读取状态)') + ->addColumn('file_path', 'string', ['limit' => 500, 'null' => false, 'comment' => '日志文件绝对路径']) + ->addColumn('inode', 'biginteger', ['default' => 0, 'comment' => '文件 inode,用于检测日志轮转', 'signed' => false]) + ->addColumn('offset', 'biginteger', ['default' => 0, 'comment' => '上次读取到的字节偏移', 'signed' => false]) + ->addColumn('last_read_time', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '上次读取时间戳', 'signed' => false]) + ->addColumn('last_line_hash', 'string', ['limit' => 32, 'default' => '', 'comment' => '上次最后一行的 md5 哈希(容错校验)']) + ->addColumn('parse_fail_count', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '解析失败累计次数', 'signed' => false]) + ->addColumn('parse_fail_samples', 'text', ['null' => true, 'comment' => '解析失败样本行(最多保留若干条)']) + ->addColumn('create_time', 'integer', ['limit' => 11, 'comment' => '创建时间戳', 'signed' => false]) + ->addColumn('update_time', 'integer', ['limit' => 11, 'comment' => '更新时间戳', 'signed' => false]) + ->addIndex(['file_path'], ['unique' => true, 'name' => 'uniq_file_path']) + ->create(); + } +} diff --git a/database/migrations/20260726100002_nginx_access_log.php b/database/migrations/20260726100002_nginx_access_log.php new file mode 100644 index 0000000..2af8c9c --- /dev/null +++ b/database/migrations/20260726100002_nginx_access_log.php @@ -0,0 +1,44 @@ +table('nginx_access_log', ['id' => false, 'primary_key' => ['id']]) + ->setComment('nginx 访问日志明细(解析入库)') + ->addColumn('id', 'biginteger', ['null' => false, 'signed' => false, 'identity' => true, 'comment' => '主键ID']) + ->addColumn('file_path', 'string', ['limit' => 500, 'null' => false, 'comment' => '来源日志文件路径']) + ->addColumn('remote_addr', 'string', ['limit' => 45, 'null' => false, 'comment' => '客户端 IP(含 IPv6)']) + ->addColumn('remote_user', 'string', ['limit' => 100, 'null' => true, 'comment' => 'HTTP 认证用户名']) + ->addColumn('time_local', 'integer', ['limit' => 11, 'null' => false, 'comment' => '请求时间戳(业务时区)', 'signed' => false]) + ->addColumn('method', 'string', ['limit' => 10, 'null' => true, 'comment' => 'HTTP 方法']) + ->addColumn('uri', 'string', ['limit' => 255, 'null' => false, 'comment' => '请求 URI(超 255 截断)']) + ->addColumn('query_string', 'string', ['limit' => 500, 'null' => false, 'default' => '', 'comment' => '查询字符串']) + ->addColumn('http_version', 'string', ['limit' => 10, 'null' => true, 'comment' => 'HTTP 协议版本']) + ->addColumn('status', 'integer', ['limit' => 11, 'default' => 0, 'comment' => 'HTTP 状态码']) + ->addColumn('body_bytes_sent', 'biginteger', ['default' => 0, 'comment' => '响应体字节数']) + ->addColumn('http_referer', 'string', ['limit' => 500, 'null' => false, 'default' => '', 'comment' => 'Referer']) + ->addColumn('http_user_agent', 'string', ['limit' => 500, 'null' => false, 'default' => '', 'comment' => 'User-Agent(超 500 截断)']) + ->addColumn('request_time', 'decimal', ['precision' => 10, 'scale' => 3, 'default' => 0, 'comment' => 'nginx 请求总耗时(秒)']) + ->addColumn('upstream_response_time', 'decimal', ['precision' => 10, 'scale' => 3, 'default' => 0, 'comment' => 'upstream 响应时间(秒)']) + ->addColumn('bytes_sent', 'biginteger', ['default' => 0, 'comment' => '总发送字节数(含 header)']) + ->addColumn('country', 'string', ['limit' => 50, 'null' => true, 'comment' => '国家(GeoIP 预留)']) + ->addColumn('province', 'string', ['limit' => 50, 'null' => true, 'comment' => '省份(GeoIP 预留)']) + ->addColumn('city', 'string', ['limit' => 50, 'null' => true, 'comment' => '城市(GeoIP 预留)']) + ->addColumn('create_time', 'integer', ['limit' => 11, 'comment' => '入库时间戳', 'signed' => false]) + ->addIndex(['time_local'], ['name' => 'idx_time_local']) + ->addIndex(['remote_addr'], ['name' => 'idx_remote_addr']) + ->addIndex(['status'], ['name' => 'idx_status']) + ->addIndex(['uri'], ['name' => 'idx_uri']) + ->addIndex(['file_path'], ['name' => 'idx_file_path']) + ->create(); + } +} diff --git a/database/migrations/20260726100003_nginx_stat_hour.php b/database/migrations/20260726100003_nginx_stat_hour.php new file mode 100644 index 0000000..51f5435 --- /dev/null +++ b/database/migrations/20260726100003_nginx_stat_hour.php @@ -0,0 +1,32 @@ +table('nginx_stat_hour') + ->setComment('nginx 按小时聚合统计') + ->addColumn('stat_date', 'integer', ['limit' => 11, 'null' => false, 'comment' => '统计日期(YYYYMMDD)', 'signed' => false]) + ->addColumn('stat_hour', 'tinyinteger', ['default' => 0, 'comment' => '小时(0-23)', 'signed' => false]) + ->addColumn('pv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '页面浏览量']) + ->addColumn('uv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '独立访客数']) + ->addColumn('total_bytes', 'biginteger', ['default' => 0, 'comment' => '总流量(字节)']) + ->addColumn('status_2xx', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '2xx 响应数']) + ->addColumn('status_3xx', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '3xx 响应数']) + ->addColumn('status_4xx', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '4xx 响应数']) + ->addColumn('status_5xx', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '5xx 响应数']) + ->addColumn('status_other', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '其他状态码响应数']) + ->addColumn('avg_request_time', 'decimal', ['precision' => 10, 'scale' => 3, 'default' => 0, 'comment' => '平均请求耗时(秒)']) + ->addIndex(['stat_date', 'stat_hour'], ['unique' => true, 'name' => 'uniq_date_hour']) + ->addIndex(['stat_date'], ['name' => 'idx_stat_date']) + ->create(); + } +} diff --git a/database/migrations/20260726100004_nginx_stat_url.php b/database/migrations/20260726100004_nginx_stat_url.php new file mode 100644 index 0000000..cc91e9e --- /dev/null +++ b/database/migrations/20260726100004_nginx_stat_url.php @@ -0,0 +1,26 @@ +table('nginx_stat_url') + ->setComment('nginx 按 URL 聚合统计') + ->addColumn('stat_date', 'integer', ['limit' => 11, 'null' => false, 'comment' => '统计日期(YYYYMMDD)', 'signed' => false]) + ->addColumn('uri', 'string', ['limit' => 255, 'null' => false, 'comment' => '请求 URI']) + ->addColumn('pv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '页面浏览量']) + ->addColumn('uv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '独立访客数']) + ->addColumn('total_bytes', 'biginteger', ['default' => 0, 'comment' => '总流量(字节)']) + ->addColumn('avg_request_time', 'decimal', ['precision' => 10, 'scale' => 3, 'default' => 0, 'comment' => '平均请求耗时(秒)']) + ->addIndex(['stat_date', 'uri'], ['unique' => true, 'name' => 'uniq_date_uri']) + ->addIndex(['stat_date'], ['name' => 'idx_stat_date']) + ->create(); + } +} diff --git a/database/migrations/20260726100005_nginx_stat_referer.php b/database/migrations/20260726100005_nginx_stat_referer.php new file mode 100644 index 0000000..c4663d7 --- /dev/null +++ b/database/migrations/20260726100005_nginx_stat_referer.php @@ -0,0 +1,24 @@ +table('nginx_stat_referer') + ->setComment('nginx 按 Referer 来源域名聚合统计') + ->addColumn('stat_date', 'integer', ['limit' => 11, 'null' => false, 'comment' => '统计日期(YYYYMMDD)', 'signed' => false]) + ->addColumn('referer_domain', 'string', ['limit' => 255, 'null' => false, 'comment' => 'Referer 来源域名(direct/unknown)']) + ->addColumn('pv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '页面浏览量']) + ->addColumn('uv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '独立访客数']) + ->addIndex(['stat_date', 'referer_domain'], ['unique' => true, 'name' => 'uniq_date_referer']) + ->addIndex(['stat_date'], ['name' => 'idx_stat_date']) + ->create(); + } +} diff --git a/database/migrations/20260726100006_nginx_stat_ua.php b/database/migrations/20260726100006_nginx_stat_ua.php new file mode 100644 index 0000000..d4dd2b9 --- /dev/null +++ b/database/migrations/20260726100006_nginx_stat_ua.php @@ -0,0 +1,26 @@ +table('nginx_stat_ua') + ->setComment('nginx 按 User-Agent 聚合统计') + ->addColumn('stat_date', 'integer', ['limit' => 11, 'null' => false, 'comment' => '统计日期(YYYYMMDD)', 'signed' => false]) + ->addColumn('ua_type', 'string', ['limit' => 20, 'default' => 'unknown', 'comment' => 'UA 类型(browser/spider/unknown)']) + ->addColumn('ua_name', 'string', ['limit' => 255, 'null' => false, 'comment' => 'UA 名称(具体型号)']) + ->addColumn('pv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '页面浏览量']) + ->addColumn('uv', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '独立访客数']) + ->addIndex(['stat_date', 'ua_type', 'ua_name'], ['unique' => true, 'name' => 'uniq_date_type_name']) + ->addIndex(['stat_date'], ['name' => 'idx_stat_date']) + ->create(); + } +}