feat(nginx-log): 新增 6 张表 phinx migration + Scheme + model

This commit is contained in:
augushong
2026-07-28 00:03:49 +08:00
parent f48e0947cb
commit 0d19881a22
18 changed files with 590 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_access_log', comment: 'nginx 访问日志明细(解析入库)')]
#[Index(columns: ['time_local'], name: 'idx_time_local', type: 'NORMAL')]
#[Index(columns: ['remote_addr'], name: 'idx_remote_addr', type: 'NORMAL')]
#[Index(columns: ['status'], name: 'idx_status', type: 'NORMAL')]
#[Index(columns: ['uri'], name: 'idx_uri', type: 'NORMAL')]
#[Index(columns: ['file_path'], name: 'idx_file_path', type: 'NORMAL')]
class NginxAccessLog extends BaseScheme
{
#[Field(type: 'bigint', nullable: false, unsigned: true, autoIncrement: true, primary: true, comment: '主键ID')]
public $id;
#[Field(type: 'varchar', length: 500, nullable: false, comment: '来源日志文件路径')]
public $file_path;
#[Field(type: 'varchar', length: 45, nullable: false, comment: '客户端 IP含 IPv6')]
public $remote_addr;
#[Field(type: 'varchar', length: 100, nullable: true, comment: 'HTTP 认证用户名')]
public $remote_user;
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, comment: '请求时间戳(业务时区)')]
public $time_local;
#[Field(type: 'varchar', length: 10, nullable: true, comment: 'HTTP 方法')]
public $method;
#[Field(type: 'varchar', length: 255, nullable: false, comment: '请求 URI超 255 截断)')]
public $uri;
#[Field(type: 'varchar', length: 500, nullable: false, default: '', comment: '查询字符串')]
public $query_string;
#[Field(type: 'varchar', length: 10, nullable: true, comment: 'HTTP 协议版本')]
public $http_version;
#[Field(type: 'int', length: 11, default: '0', comment: 'HTTP 状态码')]
public $status;
#[Field(type: 'bigint', default: '0', comment: '响应体字节数')]
public $body_bytes_sent;
#[Field(type: 'varchar', length: 500, nullable: false, default: '', comment: 'Referer')]
public $http_referer;
#[Field(type: 'varchar', length: 500, nullable: false, default: '', comment: 'User-Agent超 500 截断)')]
public $http_user_agent;
#[Field(type: 'decimal', precision: 10, scale: 3, default: '0.000', comment: 'nginx 请求总耗时(秒)')]
public $request_time;
#[Field(type: 'decimal', precision: 10, scale: 3, default: '0.000', comment: 'upstream 响应时间(秒)')]
public $upstream_response_time;
#[Field(type: 'bigint', default: '0', comment: '总发送字节数(含 header')]
public $bytes_sent;
#[Field(type: 'varchar', length: 50, nullable: true, comment: '国家GeoIP 预留)')]
public $country;
#[Field(type: 'varchar', length: 50, nullable: true, comment: '省份GeoIP 预留)')]
public $province;
#[Field(type: 'varchar', length: 50, nullable: true, comment: '城市GeoIP 预留)')]
public $city;
#[Field(type: 'int', length: 11, unsigned: true, comment: '入库时间戳')]
public $create_time;
}

View File

@@ -0,0 +1,43 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_log_position', comment: 'nginx 日志文件读取位置(增量读取状态)')]
#[Index(columns: ['file_path'], name: 'uniq_file_path', type: 'UNIQUE')]
class NginxLogPosition extends BaseScheme
{
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, autoIncrement: true, primary: true)]
public $id;
#[Field(type: 'varchar', length: 500, nullable: false, comment: '日志文件绝对路径')]
public $file_path;
#[Field(type: 'bigint', default: '0', unsigned: true, comment: '文件 inode用于检测日志轮转')]
public $inode;
#[Field(type: 'bigint', default: '0', unsigned: true, comment: '上次读取到的字节偏移')]
public $offset;
#[Field(type: 'int', length: 11, default: '0', unsigned: true, comment: '上次读取时间戳')]
public $last_read_time;
#[Field(type: 'varchar', length: 32, default: '', comment: '上次最后一行的 md5 哈希(容错校验)')]
public $last_line_hash;
#[Field(type: 'int', length: 11, default: '0', unsigned: true, comment: '解析失败累计次数')]
public $parse_fail_count;
#[Field(type: 'text', nullable: true, comment: '解析失败样本行(最多保留若干条)')]
public $parse_fail_samples;
#[Field(type: 'int', length: 11, unsigned: true, comment: '创建时间戳')]
public $create_time;
#[Field(type: 'int', length: 11, unsigned: true, comment: '更新时间戳')]
public $update_time;
}

View File

