CleanupTableStats.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * 可批量赋值的字段
  18. */
  19. protected $fillable = [
  20. 'table_name',
  21. 'record_count',
  22. 'table_size_mb',
  23. 'index_size_mb',
  24. 'data_free_mb',
  25. 'avg_row_length',
  26. 'auto_increment',
  27. 'engine',
  28. 'collation',
  29. 'has_time_field',
  30. 'time_fields',
  31. 'has_user_field',
  32. 'user_fields',
  33. 'has_status_field',
  34. 'status_fields',
  35. 'last_scanned_at',
  36. ];
  37. // field end
  38. /**
  39. * 字段类型转换
  40. */
  41. protected $casts = [
  42. 'record_count' => 'integer',
  43. 'table_size_mb' => 'float',
  44. 'index_size_mb' => 'float',
  45. 'data_free_mb' => 'float',
  46. 'avg_row_length' => 'integer',
  47. 'auto_increment' => 'integer',
  48. 'has_time_field' => 'boolean',
  49. 'time_fields' => 'array',
  50. 'has_user_field' => 'boolean',
  51. 'user_fields' => 'array',
  52. 'has_status_field' => 'boolean',
  53. 'status_fields' => 'array',
  54. 'last_scanned_at' => 'datetime',
  55. 'created_at' => 'datetime',
  56. 'updated_at' => 'datetime',
  57. ];
  58. /**
  59. * 按表名筛选
  60. */
  61. public function scopeByTable($query, string $tableName)
  62. {
  63. return $query->where('table_name', $tableName);
  64. }
  65. /**
  66. * 获取大表(记录数超过指定数量)
  67. */
  68. public function scopeLargeTables($query, int $minRecords = 10000)
  69. {
  70. return $query->where('record_count', '>', $minRecords);
  71. }
  72. /**
  73. * 获取有时间字段的表
  74. */
  75. public function scopeWithTimeFields($query)
  76. {
  77. return $query->where('has_time_field', true);
  78. }
  79. /**
  80. * 获取有用户字段的表
  81. */
  82. public function scopeWithUserFields($query)
  83. {
  84. return $query->where('has_user_field', true);
  85. }
  86. /**
  87. * 获取有状态字段的表
  88. */
  89. public function scopeWithStatusFields($query)
  90. {
  91. return $query->where('has_status_field', true);
  92. }
  93. /**
  94. * 获取表大小的可读格式
  95. */
  96. public function getTableSizeHumanAttribute(): string
  97. {
  98. $mb = $this->table_size_mb;
  99. if ($mb < 1) {
  100. return round($mb * 1024, 2) . ' KB';
  101. } elseif ($mb < 1024) {
  102. return round($mb, 2) . ' MB';
  103. } else {
  104. return round($mb / 1024, 2) . ' GB';
  105. }
  106. }
  107. /**
  108. * 获取总大小(表+索引)
  109. */
  110. public function getTotalSizeMbAttribute(): float
  111. {
  112. return $this->table_size_mb + $this->index_size_mb;
  113. }
  114. /**
  115. * 获取总大小的可读格式
  116. */
  117. public function getTotalSizeHumanAttribute(): string
  118. {
  119. $mb = $this->total_size_mb;
  120. if ($mb < 1) {
  121. return round($mb * 1024, 2) . ' KB';
  122. } elseif ($mb < 1024) {
  123. return round($mb, 2) . ' MB';
  124. } else {
  125. return round($mb / 1024, 2) . ' GB';
  126. }
  127. }
  128. }