Skip to content

Commit

Permalink
Support bucketMaxSpanSeconds and bucketRoundingSeconds in time series…
Browse files Browse the repository at this point in the history
… collections
  • Loading branch information
alcaeus committed Oct 16, 2024
1 parent d341348 commit 4ee5142
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doctrine-mongo-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@
<xs:attribute name="meta-field" type="xs:NMTOKEN" />
<xs:attribute name="granularity" type="odm:time-series-granularity" />
<xs:attribute name="expire-after-seconds" type="xs:integer" />
<xs:attribute name="bucket-max-span-seconds" type="odm:time-series-group-seconds" />
<xs:attribute name="bucket-rounding-seconds" type="odm:time-series-group-seconds" />
</xs:complexType>

<xs:simpleType name="time-series-granularity">
Expand All @@ -650,4 +652,11 @@
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="time-series-group-seconds">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1" />
<xs:maxInclusive value="31536000" />
</xs:restriction>
</xs:simpleType>

</xs:schema>
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/Annotations/TimeSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct(
public readonly ?string $metaField = null,
public readonly ?Granularity $granularity = null,
public readonly ?int $expireAfterSeconds = null,
public readonly ?int $bucketMaxSpanSeconds = null,
public readonly ?int $bucketRoundingSeconds = null,
) {
}
}
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ public function createDocumentCollection(string $documentName, ?int $maxTimeMs =
'metaField' => $class->timeSeriesOptions->metaField,
// ext-mongodb will automatically encode backed enums, so we can use the value directly here
'granularity' => $class->timeSeriesOptions->granularity,
'bucketMaxSpanSeconds' => $class->timeSeriesOptions->bucketMaxSpanSeconds,
'bucketRoundingSeconds' => $class->timeSeriesOptions->bucketRoundingSeconds,
],
static fn (mixed $value): bool => $value !== null,
);
Expand Down
29 changes: 29 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,35 @@ public function testTimeSeriesMappingWithExpireAfterSeconds(): void

self::assertSame(10, $metadata->timeSeriesOptions->expireAfterSeconds);
}

public function testTimeSeriesMappingWithBucketMaxSpanSeconds(): void
{
$metadata = $this->dm->getClassMetadata(TimeSeriesTestDocument::class);
$metadata->markAsTimeSeries(new ODM\TimeSeries('time', bucketMaxSpanSeconds: 10));

// We don't throw for invalid settings here, e.g. bucketMaxSpanSeconds not being equal to bucketRoundingSeconds
self::assertSame(10, $metadata->timeSeriesOptions->bucketMaxSpanSeconds);
}

public function testTimeSeriesMappingWithBucketRoundingSeconds(): void
{
$metadata = $this->dm->getClassMetadata(TimeSeriesTestDocument::class);
$metadata->markAsTimeSeries(new ODM\TimeSeries('time', bucketRoundingSeconds: 10));

// We don't throw for invalid settings here, e.g. bucketMaxSpanSeconds not being equal to bucketRoundingSeconds
self::assertSame(10, $metadata->timeSeriesOptions->bucketRoundingSeconds);
}

public function testTimeSeriesMappingWithGranularityAndBucketMaxSpanSeconds(): void
{
$metadata = $this->dm->getClassMetadata(TimeSeriesTestDocument::class);
$metadata->markAsTimeSeries(new ODM\TimeSeries('time', granularity: Granularity::Hours, bucketMaxSpanSeconds: 15, bucketRoundingSeconds: 20));

// We don't throw for invalid settings here, e.g. bucketMaxSpanSeconds not being equal to bucketRoundingSeconds
self::assertSame(Granularity::Hours, $metadata->timeSeriesOptions->granularity);
self::assertSame(15, $metadata->timeSeriesOptions->bucketMaxSpanSeconds);
self::assertSame(20, $metadata->timeSeriesOptions->bucketRoundingSeconds);
}
}

/** @template-extends DocumentRepository<self> */
Expand Down

0 comments on commit 4ee5142

Please sign in to comment.