@@ -0,0 +1,50 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_stat_hour', comment: 'nginx 按小时聚合统计')]
#[Index(columns: ['stat_date', 'stat_hour'], name: 'uniq_date_hour', type: 'UNIQUE')]
#[Index(columns: ['stat_date'], name: 'idx_stat_date', type: 'NORMAL')]
class NginxStatHour extends BaseScheme
{
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, autoIncrement: true, primary: true)]
public $id;
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, comment: '统计日期(YYYYMMDD)')]
public $stat_date;
#[Field(type: 'tinyint', length: 2, default: '0', unsigned: true, comment: '小时(0-23)')]
public $stat_hour;
#[Field(type: 'int', length: 11, default: '0', comment: '页面浏览量')]
public $pv;
#[Field(type: 'int', length: 11, default: '0', comment: '独立访客数')]
public $uv;
#[Field(type: 'bigint', default: '0', comment: '总流量(字节)')]
public $total_bytes;
#[Field(type: 'int', length: 11, default: '0', comment: '2xx 响应数')]
public $status_2xx;
#[Field(type: 'int', length: 11, default: '0', comment: '3xx 响应数')]
public $status_3xx;
#[Field(type: 'int', length: 11, default: '0', comment: '4xx 响应数')]
public $status_4xx;
#[Field(type: 'int', length: 11, default: '0', comment: '5xx 响应数')]
public $status_5xx;
#[Field(type: 'int', length: 11, default: '0', comment: '其他状态码响应数')]
public $status_other;
#[Field(type: 'decimal', precision: 10, scale: 3, default: '0.000', comment: '平均请求耗时(秒)')]
public $avg_request_time;
}

View File

@@ -0,0 +1,29 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_stat_referer', comment: 'nginx 按 Referer 来源域名聚合统计')]
#[Index(columns: ['stat_date', 'referer_domain'], name: 'uniq_date_referer', type: 'UNIQUE')]
#[Index(columns: ['stat_date'], name: 'idx_stat_date', type: 'NORMAL')]
class NginxStatReferer extends BaseScheme
{
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, autoIncrement: true, primary: true)]
public $id;
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, comment: '统计日期(YYYYMMDD)')]
public $stat_date;
#[Field(type: 'varchar', length: 255, nullable: false, comment: 'Referer 来源域名(direct/unknown)')]
public $referer_domain;
#[Field(type: 'int', length: 11, default: '0', comment: '页面浏览量')]
public $pv;
#[Field(type: 'int', length: 11, default: '0', comment: '独立访客数')]
public $uv;
}

View File

@@ -0,0 +1,32 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_stat_ua', comment: 'nginx 按 User-Agent 聚合统计')]
#[Index(columns: ['stat_date', 'ua_type', 'ua_name'], name: 'uniq_date_type_name', type: 'UNIQUE')]
#[Index(columns: ['stat_date'], name: 'idx_stat_date', type: 'NORMAL')]
class NginxStatUa extends BaseScheme
{
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, autoIncrement: true, primary: true)]
public $id;
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, comment: '统计日期(YYYYMMDD)')]
public $stat_date;
#[Field(type: 'varchar', length: 20, default: 'unknown', comment: 'UA 类型(browser/spider/unknown)')]
public $ua_type;
#[Field(type: 'varchar', length: 255, nullable: false, comment: 'UA 名称(具体型号)')]
public $ua_name;
#[Field(type: 'int', length: 11, default: '0', comment: '页面浏览量')]
public $pv;
#[Field(type: 'int', length: 11, default: '0', comment: '独立访客数')]
public $uv;
}

View File

@@ -0,0 +1,35 @@
<?php
namespace app\admin\scheme;
use app\common\scheme\BaseScheme;
use app\common\scheme\attribute\Table;
use app\common\scheme\attribute\Field;
use app\common\scheme\attribute\Index;
#[Table(name: 'nginx_stat_url', comment: 'nginx 按 URL 聚合统计')]
#[Index(columns: ['stat_date', 'uri'], name: 'uniq_date_uri', type: 'UNIQUE')]
#[Index(columns: ['stat_date'], name: 'idx_stat_date', type: 'NORMAL')]
class NginxStatUrl extends BaseScheme
{
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, autoIncrement: true, primary: true)]
public $id;
#[Field(type: 'int', length: 11, nullable: false, unsigned: true, comment: '统计日期(YYYYMMDD)')]
public $stat_date;
#[Field(type: 'varchar', length: 255, nullable: false, comment: '请求 URI')]
public $uri;
#[Field(type: 'int', length: 11, default: '0', comment: '页面浏览量')]
public $pv;
#[Field(type: 'int', length: 11, default: '0', comment: '独立访客数')]
public $uv;
#[Field(type: 'bigint', default: '0', comment: '总流量(字节)')]
public $total_bytes;
#[Field(type: 'decimal', precision: 10, scale: 3, default: '0.000', comment: '平均请求耗时(秒)')]
public $avg_request_time;
}