diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index d07963b3..363a8a6a 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -10,6 +10,7 @@ use App\Models\Texture; use Illuminate\Support\Str; use App\Services\OptionForm; use Illuminate\Http\Request; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use App\Services\Repositories\UserRepository; @@ -18,17 +19,33 @@ class AdminController extends Controller public function chartData() { $today = Carbon::today()->timestamp; - $xAxis = []; - $userRegistration = []; - $textureUploads = []; - for ($i = 30; $i >= 0; $i--) { - $time = Carbon::createFromTimestamp($today - $i * 86400); + $xAxis = Collection::times(30, function ($number) use ($today) { + $time = Carbon::createFromTimestamp($today - (31 - $number) * 86400); + return $time->format('m-d'); + }); - $xAxis[] = $time->format('m-d'); - $userRegistration[] = User::like('register_at', $time->toDateString())->count(); - $textureUploads[] = Texture::like('upload_at', $time->toDateString())->count(); - } + $oneMonthAgo = Carbon::createFromTimestamp($today - 30 * 86400); + + $userRegistration = User::where('register_at', '>=', $oneMonthAgo) + ->select('register_at') + ->get() + ->groupBy(function ($user) { + return substr($user->register_at, 5, 5); + }) + ->map(function ($item) { + return count($item); + }); + + $textureUploads = Texture::where('upload_at', '>=', $oneMonthAgo) + ->select('upload_at') + ->get() + ->groupBy(function ($texture) { + return substr($texture->upload_at, 5, 5); + }) + ->map(function ($item) { + return count($item); + }); return [ 'labels' => [ @@ -36,7 +53,14 @@ class AdminController extends Controller trans('admin.index.texture-uploads') ], 'xAxis' => $xAxis, - 'data' => [$userRegistration, $textureUploads] + 'data' => [ + $xAxis->map(function ($day) use ($userRegistration) { + return $userRegistration->get($day) ?? 0; + }), + $xAxis->map(function ($day) use ($textureUploads) { + return $textureUploads->get($day) ?? 0; + }), + ] ]; } diff --git a/tests/AdminControllerTest.php b/tests/AdminControllerTest.php index a575c91a..03c54e11 100644 --- a/tests/AdminControllerTest.php +++ b/tests/AdminControllerTest.php @@ -22,7 +22,12 @@ class AdminControllerTest extends BrowserKitTestCase public function testChartData() { - $this->getJson('/admin/chart')->seeJsonStructure(['labels', 'xAxis', 'data']); + $this->getJson('/admin/chart') + ->seeJson(['labels' => [ + trans('admin.index.user-registration'), + trans('admin.index.texture-uploads') + ]]) + ->seeJsonStructure(['labels', 'xAxis', 'data']); } public function testCustomize()