Ejemplo n.º 1
0
 /**
  * Collect data
  */
 protected function collectData()
 {
     $data = $this->Trackdata->get($this->IndexKey);
     $time = $this->Trackdata->get(Trackdata\Object::TIME);
     $rawdata = array();
     foreach (array_merge($this->SumDifferencesKeys, $this->AvgValuesKeys) as $key) {
         $rawdata[$key] = $this->Trackdata->get($key);
     }
     foreach ($data as $i => $val) {
         if (!isset($this->Data[$val])) {
             $this->Data[$val] = array();
             foreach (array_merge($this->SumDifferencesKeys, $this->AvgValuesKeys) as $key) {
                 $this->Data[$val][$key] = 0;
             }
         }
         foreach ($this->SumDifferencesKeys as $key) {
             $prev = $i == 0 ? 0 : $rawdata[$key][$i - 1];
             $this->Data[$val][$key] += $rawdata[$key][$i] - $prev;
         }
         $timeDiff = $i == 0 ? $time[0] : $time[$i] - $time[$i - 1];
         foreach ($this->AvgValuesKeys as $key) {
             $this->Data[$val][$key] += $rawdata[$key][$i] * $timeDiff;
         }
     }
     $this->calculateAverages();
 }
Ejemplo n.º 2
0
 /**
  * Tasks after insertion
  */
 protected function after()
 {
     parent::after();
     if (Cache::is('trackdata' . $this->NewObject->activityID())) {
         Cache::delete('trackdata' . $this->NewObject->activityID());
     }
 }
Ejemplo n.º 3
0
 /**
  * Calculate by trackdata
  * @param \Runalyze\Model\Trackdata\Object $trackdata
  */
 public function calculateByTrackdata(Trackdata\Object $trackdata)
 {
     if (!$trackdata->has(Trackdata\Object::HEARTRATE)) {
         return;
     }
     return $this->calculateByHeartrate(new TimeSeries($trackdata->get(Trackdata\Object::HEARTRATE), $trackdata->get(Trackdata\Object::TIME)));
 }
Ejemplo n.º 4
0
 /**
  * Calculate average stride length
  * @return int [cm]
  */
 public function average()
 {
     if (empty($this->Strides)) {
         return 0;
     }
     $Series = new TimeSeries($this->Strides, $this->Trackdata->time());
     $Series->calculateStatistic();
     return round($Series->mean());
 }
Ejemplo n.º 5
0
 /**
  * Init data
  * @param \Runalyze\Model\Trackdata $trackdata
  * @param string $key
  */
 protected function fillGaps(Trackdata $trackdata, $key)
 {
     $data = $trackdata->get($key);
     $last = $data[0];
     foreach ($data as &$val) {
         if ($val == 0) {
             $val = $last;
         }
         $last = $val;
     }
     $trackdata->set($key, $data);
 }
 /**
  * Construct collector
  * @param \Runalyze\Model\Trackdata\Object $trackdata
  * @param enum $key
  * @param \Runalyze\Model\Swimdata\Object $swimdata
  * @throws \InvalidArgumentException
  */
 public function __construct(Trackdata $trackdata, $key, Swimdata $swimdata)
 {
     if (!$swimdata->has($key)) {
         throw new \InvalidArgumentException('Swimdata has no data for "' . $key . '".');
     }
     $this->Key = $key;
     $this->Precision = Configuration::ActivityView()->plotPrecision();
     $this->KnowsDistance = $trackdata->has(Trackdata::DISTANCE);
     $this->init($trackdata);
     $this->LoopSwimdata = new Loop($swimdata);
     $this->collect();
 }
