Skip to content

Commit

Permalink
indentation fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirfolio3 committed Oct 13, 2020
1 parent 4753176 commit 1dc28ac
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Data Model/DispatchEvents/BatchEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct DispatchEvent: Codable, Equatable {
var tags: [String: AttributeValue]?
var revenue: AttributeValue?
var value: AttributeValue?

enum CodingKeys: String, CodingKey {
case entityID = "entity_id"
case key
Expand All @@ -139,6 +140,7 @@ struct DispatchEvent: Codable, Equatable {
revenue: AttributeValue? = nil) {

// TODO: add validation and throw here for invalid value (int, double) and revenue (int) types

self.timestamp = timestamp
self.key = key
self.entityID = entityID
Expand Down
9 changes: 9 additions & 0 deletions Sources/Data Model/ProjectConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class ProjectConfig {
}
return map
}()

lazy var experimentIdMap: [String: Experiment] = {
var map = [String: Experiment]()
allExperiments.forEach { map[$0.id] = $0 }
return map
}()

lazy var experimentFeatureMap: [String: [String]] = {
var experimentFeatureMap = [String: [String]]()
project.featureFlags.forEach { (ff) in
Expand All @@ -65,21 +67,25 @@ class ProjectConfig {
project.attributes.forEach { map[$0.key] = $0 }
return map
}()

lazy var featureFlagKeyMap: [String: FeatureFlag] = {
var map = [String: FeatureFlag]()
project.featureFlags.forEach { map[$0.key] = $0 }
return map
}()

lazy var rolloutIdMap: [String: Rollout] = {
var map = [String: Rollout]()
project.rollouts.forEach { map[$0.id] = $0 }
return map
}()

lazy var allExperiments: [Experiment] = {
return project.experiments + project.groups.map { $0.experiments }.flatMap({$0})
}()

// MARK: - Init

init(datafile: Data) throws {
do {
self.project = try JSONDecoder().decode(Project.self, from: datafile)
Expand Down Expand Up @@ -136,6 +142,7 @@ extension ProjectConfig {
}

static var observer = ProjectObserver()

}

// MARK: - Persistent Data
Expand Down Expand Up @@ -306,10 +313,12 @@ extension ProjectConfig {

// TODO: common function to trim all keys
variationKey = variationKey.trimmingCharacters(in: NSCharacterSet.whitespaces)

guard !variationKey.isEmpty else {
logger.e(.variationKeyInvalid(experimentKey, variationKey))
return false
}

guard let variation = experiment.getVariation(key: variationKey) else {
logger.e(.variationKeyInvalid(experimentKey, variationKey))
return false
Expand Down
3 changes: 3 additions & 0 deletions Sources/Implementation/DefaultDecisionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DefaultDecisionService: OPTDecisionService {
let bucketer: OPTBucketer
let userProfileService: OPTUserProfileService
lazy var logger = OPTLoggerFactory.getLogger()

init(userProfileService: OPTUserProfileService) {
self.bucketer = DefaultBucketer()
self.userProfileService = userProfileService
Expand Down Expand Up @@ -146,6 +147,7 @@ class DefaultDecisionService: OPTDecisionService {
}

return nil

}

func getVariationForFeatureExperiment(config: ProjectConfig,
Expand Down Expand Up @@ -193,6 +195,7 @@ class DefaultDecisionService: OPTDecisionService {
logger.e(.rolloutHasNoExperiments(rolloutId))
return nil
}

// Evaluate all rollout rules except for last one
for index in 0..<rolloutRules.count.advanced(by: -1) {
let loggingKey = index + 1
Expand Down
1 change: 1 addition & 0 deletions Sources/Implementation/Events/BatchEventBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class BatchEventBuilder {

return try? JSONEncoder().encode(batchEvent)
}

// MARK: - Event Tags

static func filterEventTags(_ eventTags: [String: Any]?) -> ([String: AttributeValue], AttributeValue?, AttributeValue?) {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ open class OptimizelyClient: NSObject {
atomicConfig.property = newValue
}
}

public var version: String {
return Utils.sdkVersion
}
Expand All @@ -47,6 +48,7 @@ open class OptimizelyClient: NSObject {
return false
}
}

// MARK: - Customizable Services

lazy var logger = OPTLoggerFactory.getLogger()
Expand All @@ -68,6 +70,7 @@ open class OptimizelyClient: NSObject {
public var notificationCenter: OPTNotificationCenter? {
return HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey)
}

// MARK: - Public interfaces

/// OptimizelyClient init
Expand Down Expand Up @@ -181,6 +184,7 @@ open class OptimizelyClient: NSObject {
func configSDK(datafile: Data) throws {
do {
self.config = try ProjectConfig(datafile: datafile)

datafileHandler?.startUpdates(sdkKey: self.sdkKey) { data in
// new datafile came in
self.updateConfigFromBackgroundFetch(data: data)
Expand Down Expand Up @@ -219,8 +223,10 @@ open class OptimizelyClient: NSObject {
for component in HandlerRegistryService.shared.lookupComponents(sdkKey: self.sdkKey) ?? [] {
HandlerRegistryService.shared.reInitializeComponent(service: component, sdkKey: self.sdkKey)
}

self.sendDatafileChangeNotification(data: data)
}

/**
* Use the activate method to start an experiment.
*
Expand Down Expand Up @@ -468,6 +474,7 @@ open class OptimizelyClient: NSObject {
variableKey: String,
userId: String,
attributes: OptimizelyAttributes? = nil) throws -> String {

return try getFeatureVariable(featureKey: featureKey,
variableKey: variableKey,
userId: userId,
Expand Down Expand Up @@ -644,6 +651,7 @@ open class OptimizelyClient: NSObject {
break
}
}

if let value = valueParsed {
variableMap[v.key] = value
} else {
Expand Down Expand Up @@ -710,6 +718,7 @@ open class OptimizelyClient: NSObject {

sendConversionEvent(eventKey: eventKey, userId: userId, attributes: attributes, eventTags: eventTags)
}

/// Read a copy of project configuration data model.
///
/// This call returns a snapshot of the current project configuration.
Expand All @@ -722,6 +731,7 @@ open class OptimizelyClient: NSObject {
/// - Throws: `OptimizelyError` if SDK is not ready
public func getOptimizelyConfig() throws -> OptimizelyConfig {
guard let config = self.config else { throw OptimizelyError.sdkNotReady }

return OptimizelyConfigImp(projectConfig: config)
}
}
Expand All @@ -740,6 +750,7 @@ extension OptimizelyClient {
// non-blocking (event data serialization takes time)
eventLock.async {
guard let config = self.config else { return }

guard let body = BatchEventBuilder.createImpressionEvent(config: config,
experiment: experiment,
variation: variation,
Expand All @@ -766,6 +777,7 @@ extension OptimizelyClient {
async: false)
}
}

}

func sendConversionEvent(eventKey: String,
Expand All @@ -776,6 +788,7 @@ extension OptimizelyClient {
// non-blocking (event data serialization takes time)
eventLock.async {
guard let config = self.config else { return }

guard let body = BatchEventBuilder.createConversionEvent(config: config,
eventKey: eventKey,
userId: userId,
Expand All @@ -790,6 +803,7 @@ extension OptimizelyClient {

// send notification in sync mode (functionally same as async here since it's already in background thread),
// but this will make testing simpler (timing control)

self.sendTrackNotification(eventKey: eventKey,
userId: userId,
attributes: attributes,
Expand Down Expand Up @@ -949,6 +963,7 @@ extension OptimizelyClient {
notify()
}
}

}

// MARK: - For test support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import XCTest

class BatchEventBuilderTests_Events: XCTestCase {

let experimentKey = "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2"
let userId = "test_user_1"

var optimizely: OptimizelyClient!
var eventDispatcher: FakeEventDispatcher!
var project: Project!
Expand Down Expand Up @@ -49,6 +51,7 @@ class BatchEventBuilderTests_Events: XCTestCase {
attributes: attributes)

let event = getFirstEventJSON()!

XCTAssertEqual((event["revision"] as! String), project.revision)
XCTAssertEqual((event["account_id"] as! String), project.accountId)
XCTAssertEqual(event["client_version"] as! String, Utils.sdkVersion)
Expand All @@ -60,11 +63,13 @@ class BatchEventBuilderTests_Events: XCTestCase {
let visitor = (event["visitors"] as! Array<Dictionary<String, Any>>)[0]

XCTAssertEqual(visitor["visitor_id"] as! String, userId)

let snapshot = (visitor["snapshots"] as! Array<Dictionary<String, Any>>)[0]

// attributes contents are tested separately in "BatchEventBuilder_Attributes.swift"
let eventAttributes = visitor["attributes"] as! Array<Dictionary<String, Any>>
XCTAssertEqual(eventAttributes.count, attributes.count)

let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0]

XCTAssertEqual(decision["variation_id"] as! String, expVariationId)
Expand Down Expand Up @@ -174,6 +179,7 @@ class BatchEventBuilderTests_Events: XCTestCase {
func testCreateConversionEvent() {
let eventKey = "event_single_targeted_exp"
let eventId = "10404198135"

let attributes: [String: Any] = ["s_foo": "bar"]
let eventTags: [String: Any] = ["browser": "chrome"]

Expand Down Expand Up @@ -202,6 +208,7 @@ class BatchEventBuilderTests_Events: XCTestCase {
let eventAttributes = visitor["attributes"] as! Array<Dictionary<String, Any>>
XCTAssertEqual(eventAttributes[0]["key"] as! String, "s_foo")
XCTAssertEqual(eventAttributes[0]["value"] as! String, "bar")

let decisions = snapshot["decisions"]

XCTAssertNil(decisions)
Expand Down Expand Up @@ -239,6 +246,7 @@ class BatchEventBuilderTests_Events: XCTestCase {
let eventForDispatch = eventDispatcher.events.first
XCTAssertNil(eventForDispatch)
}

}

// MARK: - Utils
Expand Down
7 changes: 7 additions & 0 deletions Tests/OptimizelyTests-DataModel/ProjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ extension ProjectTests {
let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingAudiences() {
var data: [String: Any] = ProjectTests.sampleData
data["audiences"] = nil

let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingGroups() {
var data: [String: Any] = ProjectTests.sampleData
data["groups"] = nil
Expand All @@ -107,20 +109,23 @@ extension ProjectTests {
let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingAccountId() {
var data: [String: Any] = ProjectTests.sampleData
data["accountId"] = nil

let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingEvents() {
var data: [String: Any] = ProjectTests.sampleData
data["events"] = nil

let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingRevision() {
var data: [String: Any] = ProjectTests.sampleData
data["revision"] = nil
Expand All @@ -136,6 +141,7 @@ extension ProjectTests {
let model: Project? = try? OTUtils.model(from: data)
XCTAssertNil(model)
}

func testDecodeFailWithMissingRollouts() {
var data: [String: Any] = ProjectTests.sampleData
data["rollouts"] = nil
Expand All @@ -153,6 +159,7 @@ extension ProjectTests {
}

// MARK: - Optional Fields

func testDecodeSuccessWithMissingTypedAudiences() {
var data: [String: Any] = ProjectTests.sampleData
data["typedAudiences"] = nil
Expand Down

0 comments on commit 1dc28ac

Please sign in to comment.