Skip to content

Commit

Permalink
Improve CodeQuality
Browse files Browse the repository at this point in the history
  • Loading branch information
Black-Fox-2022 committed May 24, 2024
1 parent ff84540 commit a599f69
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ identifier_name:
- env
- telemetry_api_server_version

reporter: "github-actions-logging" # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging)
#reporter: "github-actions-logging" # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging)
reporter: "xcode"
1 change: 0 additions & 1 deletion Cluster/Chart/BarChart/BarCharTopN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Charts
import DataTransferObjects

struct BarChartTopN: View {
//let query: CustomQuery
let result: TopNQueryResult

var body: some View {
Expand Down
1 change: 0 additions & 1 deletion Cluster/Chart/BarChart/BarChartGroupBy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Charts
import DataTransferObjects

struct BarChartGroupBy: View {
//let query: CustomQuery
let result: GroupByQueryResult

var body: some View {
Expand Down
1 change: 0 additions & 1 deletion Cluster/Chart/BarChart/BarChartTimeSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Charts
import DataTransferObjects

struct BarChartTimeSeries: View {
//let query: CustomQuery
let result: TimeSeriesQueryResult

var body: some View {
Expand Down
2 changes: 0 additions & 2 deletions Cluster/Chart/LineChart/LineChartTimeSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Charts
import DataTransferObjects

struct LineChartTimeSeries: View {
//let query: CustomQuery
let result: TimeSeriesQueryResult

var body: some View {
Expand All @@ -23,7 +22,6 @@ struct LineChartTimeSeries: View {
}
.interpolationMethod(.cardinal)


ForEach(result.rows, id: \.timestamp) { row in
AreaMark(x: .value("Date", row.timestamp),
y: .value("Total Count", row.result["count"]?.value ?? 0))
Expand Down
2 changes: 0 additions & 2 deletions Cluster/ClusterInstrument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI
import DataTransferObjects


struct ClusterInstrument: View {
@EnvironmentObject var api: APIClient

Expand All @@ -26,4 +25,3 @@ struct ClusterInstrument: View {
.padding()
}
}

40 changes: 6 additions & 34 deletions Cluster/QueryRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ struct QueryRunner: View {
Task {
do {
try await getQueryResult()
}
catch {
} catch {
print(error)
}
}
Expand All @@ -46,7 +45,6 @@ struct QueryRunner: View {
isLoading = false
}

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

try await getLastSuccessfulValue(taskID)
Expand All @@ -57,49 +55,23 @@ struct QueryRunner: View {
}
}

extension QueryRunner {
private enum ApiVersion: String {
case v1
case v2
case v3
}

private func urlForPath(apiVersion: ApiVersion = .v1, _ path: String..., appendTrailingSlash _: Bool = false) -> URL {
URL(string: "https://api.telemetrydeck.com/api/" + "\(apiVersion.rawValue)/" + path.joined(separator: "/") + "/")!
}

private func authenticatedURLRequest(for url: URL, httpMethod: String, httpBody: Data? = nil, contentType: String = "application/json; charset=utf-8") -> URLRequest {
var request = URLRequest(url: url)
request.httpMethod = httpMethod
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue(UserTokenDTO(id: UUID(uuidString: "7c736171-9c56-4573-b72f-f9f881c12d7b"), value: "QL41IC283ELWFYXRHC6G22VMTPFK7A3GN1L6INTHPCJGTKA4Y76YZF572P1H2U8OR6HQHUXKWP91M2URKUA49SFCYKQ2XZ8WS0WMR28N2I91OCTDHJMYBAQAIKG2QO73", user: [:]).bearerTokenAuthString, forHTTPHeaderField: "Authorization")

if let httpBody = httpBody {
request.httpBody = httpBody
}

return request
}
}

extension QueryRunner {

private func beginAsyncCalcV2() async throws -> String {
// create a query task
let queryBeginURL = api.urlForPath(apiVersion: .v3, "query","calculate-async")
let queryBeginURL = api.urlForPath(apiVersion: .v3, "query", "calculate-async")

var queryCopy = query

if queryCopy.intervals == nil && queryCopy.relativeIntervals == nil{
switch queryService.timeWindowBeginning {
case .absolute(date: _):
case .absolute:
queryCopy.intervals = [.init(beginningDate: queryService.timeWindowBeginningDate, endDate: queryService.timeWindowEndDate)]
default:
queryCopy.relativeIntervals = [RelativeTimeInterval(beginningDate: queryService.timeWindowBeginning.toRelativeDate(), endDate: queryService.timeWindowEnd.toRelativeDate())]
}
}


let response: [String: String] = try await api.post(data: queryCopy, url: queryBeginURL)
guard let taskID = response["queryTaskID"] else {
throw TransferError.decodeFailed
Expand All @@ -110,15 +82,15 @@ extension QueryRunner {

private func getLastSuccessfulValue(_ taskID: String) async throws {
// pick up the finished result
let lastSuccessfulValueURL = urlForPath(apiVersion: .v3, "task", taskID, "lastSuccessfulValue")
let lastSuccessfulValueURL = api.urlForPath(apiVersion: .v3, "task", taskID, "lastSuccessfulValue")
queryResultWrapper = try await api.get(url: lastSuccessfulValueURL)
}

private func waitUntilTaskStatusIsSuccessful(_ taskID: String) async throws {
// wait for the task to finish caluclating
var taskStatus: QueryTaskStatus = .running
while taskStatus != .successful {
let taskStatusURL = urlForPath(apiVersion: .v3, "task", taskID, "status")
let taskStatusURL = api.urlForPath(apiVersion: .v3, "task", taskID, "status")

let queryTaskStatus: QueryTaskStatusStruct = try await api.get(url: taskStatusURL)

Expand Down Expand Up @@ -152,7 +124,7 @@ extension RelativeDateDescription {
}
case .goBack(let days):
RelativeDate(.beginning, of: .day, adding: -days)
case .absolute(let date):
case .absolute:
RelativeDate(.beginning, of: .day, adding: -30)
}
}
Expand Down
10 changes: 4 additions & 6 deletions Services/QueryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ class QueryService: ObservableObject {
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):
Expand All @@ -103,13 +101,13 @@ class QueryService: ObservableObject {

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 {
Expand Down
24 changes: 22 additions & 2 deletions Telemetry Viewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1275,12 +1275,13 @@
DCE239FF24D3687D00053370 /* Sources */,
DCE23A0024D3687D00053370 /* Frameworks */,
DCE23A0124D3687D00053370 /* Resources */,
DC7DEE9D258A355F00BEA712 /* ShellScript */,
DC7DEE9D258A355F00BEA712 /* Run Script */,
C5A8D875270C5D800032560A /* Embed Foundation Extensions */,
);
buildRules = (
);
dependencies = (
809E189D2C009A1400E83D74 /* PBXTargetDependency */,
C5A8D870270C5D800032560A /* PBXTargetDependency */,
C5A8D8C5270C821B0032560A /* PBXTargetDependency */,
);
Expand Down Expand Up @@ -1360,6 +1361,7 @@
DC9C082F252CD622001C0F94 /* XCRemoteSwiftPackageReference "SwiftClient" */,
2B25EAE726D3EEE700BBBB1B /* XCRemoteSwiftPackageReference "SwiftUI-Shimmer" */,
C5AD4B6227D3928600CD7E4C /* XCRemoteSwiftPackageReference "models" */,
809E189B2C00998C00E83D74 /* XCRemoteSwiftPackageReference "SwiftLint" */,
);
productRefGroup = DCE23A0424D3687D00053370 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1501,7 +1503,7 @@
shellPath = /bin/sh;
shellScript = "# Use Xcode's copy of the Git binary\nGIT=`xcrun -find git`\n\n# Use the commit count as CFBundleVersion\nGIT_COMMIT_COUNT=`${GIT} rev-list --count HEAD`\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion ${GIT_COMMIT_COUNT}\" \"${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}\"\n";
};
DC7DEE9D258A355F00BEA712 /* ShellScript */ = {
DC7DEE9D258A355F00BEA712 /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
Expand All @@ -1511,6 +1513,7 @@
);
inputPaths = (
);
name = "Run Script";
outputFileListPaths = (
);
outputPaths = (
Expand Down Expand Up @@ -1926,6 +1929,10 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
809E189D2C009A1400E83D74 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = 809E189C2C009A1400E83D74 /* SwiftLintBuildToolPlugin */;
};
C5A8D870270C5D800032560A /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = C5A8D862270C5D7A0032560A /* TelemetryDeckWidgetExtension */;
Expand Down Expand Up @@ -2496,6 +2503,14 @@
minimumVersion = 1.0.0;
};
};
809E189B2C00998C00E83D74 /* XCRemoteSwiftPackageReference "SwiftLint" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/realm/SwiftLint";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.55.1;
};
};
C5AD4B6227D3928600CD7E4C /* XCRemoteSwiftPackageReference "models" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/TelemetryDeck/models";
Expand Down Expand Up @@ -2546,6 +2561,11 @@
package = 2B25EAE726D3EEE700BBBB1B /* XCRemoteSwiftPackageReference "SwiftUI-Shimmer" */;
productName = Shimmer;
};
809E189C2C009A1400E83D74 /* SwiftLintBuildToolPlugin */ = {
isa = XCSwiftPackageProductDependency;
package = 809E189B2C00998C00E83D74 /* XCRemoteSwiftPackageReference "SwiftLint" */;
productName = "plugin:SwiftLintBuildToolPlugin";
};
C51CB76227566148005A3FB9 /* DataTransferObjects */ = {
isa = XCSwiftPackageProductDependency;
productName = DataTransferObjects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
{
"originHash" : "c77feac7351e9360f94da77ea6664ee97c0a4c27540c7ad522fe8a1013e98df7",
"pins" : [
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "c9c3df6ab812de32bae61fc0cd1bf6d45170ebf0",
"version" : "1.8.2"
}
},
{
"identity" : "models",
"kind" : "remoteSourceControl",
Expand All @@ -9,6 +28,24 @@
"version" : "1.11.0"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "fd4df99170f5e9d7cf9aa8312aa8506e0e7a44e7",
"version" : "0.35.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
Expand All @@ -18,6 +55,15 @@
"version" : "2.6.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
}
},
{
"identity" : "swiftclient",
"kind" : "remoteSourceControl",
Expand All @@ -36,6 +82,15 @@
"version" : "1.0.3"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint",
"state" : {
"revision" : "b515723b16eba33f15c4677ee65f3fef2ce8c255",
"version" : "0.55.1"
}
},
{
"identity" : "swiftui-shimmer",
"kind" : "remoteSourceControl",
Expand All @@ -44,7 +99,34 @@
"revision" : "5659a623567cefe258d1e3e67cb65585fbb6ecb6",
"version" : "1.4.2"
}
},
{
"identity" : "swiftytexttable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
}
},
{
"identity" : "swxmlhash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "9234124cff5e22e178988c18d8b95a8ae8007f76",
"version" : "5.1.2"
}
}
],
"version" : 2
"version" : 3
}

0 comments on commit a599f69

Please sign in to comment.