Skip to content

Fix date time rounding issue for range and multirange #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/TimeSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ public function range(
?AggregationRule $rule = null
): array
{
$fromTs = $from ? (int)$from->format('Uu') / 1000 : '-';
$toTs = $to ? (int)$to->format('Uu') / 1000 : '+';
$fromTs = $from ? DateTimeUtils::timestampWithMsFromDateTime($from) : '-';
$toTs = $to ? DateTimeUtils::timestampWithMsFromDateTime($to) : '+';

$params = ['TS.RANGE', $key, $fromTs, $toTs];
if ($count !== null) {
Expand Down Expand Up @@ -292,8 +292,8 @@ public function multiRangeRaw(
?AggregationRule $rule = null
): array
{
$fromTs = $from ? (int)$from->format('Uu') / 1000 : '-';
$toTs = $to ? (int)$to->format('Uu') / 1000 : '+';
$fromTs = $from ? DateTimeUtils::timestampWithMsFromDateTime($from) : '-';
$toTs = $to ? DateTimeUtils::timestampWithMsFromDateTime($to) : '+';

$params = ['TS.MRANGE', $fromTs, $toTs];
if ($count !== null) {
Expand Down
63 changes: 63 additions & 0 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

namespace Palicao\PhpRedisTimeSeries\Tests\Integration;

use DateInterval;
use DateTime;
use DateTimeImmutable;
use Palicao\PhpRedisTimeSeries\AggregationRule;
use Palicao\PhpRedisTimeSeries\DateTimeUtils;
use Palicao\PhpRedisTimeSeries\Filter;
use Palicao\PhpRedisTimeSeries\Label;
use Palicao\PhpRedisTimeSeries\RedisClient;
Expand Down Expand Up @@ -150,4 +153,64 @@ public function testAddAndRetrieveKeysWithMultipleFilters(): void
$this->assertEquals($expectedResult, $range);
}

public function testAddAndRetrieveWithDateTimeObjectAsMultiRangeWithMultipleFilters(): void
{
$currentDate = new DateTime();
$from = $currentDate->sub(new DateInterval('P1D'));
$to = $currentDate;

$this->sut->create(
'temperature:3:11',
6000,
[new Label('sensor_id', '2'), new Label('area_id', '32')]
);
$this->sut->add(new Sample('temperature:3:11', 30, $from));
$this->sut->add(new Sample('temperature:3:11', 42, $to));

$filter = new Filter('sensor_id', '2');
$filter->add('area_id', Filter::OP_EQUALS, '32');

$range = $this->sut->multiRange($filter);

$expectedRange = [
Sample::createFromTimestamp('temperature:3:11', (float)42, DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($to->format('Y-m-d H:i:s.u'))))
];

$this->assertEquals($expectedRange, $range);
}


public function testAddAndRetrieveWithDateTimeObjectAsRange(): void
{
$from = new DateTimeImmutable('2019-11-06 20:34:17.103000');
$to = new DateTimeImmutable('2019-11-06 20:34:17.107000');

$this->sut->create(
'temperature:3:11',
null,
[new Label('sensor_id', '2'), new Label('area_id', '32')]
);

$this->sut->add(new Sample('temperature:3:11', 30, $from));
$this->sut->add(new Sample('temperature:3:11', 42, $to));

$range = $this->sut->range(
'temperature:3:11'
);

$expectedRange = [
Sample::createFromTimestamp(
'temperature:3:11',
(float)30,
DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($from->format('Y-m-d H:i:s.u')))
),
Sample::createFromTimestamp(
'temperature:3:11',
(float)42,
DateTimeUtils::timestampWithMsFromDateTime(new DateTimeImmutable($to->format('Y-m-d H:i:s.u')))
),
];

$this->assertEquals($expectedRange, $range);
}
}