| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Module\Cleanup\Models;
- use UCore\ModelCore;
- /**
- * 清理表统计模型
- *
- * 记录表的统计信息和扫描历史
- */
- class CleanupTableStats extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_table_stats';
- // field start
- /**
- * 可批量赋值的字段
- */
- protected $fillable = [
- 'table_name',
- 'record_count',
- 'table_size_mb',
- 'index_size_mb',
- 'data_free_mb',
- 'avg_row_length',
- 'auto_increment',
- 'engine',
- 'collation',
- 'has_time_field',
- 'time_fields',
- 'has_user_field',
- 'user_fields',
- 'has_status_field',
- 'status_fields',
- 'last_scanned_at',
- ];
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'record_count' => 'integer',
- 'table_size_mb' => 'float',
- 'index_size_mb' => 'float',
- 'data_free_mb' => 'float',
- 'avg_row_length' => 'integer',
- 'auto_increment' => 'integer',
- 'has_time_field' => 'boolean',
- 'time_fields' => 'array',
- 'has_user_field' => 'boolean',
- 'user_fields' => 'array',
- 'has_status_field' => 'boolean',
- 'status_fields' => 'array',
- 'last_scanned_at' => 'datetime',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 按表名筛选
- */
- public function scopeByTable($query, string $tableName)
- {
- return $query->where('table_name', $tableName);
- }
- /**
- * 获取大表(记录数超过指定数量)
- */
- public function scopeLargeTables($query, int $minRecords = 10000)
- {
- return $query->where('record_count', '>', $minRecords);
- }
- /**
- * 获取有时间字段的表
- */
- public function scopeWithTimeFields($query)
- {
- return $query->where('has_time_field', true);
- }
- /**
- * 获取有用户字段的表
- */
- public function scopeWithUserFields($query)
- {
- return $query->where('has_user_field', true);
- }
- /**
- * 获取有状态字段的表
- */
- public function scopeWithStatusFields($query)
- {
- return $query->where('has_status_field', true);
- }
- /**
- * 获取表大小的可读格式
- */
- public function getTableSizeHumanAttribute(): string
- {
- $mb = $this->table_size_mb;
-
- if ($mb < 1) {
- return round($mb * 1024, 2) . ' KB';
- } elseif ($mb < 1024) {
- return round($mb, 2) . ' MB';
- } else {
- return round($mb / 1024, 2) . ' GB';
- }
- }
- /**
- * 获取总大小(表+索引)
- */
- public function getTotalSizeMbAttribute(): float
- {
- return $this->table_size_mb + $this->index_size_mb;
- }
- /**
- * 获取总大小的可读格式
- */
- public function getTotalSizeHumanAttribute(): string
- {
- $mb = $this->total_size_mb;
-
- if ($mb < 1) {
- return round($mb * 1024, 2) . ' KB';
- } elseif ($mb < 1024) {
- return round($mb, 2) . ' MB';
- } else {
- return round($mb / 1024, 2) . ' GB';
- }
- }
- }
|