diff --git a/app/Sensor/DiskEvolution.php b/app/Sensor/DiskEvolution.php index a92d2ea919a8dd017e9640c3a876be441e70d0ab..8cc05636dbf10110725d347067ecc5d1c6b79eb7 100644 --- a/app/Sensor/DiskEvolution.php +++ b/app/Sensor/DiskEvolution.php @@ -45,7 +45,7 @@ class DiskEvolution extends \App\AbstractSensor */ public function get2Partitions(int $timeInterval) : ?array { - $records = $this->getServer()->lastRecords("disks", $timeInterval * 12); + $records = $this->getServer()->lastRecords1Day(); if (count($records) < 2) { return null; diff --git a/app/Sensor/Ifconfig.php b/app/Sensor/Ifconfig.php index ae8b400529a4d6cdcd4062879a95f40ca30c4491..bd2ce9f6651e7be72116c6321c0de65e8687402f 100644 --- a/app/Sensor/Ifconfig.php +++ b/app/Sensor/Ifconfig.php @@ -29,7 +29,7 @@ class Ifconfig extends AbstractSensor public function points() { // Get records in time ascending order - $records = $this->getServer()->lastRecords("ifconfig", 289); + $records = $this->getServer()->lastRecords1Day(); // Compute the time ordered list of arrays of interfaces $interfaces = []; diff --git a/app/Sensor/LoadAvg.php b/app/Sensor/LoadAvg.php index 32c7462a3c746837fe7b0e6a26af5f7071994d89..c1631d703c798076a5b686729b1c1b13e641e1f9 100644 --- a/app/Sensor/LoadAvg.php +++ b/app/Sensor/LoadAvg.php @@ -21,7 +21,7 @@ class LoadAvg extends AbstractSensor public function loadPoints() { - $records = $this->getServer()->lastRecords("loadavg", 288); + $records = $this->getServer()->lastRecords1Day(); $points = []; foreach ($records as $record) { diff --git a/app/Sensor/MemInfo.php b/app/Sensor/MemInfo.php index 71874d27662756b012e06c0d295317f80f7d665f..4ed7a9c5bc5b236c88bc4e86bc4e4f63da9d0083 100644 --- a/app/Sensor/MemInfo.php +++ b/app/Sensor/MemInfo.php @@ -20,7 +20,7 @@ class MemInfo extends AbstractSensor public function usedMemoryPoints() { - $records = $this->getServer()->lastRecords("memory", 288); + $records = $this->getServer()->lastRecords1Day(); $used = []; foreach ($records as $record) { @@ -36,7 +36,7 @@ class MemInfo extends AbstractSensor public function cachedMemoryPoints() { - $records = $this->getServer()->lastRecords("memory", 288); + $records = $this->getServer()->lastRecords1Day(); $points = []; foreach ($records as $record) { diff --git a/app/Sensor/Netstat.php b/app/Sensor/Netstat.php index f3f5a9231a7783cf7d88255bf64898398d42d25f..36d0a8232d220c0b76882d50a72a953364e2786c 100644 --- a/app/Sensor/Netstat.php +++ b/app/Sensor/Netstat.php @@ -20,7 +20,7 @@ class Netstat extends AbstractSensor public function points() : array { - $records = $this->getServer()->lastRecords("netstat-statistics", 289); + $records = $this->getServer()->lastRecords1Day(); if (count($records) == 0) { return []; diff --git a/app/Server.php b/app/Server.php index a2d653794abc9a07c1f6256901063b8f1dbf55a0..4525c8089886545e831878321a096b35adbef630 100644 --- a/app/Server.php +++ b/app/Server.php @@ -16,6 +16,8 @@ class Server extends Model */ private $last_record = null; + private $records_1day = null; + private static $sensors = [ \App\Sensor\LoadAvg::class, \App\Sensor\MemInfo::class, @@ -61,34 +63,27 @@ class Server extends Model } /** - * Get the last $count records containing $field. - * !! $count is the MAXIMUM number of returned records. - * To optimize mongo's usage of index, we get the last $count records - * then filter locally for records containing this record - * Records are returned in chronological order - * @param type $field - * @param type $count + * Get the last day of data. * @return type */ - public function lastRecords(string $field, int $count) + public function lastRecords1Day() { + if ($this->records_1day !== null) { + return $this->records_1day; + } + $records = \Mongo::get()->monitoring->records->find( ["server_id" => $this->id], - ["limit" => $count, "sort" => ["_id" => -1]] - ); + ["limit" => 288, "sort" => ["_id" => -1]] + )->toArray(); - $results = []; - foreach ($records as $record) { - if (isset($record->$field)) { - $results[] = $record; - } - } - - usort($results, function ($r1, $r2) { + usort($records, function ($r1, $r2) { return $r1->time > $r2->time ? 1 : -1; }); - return $results; + $this->records_1day = $records; + + return $records; } public function hasData() : bool