Skip to content

Commit

Permalink
Update dependencies (#49)
Browse files Browse the repository at this point in the history
## What was done

- [x] Update TelemetryDeck client dependency to latest release.
- [x] Refactor app telemetry.
- [x] Update development scripts.
- [x] Update package dependencies.
  • Loading branch information
darrarski authored Jul 30, 2024
2 parents d93a7f0 + 204c566 commit 25fec22
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 96 deletions.
4 changes: 2 additions & 2 deletions Projects/App/AppFeature/Sources/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ struct App: SwiftUI.App {
$0.openURL = .init { [dependencies = $0] url in
defer {
dependencies.appTelemetry.send(.init(
type: "OpenURL",
payload: ["url": url.absoluteString]
name: "OpenURL",
parameters: ["url": url.absoluteString]
))
}
return await dependencies.openURL(url)
Expand Down
19 changes: 10 additions & 9 deletions Projects/App/AppFeature/Sources/AppTelemetryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ struct AppTelemetryClient: Sendable {
var initialize: @Sendable () -> Void
var send: @Sendable (AppTelemetrySignal) -> Void

func send(_ signalType: String) {
send(AppTelemetrySignal(type: signalType))
func send(_ signalName: String) {
send(AppTelemetrySignal(name: signalName))
}
}

Expand All @@ -27,23 +27,24 @@ extension AppTelemetryClient: TestDependencyKey {
static let testValue = AppTelemetryClient()
static let previewValue = AppTelemetryClient(
initialize: { log.value.debug("initialize") },
send: { log.value.debug("send \($0.type)\($0.payload.isEmpty ? "" : " \($0.payload)")") }
send: { log.value.debug("send \($0.name)\($0.parameters.isEmpty ? "" : " \($0.parameters)")") }
)
}

extension AppTelemetryClient: DependencyKey {
static let liveValue = AppTelemetryClient(
initialize: {
guard !TelemetryManager.isInitialized, let appID = Self.appID() else { return }
TelemetryManager.initialize(with: .init(appID: appID))
guard !TelemetryManager.isInitialized,
let appID = Self.appID() else { return }
TelemetryDeck.initialize(config: .init(appID: appID))
},
send: { signal in
guard TelemetryManager.isInitialized else { return }
TelemetryManager.send(
signal.type,
for: signal.clientUser,
TelemetryDeck.signal(
signal.name,
parameters: signal.parameters,
floatValue: signal.floatValue,
with: signal.payload
customUserID: signal.customUserID
)
}
)
Expand Down
36 changes: 18 additions & 18 deletions Projects/App/AppFeature/Sources/AppTelemetryPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ import Foundation
import Mastodon
import ProjectsFeature

protocol AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { get }
protocol AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { get }
}

extension Contact.Link: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { id.appTelemetryPayload }
extension Contact.Link: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { id.appTelemetryParameters }
}

extension Contact.Link.ID: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { ["contact.link.id": rawValue] }
extension Contact.Link.ID: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { ["contact.link.id": rawValue] }
}

extension Project: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String : String] { id.appTelemetryPayload }
extension Project: AppTelemetryParametersProviding {
var appTelemetryParameters: [String : String] { id.appTelemetryParameters }
}

extension Project.ID: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] {
extension Project.ID: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] {
["project.id": "\(DateFormatter.yearMonthDay.string(from: date)) \(name)"]
}
}

extension Mastodon.Status: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { id.appTelemetryPayload }
extension Mastodon.Status: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { id.appTelemetryParameters }
}

extension Mastodon.Status.ID: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { ["mastodon.status.id": rawValue] }
extension Mastodon.Status.ID: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { ["mastodon.status.id": rawValue] }
}

extension Mastodon.MediaAttachment: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { id.appTelemetryPayload }
extension Mastodon.MediaAttachment: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { id.appTelemetryParameters }
}

