Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hypertrace.core.documentstore;

import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT;
import static org.hypertrace.core.documentstore.model.config.CustomMetricConfig.VALUE_KEY;
import static org.hypertrace.core.documentstore.utils.Utils.readFileFromResource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -27,10 +29,15 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.hypertrace.core.documentstore.expression.impl.AggregateExpression;
import org.hypertrace.core.documentstore.expression.impl.ConstantExpression;
import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression;
import org.hypertrace.core.documentstore.expression.impl.RelationalExpression;
import org.hypertrace.core.documentstore.expression.impl.UnnestExpression;
import org.hypertrace.core.documentstore.expression.operators.RelationalOperator;
import org.hypertrace.core.documentstore.metric.DocStoreMetric;
import org.hypertrace.core.documentstore.model.config.CustomMetricConfig;
import org.hypertrace.core.documentstore.model.exception.DuplicateDocumentException;
import org.hypertrace.core.documentstore.model.exception.SchemaMismatchException;
import org.hypertrace.core.documentstore.model.options.MissingColumnStrategy;
Expand Down Expand Up @@ -4092,6 +4099,80 @@ void upsertHonorsQueryTimeoutOnRowLock() throws Exception {
}
}

@Nested
@DisplayName("Custom Metrics")
class CustomMetricTests {

@Test
@DisplayName("Should report grouped counts over flat columns")
void testGroupedCountOnFlatColumns() {
final CustomMetricConfig config =
CustomMetricConfig.builder()
.collectionName(FLAT_COLLECTION_NAME)
.metricName("item.count")
.documentType(DocumentType.FLAT)
.query(
Query.builder()
.addSelection(IdentifierExpression.of("item"), "item")
.addSelection(
AggregateExpression.of(COUNT, ConstantExpression.of(1)), VALUE_KEY)
.addAggregation(IdentifierExpression.of("item"))
.build())
.build();

final List<DocStoreMetric> metrics =
postgresDatastore.getDocStoreMetricProvider().getCustomMetrics(config);
metrics.forEach(metric -> assertEquals("item.count", metric.name()));

assertEquals(
Map.of(
"Soap", 3.0, "Shampoo", 2.0, "Comb", 2.0, "Mirror", 1.0, "Bottle", 1.0, "Cup", 1.0),
metrics.stream()
.collect(
Collectors.toMap(metric -> metric.labels().get("item"), DocStoreMetric::value)));
}

@Test
@DisplayName("Should report counts over an unnested array column")
void testUnnestedArrayCount() {
final CustomMetricConfig config =
CustomMetricConfig.builder()
.collectionName(FLAT_COLLECTION_NAME)
.metricName("category.count")
.documentType(DocumentType.FLAT)
.query(
Query.builder()
.addFromClause(
UnnestExpression.of(IdentifierExpression.of("categoryTags"), false))
.addSelection(IdentifierExpression.of("categoryTags"), "category")
.addSelection(
AggregateExpression.of(COUNT, ConstantExpression.of(1)), VALUE_KEY)
.addAggregation(IdentifierExpression.of("categoryTags"))
.build())
.build();

final Map<String, Double> countsByCategory =
postgresDatastore.getDocStoreMetricProvider().getCustomMetrics(config).stream()
.collect(
Collectors.toMap(
metric -> metric.labels().get("category"), DocStoreMetric::value));

assertEquals(
Map.of(
"Hygiene",
3.0,
"PersonalCare",
2.0,
"HairCare",
2.0,
"Grooming",
2.0,
"HomeDecor",
1.0),
countsByCategory);
}
}

