-
Notifications
You must be signed in to change notification settings - Fork 511
feat: add feature usage tracker for nightly surveys #8175
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
5 commits
Select commit
Hold shift + click to select a range
03ffc6b
feat: add useFeatureUsageTracker composable for survey eligibility
christian-byrne 184d91c
fix: use fake timers in lastUsed test for deterministic CI
christian-byrne c90217d
fix: use fake timers in persistence test for deterministic CI
christian-byrne 7d2195b
fix: use fake timers in firstUsed test for deterministic timestamps
christian-byrne 0ad85d1
refactor: rename storage key to Comfy.FeatureUsage for consistency
christian-byrne 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const STORAGE_KEY = 'Comfy.FeatureUsage' | ||
|
|
||
| describe('useFeatureUsageTracker', () => { | ||
| beforeEach(() => { | ||
| localStorage.clear() | ||
| vi.resetModules() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| localStorage.clear() | ||
| }) | ||
|
|
||
| it('initializes with zero count for new feature', async () => { | ||
| const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker') | ||
| const { useCount } = useFeatureUsageTracker('test-feature') | ||
|
|
||
| expect(useCount.value).toBe(0) | ||
| }) | ||
|
|
||
| it('increments count on trackUsage', async () => { | ||
| const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker') | ||
| const { useCount, trackUsage } = useFeatureUsageTracker('test-feature') | ||
|
|
||
| expect(useCount.value).toBe(0) | ||
|
|
||
| trackUsage() | ||
| expect(useCount.value).toBe(1) | ||
|
|
||
| trackUsage() | ||
| expect(useCount.value).toBe(2) | ||
| }) | ||
|
|
||
| it('sets firstUsed only on first use', async () => { | ||
| vi.useFakeTimers() | ||
| const firstTs = 1000000 | ||
| vi.setSystemTime(firstTs) | ||
| try { | ||
| const { useFeatureUsageTracker } = | ||
| await import('./useFeatureUsageTracker') | ||
| const { usage, trackUsage } = useFeatureUsageTracker('test-feature') | ||
|
|
||
| trackUsage() | ||
| expect(usage.value?.firstUsed).toBe(firstTs) | ||
|
|
||
| vi.setSystemTime(firstTs + 5000) | ||
| trackUsage() | ||
| expect(usage.value?.firstUsed).toBe(firstTs) | ||
| } finally { | ||
| vi.useRealTimers() | ||
| } | ||
| }) | ||
|
|
||
| it('updates lastUsed on each use', async () => { | ||
| vi.useFakeTimers() | ||
| try { | ||
| const { useFeatureUsageTracker } = | ||
| await import('./useFeatureUsageTracker') | ||
| const { usage, trackUsage } = useFeatureUsageTracker('test-feature') | ||
|
|
||
| trackUsage() | ||
| const firstLastUsed = usage.value?.lastUsed ?? 0 | ||
|
|
||
| vi.advanceTimersByTime(10) | ||
| trackUsage() | ||
|
|
||
| expect(usage.value?.lastUsed).toBeGreaterThan(firstLastUsed) | ||
| } finally { | ||
| vi.useRealTimers() | ||
| } | ||
| }) | ||
|
|
||
| it('reset clears feature data', async () => { | ||
| const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker') | ||
| const { useCount, trackUsage, reset } = | ||
| useFeatureUsageTracker('test-feature') | ||
|
|
||
| trackUsage() | ||
| trackUsage() | ||
| expect(useCount.value).toBe(2) | ||
|
|
||
| reset() | ||
| expect(useCount.value).toBe(0) | ||
| }) | ||
|
|
||
| it('tracks multiple features independently', async () => { | ||
| const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker') | ||
| const featureA = useFeatureUsageTracker('feature-a') | ||
| const featureB = useFeatureUsageTracker('feature-b') | ||
|
|
||
| featureA.trackUsage() | ||
| featureA.trackUsage() | ||
| featureB.trackUsage() | ||
|
|
||
| expect(featureA.useCount.value).toBe(2) | ||
| expect(featureB.useCount.value).toBe(1) | ||
| }) | ||
|
|
||
| it('persists to localStorage', async () => { | ||
| vi.useFakeTimers() | ||
| try { | ||
| const { useFeatureUsageTracker } = | ||
| await import('./useFeatureUsageTracker') | ||
| const { trackUsage } = useFeatureUsageTracker('persisted-feature') | ||
|
|
||
| trackUsage() | ||
| await vi.runAllTimersAsync() | ||
|
|
||
| const stored = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') | ||
| expect(stored['persisted-feature']?.useCount).toBe(1) | ||
| } finally { | ||
| vi.useRealTimers() | ||
| } | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('loads existing data from localStorage', async () => { | ||
| localStorage.setItem( | ||
| STORAGE_KEY, | ||
| JSON.stringify({ | ||
| 'existing-feature': { useCount: 5, firstUsed: 1000, lastUsed: 2000 } | ||
| }) | ||
| ) | ||
|
|
||
| vi.resetModules() | ||
| const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker') | ||
| const { useCount } = useFeatureUsageTracker('existing-feature') | ||
|
|
||
| expect(useCount.value).toBe(5) | ||
| }) | ||
| }) | ||
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,46 @@ | ||
| import { useStorage } from '@vueuse/core' | ||
| import { computed } from 'vue' | ||
|
|
||
| interface FeatureUsage { | ||
| useCount: number | ||
| firstUsed: number | ||
| lastUsed: number | ||
| } | ||
|
|
||
| type FeatureUsageRecord = Record<string, FeatureUsage> | ||
|
|
||
| const STORAGE_KEY = 'Comfy.FeatureUsage' | ||
|
|
||
| /** | ||
| * Tracks feature usage for survey eligibility. | ||
| * Persists to localStorage. | ||
| */ | ||
| export function useFeatureUsageTracker(featureId: string) { | ||
| const usageData = useStorage<FeatureUsageRecord>(STORAGE_KEY, {}) | ||
|
|
||
| const usage = computed(() => usageData.value[featureId]) | ||
|
|
||
| const useCount = computed(() => usage.value?.useCount ?? 0) | ||
|
|
||
| function trackUsage() { | ||
| const now = Date.now() | ||
| const existing = usageData.value[featureId] | ||
|
|
||
| usageData.value[featureId] = { | ||
| useCount: (existing?.useCount ?? 0) + 1, | ||
| firstUsed: existing?.firstUsed ?? now, | ||
| lastUsed: now | ||
| } | ||
| } | ||
|
|
||
| function reset() { | ||
| delete usageData.value[featureId] | ||
| } | ||
|
|
||
| return { | ||
| usage, | ||
| useCount, | ||
| trackUsage, | ||
| reset | ||
| } | ||
| } |
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.