table($table)->count();
self::$benchmarkStartTime = microtime(true);
self::$benchmarkStartMemory = memory_get_usage();
DB::connection('mongodb')->enableQueryLog();
}
protected static function endBenchmark(string $table): void
{
$executionTime = microtime(true) - self::$benchmarkStartTime;
$memoryUsage = round((memory_get_usage() - self::$benchmarkStartMemory) / 1024 / 1024, 2);
$logs = DB::connection('mongodb')->getQueryLog();
$queriesCount = \count($logs);
// Get row count after we've stopped tracking queries
$rowDiff = DB::connection('mongodb')->table($table)->count() - self::$startRowCount;
$formattedTime = match (true) {
$executionTime >= 60 => sprintf('%dm %ds', floor($executionTime / 60), $executionTime % 60),
$executionTime >= 1 => round($executionTime, 2).'s',
default => round($executionTime * 1000).'ms',
};
$str = '⚡ TIME: '.$formattedTime;
$str .= '
MEM: '.$memoryUsage.'MB';
$str .= '
SQL: '.number_format($queriesCount);
$str .= '
ROWS: '.number_format($rowDiff);
$recipient = auth()->user();
Notification::make()
->success()
->title('Import compleated')
->body(new HtmlString($str))
->sendToDatabase($recipient);
}
private static function importPlayer(string $filePath): void
{
$now = now()->format('Y-m-d H:i:s');
SimpleExcelReader::create(storage_path('app/private/'.$filePath))
->getRows()
->chunk(1000)
->each(function (LazyCollection $chunk) use ($now) {
$data = [];
foreach ($chunk as $row) {
$row = array_values($row);
// check if name exists
if (is_string(preg_replace("'\W'", '', $row[1]))) {
$data[] = [
'player_id' => $row[0],
'name' => $row[1],
'birthdate' => Carbon::parse($row[2])->toDateString(),
'height' => $row[3],
'nationality' => $row[4],
'position' => $row[5],
'sport_radar_id' => $row[6],
'id_squadra_tm' => $row[7],
'full_name' => $row[8],
'no_specialities' => $row[9],
'rigorist' => $row[10],
'national_team_id' => $row[11],
'last_update' => Carbon::parse($row[12])->toDateString(),
'league_code' => $row[13],
'team_code' => $row[14],
'team_name' => $row[15],
'created_at' => $now,
'updated_at' => $now,
];
}
}
StatistikoPlayer::upsert($data, 'player_id');
});
}
private static function importEvents(string $filePath): void
{
$now = now()->format('Y-m-d H:i:s');
SimpleExcelReader::create(storage_path('app/private/'.$filePath))
->getRows()
->chunk(1000)
->each(function (LazyCollection $chunk) use ($now) {
$data = [];
foreach ($chunk as $row) {
$row = array_values($row);
dd($row);
$data[] = [
'sport_id' => 1,
'league_id' => $row[1],
'home_id' => $row[2],
'away_id' => $row[3],
'starttime' => Carbon::parse($row[4])->toDateTimeString(),
'season' => $row[5],
'gls_h' => $row[6],
'gls_a' => $row[7],
'crn_h' => $row[8],
'fls_h' => $row[9],
'ofs_h' => $row[10],
'snt_h' => $row[11],
'tsh_h' => $row[12],
'crd_h' => $row[13],
'crn_a' => $row[14],
'fls_a' => $row[15],
'ofs_a' => $row[16],
'snt_a' => $row[17],
'tsh_a' => $row[18],
'crd_a' => $row[19],
'expgoalshome_value' => $row[20],
'expgoalsaway_value' => $row[21],
'exp_handicap' => $row[22],
'exp_total' => $row[23],
'created_at' => $now,
'updated_at' => $now,
];
}
StatistikoPlayer::upsert($data, 'player_id');
});
}
}