CleanupTableStats.php 3.4 KB

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