public function analyzeTableDecisions($table_id, $variant_id) { $table = $this->read($table_id); $mongo = new Manager(sprintf("mongodb://%s:%d", env('DB_HOST'), env('DB_PORT'))); $query = new Query(['table._id' => new ObjectID($table_id), 'table.variant._id' => new ObjectId($variant_id), 'applications' => ApplicationableHelper::getApplicationId(), 'created_at' => ['$gte' => new UTCDatetime($table->updated_at->timestamp * 1000)]], ['projection' => ['rules' => 1]]); $decisions = $mongo->executeQuery(env('DB_DATABASE') . '.' . (new Decision())->getTable(), $query)->toArray(); $map = []; if (($decisionsAmount = count($decisions)) > 0) { foreach ($decisions as $decision) { $rules = $decision->rules; foreach ($rules as $rule) { if (!isset($rule->_id)) { # ignore old decisions without Rule._id continue; } $ruleIndex = strval($rule->_id); foreach ($rule->conditions as $condition) { $index = "{$ruleIndex}@" . strval($condition->_id); if (!isset($map[$index])) { $map[$index] = ['matched' => 0, 'requests' => 0]; } if ($condition->matched === true) { $map[$index]['matched']++; } $map[$index]['requests']++; } if (!isset($map[$ruleIndex])) { $map[$ruleIndex] = ['matched' => 0, 'requests' => 0]; } $map[$ruleIndex]['requests']++; if ($rule->than === $rule->decision) { $map[$ruleIndex]['matched']++; } } } } $variant = $table->getVariantForCheck($variant_id); foreach ($variant->rules as $rule) { $ruleIndex = $rule->_id; foreach ($rule->conditions as $condition) { $index = "{$ruleIndex}@" . strval($condition->_id); if (array_key_exists($index, $map)) { $condition->probability = round($map[$index]['matched'] / $map[$index]['requests'], 5); } else { $condition->probability = null; } $condition->requests = array_key_exists($index, $map) ? $map[$index]['requests'] : 0; $rule->conditions()->associate($condition); } $ruleHasRequests = array_key_exists($ruleIndex, $map); $rule->probability = $ruleHasRequests ? round($map[$ruleIndex]['matched'] / $map[$ruleIndex]['requests'], 5) : 0; $rule->requests = $ruleHasRequests ? $map[$ruleIndex]['requests'] : 0; $variant->rules()->associate($rule); } $clonedTable = clone $table; $clonedTable->variants = []; $clonedTable->variants()->associate($variant); return $clonedTable; }
public function createOrUpdate($values, $id = null) { /** @var User $user */ $user = $id ? $this->read($id) : $this->getModel()->newInstance(); if ($user instanceof Applicationable) { ApplicationableHelper::addApplication($user); } if (array_key_exists('email', $values) and !env('ACTIVATE_ALL_USERS')) { $values['temporary_email'] = $values['email']; if ($id) { unset($values['email']); } $user->createVerifyEmailToken(); } $user->fill($values)->save(); \Event::fire($id ? new Update($user) : new Create($user)); return $user; }
public function getDecisions($size = null, $table_id = null, $variant_id = null) { /** @var \Jenssegers\Mongodb\Eloquent\Builder $query */ if ($table_id) { $query = $this->getModel()->query()->where('table._id', $table_id); if ($query->count() <= 0) { $e = new ModelNotFoundException(); $e->setModel(Decision::class); throw $e; } $query = $query->orderBy(Decision::CREATED_AT, 'DESC'); } elseif ($variant_id) { $query = $this->getModel()->query()->where('table.variant._id', $variant_id); } else { $query = Decision::orderBy(Decision::CREATED_AT, 'DESC'); } $query = $query->where('applications', ApplicationableHelper::getApplicationId()); return $this->paginateQuery($query, $size); }
public function rollback($table, $model_id, $changelog_id) { return $this->response->json($this->changelogModel->rollback($table, $model_id, $changelog_id, ['model.attributes.applications' => ApplicationableHelper::getApplicationId()])); }