Skip to content

Commit 8a3fbc7

Browse files
author
Andrei Bozantan
authored
Merge pull request #93 from diffix/andrei/small-fixes
Andrei/small fixes
2 parents 92aa3ed + 2a77390 commit 8a3fbc7

10 files changed

Lines changed: 76 additions & 77 deletions

File tree

src/explorer.api/Explorers/BoolColumnExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override async Task Explore(CancellationToken cancellationToken)
2727
new DistinctColumnValues(TableName, ColumnName),
2828
cancellationToken);
2929

30-
var counts = distinctValuesQ.ResultRows.CountTotalAndSuppressed();
30+
var counts = ValueCounts.Compute(distinctValuesQ.ResultRows);
3131

3232
PublishMetric(new UntypedMetric(name: "distinct.suppressed_count", metric: counts.SuppressedCount));
3333

src/explorer.api/Explorers/CategoricalColumnExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override async Task Explore(CancellationToken cancellationToken)
2727
new DistinctColumnValues(TableName, ColumnName),
2828
cancellationToken);
2929

30-
var counts = distinctValuesQ.ResultRows.CountTotalAndSuppressed();
30+
var counts = ValueCounts.Compute(distinctValuesQ.ResultRows);
3131

3232
PublishMetric(new UntypedMetric(name: "distinct.suppressed_count", metric: counts.SuppressedCount));
3333

src/explorer.api/Explorers/DatetimeColumnExplorer.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Explorer
1+
namespace Explorer
22
{
33
using System;
44
using System.Collections.Generic;
@@ -142,19 +142,17 @@ private void ProcessLinearBuckets(
142142
{
143143
cancellationToken.ThrowIfCancellationRequested();
144144

145-
var label = group.Key;
146-
var valueCounts = group
147-
.Select(row => new AircloakValueCount<DateTime>(row.GroupingValue, row.Count, row.CountNoise));
148-
149-
var counts = valueCounts.CountTotalAndSuppressed();
150-
145+
var counts = ValueCounts.Compute(group);
151146
if (counts.SuppressedCountRatio > SuppressedRatioThreshold)
152147
{
153148
break;
154149
}
155150

151+
var label = group.Key;
152+
var metricValue = group
153+
.Select(row => new AircloakValueCount<DateTime>(row.GroupingValue, row.Count, row.CountNoise));
156154
PublishMetric(new UntypedMetric(name: $"dates_linear.{label}", metric: DatetimeMetric(
157-
counts.TotalCount, counts.SuppressedCount, valueCounts)));
155+
counts.TotalCount, counts.SuppressedCount, metricValue)));
158156
}
159157
}
160158

@@ -184,18 +182,16 @@ private void ProcessCyclicalBuckets(
184182
continue;
185183
}
186184

187-
var valueCounts = group
188-
.Select(row => new AircloakValueCount<int>(row.GroupingValue, row.Count, row.CountNoise));
189-
190-
var counts = valueCounts.CountTotalAndSuppressed();
191-
185+
var counts = ValueCounts.Compute(group);
192186
if (counts.SuppressedCountRatio > SuppressedRatioThreshold)
193187
{
194188
break;
195189
}
196190

191+
var metricValue = group
192+
.Select(row => new AircloakValueCount<int>(row.GroupingValue, row.Count, row.CountNoise));
197193
PublishMetric(new UntypedMetric(name: $"dates_cyclical.{label}", metric: DatetimeMetric(
198-
counts.TotalCount, counts.SuppressedCount, valueCounts)));
194+
counts.TotalCount, counts.SuppressedCount, metricValue)));
199195
}
200196
}
201197

src/explorer.api/Explorers/EmailColumnExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public override async Task Explore(CancellationToken cancellationToken)
3030
new TextColumnTrim(TableName, ColumnName, TextColumnTrimType.Both, EmailAddressChars),
3131
cancellationToken);
3232

33-
var counts = emailCheckQ.ResultRows.CountTotalAndSuppressed();
33+
var counts = ValueCounts.Compute(emailCheckQ.ResultRows);
3434

3535
var isEmail = counts.TotalCount == emailCheckQ.ResultRows
3636
.Where(r => r.TrimmedText == "@" || r.IsNull)

