Use LastUpdated Time Instead of Current Time for missing Stop event #87
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Potentially Fixes #69 and #81.
As far as i can tell, the above issues happen due to misbehaving Clients and or a Bad Connection to The Server, Resulting in very long and Unrealistic Play times being Recorded:
![image](https://private-user-images.githubusercontent.com/12772447/337817841-8f481072-dc88-4067-9c6a-50838e5ecd33.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0MjQ4MTUsIm5iZiI6MTcyMjQyNDUxNSwicGF0aCI6Ii8xMjc3MjQ0Ny8zMzc4MTc4NDEtOGY0ODEwNzItZGM4OC00MDY3LTljNmEtNTA4MzhlNWVjZDMzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MzElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzMxVDExMTUxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY4MDk0ZTI2MmI0NGRkODczNTA5ZjVhZTQ1ZDUyMmRjMWQwODlhM2JhYjNiZjRmZGY4NGVkOGJjMDM0YjZmMDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.NyKke7_NXl8aR0ubNDddIgXTx9y5JxmrFW_GUDPVf98)
The PlayBack Tracker is identified using the user id, device id and item id combined. Sometimes for whatever reason it seems that the SessionManager_PlaybackStop function does not get called when playback stops and the playback tracker does not get removed and no Stop event is added to the PlayBack Trackers log.
If the Server is then not restarted and the User starts a new playback of the same item on the same device some time later, the generated PlayBack Tracker identifier is the same and we run into this condition https://github.com/jellyfin/jellyfin-plugin-playbackreporting/blob/8bb1d7a2108400631390ee71f631112b5a1899eb/Jellyfin.Plugin.PlaybackReporting/EventMonitorEntryPoint.cs#L149C18-L149C19 (Can be spotted in the logs if triggered with the Message "Existing tracker found!") This triggers a cleanup of the existing Playback Tracker before starting a new one for the new Playback. The Cleanup Calls the track.CalculateDuration function to Calculate the Final duration of the Play. This function checks if the Last Event in its log is a Stop Event (which it isn't since SessionManager_PlaybackStop was never called which would normally generate this event) and appends one with the current time as the end time if it is missing.
This PR Changes the Time Used for the Generated Stop Event to be the LastUpdated Time which gets set every time SessionManager_PlaybackProgress gets called. This should be way more accurate than the current time.