Skip to content
Commits on Source (2)
......@@ -22,6 +22,8 @@ use Minds\Traits\MagicAttributes;
* @method string getPreviousState()
* @method UserState setActivityPercentage(float $activityPercentage)
* @method float getActivityPercentage()
* @method UserState setStateChange(int $stateChange)
* @method int getUserState()
*/
class UserState
{
......@@ -33,6 +35,16 @@ class UserState
const STATE_RESURRECTED = 'resurrected';
const STATE_UNKNOWN = 'unknown';
const STATE_INDEXES = [
self::STATE_UNKNOWN => 0,
self::STATE_NEW => 1,
self::STATE_COLD => 2,
self::STATE_RESURRECTED => 3,
self::STATE_CURIOUS => 4,
self::STATE_CASUAL => 5,
self::STATE_CORE => 6,
];
use MagicAttributes;
/** @var int $userGuid */
......@@ -50,8 +62,12 @@ class UserState
/** @var float $activityPercentage */
private $activityPercentage;
/** @var int $stateChange */
private $stateChange;
public function export(): array
{
$this->deriveStateChange();
return [
'user_guid' => $this->userGuid,
'reference_date' => $this->referenceDateMs,
......@@ -60,15 +76,30 @@ class UserState
'activity_percentage' => $this->activityPercentage,
'reward_factor' => RewardFactor::getForUserState($this->state),
'previous_reward_factor' => RewardFactor::getForUserState($this->previousState),
'state_change' => $this->stateChange
];
}
private function deriveStateChange(): void
{
$this->stateChange = self::stateChange($this->previousState, $this->state);
}
public static function stateChange(?string $oldState, ?string $newState): int
{
$oldStateIndex = self::STATE_INDEXES[$oldState] ?? 0;
$newStateIndex = self::STATE_INDEXES[$newState] ?? 0;
return $newStateIndex - $oldStateIndex;
}
public static function fromArray(array $data): UserState {
return (new UserState())
->setUserGuid($data['user_guid'])
->setReferenceDateMs($data['reference_date'])
->setState($data['state'])
->setPreviousState($data['previous_state'])
->setActivityPercentage($data['activity_percentage']);
->setActivityPercentage($data['activity_percentage'])
->setStateChange(self::stateChange($data['previous_state'], $data['state']));
}
}
......@@ -3,6 +3,7 @@
namespace Spec\Minds\Core\Analytics\UserStates;
use Minds\Core\Analytics\UserStates\Manager;
use Minds\Core\Analytics\UserStates\UserState;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Analytics\UserStates\ActiveUsersIterator;
use Minds\Core\Analytics\UserStates\UserStateIterator;
......@@ -77,6 +78,9 @@ class ManagerSpec extends ObjectBehavior
'state' => 'curious',
'previous_state' => 'resurrected',
'activity_percentage' => '0.14',
'reward_factor' => 1,
'previous_reward_factor' => 1.5,
'state_change' => UserState::stateChange('resurrected', 'curious')
],
];
}
......
......@@ -18,7 +18,8 @@ class UserStateSpec extends ObjectBehavior
->setReferenceDateMs(1549497600)
->setState('cold')
->setPreviousState('curious')
->setActivityPercentage(0.14);
->setActivityPercentage(0.14)
->setStateChange(UserState::stateChange('curious', 'cold'));
$export = $this->export();
$export->shouldBeArray();
$export['user_guid']->shouldEqual($this->getUserGuid());
......@@ -26,5 +27,6 @@ class UserStateSpec extends ObjectBehavior
$export['state']->shouldEqual($this->getState());
$export['previous_state']->shouldEqual($this->getPreviousState());
$export['activity_percentage']->shouldEqual($this->getActivityPercentage());
$export['state_change']->shouldEqual($this->getStateChange());
}
}