Skip to content

Commit

Permalink
Default Filter Behavior (#20)
Browse files Browse the repository at this point in the history
* Add steps and stepNames property to hasher so it actually gets included incomparisons

* Fix a warning

* Add defaultfilterbehavior property

* Rename to BaseFilters

* rename allApps to thisOrganization

* Actually decode steps, stepNames and baseFilters
  • Loading branch information
winsmith authored Jan 15, 2023
1 parent 43b114c commit 06b8f39
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Sources/DataTransferObjects/Query/BaseFilters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

/// Specifies the default filters to be attached to a query before executing it
///
/// The filters generated by this rule are combined with a query's `filter` property
/// using an `and` filter.
public enum BaseFilters: String, Codable, Hashable, Equatable {
/// Attach test mode filter and filter for all apps of the executing user's organization
case thisOrganization

/// Attach test mode filter and filter for the app the insight lives in
///
/// This fails if the query does not belong to an insight.
case thisApp

/// Attach test mode filter and filter for the example app's data
///
/// The server will execute this query as if the owner of the Example App
/// is logged in, and it will always return example data instead of an
/// actual user's data.
///
/// This is great for showing a demo of the environment.
case exampleData

/// Only available for super org, do not specify any filters
///
/// This is used internally for admin dashboards. The server will
/// throw an error if this is executed by someone who is not a member
/// of the super org.
case noFilter
}
19 changes: 17 additions & 2 deletions Sources/DataTransferObjects/Query/CustomQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import Foundation
/// Custom JSON based query
public struct CustomQuery: Codable, Hashable, Equatable {
public init(queryType: CustomQuery.QueryType, dataSource: String = "telemetry-signals",
descending: Bool? = nil, filter: Filter? = nil, intervals: [QueryTimeInterval]? = nil,
descending: Bool? = nil,
filter: Filter? = nil,
baseFilters: BaseFilters? = nil,
intervals: [QueryTimeInterval]? = nil,
relativeIntervals: [RelativeTimeInterval]? = nil, granularity: QueryGranularity,
aggregations: [Aggregator]? = nil, postAggregations: [PostAggregator]? = nil,
limit: Int? = nil, context: QueryContext? = nil,
Expand All @@ -14,6 +17,7 @@ public struct CustomQuery: Codable, Hashable, Equatable {
self.queryType = queryType
self.dataSource = DataSource(type: .table, name: dataSource)
self.descending = descending
self.baseFilters = baseFilters
self.filter = filter
self.intervals = intervals
self.relativeIntervals = relativeIntervals
Expand All @@ -31,7 +35,10 @@ public struct CustomQuery: Codable, Hashable, Equatable {
}

public init(queryType: CustomQuery.QueryType, dataSource: DataSource,
descending: Bool? = nil, filter: Filter? = nil, intervals: [QueryTimeInterval]? = nil,
descending: Bool? = nil,
filter: Filter? = nil,
baseFilters: BaseFilters? = nil,
intervals: [QueryTimeInterval]? = nil,
relativeIntervals: [RelativeTimeInterval]? = nil, granularity: QueryGranularity,
aggregations: [Aggregator]? = nil, postAggregations: [PostAggregator]? = nil,
limit: Int? = nil, context: QueryContext? = nil,
Expand All @@ -42,6 +49,7 @@ public struct CustomQuery: Codable, Hashable, Equatable {
self.queryType = queryType
self.dataSource = dataSource
self.descending = descending
self.baseFilters = baseFilters
self.filter = filter
self.intervals = intervals
self.relativeIntervals = relativeIntervals
Expand Down Expand Up @@ -72,6 +80,7 @@ public struct CustomQuery: Codable, Hashable, Equatable {
public var queryType: QueryType
public var dataSource: DataSource = .init(type: .table, name: "telemetry-signals")
public var descending: Bool?
public var baseFilters: BaseFilters?
public var filter: Filter?
public var intervals: [QueryTimeInterval]?

Expand Down Expand Up @@ -105,6 +114,7 @@ public struct CustomQuery: Codable, Hashable, Equatable {
hasher.combine(queryType)
hasher.combine(dataSource)
hasher.combine(descending)
hasher.combine(baseFilters)
hasher.combine(filter)
hasher.combine(intervals)
hasher.combine(relativeIntervals)
Expand All @@ -116,6 +126,8 @@ public struct CustomQuery: Codable, Hashable, Equatable {
hasher.combine(metric)
hasher.combine(dimensions)
hasher.combine(dimension)
hasher.combine(steps)
hasher.combine(stepNames)
}

public static func == (lhs: CustomQuery, rhs: CustomQuery) -> Bool {
Expand All @@ -128,6 +140,7 @@ public struct CustomQuery: Codable, Hashable, Equatable {
self.queryType = try container.decode(CustomQuery.QueryType.self, forKey: CustomQuery.CodingKeys.queryType)
self.dataSource = try container.decode(DataSource.self, forKey: CustomQuery.CodingKeys.dataSource)
self.descending = try container.decodeIfPresent(Bool.self, forKey: CustomQuery.CodingKeys.descending)
self.baseFilters = try container.decodeIfPresent(BaseFilters.self, forKey: CustomQuery.CodingKeys.baseFilters)
self.filter = try container.decodeIfPresent(Filter.self, forKey: CustomQuery.CodingKeys.filter)
self.relativeIntervals = try container.decodeIfPresent([RelativeTimeInterval].self, forKey: CustomQuery.CodingKeys.relativeIntervals)
self.granularity = try container.decode(QueryGranularity.self, forKey: CustomQuery.CodingKeys.granularity)
Expand All @@ -139,6 +152,8 @@ public struct CustomQuery: Codable, Hashable, Equatable {
self.dimension = try container.decodeIfPresent(DimensionSpec.self, forKey: CustomQuery.CodingKeys.dimension)
self.metric = try container.decodeIfPresent(TopNMetricSpec.self, forKey: CustomQuery.CodingKeys.metric)
self.dimensions = try container.decodeIfPresent([DimensionSpec].self, forKey: CustomQuery.CodingKeys.dimensions)
self.steps = try container.decodeIfPresent([Filter].self, forKey: CustomQuery.CodingKeys.steps)
self.stepNames = try container.decodeIfPresent([String].self, forKey: CustomQuery.CodingKeys.stepNames)

if let intervals = try? container.decode(QueryTimeIntervalsContainer.self, forKey: CustomQuery.CodingKeys.intervals) {
self.intervals = intervals.intervals
Expand Down
2 changes: 1 addition & 1 deletion Tests/QueryTests/QueryTimeIntervalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ final class QueryTimeIntervalTests: XCTestCase {


func testDecodingQueryTimeInterval() throws {
try JSONDecoder.telemetryDecoder.decode(QueryTimeIntervalsContainer.self, from: exampleData)
_ = try JSONDecoder.telemetryDecoder.decode(QueryTimeIntervalsContainer.self, from: exampleData)
}
}

0 comments on commit 06b8f39

Please sign in to comment.