private Collection getFlatCollectionWithQueryTimeout(String queryTimeout) {
String postgresConnectionUrl =
String.format("jdbc:postgresql://localhost:%s/", postgresContainer.getMappedPort(5432));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hypertrace.core.documentstore.Collection;
import org.hypertrace.core.documentstore.Datastore;
import org.hypertrace.core.documentstore.Document;
import org.hypertrace.core.documentstore.DocumentType;
import org.hypertrace.core.documentstore.model.config.CustomMetricConfig;
import org.hypertrace.core.documentstore.model.options.QueryOptions;
import org.hypertrace.core.documentstore.query.Query;
Expand All @@ -42,7 +43,6 @@ public abstract class BaseDocStoreMetricProviderImpl implements DocStoreMetricPr
@Override
public List<DocStoreMetric> getCustomMetrics(final CustomMetricConfig customMetricConfig) {
try {
final String collectionName = customMetricConfig.collectionName();
final Query query = customMetricConfig.query();
query
.getSelections()
Expand All @@ -57,7 +57,7 @@ public List<DocStoreMetric> getCustomMetrics(final CustomMetricConfig customMetr
}
});

final Collection collection = dataStore.getCollection(collectionName);
final Collection collection = getCollection(customMetricConfig);
final List<DocStoreMetric> metrics = new ArrayList<>();

try (final CloseableIterator<Document> iterator =
Expand Down Expand Up @@ -132,6 +132,14 @@ public List<DocStoreMetric> getCustomMetrics(final CustomMetricConfig customMetr
}
}

private Collection getCollection(final CustomMetricConfig customMetricConfig) {
if (customMetricConfig.documentType() == DocumentType.NESTED) {
return dataStore.getCollection(customMetricConfig.collectionName());
}
return dataStore.getCollectionForType(
customMetricConfig.collectionName(), customMetricConfig.documentType());
}

private String getStringLabelValue(JsonNode jsonNode) {
switch (jsonNode.getNodeType()) {
case STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.NonNull;
import lombok.Value;
import lombok.experimental.Accessors;
import org.hypertrace.core.documentstore.DocumentType;
import org.hypertrace.core.documentstore.query.Query;

@Value
Expand All @@ -15,4 +16,5 @@ public class CustomMetricConfig {
@NonNull String collectionName;
@NonNull String metricName;
@NonNull Query query;
@NonNull @Builder.Default DocumentType documentType = DocumentType.NESTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import static java.util.Collections.emptyList;
import static org.hypertrace.core.documentstore.model.options.DataFreshness.NEAR_REALTIME_FRESHNESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
Expand All @@ -13,6 +17,7 @@
import org.hypertrace.core.documentstore.Collection;
import org.hypertrace.core.documentstore.Datastore;
import org.hypertrace.core.documentstore.Document;
import org.hypertrace.core.documentstore.DocumentType;
import org.hypertrace.core.documentstore.JSONDocument;
import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression;
import org.hypertrace.core.documentstore.model.config.CustomMetricConfig;
Expand Down Expand Up @@ -65,7 +70,55 @@ public DocStoreMetric getConnectionCountMetric() {
}
};

when(mockDataStore.getCollection(collectionName)).thenReturn(mockCollection);
lenient().when(mockDataStore.getCollection(collectionName)).thenReturn(mockCollection);
}

@Nested
class CollectionResolutionTest {

@Test
void nestedDocumentType_usesGetCollection() {
when(mockCollection.query(query, queryOptions)).thenReturn(CloseableIterator.emptyIterator());
baseDocStoreMetricProvider.getCustomMetrics(customMetricConfig);
verify(mockDataStore).getCollection(collectionName);
verify(mockDataStore, never()).getCollectionForType(any(), any());
}

@Test
void flatDocumentType_usesGetCollectionForType() throws IOException {
final CustomMetricConfig flatConfig =
CustomMetricConfig.builder()
.collectionName(collectionName)
.metricName(metricName)
.query(query)
.documentType(DocumentType.FLAT)
.build();
when(mockDataStore.getCollectionForType(collectionName, DocumentType.FLAT))
.thenReturn(mockCollection);
when(mockCollection.query(query, queryOptions)).thenReturn(mockIterator);
when(mockIterator.hasNext()).thenReturn(true, false);
when(mockIterator.next())
.thenReturn(
new JSONDocument(
Map.of("label1", "l1value", "label2", "l2value", "metric_value", 5)));

final List<DocStoreMetric> result = baseDocStoreMetricProvider.getCustomMetrics(flatConfig);

assertEquals(
List.of(
DocStoreMetric.builder()
.name(metricName)
.value(5)
.labels(Map.of("label1", "l1value", "label2", "l2value"))
.build()),
result);
verify(mockDataStore, never()).getCollection(any());
}

@Test
void defaultDocumentType_isNested() {
assertEquals(DocumentType.NESTED, customMetricConfig.documentType());
}
}

@Nested
Expand Down
Loading