feat(analytics): 数据分析页增加访问与受控设备的国家维度分布

参考受控版本分布的饼图实现,新增国家维度分布:
- AnalyticsDailyDim 增加 KIND_VISIT_COUNTRY / KIND_DEVICE_COUNTRY 常量
- AnalyticsReportService::aggregateDay() 按 country 聚合访问 UV 与受控设备数
- present() 汇总并返回 by_visit_country / by_device_country(ISO 代码转中文名展示)
- 视图新增两个国家饼图卡片与两个国家明细标签区

性能与正确性修复:
- ensureCached() 改为一次查询校验所有按日聚合 kind 的缓存完整性,
  避免旧缓存完整时新维度不被聚合的问题
- 新增迁移为 page_visits / devices 建立 (created_at, country) 复合索引,
  对齐 os_version 维度的索引做法,加速按日期范围 + GROUP BY country 的聚合
This commit is contained in:
hashbro
2026-09-26 19:49:25 +00:00
parent 2c974c8263
commit 8d5ec411f1
4 changed files with 177 additions and 5 deletions
@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 为数据分析页国家维度聚合与设备列表国家筛选添加复合索引。
* 参考 os_version 维度的 (created_at, os, os_version) 复合索引做法,
* 让按日期范围 + GROUP BY country 的聚合查询能走索引扫描。
*/
public function up(): void
{
Schema::table('page_visits', function (Blueprint $table) {
$table->index(['created_at', 'country'], 'page_visits_created_at_country_index');
});
Schema::table('devices', function (Blueprint $table) {
$table->index(['created_at', 'country'], 'devices_created_at_country_index');
});
}
public function down(): void
{
Schema::table('page_visits', function (Blueprint $table) {
$table->dropIndex('page_visits_created_at_country_index');
});
Schema::table('devices', function (Blueprint $table) {
$table->dropIndex('devices_created_at_country_index');
});
}
};