This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from VerizonAdPlatforms/OMSDK-2163/ad-request-…
…detector Ad engine request / ad request detector
- Loading branch information
Showing
9 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
Submodule PlayerCore
updated
from 96fda6 to b61507
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sources/metrics/detectors/vrm/AdEngineRequestDetector.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2019, Oath Inc. | ||
// Licensed under the terms of the MIT License. See LICENSE.md file in project root for terms. | ||
|
||
import Foundation | ||
import PlayerCore | ||
|
||
extension Detectors { | ||
|
||
final class AdEngineRequestDetector { | ||
|
||
struct Result { | ||
let adInfo: Ad.Metrics.Info | ||
let transactionId: String? | ||
} | ||
|
||
private var processedCandidates = Set<ScheduledVRMItems.Candidate>() | ||
|
||
func process(state: PlayerCore.State) -> [Result] { | ||
return process(transactionId: state.vrmResponse?.transactionId, | ||
scheduledItems: state.vrmScheduledItems.items) | ||
} | ||
|
||
func process(transactionId: String?, | ||
scheduledItems: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>]) -> [Result] { | ||
guard scheduledItems.isEmpty == false else { return [] } | ||
|
||
struct NormalizedScheduledItem { | ||
let item: VRMCore.Item | ||
let candidate: ScheduledVRMItems.Candidate | ||
} | ||
|
||
return scheduledItems | ||
.filter { _, candidatesSet in | ||
candidatesSet.isSubset(of: processedCandidates) == false | ||
}.reduce(into: []) { result, pair in | ||
return pair.value.forEach { candidate in | ||
result.append(NormalizedScheduledItem(item: pair.key, candidate: candidate)) | ||
} | ||
}.compactMap { normalized in | ||
processedCandidates.insert(normalized.candidate) | ||
return Result(adInfo: .init(metaInfo: normalized.item.metaInfo), transactionId: transactionId) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
83 changes: 83 additions & 0 deletions
83
sources/metrics/detectors/vrm/AdEngineRequestDetectorTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2018, Oath Inc. | ||
// Licensed under the terms of the MIT License. See LICENSE.md file in project root for terms. | ||
|
||
import XCTest | ||
@testable import VerizonVideoPartnerSDK | ||
@testable import PlayerCore | ||
|
||
class AdEngineRequestDetectorTest: XCTestCase { | ||
|
||
let firstCandidate = ScheduledVRMItems.Candidate(source: .vast("")) | ||
let secondCandidate = ScheduledVRMItems.Candidate(source: .vast("")) | ||
|
||
var firstMetainfo: VRMCore.Item.MetaInfo! | ||
var secondMetainfo: VRMCore.Item.MetaInfo! | ||
var firstItem: VRMCore.Item! | ||
var secondItem: VRMCore.Item! | ||
var detector: Detectors.AdEngineRequestDetector! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
detector = Detectors.AdEngineRequestDetector() | ||
firstMetainfo = VRMCore.Item.MetaInfo(engineType: "engineType1", | ||
ruleId: "ruleId1", | ||
ruleCompanyId: "ruleCompanyId1", | ||
vendor: "vendor1", | ||
name: "name1", | ||
cpm: "cpm1") | ||
secondMetainfo = VRMCore.Item.MetaInfo(engineType: "engineType2", | ||
ruleId: "ruleId2", | ||
ruleCompanyId: "ruleCompanyId2", | ||
vendor: "vendor2", | ||
name: "name2", | ||
cpm: "cpm2") | ||
firstItem = VRMCore.Item(source: .vast(""), metaInfo: firstMetainfo) | ||
secondItem = VRMCore.Item(source: .vast(""), metaInfo: secondMetainfo) | ||
} | ||
|
||
func testEmptyDetect() { | ||
let result = detector.process(transactionId: "", scheduledItems: [:]) | ||
XCTAssertTrue(result.isEmpty) | ||
} | ||
|
||
func testCorrectDetection() { | ||
let queue: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>] = [firstItem: Set([firstCandidate])] | ||
var result = detector.process(transactionId: "", scheduledItems: queue) | ||
|
||
XCTAssertEqual(result.count, 1) | ||
XCTAssertEqual(result[0].adInfo.engineType, firstMetainfo.engineType) | ||
XCTAssertEqual(result[0].adInfo.ruleId, firstMetainfo.ruleId) | ||
XCTAssertEqual(result[0].adInfo.ruleCompanyId, firstMetainfo.ruleCompanyId) | ||
XCTAssertEqual(result[0].adInfo.vendor, firstMetainfo.vendor) | ||
XCTAssertEqual(result[0].adInfo.name, firstMetainfo.name) | ||
XCTAssertEqual(result[0].adInfo.cpm, firstMetainfo.cpm) | ||
} | ||
|
||
func testDoubleDetectSameCandidate() { | ||
let queue: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>] = [firstItem: Set([firstCandidate])] | ||
var result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertEqual(result.count, 1) | ||
|
||
result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertTrue(result.isEmpty) | ||
} | ||
|
||
func testDetectingTwoCandidates() { | ||
let queue: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>] = [firstItem: Set([firstCandidate, secondCandidate])] | ||
var result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertEqual(result.count, 2) | ||
|
||
result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertTrue(result.isEmpty) | ||
} | ||
|
||
func testDetectingTwoItems() { | ||
let queue: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>] = [firstItem: Set([firstCandidate]), | ||
secondItem: Set([secondCandidate])] | ||
var result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertEqual(result.count, 2) | ||
|
||
result = detector.process(transactionId: "", scheduledItems: queue) | ||
XCTAssertTrue(result.isEmpty) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2019, Oath Inc. | ||
// Licensed under the terms of the MIT License. See LICENSE.md file in project root for terms. | ||
|
||
import Foundation | ||
import PlayerCore | ||
|
||
extension Detectors { | ||
final class VRMRequestDetector { | ||
struct Result { | ||
let transactionId: String? | ||
} | ||
|
||
private var trackedRequests = Set<UUID>() | ||
|
||
func process(with state: PlayerCore.State) -> Result? { | ||
return process(with: state.vrmRequestStatus.request?.id, | ||
transactionId: state.vrmResponse?.transactionId) | ||
} | ||
|
||
func process(with requestId: UUID?, transactionId: String?) -> Result? { | ||
guard let requestId = requestId, | ||
trackedRequests.contains(requestId) == false else { return nil } | ||
|
||
trackedRequests.insert(requestId) | ||
return Result(transactionId: transactionId) | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
sources/metrics/detectors/vrm/VRMRequestDetectorTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2018, Oath Inc. | ||
// Licensed under the terms of the MIT License. See LICENSE.md file in project root for terms. | ||
|
||
import XCTest | ||
@testable import VerizonVideoPartnerSDK | ||
@testable import PlayerCore | ||
|
||
class VRMRequestDetectorTest: XCTestCase { | ||
|
||
func testDetectSameRequest() { | ||
let uuid = UUID() | ||
let sut = Detectors.VRMRequestDetector() | ||
|
||
var result = sut.process(with: uuid, transactionId: "id") | ||
XCTAssertEqual(result?.transactionId, "id") | ||
|
||
result = sut.process(with: uuid, transactionId: "id") | ||
XCTAssertNil(result) | ||
} | ||
|
||
func testDetectDiffRequests() { | ||
let sut = Detectors.VRMRequestDetector() | ||
|
||
var result = sut.process(with: UUID(), transactionId: "id1") | ||
XCTAssertEqual(result?.transactionId, "id1") | ||
|
||
result = sut.process(with: UUID(), transactionId: "id2") | ||
XCTAssertEqual(result?.transactionId, "id2") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.