chg: [decaying] Usage of classname instead of const, support of

`retention` taxonomy and small fix
pull/5032/head
mokaddem 2019-08-13 15:27:11 +02:00
parent beef5012f9
commit 8a6dbd033e
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 39 additions and 12 deletions

View File

@ -277,7 +277,7 @@ class DecayingModel extends AppModel
if ($model_class === false) {
continue;
}
$available_formulas[get_class($model_class)] = $model_class::EXTENDS_FORMULA;
$available_formulas[get_class($model_class)] = get_parent_class($model_class) == 'Polynomial' || get_class($model_class) == 'Polynomial' ? 'Polynomial' : get_class($model_class);
}
return $available_formulas;
}
@ -360,7 +360,7 @@ class DecayingModel extends AppModel
$last_sighting = $this->round_timestamp_to_hour($sightings[$sighting_index]['Sighting']['date_sighting']);
$sightings[$sighting_index]['Sighting']['rounded_timestamp'] = $last_sighting;
$elapsed_time = $t - $last_sighting;
$score_overtime[$t] = $this->Computation->computeScore($model, $attribute, $base_score, $elapsed_time);
$score_overtime[$t] = $this->Computation->computeScore($model, $attribute['Attribute'], $base_score, $elapsed_time);
}
$csv = 'date,value' . PHP_EOL;
foreach ($score_overtime as $t => $v) {
@ -371,7 +371,7 @@ class DecayingModel extends AppModel
'sightings' => $sightings,
'base_score_config' => $base_score_config,
'last_sighting' => $sightings[count($sightings)-1],
'current_score' => $this->Computation->computeCurrentScore($user, $model, $attribute, $base_score, $sightings[count($sightings)-1]['Sighting']['date_sighting'])
'current_score' => $this->Computation->computeCurrentScore($user, $model, $attribute['Attribute'], $base_score, $sightings[count($sightings)-1]['Sighting']['date_sighting'])
);
}

View File

@ -1,8 +1,6 @@
<?php
abstract class DecayingModelBase
{
public const EXTENDS_FORMULA = '';
public function checkLoading()
{
return 'BONFIRE LIT';

View File

@ -3,9 +3,6 @@ include_once 'Base.php';
class Polynomial extends DecayingModelBase
{
public const EXTENDS_FORMULA = 'Polynomial';
public function computeScore($model, $attribute, $base_score, $elapsed_time)
{
if ($elapsed_time < 0) {

View File

@ -1,10 +1,27 @@
<?php
include_once 'Base.php';
include_once 'Polynomial.php';
class PolynomialExtended extends DecayingModelBase
class PolynomialExtended extends Polynomial
{
public const EXTENDS_FORMULA = 'Polynomial';
function __construct() {
// setup `retention` taxonomy
$this->Taxonomy = ClassRegistry::init('Taxonomy');
$retention_taxonomy_id = $this->Taxonomy->find('first', array(
'recursive' => -1,
'conditions' => array('LOWER(Taxonomy.namespace)' => 'retention'),
'fields' => array('id')
));
if (empty($retention_taxonomy_id)) {
throw new Exception(__('`Retention` taxonomy not available'));
} else {
$retention_taxonomy_id = $retention_taxonomy_id['Taxonomy']['id'];
}
$taxonomy = $this->Taxonomy->getTaxonomy($retention_taxonomy_id, array('full' => true));
$this->retention_taxonomy = array();
foreach ($taxonomy['entries'] as $k => $entry) {
$this->retention_taxonomy[$entry['tag']] = $entry['numerical_value'];
}
}
public function computeScore($model, $attribute, $base_score, $elapsed_time)
{
@ -14,6 +31,21 @@ class PolynomialExtended extends DecayingModelBase
$delta = $model['DecayingModel']['parameters']['delta'];
$tau = $model['DecayingModel']['parameters']['tau']*24*60*60;
$score = $base_score * (1 - pow($elapsed_time / $tau, 1 / $delta));
// handle `retention` taxonomy tags
$temp = $this->__getPrioritizedTag($attribute);
$tags = $temp['tags'];
foreach ($tags as $tag) {
$tagname = $tag['Tag']['name'];
if (isset($this->retention_taxonomy[$tagname])) {
$timestamp = intval($attribute['timestamp']);
$now = time();
$eol_time = $this->retention_taxonomy[$tagname] * 24 * 60 * 60; // `retention` taxonomy numerical_value are in seconds
if (($now - $timestamp) > $eol_time) {
return 0;
}
}
}
return $score < 0 ? 0 : $score;
}