bot(); if ($bot === null) { return; } $day = now()->toDateString(); $name = $bot['name'] ?? self::OTHER; if ($bot['kind'] === 'other' && $name !== self::OTHER && $this->isOneTooMany($table, $day, $name)) { $name = self::OTHER; } // One statement, so two requests at the same moment both count. DB::table($table)->upsert( [['day' => $day, 'bot' => $name, 'kind' => $bot['kind'], 'path' => Str::limit('/'.trim(Str::before($path, '?'), '/'), 255, ''), 'hits' => 1]], ['day', 'bot', 'path'], ['hits' => DB::raw($table.'.hits + 1')], ); } private function isOneTooMany(string $table, string $day, string $name): bool { $unknown = DB::table($table)->where('day', $day)->where('kind', 'other'); if ((clone $unknown)->where('bot', $name)->exists()) { return false; } return $unknown->distinct()->count('bot') >= self::UNKNOWN_NAMES_A_DAY; } }