Skip to content

Commit

Permalink
Implemented getStats()
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Jan 11, 2024
1 parent 3821ce3 commit 06de8d6
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions lib/Phpfastcache/Extensions/Drivers/Ravendb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Phpfastcache\Extensions\Drivers\Ravendb;

use Composer\InstalledVersions;
use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
Expand All @@ -29,7 +30,11 @@
use RavenDB\Documents\Operations\DeleteByQueryOperation;
use RavenDB\Documents\Queries\IndexQuery;
use RavenDB\Documents\Session\DocumentSession;
use RavenDB\Documents\Session\QueryStatistics;
use RavenDB\Exceptions\RavenException;
use RavenDB\Http\ServerNode;
use RavenDB\ServerWide\Operations\BuildNumber;
use RavenDB\ServerWide\Operations\GetBuildNumberOperation;

/**
* Class Driver
Expand Down Expand Up @@ -67,14 +72,6 @@ public function getHelp(): string
HELP;
}

/**
* @return DriverStatistic
*/
public function getStats(): DriverStatistic
{
return new DriverStatistic();
}

/**
* @return bool
* @throws PhpfastcacheDriverConnectException
Expand All @@ -99,14 +96,6 @@ protected function driverConnect(): bool
return true;
}

/**
* @return string
*/
protected function getDatabaseName(): string
{
return $this->getConfig()->getDatabaseName() ?: static::RAVENDB_DEFAULT_DB_NAME;
}

/**
* @param ExtendedCacheItemInterface $item
* @return ?array<string, mixed>
Expand Down Expand Up @@ -251,4 +240,39 @@ protected function driverClear(): bool
$this->instance->clear();
return true;
}

/**
* @return DriverStatistic
*/
public function getStats(): DriverStatistic
{
$nodes = $this->instance->getRequestExecutor()->getTopology()->getNodes();
/** @var BuildNumber|null $buildNumber */
$buildNumber = $this->documentStorage->maintenance()->server()->send(new GetBuildNumberOperation());

return (new DriverStatistic())
->setRawData(['build' => $buildNumber, 'nodes' => $nodes])
->setInfo(
sprintf(
'Ravendb server v%s (%s), client v%s with %s node%s configured: %s',
$buildNumber?->getFullVersion() ?? 'Unknown version',
$buildNumber?->getCommitHash() ?? '********',
InstalledVersions::getPrettyVersion('ravendb/ravendb-php-client'),
count($nodes),
count($nodes) !== 1 ? 's' : '',
implode(', ', array_map(
fn(ServerNode $node) => 'Node #' . $node->getClusterTag() . ' (' . $node->getServerRole()->getValue() . ') @ ' . $node->getUrl()->getValue(),
iterator_to_array($nodes)
))
)
);
}

/**
* @return string
*/
protected function getDatabaseName(): string
{
return $this->getConfig()->getDatabaseName() ?: static::RAVENDB_DEFAULT_DB_NAME;
}
}

0 comments on commit 06de8d6

Please sign in to comment.