-
Notifications
You must be signed in to change notification settings - Fork 4
feat: cap page-view storage by byte budget instead of fixed count #114
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9557ea
feat: cap page-view storage by byte budget instead of fixed count
alexs-mparticle 2c6a42d
refactor: extract page-view storage into dedicated module
alexs-mparticle 06058aa
refactor: extract shared type guards into utils module
alexs-mparticle e0bc026
refactor: drop unreachable array branch in isEmpty
alexs-mparticle 2ea7ace
test: assert page-view field removal and use descriptive fixture names
alexs-mparticle 8367072
refactor: clarify byte-budget storage (maxLength, non-mutating)
alexs-mparticle f9e37d7
refactor: use existing LoggingService type in page-view storage
alexs-mparticle 9dbaa89
fix: Remove unnecessary comment
alexs-mparticle 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { LoggingService } from './Rokt-Kit'; | ||
| import { | ||
| readJSON, | ||
| removeKey, | ||
| readNamespacedField, | ||
| writeNamespacedField, | ||
| removeNamespacedField, | ||
| writeNamespacedFieldWithinBudget, | ||
| } from './storage'; | ||
|
|
||
| const LS_NAMESPACE_KEY = 'mp-rokt-kit'; | ||
| const LS_PAGE_VIEWS_FIELD = 'pageViews'; | ||
| const LEGACY_PAGE_VIEWS_KEY = 'mpPageViews'; | ||
| const PAGE_VIEWS_MAX_LENGTH = 100 * 1024; | ||
|
|
||
| export interface PageEvent { | ||
| pageUrl: string; | ||
| sourceMessageId: string; | ||
| timestamp: number; | ||
| activeTimeOnSite?: number; | ||
| activeTimeOnPage?: number; | ||
| } | ||
|
|
||
| export function migrateLegacyPageViewStorage(loggingService: LoggingService | null): void { | ||
| const legacyViews = readJSON(LEGACY_PAGE_VIEWS_KEY); | ||
| if (legacyViews === null) { | ||
| return; | ||
| } | ||
|
|
||
| const alreadyMigrated = readNamespacedField(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD) !== undefined; | ||
| const needsMigration = !alreadyMigrated && Array.isArray(legacyViews); | ||
|
|
||
| if (needsMigration) { | ||
| const migrated = writeNamespacedField(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD, legacyViews); | ||
| if (!migrated) { | ||
| loggingService?.log({ | ||
| message: 'Rokt Kit: Failed to migrate legacy page-view storage; retaining legacy key for retry', | ||
| code: 'PAGE_VIEW_CAPTURE_FAILED', | ||
| }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| removeKey(LEGACY_PAGE_VIEWS_KEY); | ||
| } | ||
|
|
||
| export function loadPageViews(loggingService: LoggingService | null): PageEvent[] { | ||
| migrateLegacyPageViewStorage(loggingService); | ||
| const stored = readNamespacedField(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD); | ||
| return Array.isArray(stored) ? (stored as PageEvent[]) : []; | ||
| } | ||
|
|
||
| export function writePageViews(pageViews: PageEvent[]): boolean { | ||
| return writeNamespacedFieldWithinBudget(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD, pageViews, PAGE_VIEWS_MAX_LENGTH); | ||
| } | ||
|
|
||
| export function clearPageViews(): void { | ||
| removeNamespacedField(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD); | ||
| } |
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,15 @@ | ||
| export function isObject(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| export function isString(value: unknown): value is string { | ||
| return typeof value === 'string'; | ||
| } | ||
|
|
||
| export function isEmpty(value: unknown): boolean { | ||
| if (value == null) return true; | ||
| if (typeof value === 'object') { | ||
| return Object.keys(value as object).length === 0; | ||
| } | ||
| return false; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.