CleanupTableStats.php 3.4 KB

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