Ejemplo n.º 7
0
 /**
  * Run fast smoothing for step size
  * 
  * Although this does not look nice and is not the cleanest code,
  * direct access to the arrays is approx. 5-times faster.
  * (0.02s vs 0.11s for an array of 10.000 elements)
  */
 protected function runFastSmoothingForSteps()
 {
     $distance = $this->Trackdata->distance();
     $time = $this->Trackdata->time();
     $lastDist = 0;
     $lastTime = 0;
     foreach ($distance as $i => $dist) {
         if ($i != 0 && $i % $this->StepSize == 0) {
             $pace = $dist - $lastDist > 0 ? round(($time[$i] - $lastTime) / ($dist - $lastDist)) : 0;
             if ($this->KeepArraySize) {
                 for ($j = 0; $j < $this->StepSize; ++$j) {
                     $this->Smoothed[] = $pace;
                 }
             } else {
                 $this->Smoothed[] = $pace;
             }
             $lastDist = $dist;
             $lastTime = $time[$i];
         }
     }
     if ($this->KeepArraySize && isset($i)) {
         $pace = $dist - $lastDist > 0 ? round(($time[$i] - $lastTime) / ($dist - $lastDist)) : 0;
         for ($j = count($this->Smoothed), $num = $this->Trackdata->num(); $j < $num; ++$j) {
             $this->Smoothed[] = $pace;
         }
     }
 }
Ejemplo n.º 8
0
 public function testSimpleUpdate()
 {
     $Inserter = new Inserter($this->PDO, new Object(array(Object::ACTIVITYID => 1, Object::TIME => array(20, 40, 60), Object::DISTANCE => array(0.1, 0.2, 0.3), Object::HEARTRATE => array(100, 120, 130))));
     $Inserter->setAccountID(1);
     $Inserter->insert();
     $Track = new Object($this->PDO->query('SELECT * FROM `' . PREFIX . 'trackdata` WHERE `activityid`=1')->fetch(PDO::FETCH_ASSOC));
     $Track->set(Object::HEARTRATE, array(120, 140, 150));
     $Changed = clone $Track;
     $Changed->set(Object::DISTANCE, array(0.15, 0.3, 0.45));
     $Updater = new Updater($this->PDO, $Changed, $Track);
     $Updater->setAccountID(1);
     $Updater->update();
     $Result = new Object($this->PDO->query('SELECT * FROM `' . PREFIX . 'trackdata` WHERE `activityid`=1')->fetch(PDO::FETCH_ASSOC));
     $this->assertEquals(1, $Result->activityID());
     $this->assertEquals(array(20, 40, 60), $Result->time());
     $this->assertEquals(array(0.15, 0.3, 0.45), $Result->distance());
     $this->assertEquals(array(100, 120, 130), $Result->heartRate());
 }
Ejemplo n.º 9
0
 /**
  * Update power for trackdata
  * @param array $powerData
  */
 protected function updatePowerForTrackdata(array $powerData)
 {
     if (NULL !== $this->Trackdata && (empty($powerData) && $this->Trackdata->has(Model\Trackdata\Object::POWER) || !empty($powerData) && !$this->Trackdata->has(Model\Trackdata\Object::POWER))) {
         $this->Trackdata->set(Model\Trackdata\Object::POWER, $powerData);
         $TrackdataUpdater = new Model\Trackdata\Updater($this->PDO);
         $TrackdataUpdater->setAccountID($this->AccountID);
         $TrackdataUpdater->update($this->Trackdata, array(Model\Trackdata\Object::POWER));
     }
 }
Ejemplo n.º 10
0
 /**
  * Calculate power
  */
 protected function calculatePower()
 {
     if (\Runalyze\Context::Factory()->sport($this->Object->sportid())->hasPower() && Configuration::ActivityForm()->computePower() && NULL !== $this->Trackdata) {
         $Calculator = new \Runalyze\Calculation\Power\Calculator($this->Trackdata, $this->Route);
         $Calculator->calculate();
         $this->Trackdata->set(Model\Trackdata\Object::POWER, $Calculator->powerData());
         $this->Object->set(Object::POWER, $Calculator->average());
     }
 }
Ejemplo n.º 11
0
 /**
  * Add icon for current pause
  */
 protected function addCurrentPauseIcon()
 {
     $Pause = $this->Trackdata->pauses()->at($this->PauseIndex);
     $Index = $this->RouteLoop->index();
     $Tooltip = sprintf(__('<strong>Pause</strong> of %s'), Duration::format($Pause->duration()));
     $Tooltip .= '<br>' . sprintf(__('<strong>Distance:</strong> %s'), Distance::format($this->Trackdata->at($Index, Trackdata\Object::DISTANCE)));
     $Tooltip .= '<br>' . sprintf(__('<strong>Time:</strong> %s'), Duration::format($this->Trackdata->at($Index, Trackdata\Object::TIME)));
     if ($Pause->hasHeartRateInfo()) {
         $Tooltip .= '<br>' . sprintf(__('<strong>Heart rate:</strong>') . ' ' . __('%s to %s'), $Pause->hrStart(), $Pause->hrEnd() . ' bpm');
     }
     $this->addMarker($this->Route->at($Index, Route\Object::LATITUDES), $this->Route->at($Index, Route\Object::LONGITUDES), $this->pauseIcon(), $Tooltip);
 }
