toalett-redis-timeseries/bin/example.php

49 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2020-12-28 23:32:37 +01:00
<?php
use Predis\Client;
use Toalett\Redis\Timeseries\CommandBus;
use Toalett\Redis\Timeseries\Model\Aggregation;
use Toalett\Redis\Timeseries\Model\Filter;
use Toalett\Redis\Timeseries\Model\Value;
use Toalett\Redis\Timeseries\Protocol\Command as C;
use Toalett\Redis\Timeseries\Protocol\Query as Q;
use Toalett\Redis\Timeseries\QueryService;
use Toalett\Redis\Timeseries\Timeseries;
require_once __DIR__ . '/../vendor/autoload.php';
const SERIES = 'temperature:2:32';
const LABELS = ['sensor_id' => 2, 'area_id' => 32];
$client = new Client();
$commandBus = new CommandBus($client);
$queryService = new QueryService($client);
$commandBus->dispatch(C::delete(SERIES));
$commandBus->dispatch(C::create(SERIES)->labels(LABELS));
$timeseries = new Timeseries($commandBus, SERIES);
$timeseries->add(
new Value(time() - 5, 3.2),
new Value(time() - 4, 7.8),
new Value(time() - 3, 2.3),
new Value(time() - 2, 3.4),
new Value(time() - 1, 5.4),
new Value(time() - 0, 6.4)
);
print("\n\n==> mrange (filter on area_id=32, sensor_id=(2,3,4,5), COUNT 2)\n");
$query = Q::mrange();
$query->filter(Filter::where('area_id')->is(32));
$query->filter(Filter::where('sensor_id')->isOneOf(2, 3, 4, 5));
dump($queryService->mrange($query));
print("\n\n==> mrevrange (filter on non-existing label)\n");
$query = Q::mrevrange();
$query->filter(Filter::where('some_label')->is('some_value'));
dump($queryService->mrange($query));
print("\n\n==> range (aggregating var_p, timebucket=3)\n");
$query = Q::range(SERIES)->aggregate(Aggregation::populationVariance(3));
dump($queryService->range($query));