-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetentionQueryGenerationTests.swift
More file actions
278 lines (254 loc) · 12 KB
/
RetentionQueryGenerationTests.swift
File metadata and controls
278 lines (254 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// RetentionQueryGenerationTests.swift
//
//
// Created by Daniel Jilg on 28.11.22.
//
// swiftlint:disable force_try
import DataTransferObjects
import XCTest
final class RetentionQueryGenerationTests: XCTestCase {
let tinyQuery = CustomQuery(
queryType: .groupBy,
dataSource: .init("com.telemetrydeck.all"),
filter: .and(.init(fields: [
.selector(.init(dimension: "appID", value: "79167A27-EBBF-4012-9974-160624E5D07B")),
.selector(.init(dimension: "isTestMode", value: "false")),
])),
intervals: [
QueryTimeInterval(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-09-30T00:00:00.000Z")!
),
], granularity: .all,
aggregations: [
.filtered(.init(
filter: .interval(.init(
dimension: "__time",
intervals: [
.init(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-08-31T23:59:59.000Z")!
),
]
)),
aggregator: .thetaSketch(
.init(
name: "_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z",
fieldName: "clientUser"
)
)
)),
.filtered(.init(
filter: .interval(.init(
dimension: "__time",
intervals: [
.init(
beginningDate: Date(iso8601String: "2022-09-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-09-30T23:59:59.000Z")!
),
]
)),
aggregator: .thetaSketch(
.init(
name: "_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z",
fieldName: "clientUser"
)
)
)),
],
postAggregations: [
.thetaSketchEstimate(.init(
name: "retention_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z",
field: .thetaSketchSetOp(.init(
func: .intersect,
fields: [
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z"
)),
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z"
)),
]
))
)
),
.thetaSketchEstimate(.init(
name: "retention_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z",
field: .thetaSketchSetOp(.init(
func: .intersect,
fields: [
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-08-01T00:00:00.000Z_2022-08-31T23:59:59.000Z"
)),
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z"
)),
]
))
)
),
.thetaSketchEstimate(.init(
name: "retention_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z",
field: .thetaSketchSetOp(.init(
func: .intersect,
fields: [
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z"
)),
.fieldAccess(.init(
type: .fieldAccess,
fieldName: "_2022-09-01T00:00:00.000Z_2022-09-30T23:59:59.000Z"
)),
]
))
)
),
]
)
func testThrowsWhenDatesTooClose() {
let begin_august = Date(iso8601String: "2022-08-01T00:00:00.000Z")!
let mid_august = Date(iso8601String: "2022-08-15T00:00:00.000Z")!
let end_august = Date(iso8601String: "2022-08-31T23:59:59.999Z")!
let end_september = Date(iso8601String: "2022-09-30T23:59:59.999Z")!
// Test monthly retention (default)
let monthQuery1 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: begin_august, endDate: mid_august)],
granularity: .month
)
XCTAssertThrowsError(try monthQuery1.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
let monthQuery2 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: begin_august, endDate: end_august)],
granularity: .month
)
XCTAssertThrowsError(try monthQuery2.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
let monthQuery3 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: begin_august, endDate: end_september)],
granularity: .month
)
XCTAssertNoThrow(try monthQuery3.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
// Test daily retention
let startDate = Date(iso8601String: "2022-08-01T00:00:00.000Z")!
let sameDay = Date(iso8601String: "2022-08-01T12:00:00.000Z")!
let nextDay = Date(iso8601String: "2022-08-02T00:00:00.000Z")!
let dayQuery1 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: startDate, endDate: sameDay)],
granularity: .day
)
XCTAssertThrowsError(try dayQuery1.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
let dayQuery2 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: startDate, endDate: nextDay)],
granularity: .day
)
XCTAssertNoThrow(try dayQuery2.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
// Test weekly retention
let weekStart = Date(iso8601String: "2022-08-01T00:00:00.000Z")!
let weekMid = Date(iso8601String: "2022-08-05T00:00:00.000Z")!
let weekEnd = Date(iso8601String: "2022-08-08T00:00:00.000Z")!
let weekQuery1 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: weekStart, endDate: weekMid)],
granularity: .week
)
XCTAssertThrowsError(try weekQuery1.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
let weekQuery2 = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
intervals: [QueryTimeInterval(beginningDate: weekStart, endDate: weekEnd)],
granularity: .week
)
XCTAssertNoThrow(try weekQuery2.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [UUID()], isSuperOrg: false))
}
func testExample() throws {
// Test with new compile-down approach
let appID = UUID(uuidString: "79167A27-EBBF-4012-9974-160624E5D07B")!
let query = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
appID: appID,
baseFilters: .thisApp,
testMode: false,
intervals: [QueryTimeInterval(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-09-30T00:00:00.000Z")!
)],
granularity: .month // Explicitly set to month
)
let compiledQuery = try query.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [appID], isSuperOrg: true)
// Verify the compiled query has the expected structure
XCTAssertEqual(compiledQuery.queryType, .groupBy)
XCTAssertEqual(compiledQuery.granularity, .all)
XCTAssertNotNil(compiledQuery.aggregations)
XCTAssertNotNil(compiledQuery.postAggregations)
// The generated query should match the expected structure from tinyQuery
// (though the exact aggregator names might differ due to date formatting)
}
func testRetentionWithDifferentGranularities() throws {
let appID = UUID(uuidString: "79167A27-EBBF-4012-9974-160624E5D07B")!
// Test daily retention - 7 days should generate 8 intervals (0-7 inclusive)
let dailyQuery = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
appID: appID,
baseFilters: .thisApp,
testMode: false,
intervals: [QueryTimeInterval(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-08-07T23:59:59.000Z")!
)],
granularity: .day
)
let compiledDailyQuery = try dailyQuery.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [appID], isSuperOrg: true)
XCTAssertEqual(compiledDailyQuery.aggregations?.count, 7) // 7 days
// Post-aggregations should be n*(n+1)/2 for n intervals
XCTAssertEqual(compiledDailyQuery.postAggregations?.count, 28) // 7*8/2 = 28
// Test weekly retention - 4 weeks
let weeklyQuery = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
appID: appID,
baseFilters: .thisApp,
testMode: false,
intervals: [QueryTimeInterval(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-08-29T00:00:00.000Z")!
)],
granularity: .week
)
let compiledWeeklyQuery = try weeklyQuery.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [appID], isSuperOrg: true)
XCTAssertEqual(compiledWeeklyQuery.aggregations?.count, 5) // 5 weeks (spans into 5th week)
XCTAssertEqual(compiledWeeklyQuery.postAggregations?.count, 15) // 5*6/2 = 15
// Test monthly retention - 3 months
let monthlyQuery = CustomQuery(
queryType: .retention,
dataSource: "com.telemetrydeck.all",
appID: appID,
baseFilters: .thisApp,
testMode: false,
intervals: [QueryTimeInterval(
beginningDate: Date(iso8601String: "2022-08-01T00:00:00.000Z")!,
endDate: Date(iso8601String: "2022-10-31T00:00:00.000Z")!
)],
granularity: .month
)
let compiledMonthlyQuery = try monthlyQuery.precompile(namespace: nil, useNamespace: false, organizationAppIDs: [appID], isSuperOrg: true)
XCTAssertEqual(compiledMonthlyQuery.aggregations?.count, 3) // 3 months
XCTAssertEqual(compiledMonthlyQuery.postAggregations?.count, 6) // 3*4/2 = 6
}
}