Ejemplo n.º 12
0
 /**
  * Update stride length
  */
 protected function updateStrideLength()
 {
     if ($this->hasChanged(Object::SPORTID) || true) {
         if ($this->NewObject->sportid() == Configuration::General()->runningSport()) {
             if (NULL !== $this->Trackdata && $this->Trackdata->has(Model\Trackdata\Object::CADENCE)) {
                 $Calculator = new \Runalyze\Calculation\StrideLength\Calculator($this->Trackdata);
                 $Calculator->calculate();
                 $this->NewObject->set(Object::STRIDE_LENGTH, $Calculator->average());
             } elseif ($this->NewObject->cadence() > 0) {
                 $this->NewObject->set(Object::STRIDE_LENGTH, \Runalyze\Calculation\StrideLength\Calculator::forActivity($this->NewObject));
             }
         } else {
             $this->NewObject->set(Object::STRIDE_LENGTH, 0);
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * Calculate swim values
  */
 protected function calculateSwimValues()
 {
     if (null !== $this->Trackdata && null !== $this->Swimdata) {
         if ($this->Swimdata->stroke()) {
             $this->Object->set(Object::TOTAL_STROKES, array_sum($this->Swimdata->stroke()));
         }
         if ($this->Object->totalStrokes() && $this->Trackdata->totalTime()) {
             $num = $this->Trackdata->num();
             $totaltime = $this->Trackdata->totalTime();
             $totalstrokes = $this->Object->totalStrokes();
             if (!empty($totalstrokes) && !empty($totaltime) & !empty($num) && $totalstrokes != 0) {
                 $this->Object->set(Object::SWOLF, round(($totalstrokes + $totaltime) / $num));
             }
         }
     }
 }
Ejemplo n.º 14
0
 /**
  * Save changes for trackdata
  */
 protected function saveChangesForTrackdata()
 {
     if (null === $this->Trackdata || $this->OldTrackdata->isEmpty()) {
         return;
     }
     $this->Trackdata->synchronize();
     if ($this->Trackdata->isEmpty()) {
         $Deleter = new Model\Trackdata\Deleter($this->PDO, $this->Trackdata);
         $Deleter->setAccountID($this->AccountID);
         $Deleter->delete();
         $this->Trackdata = null;
     } else {
         $Updater = new Model\Trackdata\Updater($this->PDO, $this->Trackdata, $this->OldTrackdata);
         $Updater->setAccountID($this->AccountID);
         $Updater->update();
     }
 }
Ejemplo n.º 15
0
 public function testSimpleInsert()
 {
     $T = new Object(array(Object::ACTIVITYID => 1, Object::TIME => array(20, 40, 60), Object::DISTANCE => array(0.1, 0.2, 0.3), Object::HEARTRATE => array(100, 120, 130)));
     $T->pauses()->add(new Pause(40, 10));
     $I = new Inserter($this->PDO, $T);
     $I->setAccountID(1);
     $I->insert();
     $data = $this->PDO->query('SELECT * FROM `' . PREFIX . 'trackdata` WHERE `accountid`=1')->fetch(PDO::FETCH_ASSOC);
     $N = new Object($data);
     $this->assertEquals(1, $N->activityID());
     $this->assertEquals(array(20, 40, 60), $N->time());
     $this->assertEquals(array(0.1, 0.2, 0.3), $N->distance());
     $this->assertEquals(array(100, 120, 130), $N->heartRate());
     $this->assertEmpty($N->pace());
     $this->assertFalse($N->pauses()->isEmpty());
 }
Ejemplo n.º 16
0
 /**
  * Calculate trimp
  * @return int
  */
 public function calculateTrimp()
 {
     if ($this->knowsTrackdata() && $this->Trackdata->has(Model\Trackdata\Object::HEARTRATE)) {
         $Collector = new Trimp\DataCollector($this->Trackdata->heartRate(), $this->Trackdata->time());
         $data = $Collector->result();
     } elseif ($this->Activity->hrAvg() > 0) {
         $data = array($this->Activity->hrAvg() => $this->Activity->duration());
     } else {
         $Factory = Context::Factory();
         if ($this->Activity->typeid() > 0) {
             $data = array($Factory->type($this->Activity->typeid())->hrAvg() => $this->Activity->duration());
         } else {
             $data = array($Factory->sport($this->Activity->sportid())->avgHR() => $this->Activity->duration());
         }
     }
     $Athlete = Context::Athlete();
     $Calculator = new Trimp\Calculator($Athlete, $data);
     return round($Calculator->value());
 }
Ejemplo n.º 17
0
 /**
  * Calculate pace array
  * @param \Runalyze\Model\Trackdata\Object $trackdata
  */
 public function __construct(Trackdata\Object $trackdata)
 {
     if ($trackdata->has(Trackdata\Object::TIME) && $trackdata->has(Trackdata\Object::DISTANCE)) {
         $this->Smoother = new PaceSmoother($trackdata, true);
     }
 }
Ejemplo n.º 18
0
 /**
  * Keys to insert
  * @return array
  */
 protected function keys()
 {
     return array_merge(array(self::ACCOUNTID), Object::allProperties());
 }
Ejemplo n.º 19
0
 /**
  * @return boolean
  */
 public function hasTrackdata()
 {
     return !$this->Trackdata->isEmpty();
 }
Ejemplo n.º 20
0
 /**
  * Define x-axis
  * @param \Runalyze\Model\Trackdata\Object $trackdata
  */
 protected function defineXAxis(Trackdata $trackdata)
 {
     if (Configuration::ActivityView()->usesTimeAsXAxis() && $trackdata->has(Trackdata::TIME) && $trackdata->totalTime() > 0) {
         $this->XAxis = self::X_AXIS_TIME;
     } elseif ($trackdata->has(Trackdata::DISTANCE) && $trackdata->totalDistance() > 0) {
         $this->XAxis = self::X_AXIS_DISTANCE;
     } elseif ($trackdata->has(Trackdata::TIME) && $trackdata->totalTime() > 0) {
         $this->XAxis = self::X_AXIS_TIME;
     } else {
         $this->XAxis = self::X_AXIS_INDEX;
     }
 }
Ejemplo n.º 21
0
 /**
  * @param \Runalyze\Model\Trackdata\Object $Object
  * @param array $AdditionalData
  */
 protected function addTrackdataAveragesToDataFrom(Trackdata\Object $Object, array &$AdditionalData)
 {
     $KeysToAverage = array(Activity\Object::CADENCE => Trackdata\Object::CADENCE, Activity\Object::GROUNDCONTACT => Trackdata\Object::GROUNDCONTACT, Activity\Object::VERTICAL_OSCILLATION => Trackdata\Object::VERTICAL_OSCILLATION);
     $NewLoop = new Trackdata\Loop($Object);
     $NewLoop->goToEnd();
     foreach ($KeysToAverage as $objectKey => $trackdataKey) {
         if ($Object->has($trackdataKey)) {
             $AdditionalData[$objectKey] = $NewLoop->average($trackdataKey);
         }
     }
 }
Ejemplo n.º 22
0
 public function fillSwolfArray(Trackdata\Object &$trackdata)
 {
     if ($this->stroke() && $trackdata->has(Trackdata\Object::TIME)) {
         $TrackLoop = new Trackdata\Loop($trackdata);
         $Loop = new Loop($this);
         $max = $Loop->num();
         $swolf = array();
         $swolfcycles = array();
         for ($i = 1; $i <= $max; ++$i) {
             $duration = $TrackLoop->difference(Trackdata\Object::TIME);
             $swolf[] = $duration + $Loop->stroke();
             $swolfcycles[] = $duration + $Loop->stroke() / 2;
             $Loop->nextStep();
             $TrackLoop->nextStep();
         }
         $this->set(Object::SWOLF, $swolf);
         $this->set(Object::SWOLFCYCLES, $swolfcycles);
     }
 }