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

Implement media extension #8

Merged
merged 13 commits into from
Mar 24, 2021
Merged

Conversation

rymorale
Copy link
Contributor

@rymorale rymorale commented Mar 4, 2021

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

- Handle tracker requests by creating a new session and then map the given tracker id to the returned session id
- TODO: Handle track requests to call corresponding tracker APIs
- Listen for configuration / privacy changes and update media states
@rymorale
Copy link
Contributor Author

rymorale commented Mar 4, 2021

Marking WIP until MediaService is available and further functionality can be added. Opening PR for now to get some early feedback.

@codecov
Copy link

codecov bot commented Mar 4, 2021

Codecov Report

Merging #8 (8a60db9) into dev (95dc9b9) will increase coverage by 0.54%.
The diff coverage is 89.32%.

@@            Coverage Diff             @@
##              dev       #8      +/-   ##
==========================================
+ Coverage   93.84%   94.38%   +0.54%     
==========================================
  Files          13       15       +2     
  Lines        1363     1459      +96     
==========================================
+ Hits         1279     1377      +98     
+ Misses         84       82       -2     

/// Parameters:
/// - event: The `Event` for which shared state is to be retrieved.
/// - dependencies: An array of names of event's dependencies.
private func updateMediaState(forEvent event: Event, dependencies: [String]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us remove the method from this class. We will let MediaService maintain MediaState updates.

/// - Parameter:
/// - event: The configuration response event
private func handleConfigurationResponseEvent(_ event: Event) {
updateMediaState(forEvent: event, dependencies: dependencies)
Copy link
Contributor

@praveek praveek Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also listen for EventHub SharedState update and pass it to MediaService ?


let trackerConfig = trackerRequestData[MediaConstants.Tracker.EVENT_PARAM] as? [String: Any] ?? [:]

mediaState.setTrackerConfig(with: trackerConfig)
Copy link
Contributor

@praveek praveek Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracker config is not stored in media state. We pass it to init method when creating a tracker.
It should be something like
trackerMapping[trackerId] = MediaTracker(config: trackerConfig, hitProcessor: mediaService)

public var name = MediaConstants.EXTENSION_NAME
public var friendlyName = MediaConstants.FRIENDLY_NAME
public static var extensionVersion = MediaConstants.EXTENSION_VERSION
public var metadata: [String: String]?
let dependencies: [String] = [MediaConstants.Configuration.SHARED_STATE_NAME, MediaConstants.Identity.SHARED_STATE_NAME, MediaConstants.Analytics.SHARED_STATE_NAME]
#if DEBUG
var sessionIdToTrackerIdMapping: [String: String] = [:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't map sessionId to trackerId.
instead
let trackerMapping: [String: MediaEventTracking]

//TOO
// TODO: revisit when media service implemented

updateMediaState(forEvent: event, dependencies: dependencies)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do it for every trackRequest.

#if DEBUG
var sessionIdToTrackerIdMapping: [String: String] = [:]
var mediaState: MediaState
var trackerCalled = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this variable.


class MediaState {
//TODO: implementation of this class.
private let LOG_TAG = "MediaState"
private(set) var privacyStatus: PrivacyStatus = .unknown
private var trackerConfig: [String: Any]?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this one.

private(set) var blob: String?
private(set) var visitorCustomerIDs: [[String: Any]]?

private(set) var dispatchQueue: DispatchQueue = DispatchQueue(label: MediaConstants.FRIENDLY_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really don't need a dispatchQueue just for updating variables.

*/
import Foundation

class MediaCollectionEventTracking: MediaEventTracking {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this class.

Copy link
Contributor

@praveek praveek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested

- add stubs for MediaCoreTracker and MediaService
- add FakeMediaCoreTracker for tests
- remove references to sessionId
@rymorale rymorale changed the title [WIP] Implement media extension Implement media extension Mar 11, 2021
let trackerConfig = eventData[MediaConstants.Tracker.EVENT_PARAM] as? [String: Any] ?? [:]

Log.debug(label: LOG_TAG, "\(#function) - Creating tracker with tracker id: \(trackerId).")
trackers[trackerId] = MediaEventTracker(hitProcessor: mediaService!, config: trackerConfig)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid forced unwrapping mediaService.

}

guard let trackerId = eventData[MediaConstants.Tracker.ID] as? String, trackerId.count > 0 else {
Log.debug(label: LOG_TAG, "\(#function) - Tracker ID is nil, unable to create tracker.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "Tracker ID is invalid, unable to create internal tracker."

}

Log.debug(label: LOG_TAG, "\(#function) - tracking media for tracker id: \(trackerId).")
_ = trackMedia(trackerId: trackerId, eventData: eventData)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simply call trackMedia right?

Copy link
Contributor Author

@rymorale rymorale Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll make trackMedia's result discardable

}

private func trackMedia(trackerId: String, eventData: [String: Any]) -> Bool {
if let tracker = trackers[trackerId] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think guard let would be cleaner


enum MediaConstants {
static let EXTENSION_NAME = "com.adobe.module.media"
static let FRIENDLY_NAME = "Media Analytics"
static let EXTENSION_VERSION = "0.0.1"
static let DATASTORE_NAME = EXTENSION_NAME
static let DEFAULT_PRIVACY_STATUS = PrivacyStatus.unknown
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have this in MediaState, since it is managed and handled there only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup good point. removed the constant and declared it within media state.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we can get rid of core import as well then.

public var name = MediaConstants.EXTENSION_NAME
public var friendlyName = MediaConstants.FRIENDLY_NAME
public static var extensionVersion = MediaConstants.EXTENSION_VERSION
public var metadata: [String: String]?
#if DEBUG
var trackers: [String: MediaEventTracker]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use MediaEventTrackable protocol instead of MediaEventTracker?

public var name = MediaConstants.EXTENSION_NAME
public var friendlyName = MediaConstants.FRIENDLY_NAME
public static var extensionVersion = MediaConstants.EXTENSION_VERSION
public var metadata: [String: String]?
#if DEBUG
var trackers: [String: MediaEventTracker]
var mediaState: MediaState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove mediaState from the extension and let MediaService maintain it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i'll update


/// Handler for media tracker creation events
if mediaState.getPrivacyStatus() == .optedOut {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should directly read privacy status from EventData instead of reading it from Media state

#if DEBUG
var trackers: [String: MediaEventTracker]
var mediaState: MediaState
var mediaService: MediaService?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make MediaService non optional?

return
}

guard let trackerId = eventData[MediaConstants.Tracker.ID] as? String, trackerId.count > 0 else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: !trackerId.empty

Log.debug(label: LOG_TAG, "\(#function) - Privacy status is opted-out. Clearing persisted media sessions.")
// clear tracked sessions and end all running sessions within the media service
trackers.removeAll()
mediaService?.abortAllSessions()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MediaService can internally call abortAllSessions in response to updateState.

@rymorale rymorale merged commit 2f1e2ed into adobe:dev Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants