Skip to content

Commit

Permalink
Merge pull request #20 from at-internet/develop
Browse files Browse the repository at this point in the history
v3.0.8
  • Loading branch information
alexey-troshkov authored Nov 29, 2023
2 parents a4a61da + f6eda75 commit 51d331e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 36 deletions.
2 changes: 1 addition & 1 deletion PianoAnalytics-AppExtension.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PianoAnalytics-AppExtension'
s.version = '3.0.7'
s.version = '3.0.8'
s.summary = 'Piano Analytics solution for extension Apple devices'
s.homepage = 'https://github.com/at-internet/piano-analytics-apple'
s.documentation_url = 'https://developers.atinternet-solutions.com/piano-analytics'
Expand Down
2 changes: 1 addition & 1 deletion PianoAnalytics.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PianoAnalytics'
s.version = '3.0.7'
s.version = '3.0.8'
s.summary = 'Piano Analytics library for Apple devices'
s.homepage = 'https://github.com/at-internet/piano-analytics-apple'
s.documentation_url = 'https://developers.atinternet-solutions.com/piano-analytics'
Expand Down
4 changes: 3 additions & 1 deletion Sources/PianoAnalytics/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ public final class BuiltModel {

public final let uri: String?
public final let body: String?
public final let chunks: [String]?
final let mustBeSaved: Bool

init(uri: String?, body: String?, mustBeSaved: Bool) {
init(uri: String?, body: String?, chunks: [String]?, mustBeSaved: Bool) {
self.uri = uri
self.body = body
self.chunks = chunks
self.mustBeSaved = mustBeSaved
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/PianoAnalytics/PianoAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class PianoAnalytics {
// MARK: PUBLIC SECTION

/// SDK version
public static let sdkVersion = "3.0.7"
public static let sdkVersion = "3.0.8"

/// Send event
///
Expand Down
23 changes: 16 additions & 7 deletions Sources/PianoAnalytics/Steps/BuildStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class BuildStep: Step {

private static let RequestUriFormat = "https://%@%@?s=%@&idclient=%@"
private static let EventsField = "events"
private static let ChunkSize = 50

// MARK: Step Implementation

Expand All @@ -55,12 +56,15 @@ final class BuildStep: Step {
contextProperties[InternalContextPropertiesStep.ConnectionTypeProperty] = ContextProperty(value: ConnectionType.Offline.rawValue)
mustBeSaved = true
}

/// BODY
let body = PianoAnalyticsUtils.toJSONData([
// TODO
BuildStep.EventsField: events.map {$0.toMap(context: [:])}
]) ?? Data()

var chunks: [String] = []
stride(from: 0, to: m.events.count, by: BuildStep.ChunkSize).forEach { i in
if let chunk = PianoAnalyticsUtils.toJSONData([
BuildStep.EventsField: events[i..<min(i + BuildStep.ChunkSize, m.events.count)].map {$0.toMap(context: [:])}
]) {
chunks.append(String(decoding: chunk, as: UTF8.self))
}
}

/// URI
let visitorId = conf.get(ConfigurationKey.VisitorId)
Expand All @@ -70,7 +74,12 @@ final class BuildStep: Step {
conf.get(ConfigurationKey.Site),
visitorId)

m.builtModel = BuiltModel(uri: uri, body: String(decoding: body, as: UTF8.self), mustBeSaved: mustBeSaved)
m.builtModel = BuiltModel(
uri: uri,
body: nil,
chunks: chunks,
mustBeSaved: mustBeSaved
)
return true

}
Expand Down
4 changes: 4 additions & 0 deletions Sources/PianoAnalytics/Steps/CrashHandlingStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ final class CrashHandlingStep: Step {
// MARK: Step Implementation

func processSetConfig(m: inout Model) {
if #available(watchOS 1.0, *) {
return
}

/// REQUIREMENTS
let conf = m.configuration

Expand Down
15 changes: 1 addition & 14 deletions Sources/PianoAnalytics/Steps/SendStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,6 @@ final class SendStep: Step {
}

func processSendEvents(m: inout Model, p: PianoAnalyticsWorkProtocol?) -> Bool {
/// REQUIREMENTS
let conf = m.configuration
let stored = m.storage

guard let buildModel = m.builtModel else {
return false
}

let userAgent = conf.get(ConfigurationKey.CustomUserAgent)
let offlineSendInterval = Double(conf.get(ConfigurationKey.OfflineSendInterval).toInt())
self.sendStoredData(stored, userAgent: userAgent, intervalInMs: offlineSendInterval)
self.send(buildModel, userAgent: userAgent)

return true
return processSendOfflineData(m: &m, p: p)
}
}
29 changes: 20 additions & 9 deletions Sources/PianoAnalytics/Steps/StorageStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ final class StorageStep: Step {
let content = try String(contentsOf: f)
if let data = Crypt.decrypt(data: content) {
if let map = PianoAnalyticsUtils.fromJSONData(Data(data.utf8)) {
storedData[f.absoluteString] = BuiltModel(uri: map[StorageStep.UriField], body: map[StorageStep.BodyField], mustBeSaved: false)
storedData[f.absoluteString] = BuiltModel(
uri: map[StorageStep.UriField],
body: map[StorageStep.BodyField],
chunks: nil,
mustBeSaved: false
)
}
}
}
Expand Down Expand Up @@ -148,20 +153,26 @@ final class StorageStep: Step {
let conf = m.configuration
let encryptionMode = EncryptionMode.init(rawValue: conf.get(ConfigurationKey.OfflineEncryptionMode)) ?? EncryptionMode.IfCompatible

if let builtModel = m.builtModel, builtModel.mustBeSaved {
guard let jsonData = PianoAnalyticsUtils.toJSONData([
StorageStep.UriField: builtModel.uri,
StorageStep.BodyField: builtModel.body
]) else {
if let builtModel = m.builtModel {
if let chunks = builtModel.chunks {
chunks.forEach { chunk in
guard let jsonData = PianoAnalyticsUtils.toJSONData([
StorageStep.UriField: builtModel.uri,
StorageStep.BodyField: chunk
]) else {
return
}
self.storeData(String(decoding: jsonData, as: UTF8.self), encryptionMode: encryptionMode)
}
}

if builtModel.mustBeSaved {
return false
}
self.storeData(String(decoding: jsonData, as: UTF8.self), encryptionMode: encryptionMode)
return false
}

/// Retrieve stored data
m.storage = self.readData()
return true

}
}
17 changes: 15 additions & 2 deletions Tests/PianoAnalyticsTests/PropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ class TestProtocol: PianoAnalyticsWorkProtocol {
}

func onBeforeSend(built: BuiltModel?, stored: [String: BuiltModel]?) -> Bool {
builtModel = built
completionHandler(built, stored)
do {
try stored?.forEach { key, _ in
if let url = URL(string: key), FileManager.default.fileExists(atPath: key.replacingOccurrences(of: "file://", with: "")) {
try FileManager.default.removeItem(at: url)
}
}
} catch {}

builtModel = stored?.first?.value
completionHandler(builtModel, stored)

return false
}
}
Expand All @@ -55,10 +64,12 @@ class PropertiesTests: XCTestCase {
// Put setup code here. This method is called before the invocation of each test method in the class.
clearStorage()
self.pa = PianoAnalytics(configFileLocation: "default-test.json")
pa.deleteOfflineData()
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
pa.deleteOfflineData()
}

func clearStorage() {
Expand Down Expand Up @@ -156,9 +167,11 @@ class PropertiesTests: XCTestCase {
self.pa.sendEvent(Event("another", data: [:]), config: nil, p: TestProtocol { built, _ in
localBuilt1 = built
})
self.pa.deleteOfflineData()
self.pa.sendEvent(Event("toto", data: [:]), config: nil, p: TestProtocol { built, _ in
localBuilt2 = built
})
self.pa.deleteOfflineData()
self.pa.sendEvent(Event("tata", data: [:]), config: nil, p: TestProtocol { built, _ in
localBuilt3 = built
expectation.fulfill()
Expand Down
2 changes: 2 additions & 0 deletions Tests/PianoAnalyticsTests/UsersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class UsersTests: XCTestCase {
// Put setup code here. This method is called before the invocation of each test method in the class.
clearStorage()
self.pa = PianoAnalytics(configFileLocation: "default-test.json")
pa.deleteOfflineData()
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
pa.deleteOfflineData()
}

func clearStorage() {
Expand Down

0 comments on commit 51d331e

Please sign in to comment.