Skip to content

Commit

Permalink
Main rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 15, 2024
1 parent 49d86d0 commit 3720dd2
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 216 deletions.
36 changes: 2 additions & 34 deletions Sources/Common/Backport.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import Foundation

<<<<<<<< HEAD:Sources/Confidence/Backport.swift
public extension URL {
struct Backport {
|||||||| parent of 44209ae (Finalize the network layer for events):Sources/ConfidenceProvider/Utils/Backport.swift
extension URL {
struct Backport {
========
extension URL {
public struct Backport {
>>>>>>>> 44209ae (Finalize the network layer for events):Sources/Common/Backport.swift
var base: URL

public init(base: URL) {
init(base: URL) {
self.base = base
}
}
Expand All @@ -22,16 +14,8 @@ extension URL {
}
}

<<<<<<<< HEAD:Sources/Confidence/Backport.swift
public extension URL.Backport {
var path: String {
|||||||| parent of 44209ae (Finalize the network layer for events):Sources/ConfidenceProvider/Utils/Backport.swift
extension URL.Backport {
var path: String {
========
extension URL.Backport {
public var path: String {
>>>>>>>> 44209ae (Finalize the network layer for events):Sources/Common/Backport.swift
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
return self.base.path(percentEncoded: false)
} else {
Expand All @@ -52,31 +36,15 @@ extension URL.Backport {
}
}

<<<<<<<< HEAD:Sources/Confidence/Backport.swift
public extension Date {
struct Backport {
|||||||| parent of 44209ae (Finalize the network layer for events):Sources/ConfidenceProvider/Utils/Backport.swift
extension Date {
struct Backport {
========
extension Date {
public struct Backport {
>>>>>>>> 44209ae (Finalize the network layer for events):Sources/Common/Backport.swift
}

static public var backport: Backport.Type { Backport.self }
}

<<<<<<<< HEAD:Sources/Confidence/Backport.swift
public extension Date.Backport {
static var now: Date {
|||||||| parent of 44209ae (Finalize the network layer for events):Sources/ConfidenceProvider/Utils/Backport.swift
extension Date.Backport {
static var now: Date {
========
extension Date.Backport {
static public var now: Date {
>>>>>>>> 44209ae (Finalize the network layer for events):Sources/Common/Backport.swift
if #available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) {
return Date.now
} else {
Expand All @@ -92,7 +60,7 @@ extension Date.Backport {
}
}

static func toISOString(date: Date) -> String {
static public func toISOString(date: Date) -> String {
if #available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) {
return date.ISO8601Format()
} else {
Expand Down
100 changes: 53 additions & 47 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,56 @@ public class Confidence: ConfidenceEventSender {
public let clientSecret: String
public var timeout: TimeInterval
public var region: ConfidenceRegion
let eventSenderEngine: EventSenderEngine
public var initializationStrategy: InitializationStrategy
private var removedContextKeys: Set<String> = Set()
private var client: ConfidenceClient

required init(
clientSecret: String,
timeout: TimeInterval,
region: ConfidenceRegion,
eventSenderEngine: EventSenderEngine,
initializationStrategy: InitializationStrategy,
context: ConfidenceStruct = [:],
client: ConfidenceClient,
parent: ConfidenceEventSender? = nil
) {
self.eventSenderEngine = eventSenderEngine
self.clientSecret = clientSecret
self.timeout = timeout
self.region = region
self.initializationStrategy = initializationStrategy
self.context = context
self.client = client
self.parent = parent
}

public convenience init(
clientSecret: String,
timeout: TimeInterval,
region: ConfidenceRegion,
initializationStrategy: InitializationStrategy,
context: ConfidenceStruct = [:],
parent: ConfidenceEventSender? = nil
) {
self.init(
clientSecret: clientSecret,
timeout: timeout,
region: region,
initializationStrategy: initializationStrategy,
client: RemoteConfidenceClient(
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
timeout: timeout,
region: region),
metadata: ConfidenceMetadata(
name: "SDK_ID_SWIFT_CONFIDENCE",
version: "0.1.4") // x-release-please-version)
)
)
}
// public convenience init(
// clientSecret: String,
// timeout: TimeInterval,
// region: ConfidenceRegion,
// initializationStrategy: InitializationStrategy,
// context: ConfidenceStruct = [:],
// parent: ConfidenceEventSender? = nil
// ) {
// self.init(
// clientSecret: clientSecret,
// timeout: timeout,
// region: region,
// eventSenderEngine: self.eventSenderEngine,
// initializationStrategy: initializationStrategy,
// client: RemoteConfidenceClient(
// options: ConfidenceClientOptions(
// credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
// timeout: timeout,
// region: region),
// metadata: ConfidenceMetadata(
// name: "SDK_ID_SWIFT_CONFIDENCE",
// version: "0.1.4") // x-release-please-version)
// )
// )
// }

public func send(name: String, payload: ConfidenceStruct) {
print("Sending: \"\(name)\".\nMessage: \(payload)\nContext: \(context)")
Task {
// TODO: This will be called inside the EventSenderEngine once implemented
try? await client.upload(batch: [
ConfidenceEvent(
name: name,
payload: NetworkTypeMapper.from(value: payload),
time: Date.backport.nowISOString)
])
}
eventSenderEngine.send(name: name, message: payload)
}


Expand Down Expand Up @@ -92,9 +84,9 @@ public class Confidence: ConfidenceEventSender {
clientSecret: clientSecret,
timeout: timeout,
region: region,
eventSenderEngine: eventSenderEngine,
initializationStrategy: initializationStrategy,
context: context,
client: client,
parent: self)
}
}
Expand All @@ -105,9 +97,15 @@ extension Confidence {
var timeout: TimeInterval = 10.0
var region: ConfidenceRegion = .global
var initializationStrategy: InitializationStrategy = .fetchAndActivate
let eventStorage: EventStorage

public init(clientSecret: String) {
self.clientSecret = clientSecret
do {
eventStorage = try EventStorageImpl()
} catch {
eventStorage = EventStorageInMemory()
}
}

public func withTimeout(timeout: TimeInterval) -> Builder {
Expand All @@ -127,20 +125,28 @@ extension Confidence {
}

public func build() -> Confidence {
let uploader = RemoteConfidenceClient(
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
timeout: timeout,
region: region),
metadata: ConfidenceMetadata(
name: "SDK_ID_SWIFT_CONFIDENCE",
version: "0.1.4") // x-release-please-version
)
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: clientSecret,
uploader: uploader,
storage: eventStorage,
flushPolicies: [])
return Confidence(
clientSecret: clientSecret,
timeout: timeout,
region: region,
eventSenderEngine: eventSenderEngine,
initializationStrategy: initializationStrategy,
client: RemoteConfidenceClient(
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
timeout: timeout,
region: region),
metadata: ConfidenceMetadata(
name: "SDK_ID_SWIFT_CONFIDENCE",
version: "0.1.4") // x-release-please-version
)
context: [:],
parent: nil
)
}
}
Expand Down
8 changes: 1 addition & 7 deletions Sources/Confidence/ConfidenceClient/ConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@ import Common

protocol ConfidenceClient {
// Returns true if the batch has been correctly processed by the backend
func upload(batch: [ConfidenceEvent]) async throws -> Bool
}

struct ConfidenceEvent: Codable {
var name: String
var payload: NetworkStruct
var time: String
func upload(events: [NetworkEvent]) async throws -> Bool
}
18 changes: 6 additions & 12 deletions Sources/Confidence/ConfidenceClient/RemoteConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Common
import os

public class RemoteConfidenceClient: ConfidenceClient {
private var options: ConfidenceClientOptions
private var options: ConfidenceClientOptions
private let metadata: ConfidenceMetadata
private var httpClient: HttpClient
private var baseUrl: String
Expand All @@ -26,16 +26,10 @@ public class RemoteConfidenceClient: ConfidenceClient {
self.metadata = metadata
}

func upload(batch: [ConfidenceEvent]) async throws -> Bool {
func upload(events: [NetworkEvent]) async throws -> Bool {
let timeString = Date.backport.nowISOString
let request = PublishEventRequest(
events: batch.map { event in
Event(
eventDefinition: "eventDefinitions/\(event.name)",
payload: event.payload,
time: timeString
)
},
events: events,
clientSecret: options.credentials.getSecret(),
sendTime: timeString,
sdk: Sdk(id: metadata.name, version: metadata.version)
Expand Down Expand Up @@ -71,16 +65,16 @@ public class RemoteConfidenceClient: ConfidenceClient {
}

struct PublishEventRequest: Codable {
var events: [Event]
var events: [NetworkEvent]
var clientSecret: String
var sendTime: String
var sdk: Sdk
}

struct Event: Codable {
struct NetworkEvent: Codable {
var eventDefinition: String
var payload: NetworkStruct
var time: String
var eventTime: String
}

struct PublishEventResponse: Codable {
Expand Down
27 changes: 14 additions & 13 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Combine
import Common
import Foundation

protocol FlushPolicy {
Expand All @@ -7,12 +8,8 @@ protocol FlushPolicy {
func shouldFlush() -> Bool
}

protocol Clock {
func now() -> Date
}

protocol EventSenderEngine {
func send(name: String, message: ConfidenceStruct) throws
func send(name: String, message: ConfidenceStruct)
func shutdown()
}

Expand All @@ -25,16 +22,13 @@ final class EventSenderEngineImpl: EventSenderEngine {
private let flushPolicies: [FlushPolicy]
private let uploader: ConfidenceClient
private let clientSecret: String
private let clock: Clock

init(
clientSecret: String,
uploader: ConfidenceClient,
clock: Clock,
storage: EventStorage,
flushPolicies: [FlushPolicy]
) {
self.clock = clock
self.uploader = uploader
self.clientSecret = clientSecret
self.storage = storage
Expand Down Expand Up @@ -63,8 +57,15 @@ final class EventSenderEngineImpl: EventSenderEngine {
try self.storage.startNewBatch()
let ids = try storage.batchReadyIds()
for id in ids {
let events = try self.storage.eventsFrom(id: id)
let shouldCleanup = try await self.uploader.upload(batch: events)
let events: [NetworkEvent] = try self.storage.eventsFrom(id: id)
.compactMap({ event in
let networkPayload = event.payload.compactMapValues({ payloadValue in try? NetworkTypeMapper.convertValue(payloadValue)})
return NetworkEvent(
eventDefinition: event.name,
payload: NetworkStruct(fields: networkPayload),
eventTime: Date.backport.toISOString(date: event.eventTime))
})
let shouldCleanup = try await self.uploader.upload(events: events)
if shouldCleanup {
try storage.remove(id: id)
}
Expand All @@ -75,11 +76,11 @@ final class EventSenderEngineImpl: EventSenderEngine {
.store(in: &cancellables)
}

func send(name: String, message: [String: ConfidenceValue]) throws {
func send(name: String, message: [String: ConfidenceValue]) {
writeReqChannel.send(ConfidenceEvent(
name: name,
payload: try NetworkTypeMapper.from(value: message),
time: Date.backport.nowISOString)
payload: message,
eventTime: Date.backport.now)
)
}

Expand Down
15 changes: 0 additions & 15 deletions Sources/Confidence/EventSenderStorage.swift

This file was deleted.

Loading

0 comments on commit 3720dd2

Please sign in to comment.