extension Mastodon.MediaAttachment.ID: AppTelemetryPayloadProviding {
var appTelemetryPayload: [String: String] { ["mastodon.media-attachment.id": rawValue] }
extension Mastodon.MediaAttachment.ID: AppTelemetryParametersProviding {
var appTelemetryParameters: [String: String] { ["mastodon.media-attachment.id": rawValue] }
}

private extension DateFormatter {
Expand Down
42 changes: 21 additions & 21 deletions Projects/App/AppFeature/Sources/AppTelemetryReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ struct AppTelemetryReducer<State, Action>: Reducer, Sendable {
let action = UncheckedSendable(action)
return .run(priority: .low) { send in
appTelemetry.send(.init(
type: describe(action.wrappedValue),
payload: payload(for: action.wrappedValue)
name: describe(action.wrappedValue),
parameters: self.parameters(for: action.wrappedValue)
))
}
}
Expand Down Expand Up @@ -72,37 +72,37 @@ struct AppTelemetryReducer<State, Action>: Reducer, Sendable {
return sanitizedName
}

private func payload(for value: Any) -> [String: String] {
var payload: [String: String] = [:]
private func parameters(for value: Any) -> [String: String] {
var parameters: [String: String] = [:]
if let error = value as? any Error {
payload["error.localizedDescription"] = error.localizedDescription
parameters["error.localizedDescription"] = error.localizedDescription
let nsError = error as NSError
payload["error.domain"] = nsError.domain
payload["error.code"] = "\(nsError.code)"
parameters["error.domain"] = nsError.domain
parameters["error.code"] = "\(nsError.code)"
}
if let providedPayload = (value as? any AppTelemetryPayloadProviding)?.appTelemetryPayload {
payload.addPayload(providedPayload)
if let providedParameters = (value as? any AppTelemetryParametersProviding)?.appTelemetryParameters {
parameters.addParameters(providedParameters)
}
let mirror = Mirror(reflecting: value)
switch mirror.displayStyle {
case .enum:
if let child = mirror.children.first {
let childPayload = self.payload(for: child.value)
payload.addPayload(childPayload)
let childParameters = self.parameters(for: child.value)
parameters.addParameters(childParameters)
}
case .optional:
if let child = mirror.children.first {
payload = self.payload(for: child.value)
parameters = self.parameters(for: child.value)
}
case .tuple:
for (_, value) in mirror.children {
let childPayload = self.payload(for: value)
payload.addPayload(childPayload)
let childParameters = self.parameters(for: value)
parameters.addParameters(childParameters)
}
default:
break
}
return payload
return parameters
}

private func toupleChildLabel(_ label: String?) -> String? {
Expand All @@ -117,21 +117,21 @@ private extension Collection {
}

private extension [String: String] {
mutating func addPayload(_ payload: [String: String]) {
for (key, value) in payload {
addPayload(key, value)
mutating func addParameters(_ parameters: [String: String]) {
for (key, value) in parameters {
addParameter(key, value)
}
}

mutating func addPayload(_ key: String, _ value: String) {
mutating func addParameter(_ key: String, _ value: String) {
if self[key] == nil {
self[key] = value
} else if let match = key.matches(of: #/^(?<key>.*)_(?<number>[0-9]+)$/#).first {
let duplicateKey = match.output.key
let nextNumber = (Int(match.output.number) ?? 0) + 1
addPayload("\(duplicateKey)_\(nextNumber)", value)
addParameter("\(duplicateKey)_\(nextNumber)", value)
} else {
addPayload("\(key)_1", value)
addParameter("\(key)_1", value)
}
}
}
6 changes: 3 additions & 3 deletions Projects/App/AppFeature/Sources/AppTelemetrySignal.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct AppTelemetrySignal: Equatable, Sendable {
var type: String
var clientUser: String?
var name: String
var parameters: [String: String] = [:]
var floatValue: Double?
var payload: [String: String] = [:]
var customUserID: String?
}
Loading

0 comments on commit 25fec22

Please sign in to comment.