Skip to content
Snippets Groups Projects
Commit c5687696 authored by Tibo's avatar Tibo
Browse files

refactor lastrecords method and enable caching

parent 646cfd51
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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 = [];
......
......@@ -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) {
......
......@@ -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) {
......
......@@ -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 [];
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment