public function finalize()
{
$app = Yii::$app;
$request = $app->request;
if (!$this->user_id && $request instanceof \yii\web\Request) {
$user = $app->user;
$this->user_id = $user->isGuest ? 0 : $user->id;
}
$this->duration = microtime(true) - YII_BEGIN_TIME;
$this->memory_max = memory_get_peak_usage();
return $this->save(false, ['duration', 'memory_max', 'user_id']);
}