src/explorer.api/Explorers/MinMaxExplorer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ private async Task RefinedEstimate(bool isMin, CancellationToken cancellationTok
5252
// Constrained min/max query to get an improved estimate
5353
var estimate = await estimator(result, cancellationToken);
5454

55-
// If there are no longer enough values in the constrained range to compute an anonymised min/max,
55+
// If there are no longer enough values in the constrained range to compute an anonymised min/max,
5656
// the query will return `null` => we can't improve further on the result.
57+
// Same thing if the results start to diverge (second part of if condition).
5758
if ((!estimate.HasValue) ||
58-
// Same thing if the results start to diverge.
5959
(isMin ? estimate >= result : estimate <= result))
6060
{
6161
break;

src/explorer.api/Explorers/TextColumnExplorer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public override async Task Explore(CancellationToken cancellationToken)
3030
new DistinctColumnValues(TableName, ColumnName),
3131
cancellationToken);
3232

33-
var counts = distinctValuesQ.ResultRows.CountTotalAndSuppressed();
33+
var counts = ValueCounts.Compute(distinctValuesQ.ResultRows);
3434

3535
PublishMetric(new UntypedMetric(name: "distinct.suppressed_count", metric: counts.SuppressedCount));
3636

@@ -73,7 +73,7 @@ private async Task<IEnumerable<Prefix>> ExplorePrefixes(CancellationToken cancel
7373
new TextColumnPrefix(TableName, ColumnName, length),
7474
cancellationToken);
7575

76-
var counts = prefixesQ.ResultRows.CountTotalAndSuppressed();
76+
var counts = ValueCounts.Compute(prefixesQ.ResultRows);
7777
var avgCount = (double)counts.NonSuppressedCount / counts.NonSuppressedRows;
7878

7979
var prefixes =

src/explorer.api/Extensions/DiffixExtensions.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace Explorer.Diffix.Extensions
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Explorer.Diffix.Interfaces;
6+
7+
internal sealed class ValueCounts
8+
{
9+
private ValueCounts()
10+
{
11+
}
12+
13+
public long TotalCount { get; private set; } = 0;
14+
15+
public long SuppressedCount { get; private set; } = 0;
16+
17+
public long TotalRows { get; private set; } = 0;
18+
19+
public long SuppressedRows { get; private set; } = 0;
20+
21+
public long NonSuppressedRows => TotalRows - SuppressedRows;
22+
23+
public long NonSuppressedCount => TotalCount - SuppressedCount;
24+
25+
public double SuppressedCountRatio => (double)SuppressedCount / TotalCount;
26+
27+
public static ValueCounts Compute<T>(IEnumerable<T> rows)
28+
where T : ICountAggregate, ISuppressible
29+
{
30+
return rows.Aggregate(new ValueCounts(), AccumulateRow);
31+
}
32+
33+
public ValueCounts AccumulateRow<T>(T row)
34+
where T : ICountAggregate, ISuppressible
35+
{
36+
TotalCount += row.Count;
37+
TotalRows++;
38+
if (row.IsSuppressed)
39+
{
40+
SuppressedCount += row.Count;
41+
SuppressedRows++;
42+
}
43+
44+
return this;
45+
}
46+
47+
private static ValueCounts AccumulateRow<T>(ValueCounts vc, T row)
48+
where T : ICountAggregate, ISuppressible
49+
{
50+
vc.AccumulateRow(row);
51+
return vc;
52+
}
53+
}
54+
}

src/explorer.api/Queries/GroupingSetsResult.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Explorer.Diffix.Extensions;
99
using Explorer.Diffix.Interfaces;
1010

11-
internal abstract class GroupingSetsResult<T> : ICountAggregate, IGroupingSetsAggregate<T>
11+
internal abstract class GroupingSetsResult<T> : ICountAggregate, ISuppressible, IGroupingSetsAggregate<T>
1212
{
1313
protected GroupingSetsResult(ref Utf8JsonReader reader, int groupSize)
1414
{
@@ -26,5 +26,7 @@ protected GroupingSetsResult(ref Utf8JsonReader reader, int groupSize)
2626
public long Count { get; }
2727

2828
public double? CountNoise { get; }
29+
30+
public bool IsSuppressed => GroupingValue.IsSuppressed;
2931
}
3032
}

tests/explorer.api.tests/TextColumnTrimTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async void TestEmailPositive()
2525
TestDataSource,
2626
nameof(TextColumnTrimTests));
2727

28-
var counts = query.ResultRows.CountTotalAndSuppressed();
28+
var counts = ValueCounts.Compute(query.ResultRows);
2929

3030
var isEmail = counts.TotalCount == query.ResultRows
3131
.Where(r => r.TrimmedText == "@")
@@ -42,7 +42,7 @@ public async void TestEmailNegative()
4242
TestDataSource,
4343
nameof(TextColumnTrimTests));
4444

45-
var counts = query.ResultRows.CountTotalAndSuppressed();
45+
var counts = ValueCounts.Compute(query.ResultRows);
4646

4747
var isEmail = counts.TotalCount == query.ResultRows
4848
.Where(r => r.TrimmedText == "@")

0 commit comments

Comments
 (0)