CleanupTableStats.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * 清理表统计模型
  6. *
  7. * 记录表的统计信息和扫描历史
  8. */
  9. class CleanupTableStats extends ModelCore
  10. {
  11. /**
  12. * 数据表名
  13. */
  14. protected $table = 'cleanup_table_stats';
  15. // field start
  16. /**
  17. * @property int $id 主键ID
  18. * @property string $table_name 表名
  19. * @property int $record_count 记录总数
  20. * @property float $table_size_mb 表大小(MB)
  21. * @property float $index_size_mb 索引大小(MB)
  22. * @property float $data_free_mb 碎片空间(MB)
  23. * @property int $avg_row_length 平均行长度
  24. * @property int $auto_increment 自增值
  25. * @property string $oldest_record_time 最早记录时间
  26. * @property string $newest_record_time 最新记录时间
  27. * @property string $scan_time 扫描时间
  28. * @property \Carbon\Carbon $created_at 创建时间
  29. * @property \Carbon\Carbon $updated_at 更新时间
  30. */
  31. // field end
  32. /**
  33. * 字段类型转换
  34. */
  35. protected $casts = [
  36. 'record_count' => 'integer',
  37. 'table_size_mb' => 'float',
  38. 'index_size_mb' => 'float',
  39. 'data_free_mb' => 'float',
  40. 'avg_row_length' => 'integer',
  41. 'auto_increment' => 'integer',
  42. 'has_time_field' => 'boolean',
  43. 'time_fields' => 'array',
  44. 'has_user_field' => 'boolean',
  45. 'user_fields' => 'array',
  46. 'has_status_field' => 'boolean',
  47. 'status_fields' => 'array',
  48. 'last_scanned_at' => 'datetime',
  49. 'created_at' => 'datetime',
  50. 'updated_at' => 'datetime',
  51. ];
  52. /**
  53. * 按表名筛选
  54. */
  55. public function scopeByTable($query, string $tableName)
  56. {
  57. return $query->where('table_name', $tableName);
  58. }
  59. /**
  60. * 获取大表(记录数超过指定数量)
  61. */
  62. public function scopeLargeTables($query, int $minRecords = 10000)
  63. {
  64. return $query->where('record_count', '>', $minRecords);
  65. }
  66. /**
  67. * 获取有时间字段的表
  68. */
  69. public function scopeWithTimeFields($query)
  70. {
  71. return $query->where('has_time_field', true);
  72. }
  73. /**
  74. * 获取有用户字段的表
  75. */
  76. public function scopeWithUserFields($query)
  77. {
  78. return $query->where('has_user_field', true);
  79. }
  80. /**
  81. * 获取有状态字段的表
  82. */
  83. public function scopeWithStatusFields($query)
  84. {
  85. return $query->where('has_status_field', true);
  86. }
  87. /**
  88. * 获取表大小的可读格式
  89. */
  90. public function getTableSizeHumanAttribute(): string
  91. {
  92. $mb = $this->table_size_mb;
  93. if ($mb < 1) {
  94. return round($mb * 1024, 2) . ' KB';
  95. } elseif ($mb < 1024) {
  96. return round($mb, 2) . ' MB';
  97. } else {
  98. return round($mb / 1024, 2) . ' GB';
  99. }
  100. }
  101. /**
  102. * 获取总大小(表+索引)
  103. */
  104. public function getTotalSizeMbAttribute(): float
  105. {
  106. return $this->table_size_mb + $this->index_size_mb;
  107. }
  108. /**
  109. * 获取总大小的可读格式
  110. */
  111. public function getTotalSizeHumanAttribute(): string
  112. {
  113. $mb = $this->total_size_mb;
  114. if ($mb < 1) {
  115. return round($mb * 1024, 2) . ' KB';
  116. } elseif ($mb < 1024) {
  117. return round($mb, 2) . ' MB';
  118. } else {
  119. return round($mb / 1024, 2) . ' GB';
  120. }
  121. }
  122. }