Skip to content

Commit

Permalink
feat: add functionality to manual flush events (#122)
Browse files Browse the repository at this point in the history
* fix: set flush policy batch size to 10

* feat: add manual flush capabilities

* docs: Add demo app flush button
  • Loading branch information
nicklasl authored May 23, 2024
1 parent 04274fd commit 475df55
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct ContentView: View {
color.color = .red
}
}
Button("Flush 🚽") {
confidence.flush()
}
}
.padding()
} else if case .error(let error) = status.state {
Expand Down
6 changes: 5 additions & 1 deletion Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public class Confidence: ConfidenceEventSender {
}
}

public func flush() {
eventSenderEngine.flush()
}

private func withLock(callback: @escaping (Confidence) -> Void) {
confidenceQueue.sync { [weak self] in
guard let self = self else {
Expand Down Expand Up @@ -308,7 +312,7 @@ extension Confidence {
clientSecret: clientSecret,
uploader: uploader,
storage: eventStorage,
flushPolicies: [SizeFlushPolicy(batchSize: 1)])
flushPolicies: [SizeFlushPolicy(batchSize: 10)])
return Confidence(
clientSecret: clientSecret,
region: region,
Expand Down
5 changes: 5 additions & 0 deletions Sources/Confidence/ConfidenceEventSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public protocol ConfidenceEventSender: Contextual {
The ConfidenceProducer can be used to push context changes or event tracking
*/
func track(producer: ConfidenceProducer)

/**
Schedule a manual flush of the event data currently stored on disk
*/
func flush()
}
15 changes: 11 additions & 4 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ protocol FlushPolicy {
protocol EventSenderEngine {
func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct)
func shutdown()
func flush()
}

final class EventSenderEngineImpl: EventSenderEngine {
Expand All @@ -33,13 +34,15 @@ final class EventSenderEngineImpl: EventSenderEngine {
self.uploader = uploader
self.clientSecret = clientSecret
self.storage = storage
self.flushPolicies = flushPolicies
self.flushPolicies = flushPolicies + [ManualFlushPolicy()]

writeReqChannel.sink { [weak self] event in
guard let self = self else { return }
do {
try self.storage.writeEvent(event: event)
} catch {
if event.name != manualFlushEvent.name { // skip storing flush events.
do {
try self.storage.writeEvent(event: event)
} catch {
}
}

self.flushPolicies.forEach { policy in policy.hit(event: event) }
Expand Down Expand Up @@ -106,6 +109,10 @@ final class EventSenderEngineImpl: EventSenderEngine {
)
}

func flush() {
writeReqChannel.send(manualFlushEvent)
}

func shutdown() {
for cancellable in cancellables {
cancellable.cancel()
Expand Down
19 changes: 19 additions & 0 deletions Sources/Confidence/ManualFlushPolicy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation

let manualFlushEvent = ConfidenceEvent(name: "manual_flush", payload: [:], eventTime: Date.backport.now)

class ManualFlushPolicy: FlushPolicy {
private var flushRequested = false

func reset() {
flushRequested = false
}

func hit(event: ConfidenceEvent) {
flushRequested = event.name == manualFlushEvent.name
}

func shouldFlush() -> Bool {
return flushRequested
}
}
49 changes: 49 additions & 0 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,53 @@ final class EventSenderEngineTest: XCTestCase {

XCTAssertEqual(storage.isEmpty(), false)
}

func testManualFlushWorks() throws {
let uploaderMock = EventUploaderMock()
let storageMock = EventStorageMock()
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
uploader: uploaderMock,
storage: storageMock,
// no other flush policy is set which means that only manual flushes will trigger upload
flushPolicies: []
)

eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:])
eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:])
eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:])
eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:])
XCTAssertEqual(storageMock.events.count, 4)
XCTAssertNil(uploaderMock.calledRequest)

eventSenderEngine.flush()

let expectation = XCTestExpectation(description: "Upload finished")
let cancellable = uploaderMock.subject.sink { _ in
expectation.fulfill()
}
wait(for: [expectation], timeout: 1)
let uploadRequest = uploaderMock.calledRequest
XCTAssertEqual(uploadRequest?.count, 4)

cancellable.cancel()
}


func testManualFlushEventIsNotStored() throws {
let uploaderMock = EventUploaderMock()
let storageMock = EventStorageMock()
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
uploader: uploaderMock,
storage: storageMock,
// no other flush policy is set which means that only manual flushes will trigger upload
flushPolicies: []
)

eventSenderEngine.flush()

XCTAssertEqual(storageMock.events.count, 0)
XCTAssertNil(uploaderMock.calledRequest)
}
}
4 changes: 2 additions & 2 deletions Tests/ConfidenceTests/EventUploaderMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ final class EventUploaderMock: ConfidenceClient {
}

final class EventStorageMock: EventStorage {
private var events: [ConfidenceEvent] = []
private var batches: [String: [ConfidenceEvent]] = [:]
var events: [ConfidenceEvent] = []
var batches: [String: [ConfidenceEvent]] = [:]
var removeCallback: () -> Void = {}

func startNewBatch() throws {
Expand Down
4 changes: 4 additions & 0 deletions Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class EventSenderEngineMock: EventSenderEngine {
func shutdown() {
// NO-OP
}

func flush() {
// NO-OP
}
}

0 comments on commit 475df55

Please sign in to comment.