PHP code example of davos / graphy
1. Go to this page and download the library: Download davos/graphy library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
davos / graphy example snippets
use Davos\Graphy\Manager\Factory\ManagerFactory;
ManagerFactory::configure([
'path' => __DIR__ . '/storage/rrd',
'path_mapper' => false,
'driver' => 'ext',
'permission' => 0644,
'create_directories' => true,
'directory_permission' => 0775,
'timezone' => 'UTC',
]);
namespace App\Models;
use Davos\Graphy\Builder\DS;
use Davos\Graphy\Builder\RRA;
use Davos\Graphy\RRD;
use Davos\Graphy\ValueObjects\Duration;
final class Power extends RRD
{
protected string|Duration $step = '1s';
protected string $start = 'now-2y';
protected function dataSources(): array
{
return [
DS::name('watts')
->gauge()
->heartbeat(300)
->min(0)
->max(24000),
];
}
protected function roundRobinArchives(): array
{
return [
'raw' => RRA::average()->steps(1)->rows(864000),
'1m_avg' => RRA::average()->steps(60)->rows(129600),
'1h_avg' => RRA::average()->steps(3600)->rows(13392),
'1d_avg' => RRA::average()->steps(86400)->rows(3660),
];
}
}
Power::create('power');
Power::update('power', [
'watts' => 42,
]);
$data = Power::fetch('power')
->average()
->resolution('1m')
->start('now-1d')
->end('now')
->run()
->get();
use Davos\Graphy\Manager\Factory\ManagerFactory;
ManagerFactory::configure([
'path' => __DIR__ . '/storage/rrd',
'path_mapper' => false,
'driver' => 'ext',
'permission' => 0644,
'create_directories' => true,
'directory_permission' => 0775,
'timezone' => 'UTC',
]);
namespace App\Models;
use Davos\Graphy\Builder\DS;
use Davos\Graphy\Builder\RRA;
use Davos\Graphy\RRD;
use Davos\Graphy\ValueObjects\Duration;
final class Power extends RRD
{
protected string|Duration $step = '1s';
protected string $start = 'now-2y';
protected function dataSources(): array
{
return [
DS::name('watts')
->gauge()
->heartbeat(300)
->min(0)
->max(24000),
];
}
protected function roundRobinArchives(): array
{
return [
'raw' => RRA::average()->steps(1)->rows(864000),
'1m_avg' => RRA::average()->steps(60)->rows(129600),
'1h_avg' => RRA::average()->steps(3600)->rows(13392),
'1d_avg' => RRA::average()->steps(86400)->rows(3660),
];
}
}
'1m_avg' => RRA::average()
->steps(60)
->rows(129600),
Power::create('power');
Power::update('power', [
'watts' => 42,
]);
Power::update('power', [
1699920000 => ['watts' => 42],
1699920300 => ['watts' => 45],
]);
Power::update('power', [
[
'time' => 1699920000,
'values' => ['watts' => 42],
],
[
'time' => 1699920300,
'values' => ['watts' => 45],
],
]);
$data = Power::fetch('power')
->average()
->resolution('1m')
->start('now-1d')
->end('now')
->run()
->get();
->average()
->min()
->max()
->last()
$fetcher = Power::fetch('power', chunkSize: 5_000)
->average()
->resolution('1m')
->start('now-1y')
->run();
$data = Power::fetch('power')
->fromArchive('1h_avg')
->run()
->get();
/**
* @var array{
* timestamps: list<int|string|null|false>,
* datasets: array<string, list<float|null>>
* } $data
*/
[
'timestamps' => [
1721347200,
1721347260,
],
'datasets' => [
'watts' => [
124.5,
null,
],
],
]
$cursor = Power::fetch('power')
->average()
->resolution('1m')
->start('now-1y')
->run()
->cursor();
foreach ($cursor as $timestamp => $datasets) {
$watts = $datasets['watts'];
// Process or stream the row without building the complete get() array.
}
use Davos\Graphy\Fetch\Group\Interval\DayInterval;
$data = Power::fetch('power')
->average()
->resolution('1m')
->start('now-30d')
->end('now')
->timezone('Europe/Sofia')
->run()
->group(DayInterval::for(1))
->labels(DayInterval::for(1))
->get();
use Davos\Graphy\Fetch\Group\Interval\SecondInterval;
use Davos\Graphy\Fetch\Group\Interval\MinuteInterval;
use Davos\Graphy\Fetch\Group\Interval\HourInterval;
use Davos\Graphy\Fetch\Group\Interval\DayInterval;
use Davos\Graphy\Fetch\Group\Interval\WeekInterval;
use Davos\Graphy\Fetch\Group\Interval\MonthInterval;
use Davos\Graphy\Fetch\Group\Interval\YearInterval;
DayInterval::for(1); // daily
DayInterval::for(7); // every seven days
WeekInterval::for(2); // every two weeks
$fetcher->group(DayInterval::for(1));
$fetcher->labels(
DayInterval::for(1),
'Y-m-d',
);
$data = Power::fetch('power')
->average()
->resolution('1h')
->start('now-7d')
->timezone('Europe/Sofia')
->run()
->labels(DayInterval::for(1), 'Y-m-d')
->get();
// Preserve the Unix timestamp.
->labels(DayInterval::for(1), 'Y-m-d', 'ts')
// Return null for non-boundary points.
->labels(DayInterval::for(1), 'Y-m-d', null)
// Return false for non-boundary points.
->labels(DayInterval::for(1), 'Y-m-d', false)
// Format non-boundary points with another date format.
->labels(DayInterval::for(1), 'Y-m-d', 'H:i')
DS::name('watts')
DS::name('watts')
->gauge()
->heartbeat(300)
->min(0)
->max(24000);
DS::name('kilowatts')
->compute('watts,1000,/');
RRA::average()
RRA::min()
RRA::max()
RRA::last()
RRA::average()
->xff(0.5)
->steps(60)
->rows(129600);
'30'
'30s'
'5m'
'2h'
'7d'
'2w'
'3M'
'1y'
->start(1721347200) // Unix timestamp as an integer
->start('now-1d')
->start('end-30m')
->start('start+2h')
->start('2026-07-20 12:00:00')
->end('now')
use Davos\Graphy\Create\CreateOptions;
use Davos\Graphy\ValueObjects\Flag;
Power::create('power', [
new Flag(CreateOptions::NO_OVERWRITE),
]);
CreateOptions::STEP
CreateOptions::START
CreateOptions::NO_OVERWRITE
CreateOptions::DAEMON
CreateOptions::TEMPLATE
CreateOptions::FROM_SOURCE
use Davos\Graphy\Update\UpdateOptions;
use Davos\Graphy\ValueObjects\Flag;
Power::update(
'power',
['watts' => 42],
[
new Flag(UpdateOptions::SKIP_PAST_UPDATES),
],
);
UpdateOptions::TEMPLATE
UpdateOptions::SKIP_PAST_UPDATES
UpdateOptions::DAEMON
FetchOptions::RESOLUTION
FetchOptions::START
FetchOptions::END
FetchOptions::ALIGN_START
FetchOptions::DAEMON
'path_mapper' => false,
ManagerFactory::configure([
'path' => __DIR__ . '/storage/rrd',
'path_mapper' => function (string $file): string {
[$category, $id] = explode('_', $file, 2);
$bucket = ((int) $id) % 5;
return match ($category) {
'zone' => "zones/{$bucket}/{$file}",
'traffic' => "traffic/{$bucket}/{$file}",
'client' => "clients/{$bucket}/{$file}",
default => $file,
};
},
'driver' => 'ext',
'permission' => 0644,
'create_directories' => true,
'directory_permission' => 0775,
'timezone' => 'UTC',
]);
Zone::create('zone_123');
use Davos\Graphy\Shared\Exceptions\CommandDefinitionException;
use Davos\Graphy\Shared\Exceptions\ConfigException;
use Davos\Graphy\Shared\Exceptions\RrdToolExecutionException;
try {
$data = Power::fetch('power')
->average()
->resolution('1m')
->start('now-1d')
->run()
->get();
} catch (CommandDefinitionException $exception) {
// Invalid Graphy command definition:
// missing archive, invalid range, invalid grouping resolution, etc.
} catch (RrdToolExecutionException $exception) {
// The RRD extension or RRDTool rejected the generated command.
} catch (ConfigException $exception) {
// Missing or invalid Graphy configuration.
}
RRA::average()->steps(60)->rows(1000)
->average()
->resolution('1m')
text
storage/rrd/zones/3/zone_123.rrd
bash
composer install
php examples/watts/fill.php
php -S 127.0.0.1:8080 -t examples/watts