Skip to content

Commit 53bc748

Browse files
bsneedBrandon Sneed
and
Brandon Sneed
authored
Additions to enable CLI support (#148)
* Adds property to determine if any events are pending being sent. * Make public and rename * refactro * fine tuning * additional checks * logic fix * optimization * fixed event count set * stop bypassing sync queue * fixed optional * added another convenience method * fix missing file handle * fix missing optional * another fix * fixes * more fixes * and another * utility method * docs update * Add screen call to example * Improve error message for path lookup failure. * Fixed cellular carrier path. Co-authored-by: Brandon Sneed <[email protected]>
1 parent 508ecc9 commit 53bc748

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2828

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

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

3233
return true
3334
}

Examples/other_plugins/CellularCarrier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CellularCarrier: Plugin {
5959
func execute<T: RawEvent>(event: T?) -> T? {
6060
guard var workingEvent = event else { return event }
6161

62-
if let isCellular: Bool = workingEvent.context?[keyPath: "network"],
62+
if let isCellular: Bool = workingEvent.context?[keyPath: "network.cellular"],
6363
isCellular,
6464
let carriers = self.carriers
6565
{

Sources/Segment/Analytics.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,43 @@ extension Analytics {
157157
}
158158

159159
}
160+
161+
extension Analytics {
162+
/// Determine if there are any events that have yet to be sent to Segment
163+
public var hasUnsentEvents: Bool {
164+
if let segmentDest = self.find(pluginType: SegmentDestination.self) {
165+
if segmentDest.pendingUploads > 0 {
166+
return true
167+
}
168+
if segmentDest.eventCount > 0 {
169+
return true
170+
}
171+
}
172+
173+
if let files = storage.read(Storage.Constants.events) {
174+
if files.count > 0 {
175+
return true
176+
}
177+
}
178+
179+
return false
180+
}
181+
182+
/// Provides a list of finished, but unsent events.
183+
public var pendingUploads: [URL]? {
184+
return storage.read(Storage.Constants.events)
185+
}
186+
187+
/// Wait until the Analytics object has completed startup.
188+
/// This method is primarily useful for command line utilities where
189+
/// it's desirable to wait until the system is up and running
190+
/// before executing commands. GUI apps could potentially use this via
191+
/// a background thread if needed.
192+
public func waitUntilStarted() {
193+
if let startupQueue = find(pluginType: StartupQueue.self) {
194+
while startupQueue.running != true {
195+
RunLoop.main.run(until: Date.distantPast)
196+
}
197+
}
198+
}
199+
}

Sources/Segment/Plugins/SegmentDestination.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class SegmentDestination: DestinationPlugin {
4747
private var apiKey: String? = nil
4848
private var apiHost: String? = nil
4949

50-
@Atomic private var eventCount: Int = 0
50+
@Atomic internal var eventCount: Int = 0
5151
internal var flushTimer: QueueTimer? = nil
5252

5353
internal func initialSetup() {
@@ -99,7 +99,6 @@ public class SegmentDestination: DestinationPlugin {
9999
storage.write(.events, value: event)
100100
eventCount += 1
101101
if eventCount >= analytics.configuration.values.flushAt {
102-
eventCount = 0
103102
flush()
104103
}
105104
}
@@ -112,6 +111,7 @@ public class SegmentDestination: DestinationPlugin {
112111
// Read events from file system
113112
guard let data = storage.read(Storage.Constants.events) else { return }
114113

114+
eventCount = 0
115115
cleanupUploads()
116116

117117
analytics.log(message: "Uploads in-progress: \(pendingUploads)")
@@ -124,6 +124,7 @@ public class SegmentDestination: DestinationPlugin {
124124
switch result {
125125
case .success(_):
126126
storage.remove(file: url)
127+
self.cleanupUploads()
127128
default:
128129
analytics.logFlush()
129130
}

Sources/Segment/Plugins/StartupQueue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import Foundation
99
import Sovran
1010

11-
internal class StartupQueue: Plugin, Subscriber {
11+
public class StartupQueue: Plugin, Subscriber {
1212
static let maxSize = 1000
1313

14-
@Atomic var running: Bool = false
14+
@Atomic public var running: Bool = false
1515

16-
let type: PluginType = .before
16+
public let type: PluginType = .before
1717

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

2727
required init() { }
2828

29-
func execute<T: RawEvent>(event: T?) -> T? {
29+
public func execute<T: RawEvent>(event: T?) -> T? {
3030
if running == false, let e = event {
3131
// timeline hasn't started, so queue it up.
3232
syncQueue.sync {

Sources/Segment/Utilities/JSON.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ extension JSON {
397397
do {
398398
result = try JSONDecoder().decode(T.self, from: jsonData)
399399
} catch {
400-
Analytics.segmentLog(message: "Unable to decode object to a Codable: \(error)", kind: .error)
400+
Analytics.segmentLog(message: "Unable to decode object (\(keyPath)) to a Codable: \(error)", kind: .error)
401401
}
402402
}
403403
if result == nil {

Sources/Segment/Utilities/Storage.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ extension Storage {
285285
}
286286

287287
private func finish(file: URL) {
288+
if self.fileHandle == nil {
289+
self.fileHandle = try? FileHandle(forWritingTo: file)
290+
}
291+
288292
guard let fileHandle = self.fileHandle else {
289293
// we haven't actually started a file yet and being told to flush
290294
// so ignore it and get out.

0 commit comments

Comments
 (0)