mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
This reverts commit 1752d1efd4
.
This commit is contained in:
@@ -27,8 +27,6 @@ namespace App\Polling\Measure;
|
||||
|
||||
use DB;
|
||||
use Illuminate\Database\Events\QueryExecuted;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
class MeasurementManager
|
||||
{
|
||||
@@ -38,16 +36,20 @@ class MeasurementManager
|
||||
const NO_COLOR = "\e[0m";
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Support\Collection<MeasurementCollection>
|
||||
* @var MeasurementCollection
|
||||
*/
|
||||
private static $categories;
|
||||
private static $snmp;
|
||||
|
||||
/**
|
||||
* @var MeasurementCollection
|
||||
*/
|
||||
private static $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (self::$categories === null) {
|
||||
self::$categories = new Collection;
|
||||
self::$categories->put('snmp', new MeasurementCollection());
|
||||
self::$categories->put('db', new MeasurementCollection());
|
||||
if (self::$snmp === null) {
|
||||
self::$snmp = new MeasurementCollection();
|
||||
self::$db = new MeasurementCollection();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,20 +64,12 @@ class MeasurementManager
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update statistics for the given category
|
||||
*/
|
||||
public function record(string $category, Measurement $measurement): void
|
||||
{
|
||||
$this->getCategory($category)->record($measurement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update statistics for db operations
|
||||
*/
|
||||
public function recordDb(Measurement $measurement): void
|
||||
{
|
||||
$this->record('db', $measurement);
|
||||
self::$db->record($measurement);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,24 +77,25 @@ class MeasurementManager
|
||||
*/
|
||||
public function printChangedStats(): void
|
||||
{
|
||||
$dsStats = app('Datastore')->getStats()->map(function (MeasurementCollection $stats, $datastore) {
|
||||
return sprintf('%s%s%s: [%d/%.2fs]', self::DATASTORE_COLOR, $datastore, self::NO_COLOR, $stats->getCountDiff(), $stats->getDurationDiff());
|
||||
});
|
||||
|
||||
Log::info(sprintf(
|
||||
'>> %sSNMP%s: [%d/%.2fs] %sMySQL%s: [%d/%.2fs] %s',
|
||||
printf(
|
||||
'>> %sSNMP%s: [%d/%.2fs] %sMySQL%s: [%d/%.2fs]',
|
||||
self::SNMP_COLOR,
|
||||
self::NO_COLOR,
|
||||
$this->getCategory('snmp')->getCountDiff(),
|
||||
$this->getCategory('snmp')->getDurationDiff(),
|
||||
self::$snmp->getCountDiff(),
|
||||
self::$snmp->getDurationDiff(),
|
||||
self::DB_COLOR,
|
||||
self::NO_COLOR,
|
||||
$this->getCategory('db')->getCountDiff(),
|
||||
$this->getCategory('db')->getDurationDiff(),
|
||||
$dsStats->implode(' ')
|
||||
));
|
||||
self::$db->getCountDiff(),
|
||||
self::$db->getDurationDiff()
|
||||
);
|
||||
|
||||
app('Datastore')->getStats()->each(function (MeasurementCollection $stats, $datastore) {
|
||||
printf(' %s%s%s: [%d/%.2fs]', self::DATASTORE_COLOR, $datastore, self::NO_COLOR, $stats->getCountDiff(), $stats->getDurationDiff());
|
||||
});
|
||||
|
||||
$this->checkpoint();
|
||||
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +103,8 @@ class MeasurementManager
|
||||
*/
|
||||
public function checkpoint(): void
|
||||
{
|
||||
self::$categories->each->checkpoint();
|
||||
self::$snmp->checkpoint();
|
||||
self::$db->checkpoint();
|
||||
app('Datastore')->getStats()->each->checkpoint();
|
||||
}
|
||||
|
||||
@@ -117,7 +113,7 @@ class MeasurementManager
|
||||
*/
|
||||
public function recordSnmp(Measurement $measurement): void
|
||||
{
|
||||
$this->record('snmp', $measurement);
|
||||
self::$snmp->record($measurement);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,36 +121,22 @@ class MeasurementManager
|
||||
*/
|
||||
public function printStats(): void
|
||||
{
|
||||
$this->printSummary('SNMP', $this->getCategory('snmp'), self::SNMP_COLOR);
|
||||
$this->printSummary('SQL', $this->getCategory('db'), self::DB_COLOR);
|
||||
$this->printSummary('SNMP', self::$snmp, self::SNMP_COLOR);
|
||||
$this->printSummary('SQL', self::$db, self::DB_COLOR);
|
||||
|
||||
app('Datastore')->getStats()->each(function (MeasurementCollection $stats, string $datastore) {
|
||||
$this->printSummary($datastore, $stats, self::DATASTORE_COLOR);
|
||||
});
|
||||
}
|
||||
|
||||
public function getCategory(string $category): MeasurementCollection
|
||||
private function printSummary(string $name, MeasurementCollection $collection, string $color = ''): void
|
||||
{
|
||||
if (! self::$categories->has($category)) {
|
||||
self::$categories->put($category, new MeasurementCollection());
|
||||
}
|
||||
printf('%s%s%s [%d/%.2fs]:', $color, $name, $color ? self::NO_COLOR : '', $collection->getTotalCount(), $collection->getTotalDuration());
|
||||
|
||||
return self::$categories->get($category);
|
||||
}
|
||||
|
||||
public function printSummary(string $name, MeasurementCollection $collection, string $color = ''): void
|
||||
{
|
||||
$summaries = $collection->map(function (MeasurementSummary $stat) {
|
||||
return sprintf('%s[%d/%.2fs]', ucfirst($stat->getType()), $stat->getCount(), $stat->getDuration());
|
||||
$collection->each(function (MeasurementSummary $stat) {
|
||||
printf(' %s[%d/%.2fs]', ucfirst($stat->getType()), $stat->getCount(), $stat->getDuration());
|
||||
});
|
||||
|
||||
Log::info(sprintf('%s%s%s [%d/%.2fs]: %s',
|
||||
$color,
|
||||
$name,
|
||||
$color ? self::NO_COLOR : '',
|
||||
$collection->getTotalCount(),
|
||||
$collection->getTotalDuration(),
|
||||
$summaries->implode(' ')
|
||||
));
|
||||
echo PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user