. * * @link https://www.librenms.org * @copyright 2020 Tony Murray * @author Tony Murray */ namespace LibreNMS\Data\Measure; use Illuminate\Support\Collection; class MeasurementCollection extends Collection { public function getTotalCount() { return $this->sumStat('getCount'); } public function getTotalDuration() { return $this->sumStat('getDuration'); } public function getCountDiff() { return $this->sumStat('getCountDiff'); } public function getDurationDiff() { return $this->sumStat('getDurationDiff'); } public function checkpoint() { $this->each->checkpoint(); } public function record(Measurement $measurement) { $type = $measurement->getType(); if (! $this->has($type)) { $this->put($type, new MeasurementSummary($type)); } $this->get($type)->add($measurement); } private function sumStat($function) { return $this->reduce(function ($sum, $measurement) use ($function) { $sum += $measurement->$function(); return $sum; }, 0); } }