Skip to content
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

Decorated impressions #599

Merged
merged 3 commits into from
Dec 20, 2024
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
6 changes: 6 additions & 0 deletions Split.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@
C5977C642BF7D5FC003E293A /* HttpParameter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A3F5202BEBDAD6009FACD3 /* HttpParameter.swift */; };
C5A3F51F2BEBD893009FACD3 /* Spec.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A3F51E2BEBD893009FACD3 /* Spec.swift */; };
C5A3F5212BEBDAD6009FACD3 /* HttpParameter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A3F5202BEBDAD6009FACD3 /* HttpParameter.swift */; };
C5BD1E4C2D11A993008EF198 /* DecoratedImpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5BD1E4B2D11A98E008EF198 /* DecoratedImpression.swift */; };
C5BD1E4D2D11A993008EF198 /* DecoratedImpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5BD1E4B2D11A98E008EF198 /* DecoratedImpression.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -1919,6 +1921,7 @@
C5977C582BF53F43003E293A /* splitchanges_unsupported.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = splitchanges_unsupported.json; sourceTree = "<group>"; };
C5A3F51E2BEBD893009FACD3 /* Spec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Spec.swift; sourceTree = "<group>"; };
C5A3F5202BEBDAD6009FACD3 /* HttpParameter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpParameter.swift; sourceTree = "<group>"; };
C5BD1E4B2D11A98E008EF198 /* DecoratedImpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecoratedImpression.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -2145,6 +2148,7 @@
3B6DEE8720EA6AE00067435E /* Impressions */ = {
isa = PBXGroup;
children = (
C5BD1E4B2D11A98E008EF198 /* DecoratedImpression.swift */,
955E12322BFBF22800AE6D10 /* UniqueKey.swift */,
952E2679283410620015D633 /* HashedImpression.swift */,
95D9446A283BEE4D00D7FFED /* UniqueKeys.swift */,
Expand Down Expand Up @@ -4081,6 +4085,7 @@
5905D4CC2555F99D006DA3B1 /* ImpressionEntity.swift in Sources */,
59B8B8B0220C6E3F003E0D5A /* ValidationErrorInfo.swift in Sources */,
956A7E252979F2290080D53C /* SplitsEncoder.swift in Sources */,
C5BD1E4C2D11A993008EF198 /* DecoratedImpression.swift in Sources */,
955A5A52258D04DF00CAEE9F /* RecorderWorker.swift in Sources */,
593225FF24A4D8FD00496D8B /* HttpResponse.swift in Sources */,
952E266B2833E35A0015D633 /* PersistentUniqueKeysStorage.swift in Sources */,
Expand Down Expand Up @@ -4596,6 +4601,7 @@
95B02CD828D0BDC20030EC8B /* String+Utils.swift in Sources */,
95B02CD928D0BDC20030EC8B /* Array+DynamicCodable.swift in Sources */,
95B02CDA28D0BDC20030EC8B /* Dictionary+DynamicCodable.swift in Sources */,
C5BD1E4D2D11A993008EF198 /* DecoratedImpression.swift in Sources */,
95B02CDB28D0BDC20030EC8B /* Bundle+Finder.swift in Sources */,
95B02CDC28D0BDC20030EC8B /* Bundle+Name.swift in Sources */,
95B02CDD28D0BDC20030EC8B /* UInt64+bits.swift in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions Split/Engine/DefaultTreatmentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ extension DefaultTreatmentManager {
}

private func logImpression(label: String, changeNumber: Int64? = nil,
treatment: String, splitName: String, attributes: [String: Any]? = nil, trackImpressions: Bool) {
treatment: String, splitName: String, attributes: [String: Any]? = nil,
trackImpressions: Bool) {

let keyImpression = KeyImpression(featureName: splitName,
keyName: key.matchingKey,
Expand All @@ -273,7 +274,8 @@ extension DefaultTreatmentManager {
label: (splitConfig.isLabelsEnabled ? label : nil),
time: Date().unixTimestampInMiliseconds(),
changeNumber: changeNumber)
impressionLogger.pushImpression(impression: keyImpression)
impressionLogger.pushImpression(
impression: DecoratedImpression(impression: keyImpression, trackImpressions: trackImpressions))

if let externalImpressionHandler = splitConfig.impressionListener {
let impression = keyImpression.toImpression()
Expand Down
2 changes: 1 addition & 1 deletion Split/Engine/Evaluator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DefaultEvaluator: Evaluator {
private let mySegmentsStorage: MySegmentsStorage
private let myLargeSegmentsStorage: MySegmentsStorage?

init(splitsStorage: SplitsStorage,
init(splitsStorage: SplitsStorage,
mySegmentsStorage: MySegmentsStorage,
myLargeSegmentsStorage: MySegmentsStorage?) {
self.splitsStorage = splitsStorage
Expand Down
16 changes: 16 additions & 0 deletions Split/Impressions/DecoratedImpression.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// DecoratedImpression.swift
// Split
//
// Copyright © 2024 Split. All rights reserved.
//

struct DecoratedImpression {
let impression: KeyImpression
let trackImpressions: Bool

init(impression: KeyImpression, trackImpressions: Bool) {
self.impression = impression
self.trackImpressions = trackImpressions
}
}
19 changes: 11 additions & 8 deletions Split/Network/Sync/ImpressionsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protocol ImpressionsTracker: AnyObject {
func resume()
func stop(_ service: RecordingService)
func flush()
func push(_ impression: KeyImpression)
func push(_ decoratedImpression: DecoratedImpression)
func destroy()
func enableTracking(_ enable: Bool)
func enablePersistence(_ enable: Bool)
Expand Down Expand Up @@ -96,19 +96,21 @@ class DefaultImpressionsTracker: ImpressionsTracker {
}
}

func push(_ impression: KeyImpression) {
func push(_ decoratedImpression: DecoratedImpression) {

if !isTrackingEnabled {
Logger.v("Impression not tracked because tracking is disabled")
return
}

let impression = decoratedImpression.impression

// This should not happen
guard let featureName = impression.featureName else {
return
}

if isNoneImpressionsMode() {
if isNoneImpressionsMode() || !decoratedImpression.trackImpressions {
uniqueKeyTracker?.track(userKey: impression.keyName, featureName: featureName)
impressionsCounter?.inc(featureName: featureName, timeframe: impression.time, amount: 1)
if uniqueKeyFlushChecker?
Expand Down Expand Up @@ -185,8 +187,7 @@ class DefaultImpressionsTracker: ImpressionsTracker {
if !isPersistenceEnabled {
return
}
if isOptimizedImpressionsMode() || isNoneImpressionsMode(),
let counts = impressionsCounter?.popAll() {
if let counts = impressionsCounter?.popAll() {
storageContainer.impressionsCountStorage.pushMany(counts: counts)
}
}
Expand All @@ -196,9 +197,8 @@ class DefaultImpressionsTracker: ImpressionsTracker {
if !isPersistenceEnabled {
return
}
if isNoneImpressionsMode() {
uniqueKeyTracker?.saveAndClear()
}

uniqueKeyTracker?.saveAndClear()
}

private func saveHashedImpressions() {
Expand All @@ -210,9 +210,12 @@ class DefaultImpressionsTracker: ImpressionsTracker {
switch splitConfig.$impressionsMode {
case .optimized:
createImpressionsRecorder()
createUniqueKeysRecorder()
createImpressionsCountRecorder()
case .debug:
createImpressionsRecorder()
createUniqueKeysRecorder()
createImpressionsCountRecorder()
case .none:
createUniqueKeysRecorder()
createImpressionsCountRecorder()
Expand Down
2 changes: 1 addition & 1 deletion Split/Network/Sync/SyncCommons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ struct SplitStorageContainer {
}

protocol ImpressionLogger {
func pushImpression(impression: KeyImpression)
func pushImpression(impression: DecoratedImpression)
}
2 changes: 1 addition & 1 deletion Split/Network/Sync/Synchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class DefaultSynchronizer: Synchronizer {
}
}

func pushImpression(impression: KeyImpression) {
func pushImpression(impression: DecoratedImpression) {
flushQueue.async { [weak self] in
guard let self = self else { return }

Expand Down
5 changes: 2 additions & 3 deletions SplitTests/Fake/ImpressionsLoggerStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import Foundation
class ImpressionsLoggerStub: ImpressionLogger {
var impressions = [String: KeyImpression]()
var impressionsPushedCount = 0
func pushImpression(impression: KeyImpression) {
func pushImpression(impression: DecoratedImpression) {
let impression = impression.impression
guard let splitName = impression.featureName else {
return
}
impressions[splitName] = impression
impressionsPushedCount+=1
}


}
2 changes: 1 addition & 1 deletion SplitTests/Fake/Streaming/ImpressionsTrackerStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ImpressionsTrackerStub: ImpressionsTracker {
}

var pushCalled = false
func push(_ impression: KeyImpression) {
func push(_ impression: DecoratedImpression) {
pushCalled = true
}

Expand Down
2 changes: 1 addition & 1 deletion SplitTests/Fake/Streaming/SynchronizerSpy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SynchronizerSpy: Synchronizer {
splitSynchronizer.pushEvent(event: event)
}

func pushImpression(impression: KeyImpression) {
func pushImpression(impression: DecoratedImpression) {
pushImpressionCalled = true
splitSynchronizer.pushImpression(impression: impression)
}
Expand Down
2 changes: 1 addition & 1 deletion SplitTests/Fake/Streaming/SynchronizerStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class SynchronizerStub: Synchronizer {
pushEventCalled = true
}

func pushImpression(impression: KeyImpression) {
func pushImpression(impression: DecoratedImpression) {
pushImpressionCalled = true
}

Expand Down
Loading