Skip to content

Commit

Permalink
Merge pull request #193 from TelemetryDeck/UpdateClusterInstrument
Browse files Browse the repository at this point in the history
Update cluster instrument
  • Loading branch information
Black-Fox-2022 authored May 29, 2024
2 parents 8547355 + adbbd48 commit 8e185d5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 447 deletions.
45 changes: 44 additions & 1 deletion Cluster/QueryRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ struct QueryRunner: View {

@State var queryResultWrapper: QueryResultWrapper?
@State var isLoading: Bool = false
@State var taskID: String = ""

var body: some View {
VStack(spacing: 10){
if let queryResult = queryResultWrapper?.result {
ClusterChart(query: query, result: queryResult, type: type)
.frame(height: 135)
.id(queryResultWrapper)
.padding(.horizontal)
} else {
SondrineAnimation()
.frame(width: 100, height: 100)
.opacity(0.5)
.padding()
}

HStack(spacing: 3){
if isLoading {
ProgressView()
.scaleEffect(0.75)
.frame(height: 5)
}
Spacer()
if let queryResultWrapper = queryResultWrapper {
Text("Updated")
Expand All @@ -50,6 +63,33 @@ struct QueryRunner: View {
}
}
}
.onChange(of: queryService.isTestingMode) {
Task {
do {
try await getQueryResult()
} catch {
print(error)
}
}
}
.onChange(of: queryService.timeWindowBeginning) {
Task {
do {
try await getQueryResult()
} catch {
print(error)
}
}
}
.onChange(of: queryService.timeWindowEnd) {
Task {
do {
try await getQueryResult()
} catch {
print(error)
}
}
}
}

private func getQueryResult() async throws {
Expand All @@ -58,7 +98,7 @@ struct QueryRunner: View {
isLoading = false
}

let taskID = try await beginAsyncCalcV2()
taskID = try await beginAsyncCalcV2()

try await getLastSuccessfulValue(taskID)

Expand All @@ -84,6 +124,9 @@ extension QueryRunner {
queryCopy.relativeIntervals = [RelativeTimeInterval(beginningDate: queryService.timeWindowBeginning.toRelativeDate(), endDate: queryService.timeWindowEnd.toRelativeDate())]
}
}
if queryCopy.testMode == nil {
queryCopy.testMode = queryService.isTestingMode
}

let response: [String: String] = try await api.post(data: queryCopy, url: queryBeginURL)
guard let taskID = response["queryTaskID"] else {
Expand Down
84 changes: 0 additions & 84 deletions Services/QueryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,88 +72,4 @@ class QueryService: ObservableObject {
}
}

func getInsightQuery(ofInsightWithID insightID: DTOv2.Insight.ID) async throws -> CustomQuery {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<CustomQuery, Error>) in
let url = api.urlForPath(apiVersion: .v3, "insights", insightID.uuidString, "query")

struct ProduceQueryBody: Codable {
/// Is Test Mode enabled? (nil means false)
public var testMode: Bool?
/// Which time intervals are we looking at?
public var relativeInterval: RelativeTimeInterval?
public var interval: QueryTimeInterval?
}

let produceQueryBody = ProduceQueryBody(testMode: isTestingMode, interval: .init(beginningDate: timeWindowBeginningDate, endDate: timeWindowEndDate))

api.post(produceQueryBody, to: url) { (result: Result<CustomQuery, TransferError>) in
switch result {
case .success(let query):
continuation.resume(returning: query)

case .failure(let error):
self.errorService.handle(transferError: error)
continuation.resume(throwing: error)
}
}
}
}

func createTask(forQuery query: CustomQuery) async throws -> [String: String] {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[String: String], Error>) in

// If the query has no specified interval, give it the default interval
var query = query
if query.relativeIntervals == nil && query.intervals == nil {
query.intervals = [.init(beginningDate: timeWindowBeginningDate, endDate: timeWindowEndDate)]
}

let url = api.urlForPath(apiVersion: .v3, "query", "calculate-async")
api.post(query, to: url) { (result: Result<[String: String], TransferError>) in
switch result {
case .success(let taskID):

continuation.resume(returning: taskID)

case .failure(let error):
self.errorService.handle(transferError: error)
continuation.resume(throwing: error)
}
}
}
}

func getTaskResult(forTaskID taskID: String) async throws -> QueryResultWrapper {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<QueryResultWrapper, Error>) in
let url = api.urlForPath(apiVersion: .v3, "task", taskID, "lastSuccessfulValue")
api.get(url) { (result: Result<QueryResultWrapper, TransferError>) in
switch result {
case .success(let queryResult):

continuation.resume(returning: queryResult)

case .failure(let error):
self.errorService.handle(transferError: error)
continuation.resume(throwing: error)
}
}
}
}

func getTaskStatus(forTaskID taskID: String) async throws -> QueryTaskStatus {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<QueryTaskStatus, Error>) in
let url = api.urlForPath(apiVersion: .v3, "task", taskID, "status")
api.get(url) { (result: Result<QueryTaskStatusStruct, TransferError>) in
switch result {
case .success(let queryStatusStruct):
let queryStatus = queryStatusStruct.status
continuation.resume(returning: queryStatus)

case .failure(let error):
self.errorService.handle(transferError: error)
continuation.resume(throwing: error)
}
}
}
}
}
Loading

0 comments on commit 8e185d5

Please sign in to comment.