Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed May 25, 2023
1 parent 1f4453d commit 78deef5
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 74 deletions.
9 changes: 4 additions & 5 deletions Sources/Telemetry/Telemetry+Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ extension Telemetry {
* Action call that can take type
* - Remark: The class support tracking of sessions, screen/page views, events and timings with optional custom dimension parameters.
* - Remark: For a full list of all the supported parameters please refer to the [Google Analytics parameter reference](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters)

Check failure on line 7 in Sources/Telemetry/Telemetry+Action.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 200 characters or less; currently it has 207 characters (line_length)
* - Fixme: ⚠️️ add example
* - Fixme: ⚠️️ Add example
* - Parameters:
* - action: - Fixme: ⚠️️ doc
* - action: - Fixme: ⚠️️ Add doc
* - complete: Only use complete for the GA type
*/
public static func action(_ action: ActionKind, complete: Complete? = nil) {
Expand All @@ -26,10 +26,9 @@ extension Telemetry {
/**
* - Remark: If you get: swift a server with the specified hostname could not be found. Make sure to enable outgoing network in sandbox: https://stackoverflow.com/a/57292829/5389500
* - Parameters:
* - send: timing, exception, pageview, session, exception etc
* - parameters: custom params for the type
* - type: - Fixme: ⚠️️ add doc
* - complete: Call back with success or not. useful for Unit-testing when dealing with async code etc
* - type: timing, exception, pageview, session, exception etc
* - complete: Call back with success or not. Useful for Unit-testing when dealing with async code etc
*/
internal static func send(type: String?, parameters: [String: String], complete: Complete? = nil) {
var queryArgs = Self.queryArgs // Meta data
Expand Down
8 changes: 4 additions & 4 deletions Sources/Telemetry/Telemetry+Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
*/
extension Telemetry {
public typealias Complete = (_ success: Bool) -> Void
static let baseURL: URL? = .init(string: "https://www.google-analytics.com/")
internal static let baseURL: URL? = .init(string: "https://www.google-analytics.com/")
}
/**
* variables
Expand All @@ -31,17 +31,17 @@ extension Telemetry {
/**
* Options are: .vendor, .userdef, .keychain
* - Remark: Type of persistence
* - Fixme: ⚠️️ rename to currentIdentifierType? or curIdType?
* - Fixme: ⚠️️ Rename to currentIdentifierType? or curIdType?
*/
public static var idType: IDType = .userdefault
/**
* network, rename to urlSession
* Network, rename to urlSession
*/
public static let session = URLSession.shared
/**
* Telemetry type
* - Description: A way to switch from ga-endpoint to aggregator-endpoint
* - Fixme: ⚠️️ rename to endPointType?
* - Fixme: ⚠️️ Rename to endPointType? EPType ? Maybe
*/
public static var tmType: TMType = .ga // .agg()
}
21 changes: 14 additions & 7 deletions Sources/Telemetry/ext/Dict+Ext.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Foundation

extension Dictionary {
func combinedWith(_ other: [Key: Value]) -> [Key: Value] {
var dict = self
for (key, value) in other {
dict[key] = value
}
return dict
}
/**
* - Fixme: ⚠️️ add doc
* - Parameter other: - Fixme: ⚠️️ add doc
* - Returns: - Fixme: ⚠️️ add doc
*/
func combinedWith(_ other: [Key: Value]) -> [Key: Value] {
var dict = self
for (key, value) in other {
dict[key] = value
}
return dict
}
}
17 changes: 11 additions & 6 deletions Sources/Telemetry/ext/Keychain.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import Foundation
/**
* - Note: from here: https://stackoverflow.com/a/51642962/5389500
* A simple getter and setter wrapper for keychain
* - Note: From here: https://stackoverflow.com/a/51642962/5389500
* - Fixme: ⚠️️ Add examples
*/
class Keychain {
internal class Keychain {
/**
* Set value for key in keychain
* - Parameters:
* - key: - Fixme: ⚠️️ add doc
* - value: - Fixme: ⚠️️ add doc
*/
static func set(key: String, value: String) throws {
internal static func set(key: String, value: String) throws {
guard let valueData = value.data(using: .utf8) else {
print("Keychain: Unable to store data, invalid input - key: \(key), value: \(value)")
Swift.print("Keychain: Unable to store data, invalid input - key: \(key), value: \(value)")
return
}
do { // Delete old value if stored first
try delete(itemKey: key)
} catch {
print("Keychain: nothing to delete...")
Swift.print("Keychain: nothing to delete...")
}
let queryAdd: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
Expand All @@ -32,7 +37,7 @@ class Keychain {
/**
* Get value from keychain
*/
static func get(key: String) throws -> String? {
internal static func get(key: String) throws -> String? {
let queryLoad: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key as AnyObject,
Expand Down
18 changes: 10 additions & 8 deletions Sources/Telemetry/type/Event.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import Foundation
/**
* - Fixme: ⚠️️ rename to TMEvent
* Event
*/
public struct Event: ActionKind {
public let category: String
public let action: String
public let label: String
public let params: [String: String]
/**
* Tracks an event to Google Analytics.
* - Remark: Generic events are reported using `event(_:action:label:parameters:)`.
* - Fixme: ⚠️️ rename to TMEvent
* Tracks an event to Google Analytics
* - Remark: Generic events are reported using `event(_:action:label:parameters:)`
* - Parameters:
* - category: The category of the event (ec).
* - action: The action of the event (ea).
* - label: The label of the event (el).
* - params: A dictionary of additional parameters for the event.
* - category: The category of the event (ec)
* - action: The action of the event (ea)
* - label: The label of the event (el)
* - params: A dictionary of additional parameters for the event
*/
public init(category: String, action: String, label: String = "", params: [String: String] = .init()) {
self.category = category
Expand All @@ -24,6 +23,9 @@ public struct Event: ActionKind {
self.params = params
}
}
/**
* Ext
*/
extension Event {
public var key: String { "event" }
public var output: [String: String] {
Expand Down
15 changes: 8 additions & 7 deletions Sources/Telemetry/type/Exception.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Foundation
// - Fixme: ⚠️️ rename to TMException
/**
* Exception
*/
public struct Exception: ActionKind {
public let description: String
public let isFatal: Bool
public let params: [String: String]
/**
* Tracks an exception event to Google Analytics.
* - remark: Exceptions are reported using `exception(_:isFatal:parameters:)`
* - Fixme: ⚠️️ rename to TM...
* Tracks an exception event to Google Analytics
* - Remark: Exceptions are reported using `exception(_:isFatal:parameters:)`
* - Parameters:
* - description: The description of the exception (ec).
* - isFatal: Indicates if the exception was fatal to the execution of the program (exf).
* - params: A dictionary of additional parameters for the event.
* - description: The description of the exception (ec)
* - isFatal: Indicates if the exception was fatal to the execution of the program (exf)
* - params: A dictionary of additional parameters for the event
*/
public init(description: String, isFatal: Bool, params: [String: String] = .init()) {
self.description = description
Expand Down
8 changes: 6 additions & 2 deletions Sources/Telemetry/type/ScreenView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation
// - Fixme: ⚠️️ rename to TMScreenView
/**
* ScreenView
*/
public struct ScreenView: ActionKind {
public let name: String
public let params: [String: String]
Expand All @@ -8,7 +10,6 @@ public struct ScreenView: ActionKind {
* - Remark: - Screen (page) views are reported using `screenView(_:parameters:)` with the name of the screen.
* - Remark: `dh` - hostname as appIdentifier and `dp` - path as screen name with leading `/`
* - Remark: and optional `dt` - document title as screen name pageview parameters for valid hit request.
* - Fixme: ⚠️️ rename to TM...
* - Parameters:
* - name: The name of the screen. Make sure it does not have spaces, use .replacingOccurrences(of: " ", with: "") etc

Check warning on line 14 in Sources/Telemetry/type/ScreenView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 123 characters (line_length)
* - params: A dictionary of additional parameters for the event.
Expand All @@ -18,6 +19,9 @@ public struct ScreenView: ActionKind {
self.params = params
}
}
/**
* Ext
*/
extension ScreenView {
public var key: String { "pageview" }
public var output: [String: String] {
Expand Down
6 changes: 4 additions & 2 deletions Sources/Telemetry/type/Session.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import Foundation
/**
* - Fixme: ⚠️️ Rename to TMSession?
* Session
*/
public struct Session: ActionKind {
public let start: Bool
public let params: [String: String]
/**
* Tracks a session start to Google Analytics by setting the `sc` parameter of the request. The `dp` parameter is set to the name of the application.

Check warning on line 9 in Sources/Telemetry/type/Session.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 152 characters (line_length)
* - Remark: Sessions are reported with `session(_:parameters:)` with the first parameter set to true for session start or false for session end

Check warning on line 10 in Sources/Telemetry/type/Session.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 147 characters (line_length)
* - Fixme: ⚠️️ rename to TM...?
* - Parameter start: true indicate session started, false - session finished.
*/
public init(start: Bool, params: [String: String] = .init()) {
self.start = start
self.params = params
}
}
/**
* Ext
*/
extension Session {
public var key: String { "session" }
public var output: [String: String] {
Expand Down
14 changes: 8 additions & 6 deletions Sources/Telemetry/type/Timing.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
/**
* - Fixme: ⚠️️ Rename to TMTiming
* Timing
*/
public struct Timing: ActionKind {
public let category: String
Expand All @@ -11,12 +11,11 @@ public struct Timing: ActionKind {
/**
* Tracks a timing to Google Analytics.
* - Remark: Timings are reported using `timing(_:name:label:time:parameters:)` with time parameter in seconds
* - Fixme: ⚠️️ rename to TM...
* - Parameters:
* - category: The category of the timing (utc).
* - name: The variable name of the timing (utv).
* - label: The variable label for the timing (utl).
* - time: Length of the timing (utt).
* - category: The category of the timing (utc)
* - name: The variable name of the timing (utv)
* - label: The variable label for the timing (utl)
* - time: Length of the timing (utt)
* - params: A dictionary of additional parameters for the timing
*/
public init(category: String, name: String, label: String = "", time: TimeInterval, params: [String: String] = .init()) {

Check warning on line 21 in Sources/Telemetry/type/Timing.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 124 characters (line_length)
Expand All @@ -27,6 +26,9 @@ public struct Timing: ActionKind {
self.params = params
}
}
/**
* Extension
*/
extension Timing {
public var key: String { "timing" }
public var output: [String: String] {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Telemetry/type/util/ActionKind.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Foundation
/**
* Protocol for structs types
* - Fixme: ⚠️️ add more doc
* - Fixme: ⚠️️ Rename to TMActionKind? or TMAction?
* - Fixme: ⚠️️ probably remove params from json etc
* - Fixme: ⚠️️ Probably remove params from json etc
*/
public protocol ActionKind: Codable {
var params: [String: String] { get }
var output: [String: String] { get }
var key: String { get } // - Fixme: ⚠️️ add doc
var key: String { get }
}
24 changes: 13 additions & 11 deletions Sources/Telemetry/type/util/Aggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Foundation
import FileSugar
import JSONSugar
/**
* Local aggregator (for testing, useful for UITests etc and testing beore setting up GA etc)
* - Fixme: ⚠️️ we could also use structured json, and store everything in one array etc?
* - Fixme: ⚠️️ add timestamping?
* - Fixme: ⚠️️ add more advance sessions, with uuid and timestamp etc
* - Fixme: ⚠️️ add support for storing meta data etc. Might require sqlite etc. since json file will get big and cluttered etc
* Local aggregator
* - Remark: This can be used to save logs etc.
* - Remark: For testig it's better to use Console output. Se Logger repo for code etc
* - Fixme: ⚠️️ We could also use structured json, and store everything in one array etc?
* - Fixme: ⚠️️ Add timestamping?
* - Fixme: ⚠️️ Add more advance sessions, with uuid and timestamp etc
* - Fixme: ⚠️️ Add support for storing meta data etc. Might require sqlite etc. since json file will get big and cluttered etc

Check warning on line 11 in Sources/Telemetry/type/util/Aggregator.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 125 characters (line_length)
* - Fixme: ⚠️️ Rename to TMAggregator?
*/
public class Aggregator: Codable {
Expand All @@ -28,7 +30,7 @@ public class Aggregator: Codable {
extension Aggregator {
/**
* Add action
* - Fixme: ⚠️️ rename to send?
* - Fixme: ⚠️️ Rename to send? or noterize or something?
*/
public func append(action: ActionKind) throws {
switch action {
Expand All @@ -37,7 +39,7 @@ extension Aggregator {
case let exception as Exception: exceptions.append(exception)
case let screenView as ScreenView: screenViews.append(screenView)
case let timing as Timing: timings.append(timing)
default: Swift.print("not supported")
default: Swift.print("⚠️️ Not supported")
}
try persist() // (save on each call)
}
Expand All @@ -53,7 +55,7 @@ extension Aggregator {
public static let tempFilePath: String = "\(NSHomeDirectory())/store.json" // or use tempfolder etc
/**
* Save current state to a file
* - Fixme: ⚠️️ (add sqlite later, or coredata)
* - Fixme: ⚠️️ Add sqlite later, or coredata
*/
public func persist() throws {
let data: Data = try self.encode()
Expand All @@ -63,8 +65,8 @@ extension Aggregator {
/**
* Load previouse saved aggregator
* - Parameters:
* - filePath: path to store file
* - reset: reset store file or not
* - filePath: Path to store file
* - reset: Reset store file or not
*/
public static func initiate(filePath: String = tempFilePath, reset: Bool = false) throws -> Aggregator {
if reset { try FileManager().removeItem(atPath: filePath) }
Expand All @@ -82,7 +84,7 @@ extension Aggregator {
extension Aggregator {
/**
* Read Aggregator stats:
* - Fixme: ⚠️️ add exceptions-fatal: 4 (only errors) etc?
* - Fixme: ⚠️️ Add exceptions-fatal: 4 (only errors) etc?
*/
public var stats: String {
var output: String = ""
Expand Down
6 changes: 4 additions & 2 deletions Sources/Telemetry/type/util/TMType.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Foundation

/**
* - Fixme: ⚠️️ Rename to EndPointType OR EPType
*/
public enum TMType {
case ga, agg(_ agg: Aggregator)
}
extension TMType {
/**
* Convenient getter for aggregator (since its stored in the type)
* Convenient getter for aggregator (since it's stored in the type)
*/
public var aggregator: Aggregator? {
if case .agg(let aggregator) = self {
Expand Down
Loading

0 comments on commit 78deef5

Please sign in to comment.