Skip to content

Commit

Permalink
Merge pull request #9 from MindscapeHQ/more-aggregators
Browse files Browse the repository at this point in the history
More aggregators
  • Loading branch information
Jeremy Norman authored Jan 3, 2019
2 parents cd370ce + 0134b6f commit 673d961
Show file tree
Hide file tree
Showing 51 changed files with 465 additions and 95 deletions.
50 changes: 23 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ It filters the data where the country code is 'US' and the data timestamp is wit
It then returns the total pages added by hour in a descending order.

```csharp
var response = _druidClient.Timeseries<QueryResult>(q => q
var response = _druidClient.Timeseries<T>(q => q
.Descending(true)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Added))
.Filter(new SelectorFilter(Wikiticker.Dimensions.CountryCode, "US"))
.DataSource(Wikiticker.DataSource)
.Aggregations(new LongSumAggregator("totalAdded", "added"))
.Filter(new SelectorFilter("countryIsoCode", "US"))
.DataSource("wikiticker")
.Interval(FromDate, ToDate)
.Granularity(Granularities.Hour)
);
Expand All @@ -44,16 +44,16 @@ It filters the data where the country code is 'US' and the user was anonymous an
It then returns the top 5 pages by count.

```csharp
var response = _druidClient.TopN<QueryResult>(q => q
.Metric(Wikiticker.Metrics.Count)
.Dimension(Wikiticker.Dimensions.Page)
var response = _druidClient.TopN<T>(q => q
.Metric("totalCount")
.Dimension("page")
.Threshold(5)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.Aggregations(new LongSumAggregator("totalCount", "count"))
.Filter(new AndFilter(
new SelectorFilter(Wikiticker.Dimensions.IsAnonymous, "true"),
new SelectorFilter(Wikiticker.Dimensions.CountryCode, "US")
new SelectorFilter("isAnonymous", "true"),
new SelectorFilter("countryIsoCode", "US")
))
.DataSource(Wikiticker.DataSource)
.DataSource("wikiticker")
.Interval(FromDate, ToDate)
.Granularity(Granularities.All)
);
Expand All @@ -66,10 +66,10 @@ The following example query is performing a groupBy query against the sample wik
It returns the sum of page count grouped by Country name, then by city name and finally by page name.

```csharp
var response = _druidClient.GroupBy<QueryResult>(q => q
.Dimensions(Wikiticker.Dimensions.CountryName, Wikiticker.Dimensions.CityName, Wikiticker.Dimensions.Page)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.DataSource(Wikiticker.DataSource)
var response = _druidClient.GroupBy<T>(q => q
.Dimensions("countryName", "cityName", "page")
.Aggregations(new LongSumAggregator("totalCount", "count"))
.DataSource("wikiticker")
.Interval(FromDate, ToDate)
.Granularity(Granularities.All)
);
Expand All @@ -82,12 +82,12 @@ The following example query is performing a select query against the sample wiki
It selects the country name, city name, page, added and deleted values, filtered to anonymous users and limited to 10 records.

```csharp
var response = _druidClient.Select<QueryResult>(q => q
.Dimensions(Wikiticker.Dimensions.CountryName, Wikiticker.Dimensions.CityName, Wikiticker.Dimensions.Page)
.Metrics(Wikiticker.Metrics.Added, Wikiticker.Metrics.Deleted)
var response = _druidClient.Select<T>(q => q
.Dimensions("countryName", "cityName", "page")
.Metrics("added", "deleted")
.Paging(new PagingSpec(10))
.Filter(new SelectorFilter(Wikiticker.Dimensions.IsAnonymous, "true"))
.DataSource(Wikiticker.DataSource)
.Filter(new SelectorFilter("isAnonymous", "true"))
.DataSource("wikiticker")
.Interval(FromDate, ToDate)
);
```
Expand All @@ -99,10 +99,10 @@ The following example query is performing a search query against the sample wiki
It searches for pages that contain the term "Dragon" and returns the page dimension value limited to the top 10 records.

```csharp
var response = DruidClient.Search(q => q
.DataSource(Wikiticker.DataSource)
var response = _druidClient.Search(q => q
.DataSource("wikiticker")
.Granularity(Granularities.All)
.SearchDimensions(Wikiticker.Dimensions.Page)
.SearchDimensions("page")
.Query(new ContainsSearchQuery("Dragon"))
.Limit(10)
.Interval(FromDate, ToDate)
Expand Down Expand Up @@ -132,10 +132,6 @@ solution that can be used by implementing the interface in a simple pass-through
* Union data source
* Time boundary queries
* Scan queries
* JavaScript aggregator
* Cardinality aggregator
* HyperUnique aggregator
* Filtered aggregator
* Extraction filter
* Interval filter
* Extraction dimensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Execute()
{
var response = DruidClient.GroupBy<QueryResult>(q => q
.Dimensions(Wikiticker.Dimensions.CountryName)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.Aggregations(new LongSumAggregator("totalCount", Wikiticker.Metrics.Count))
.DataSource(Wikiticker.DataSource)
.Filter(new NotFilter(new SelectorFilter(Wikiticker.Dimensions.CountryName, string.Empty)))
.Interval(FromDate, ToDate)
Expand All @@ -34,20 +34,20 @@ public void QueryHasCorrectNumberOfResults()
public void FirstResultIsCorrect()
{
Assert.That(_results.First().CountryName, Is.EqualTo("Albania"));
Assert.That(_results.First().Count, Is.EqualTo(2));
Assert.That(_results.First().TotalCount, Is.EqualTo(2));
}

[Test]
public void LastResultIsCorrect()
{
Assert.That(_results.Last().CountryName, Is.EqualTo("Zimbabwe"));
Assert.That(_results.Last().Count, Is.EqualTo(3));
Assert.That(_results.Last().TotalCount, Is.EqualTo(3));
}

private class QueryResult
{
public string CountryName { get; set; }
public int Count { get; set; }
public int TotalCount { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Execute()
{
var response = DruidClient.Timeseries<QueryResult>(q => q
.Descending(true)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Added))
.Aggregations(new LongSumAggregator("totalAdded", Wikiticker.Metrics.Added))
.Filter(new SelectorFilter(Wikiticker.Dimensions.CountryCode, "US"))
.DataSource(Wikiticker.DataSource)
.Interval(FromDate, ToDate)
Expand All @@ -35,19 +35,19 @@ public void QueryHasCorrectNumberOfResults()
public void FirstResultIsCorrect()
{
Assert.That(_results.First().Timestamp, Is.EqualTo(new DateTime(2015, 9, 12, 23, 0, 0,DateTimeKind.Utc)));
Assert.That(_results.First().Result.Added, Is.EqualTo(3913));
Assert.That(_results.First().Result.TotalAdded, Is.EqualTo(3913));
}

[Test]
public void LastResultIsCorrect()
{
Assert.That(_results.Last().Timestamp, Is.EqualTo(new DateTime(2015, 9, 12, 0, 0, 0,DateTimeKind.Utc)));
Assert.That(_results.Last().Result.Added, Is.EqualTo(88));
Assert.That(_results.Last().Result.TotalAdded, Is.EqualTo(88));
}

private class QueryResult
{
public int Added { get; set; }
public int TotalAdded { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class AsyncBasicTop10Pages : TestQueryBase
public async Task Execute()
{
var response = await DruidClient.TopNAsync<QueryResult>(q => q
.Metric(Wikiticker.Metrics.Count)
.Metric("totalCount")
.Dimension(Wikiticker.Dimensions.Page)
.Threshold(10)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.Aggregations(new LongSumAggregator("totalCount", Wikiticker.Metrics.Count))
.DataSource(Wikiticker.DataSource)
.Interval(FromDate, ToDate)
.Granularity(Granularities.All)
Expand All @@ -36,21 +36,21 @@ public void QueryHasCorrectNumberOfResults()
public void FirstResultIsCorrect()
{
Assert.That(_results.First().Page, Is.EqualTo("Wikipedia:Vandalismusmeldung"));
Assert.That(_results.First().Count, Is.EqualTo(33));
Assert.That(_results.First().TotalCount, Is.EqualTo(33));
}

[Test]
public void LastResultIsCorrect()
{
Assert.That(_results.Last().Page, Is.EqualTo("Wikipedia:Requests for page protection"));
Assert.That(_results.Last().Count, Is.EqualTo(17));
Assert.That(_results.Last().TotalCount, Is.EqualTo(17));
}

internal class QueryResult
{
public string Page { get; set; }

public int Count { get; set; }
public int TotalCount { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class BasicTop10Pages : TestQueryBase
public void Execute()
{
var response = DruidClient.TopN<QueryResult>(q => q
.Metric(Wikiticker.Metrics.Count)
.Dimension(Wikiticker.Dimensions.Page)
.Threshold(10)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.Aggregations(new LongSumAggregator("totalCount", Wikiticker.Metrics.Count))
.Metric("totalCount")
.DataSource(Wikiticker.DataSource)
.Interval(FromDate, ToDate)
.Granularity(Granularities.All)
Expand All @@ -35,21 +35,21 @@ public void QueryHasCorrectNumberOfResults()
public void FirstResultIsCorrect()
{
Assert.That(_results.First().Page, Is.EqualTo("Wikipedia:Vandalismusmeldung"));
Assert.That(_results.First().Count, Is.EqualTo(33));
Assert.That(_results.First().TotalCount, Is.EqualTo(33));
}

[Test]
public void LastResultIsCorrect()
{
Assert.That(_results.Last().Page, Is.EqualTo("Wikipedia:Requests for page protection"));
Assert.That(_results.Last().Count, Is.EqualTo(17));
Assert.That(_results.Last().TotalCount, Is.EqualTo(17));
}

internal class QueryResult
{
public string Page { get; set; }

public int Count { get; set; }
public int TotalCount { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class FilteredTop5Pages : TestQueryBase
public void Execute()
{
var response = DruidClient.TopN<QueryResult>(q => q
.Metric(Wikiticker.Metrics.Count)
.Metric("totalCount")
.Dimension(Wikiticker.Dimensions.Page)
.Threshold(5)
.Aggregations(new LongSumAggregator(Wikiticker.Metrics.Count))
.Aggregations(new LongSumAggregator("totalCount", Wikiticker.Metrics.Count))
.Filter(new AndFilter(
new SelectorFilter(Wikiticker.Dimensions.IsAnonymous, "true"),
new SelectorFilter(Wikiticker.Dimensions.CountryCode, "US")
Expand All @@ -39,21 +39,21 @@ public void QueryHasCorrectNumberOfResults()
public void FirstResultIsCorrect()
{
Assert.That(_results.First().Page, Is.EqualTo("The Naked Brothers Band (TV series)"));
Assert.That(_results.First().Count, Is.EqualTo(10));
Assert.That(_results.First().TotalCount, Is.EqualTo(10));
}

[Test]
public void LastResultIsCorrect()
{
Assert.That(_results.Last().Page, Is.EqualTo("Total Drama Presents: The Ridonculous Race"));
Assert.That(_results.Last().Count, Is.EqualTo(4));
Assert.That(_results.Last().TotalCount, Is.EqualTo(4));
}

internal class QueryResult
{
public string Page { get; set; }

public int Count { get; set; }
public int TotalCount { get; set; }
}
}
}
14 changes: 3 additions & 11 deletions Raygun.Druid4Net.Tests/Fluent/Aggregations/AggregatorTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@ namespace Raygun.Druid4Net.Tests.Fluent.Aggregations
[TestFixture]
public abstract class AggregatorTestsBase
{
protected abstract BaseAggregator GetAggregator(string name, string fieldName = null);
protected abstract BaseAggregator GetAggregator(string name, string fieldName);
protected abstract string ExpectedAggregatorType { get; }

[Test]
public void Constructor_TypeIsCorrect()
{
var aggregator = GetAggregator("test");
var aggregator = GetAggregator("test", "another");
Assert.That(aggregator.Type, Is.EqualTo(ExpectedAggregatorType));
}

[Test]
public void Constructor_WithName_NameIsSet()
{
var aggregator = GetAggregator("test");
Assert.That(aggregator.Name, Is.EqualTo("test"));
Assert.That(aggregator.FieldName, Is.EqualTo("test"));
}

[Test]
public void Constructor_WithFieldName_FieldNameIsSet()
public void Constructor_WithNameAndFieldName_ValuesAreSet()
{
var aggregator = GetAggregator("test", "another");
Assert.That(aggregator.Name, Is.EqualTo("test"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace Raygun.Druid4Net.Tests.Fluent.Aggregations
{
[TestFixture]
public class CardinalityAggregatorTests
{
[Test]
public void Constructor_TypeIsCorrect()
{
var aggregator = new CardinalityAggregator("test", new string[0]);
Assert.That(aggregator.Type, Is.EqualTo("cardinality"));
}

[Test]
public void Constructor_WithAllValuesAndDimensionSpecEnumerable_AllValuesAreSet()
{
var dim1 = new DefaultDimension("first_name");
var dim2 = new DefaultDimension("last_name");
var fields = new List<IDimensionSpec> { dim1, dim2 };
var aggregator = new CardinalityAggregator("distinct_people", fields, true, false);

Assert.That(aggregator.Name, Is.EqualTo("distinct_people"));
Assert.That(aggregator.ByRow, Is.True);
Assert.That(aggregator.Round, Is.False);
Assert.That(aggregator.Fields, Contains.Item(dim1));
Assert.That(aggregator.Fields, Contains.Item(dim2));
}

[Test]
public void Constructor_WithAllValuesAndDimensionSpecParams_AllValuesAreSet()
{
var dim1 = new DefaultDimension("first_name");
var dim2 = new DefaultDimension("last_name");
var aggregator = new CardinalityAggregator("distinct_people", true, true, dim1, dim2);

Assert.That(aggregator.Name, Is.EqualTo("distinct_people"));
Assert.That(aggregator.ByRow, Is.True);
Assert.That(aggregator.Round, Is.True);
Assert.That(aggregator.Fields, Contains.Item(dim1));
Assert.That(aggregator.Fields, Contains.Item(dim2));
}

[Test]
public void Constructor_WithAllValuesAndDimensionStringEnumerable_AllValuesAreSet()
{
var fields = new List<string> { "first_name", "last_name" };
var aggregator = new CardinalityAggregator("distinct_people", fields, false, false);

Assert.That(aggregator.Name, Is.EqualTo("distinct_people"));
Assert.That(aggregator.ByRow, Is.False);
Assert.That(aggregator.Round, Is.False);
Assert.That(aggregator.Fields.OfType<DefaultDimension>().Any(f => f.Dimension == "first_name"), Is.True);
Assert.That(aggregator.Fields.OfType<DefaultDimension>().Any(f => f.Dimension == "last_name"), Is.True);
}

[Test]
public void Constructor_WithAllValuesAndDimensionStringParams_AllValuesAreSet()
{
var aggregator = new CardinalityAggregator("distinct_people", false, true, "first_name", "last_name");

Assert.That(aggregator.Name, Is.EqualTo("distinct_people"));
Assert.That(aggregator.ByRow, Is.False);
Assert.That(aggregator.Round, Is.True);
Assert.That(aggregator.Fields.OfType<DefaultDimension>().Any(f => f.Dimension == "first_name"), Is.True);
Assert.That(aggregator.Fields.OfType<DefaultDimension>().Any(f => f.Dimension == "last_name"), Is.True);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Raygun.Druid4Net.Tests.Fluent.Aggregations
[TestFixture]
public class DoubleFirstAggregatorTests : AggregatorTestsBase
{
protected override BaseAggregator GetAggregator(string name, string fieldName = null)
protected override BaseAggregator GetAggregator(string name, string fieldName)
{
return new DoubleFirstAggregator(name, fieldName);
}
Expand Down
Loading

0 comments on commit 673d961

Please sign in to comment.