Repository navigation
Count listens per page, voice and day #38
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
Open
arnelirobles
wants to merge
10
commits into
main
Choose a base branch
from
feat/listen-counts-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e13d77b
count listens in memory and write them once a minute
arnelirobles 780e63a
document listen counts and the new table
arnelirobles 9320072
list the flush service in the composer test instead of resolving it
arnelirobles d14493c
run the test site without generated models and note the models mode c…
arnelirobles 0cd95a7
say the next start works after the models mode check
arnelirobles 33aab94
count no listen for a cancelled request, fail a stalled test boot in …
arnelirobles a4be98c
wait for Umbraco to finish booting before the first request in the te…
arnelirobles 171f1f6
send the asked voice in the query too in the stored voice test
arnelirobles e008778
keep the listen flush running on a bad options reload and fix its war…
arnelirobles 20443f2
allow the listen table's two schema consts in the public const test
arnelirobles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
| namespace BaryoDev.Umbraco.ReadAloud.Listens; | ||
|
|
||
| /// <summary>Where counts are written. A seam so the flush service can be tested without a database.</summary> | ||
| internal interface IListenStore | ||
| { | ||
| /// <summary>Adds <paramref name="count"/> to the row for the key, creating it when it is missing.</summary> | ||
| void Add(ListenKey key, int count); | ||
| } |
108 changes: 108 additions & 0 deletions
108
src/BaryoDev.Umbraco.ReadAloud/Listens/ListenCounter.cs
This file contains hidden or 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,108 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace BaryoDev.Umbraco.ReadAloud.Listens; | ||
|
|
||
| /// <summary> | ||
| /// Counts plays in memory per article, voice and UTC day until the flush service writes them. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Holds a count and a key and nothing else, so there is nothing here that could say who listened. | ||
| /// The day is part of the key, so a flush that straddles UTC midnight splits correctly. | ||
| /// </remarks> | ||
| public sealed class ListenCounter | ||
| { | ||
| /// <summary>The most keys held in memory. Past it the oldest day is dropped first.</summary> | ||
| internal const int MaxKeys = 10_000; | ||
|
|
||
| private readonly IOptionsMonitor<ReadAloudOptions> _options; | ||
| private readonly TimeProvider _time; | ||
| private readonly ILogger<ListenCounter>? _logger; | ||
| private readonly object _gate = new(); | ||
| private Dictionary<ListenKey, int> _counts = []; | ||
|
|
||
| /// <summary>Creates the counter.</summary> | ||
| public ListenCounter( | ||
| IOptionsMonitor<ReadAloudOptions> options, | ||
| TimeProvider time, | ||
| ILogger<ListenCounter>? logger = null) | ||
| { | ||
| _options = options; | ||
| _time = time; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| /// <summary>Counts one play of a node in a voice, today in UTC. Does nothing when counting is off.</summary> | ||
| public void Add(Guid nodeKey, string voice) | ||
| { | ||
| if (!_options.CurrentValue.CountListens) return; | ||
|
|
||
| var key = new ListenKey(nodeKey, voice, ListenKey.DayOf(_time.GetUtcNow())); | ||
|
|
||
| lock (_gate) | ||
| { | ||
| if (!_counts.ContainsKey(key) && _counts.Count >= MaxKeys) DropOldestDay(); | ||
|
|
||
| _counts[key] = _counts.GetValueOrDefault(key) + 1; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Takes everything counted so far and starts again from empty.</summary> | ||
| internal Dictionary<ListenKey, int> Drain() | ||
| { | ||
| lock (_gate) | ||
| { | ||
| var taken = _counts; | ||
| _counts = []; | ||
| return taken; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Puts drained counts back after a failed write, on top of whatever arrived since.</summary> | ||
| internal void MergeBack(IReadOnlyDictionary<ListenKey, int> counts) | ||
| { | ||
| lock (_gate) | ||
| { | ||
| foreach (var (key, n) in counts) _counts[key] = _counts.GetValueOrDefault(key) + n; | ||
|
|
||
| while (_counts.Count > MaxKeys) | ||
| { | ||
| if (!DropOldestDay()) break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Forgets everything held. Used when counting is switched off.</summary> | ||
| internal void Clear() | ||
| { | ||
| lock (_gate) _counts = []; | ||
| } | ||
|
|
||
| /// <summary>What is held now, for tests.</summary> | ||
| internal IReadOnlyDictionary<ListenKey, int> Snapshot() | ||
| { | ||
| lock (_gate) return new Dictionary<ListenKey, int>(_counts); | ||
| } | ||
|
|
||
| /// <summary>Removes every key of the oldest day held. Call with the lock taken.</summary> | ||
| private bool DropOldestDay() | ||
| { | ||
| if (_counts.Count == 0) return false; | ||
|
|
||
| var oldest = _counts.Keys.Min(k => k.DayUtc); | ||
| var dropped = 0; | ||
| foreach (var key in _counts.Keys.Where(k => k.DayUtc == oldest).ToList()) | ||
| { | ||
| _counts.Remove(key); | ||
| dropped++; | ||
| } | ||
|
|
||
| _logger?.LogWarning( | ||
| "Read-aloud listen counts reached {Cap} keys in memory. Dropped {Dropped} keys for day {Day}.", | ||
| MaxKeys, | ||
| dropped, | ||
| oldest); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discard pending counts on the disable transition.
If
CountListenschanges from true to false and back to true between flushes, the pending counts survive and are later written. The constructors and composer do not register a change handler. Sampling the current value loses the disable transition. This contradicts the documented discard behavior. (raw.githubusercontent.com)Observe options changes and invalidate pending counts when counting is disabled. Preserve new counts accepted after re-enabling.