Skip to content

Commit

Permalink
Expose event store from emitter controller to be able to remove all e…
Browse files Browse the repository at this point in the history
…vents from database (close #834)

PR #870

---------

Co-authored-by: Daniel Gutiérrez Ayuso <[email protected]>
  • Loading branch information
matus-tomlein and danigutierrezayuso committed Feb 1, 2024
1 parent 4cbc55f commit b394803
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Core/Emitter/EmitterControllerImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class EmitterControllerImpl: Controller, EmitterController {
}
}

var eventStore: EventStore {
return emitter.eventStore
}

// MARK: - Methods

func flush() {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Core/InternalQueue/EmitterControllerIQWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class EmitterControllerIQWrapper: EmitterController {
get { return InternalQueue.sync { controller.maxEventStoreAge } }
set { InternalQueue.sync { controller.maxEventStoreAge = newValue } }
}

var eventStore: EventStore {
get { return InternalQueue.sync { EventStoreIQWrapper(eventStore: controller.eventStore) } }
}

// MARK: - Methods

Expand Down
52 changes: 52 additions & 0 deletions Sources/Core/InternalQueue/EventStoreIQWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2013-present Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License
// Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

import Foundation

class EventStoreIQWrapper: NSObject, EventStore {

private let eventStore: EventStore

init(eventStore: EventStore) {
self.eventStore = eventStore
}

func addEvent(_ payload: Payload) {
InternalQueue.sync { eventStore.addEvent(payload) }
}

func removeEvent(withId storeId: Int64) -> Bool {
return InternalQueue.sync { eventStore.removeEvent(withId: storeId) }
}

func removeEvents(withIds storeIds: [Int64]) -> Bool {
return InternalQueue.sync { eventStore.removeEvents(withIds: storeIds) }
}

func removeAllEvents() -> Bool {
return InternalQueue.sync { eventStore.removeAllEvents() }
}

func count() -> UInt {
return InternalQueue.sync { eventStore.count() }
}

func emittableEvents(withQueryLimit queryLimit: UInt) -> [EmitterEvent] {
return InternalQueue.sync { eventStore.emittableEvents(withQueryLimit: queryLimit) }
}

func removeOldEvents(maxSize: Int64, maxAge: TimeInterval) {
return InternalQueue.sync { eventStore.removeOldEvents(maxSize: maxSize, maxAge: maxAge) }
}

}
3 changes: 3 additions & 0 deletions Sources/Snowplow/Controllers/EmitterController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public protocol EmitterController: EmitterConfigurationProtocol {
/// Whether the emitter is currently sending events.
@objc
var isSending: Bool { get }
/// The EventStore being used by the emitter.
@objc
var eventStore: EventStore { get }
@objc
func flush()
/// Pause emitting events.
Expand Down
21 changes: 21 additions & 0 deletions Tests/Configurations/TestEmitterConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ class TestEmitterConfiguration: XCTestCase {
XCTAssertEqual(0, tracker.emitter?.dbCount)
}

func testAllowsAccessToTheEventStore() {
let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 200)
let networkConfig = NetworkConfiguration(networkConnection: networkConnection)

let tracker = createTracker(networkConfig: networkConfig, emitterConfig: EmitterConfiguration())

tracker.emitter?.pause()
for i in 0..<10 {
_ = tracker.track(Structured(category: "cat", action: "act").value(NSNumber(value: i)))
}
Thread.sleep(forTimeInterval: 0.5)

XCTAssertEqual(10, tracker.emitter?.dbCount)
XCTAssertEqual(10, tracker.emitter?.eventStore.count())

XCTAssertTrue(tracker.emitter?.eventStore.removeAllEvents() ?? false)

XCTAssertEqual(0, tracker.emitter?.dbCount)
XCTAssertEqual(0, tracker.emitter?.eventStore.count())
}

private func createTracker(networkConfig: NetworkConfiguration, emitterConfig: EmitterConfiguration) -> TrackerController {
let trackerConfig = TrackerConfiguration()
trackerConfig.installAutotracking = false
Expand Down

0 comments on commit b394803

Please sign in to comment.