getBody()->getContents(); $dramas = json_decode($response); $dramas = $dramas->dramas; $drama_list = []; foreach($dramas as $drama) { $drama_roles = []; foreach($drama->roles as $role) { $drama_list[$role->name] = $drama->name; } } Cache::forever('drama_names',$drama_list); } return Cache::get('drama_names'); } public static function getRanking() { $response = Guzzle::get("https://api.matsurihi.me/mltd/v1/election/current"); return json_decode($response->getBody()->getContents()); } public static function createSnapshot() { $rankings = Election::getRanking(); $hasSnapshot = true; DB::beginTransaction(); foreach($rankings as $ladder) { $existingSnapshot = Rank::where('role',$ladder->name)->where('summary_time',Carbon::parse($ladder->summaryTime))->count(); if($existingSnapshot == 0) { $idols = []; foreach($ladder->data[0] as $data) { $rank = new Rank(); $rank->role = $ladder->name; $rank->character = $data->idol_name; $idols[] = $data->idol_name; $rank->score = $data->score; $rank->summary_time = Carbon::parse($ladder->summaryTime); $rank->save(); } $remnant_idols = Rank::where('role',$ladder->name)->whereNotIn('character',$idols)->orderBy('summary_time','desc')->get()->groupBy('character'); foreach($remnant_idols as $remnant_char => $remnant) { $rank = new Rank(); $rank->role = $ladder->name; $rank->character = $remnant_char; $rank->score = $remnant->first()->score; $rank->summary_time = Carbon::parse($ladder->summaryTime); $rank->save(); \Log::error($remnant_char.' '.$remnant->first()->score); } } else { \Log::info("snapshot has been taken for " . (string)$ladder->summaryTime); } } DB::commit(); Election::generateChartPointsNew(); } public static function generateChartPointsNew() { $ranks = Rank::orderBy('summary_time')->get()->groupBy('role'); $timepoints = Rank::distinct('summary_time')->orderBy('summary_time')->pluck('summary_time'); Cache::forever('timepoints',$timepoints); foreach($ranks as $roleName => $roleRank) { $dataPoints = []; $ladder = $roleRank->sortBy('summary_time')->groupBy('character')->sortByDesc(function($rank,$key) { return $rank->last()->score; }); foreach($ladder as $character => $rank) { $standing = $rank; $standing = $standing->pad(-$timepoints->count(),(int)0); $dataPoints[$character] = $standing->pluck('score'); } Cache::forever('data_'.$roleName,$dataPoints); } } public static function generateChartPoints() { $ranks = Rank::all()->groupBy('role'); foreach($ranks as $roleName => $roleRank) { $chartPoint = []; $idols = $roleRank->unique('character')->pluck('character'); $timepoints = $roleRank->sortBy('summary_time')->unique('summary_time')->pluck('summary_time'); foreach($idols as $idol) { foreach($timepoints as $timepoint) { $chartPoint[$idol][$timepoint->toDateTimeString()] = 0; } foreach($roleRank->where('character',$idol) as $point) { $chartPoint[$idol][$point->summary_time->toDateTimeString()] = $point->score; } } Cache::forever('chart_point_'.$roleName,$chartPoint); } } }