Skip to content

Additions to enable CLI support #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

Analytics.support.track(name: "test event")

Analytics.main.screen(title: "home screen shown", category: nil, properties: nil)

return true
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/other_plugins/CellularCarrier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CellularCarrier: Plugin {
func execute<T: RawEvent>(event: T?) -> T? {
guard var workingEvent = event else { return event }

if let isCellular: Bool = workingEvent.context?[keyPath: "network"],
if let isCellular: Bool = workingEvent.context?[keyPath: "network.cellular"],
isCellular,
let carriers = self.carriers
{
Expand Down
40 changes: 40 additions & 0 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,43 @@ extension Analytics {
}

}

extension Analytics {
/// Determine if there are any events that have yet to be sent to Segment
public var hasUnsentEvents: Bool {
if let segmentDest = self.find(pluginType: SegmentDestination.self) {
if segmentDest.pendingUploads > 0 {
return true
}
if segmentDest.eventCount > 0 {
return true
}
}

if let files = storage.read(Storage.Constants.events) {
if files.count > 0 {
return true
}
}

return false
}

/// Provides a list of finished, but unsent events.
public var pendingUploads: [URL]? {
return storage.read(Storage.Constants.events)
}

/// Wait until the Analytics object has completed startup.
/// This method is primarily useful for command line utilities where
/// it's desirable to wait until the system is up and running
/// before executing commands. GUI apps could potentially use this via
/// a background thread if needed.
public func waitUntilStarted() {
if let startupQueue = find(pluginType: StartupQueue.self) {
while startupQueue.running != true {
RunLoop.main.run(until: Date.distantPast)
}
}
}
}
5 changes: 3 additions & 2 deletions Sources/Segment/Plugins/SegmentDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SegmentDestination: DestinationPlugin {
private var apiKey: String? = nil
private var apiHost: String? = nil

@Atomic private var eventCount: Int = 0
@Atomic internal var eventCount: Int = 0
internal var flushTimer: QueueTimer? = nil

internal func initialSetup() {
Expand Down Expand Up @@ -99,7 +99,6 @@ public class SegmentDestination: DestinationPlugin {
storage.write(.events, value: event)
eventCount += 1
if eventCount >= analytics.configuration.values.flushAt {
eventCount = 0
flush()
}
}
Expand All @@ -112,6 +111,7 @@ public class SegmentDestination: DestinationPlugin {
// Read events from file system
guard let data = storage.read(Storage.Constants.events) else { return }

eventCount = 0
cleanupUploads()

analytics.log(message: "Uploads in-progress: \(pendingUploads)")
Expand All @@ -124,6 +124,7 @@ public class SegmentDestination: DestinationPlugin {
switch result {
case .success(_):
storage.remove(file: url)
self.cleanupUploads()
Copy link

@grzegorzkrukowski grzegorzkrukowski Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bsneed - why this line has been added here when there is another call of this method that happens independently from the result value?

https://github.com/segmentio/analytics-swift/pull/148/files#diff-92679f628b3def3ba245e4435edfcea09ea06a77277e6e2497aa331efbf46028R136

Copy link
Contributor Author

@bsneed bsneed Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch @grzegorzkrukowski ! The cleanupUploads call here isn't necessary. Fortunately the subsequent call results in a no-op.

default:
analytics.logFlush()
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Segment/Plugins/StartupQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import Foundation
import Sovran

internal class StartupQueue: Plugin, Subscriber {
public class StartupQueue: Plugin, Subscriber {
static let maxSize = 1000

@Atomic var running: Bool = false
@Atomic public var running: Bool = false

let type: PluginType = .before
public let type: PluginType = .before

var analytics: Analytics? = nil {
public var analytics: Analytics? = nil {
didSet {
analytics?.store.subscribe(self, handler: runningUpdate)
}
Expand All @@ -26,7 +26,7 @@ internal class StartupQueue: Plugin, Subscriber {

required init() { }

func execute<T: RawEvent>(event: T?) -> T? {
public func execute<T: RawEvent>(event: T?) -> T? {
if running == false, let e = event {
// timeline hasn't started, so queue it up.
syncQueue.sync {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ extension JSON {
do {
result = try JSONDecoder().decode(T.self, from: jsonData)
} catch {
Analytics.segmentLog(message: "Unable to decode object to a Codable: \(error)", kind: .error)
Analytics.segmentLog(message: "Unable to decode object (\(keyPath)) to a Codable: \(error)", kind: .error)
}
}
if result == nil {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Segment/Utilities/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ extension Storage {
}

private func finish(file: URL) {
if self.fileHandle == nil {
self.fileHandle = try? FileHandle(forWritingTo: file)
}

guard let fileHandle = self.fileHandle else {
// we haven't actually started a file yet and being told to flush
// so ignore it and get out.
Expand Down