@@ -15,7 +15,12 @@ public extension CustomQuery {
1515 /// @warn Both precompile AND compileToRunnableQuery need to be run before a query can safely be handed to Druid!
1616 ///
1717 /// @see compileToRunnableQuery
18- func precompile( namespace: String ? = nil , organizationAppIDs: [ UUID ] , isSuperOrg: Bool ) throws -> CustomQuery {
18+ func precompile(
19+ namespace: String ? = nil ,
20+ namespaceAvailableAfter: Date ? = nil ,
21+ organizationAppIDs: [ UUID ] ,
22+ isSuperOrg: Bool
23+ ) throws -> CustomQuery {
1924 guard ( compilationStatus ?? . notCompiled) == . notCompiled else {
2025 throw QueryGenerationError . compilationStatusError
2126 }
@@ -36,7 +41,13 @@ public extension CustomQuery {
3641 }
3742
3843 // Apply base filters and data source
39- query = try Self . applyBaseFilters ( namespace: namespace, query: query, organizationAppIDs: organizationAppIDs, isSuperOrg: isSuperOrg)
44+ query = try Self . applyBaseFilters (
45+ namespace: namespace,
46+ namespaceAvailableAfter: namespaceAvailableAfter,
47+ query: query,
48+ organizationAppIDs: organizationAppIDs,
49+ isSuperOrg: isSuperOrg
50+ )
4051
4152 // Update compilationStatus so the next steps in the pipeline are sure the query has been precompiled
4253 query. compilationStatus = . precompiled
@@ -136,7 +147,13 @@ public extension CustomQuery {
136147}
137148
138149extension CustomQuery {
139- static func applyBaseFilters( namespace: String ? , query: CustomQuery , organizationAppIDs: [ UUID ] ? , isSuperOrg: Bool ) throws -> CustomQuery {
150+ static func applyBaseFilters(
151+ namespace: String ? ,
152+ namespaceAvailableAfter: Date ? ,
153+ query: CustomQuery ,
154+ organizationAppIDs: [ UUID ] ? ,
155+ isSuperOrg: Bool
156+ ) throws -> CustomQuery {
140157 // make an editable copy of the query
141158 var query = query
142159
@@ -173,9 +190,20 @@ extension CustomQuery {
173190 " com.telemetrydeck.compacted "
174191 ]
175192
193+ // Calculate earliest interval date, to compare against namespaceAvailableAfter
194+ var earliestIntervalDate = Date . distantFuture
195+ for interval in query. intervals ?? [ ] {
196+ earliestIntervalDate = min ( interval. beginningDate, earliestIntervalDate)
197+ }
198+ for relativeInterval in query. relativeIntervals ?? [ ] {
199+ let interval = QueryTimeInterval . from ( relativeTimeInterval: relativeInterval)
200+ earliestIntervalDate = min ( interval. beginningDate, earliestIntervalDate)
201+ }
202+
203+ // Decide the data source based on the data source property and namespaces
176204 if let dataSource = query. dataSource, allowedDataSourceNames. contains ( dataSource. name) {
177205 query. dataSource = . init( dataSource. name)
178- } else if let namespace {
206+ } else if let namespace, ( namespaceAvailableAfter ?? Date . distantPast ) < earliestIntervalDate {
179207 query. dataSource = . init( namespace)
180208 } else {
181209 query. dataSource = . init( " telemetry-signals " )
0 commit comments