-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add stats for using vocamac #148
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // UserStats.swift | ||
| // VocaMac | ||
| // | ||
| // Data model for tracking user usage statistics. | ||
|
|
||
| import Foundation | ||
|
|
||
| struct UserStats: Codable { | ||
| /// Total number of words transcribed across all sessions | ||
| var totalWords: Int = 0 | ||
|
|
||
| /// Total number of successful transcriptions performed | ||
| var totalTranscriptions: Int = 0 | ||
|
|
||
| /// Total duration of audio recorded in seconds | ||
| var totalAudioDurationSeconds: Double = 0 | ||
|
|
||
| /// Date of the most recent transcription | ||
| var lastUsageDate: Date? | ||
|
|
||
| /// Current consecutive days of usage | ||
| var currentStreak: Int = 0 | ||
|
|
||
| /// Highest consecutive days of usage recorded | ||
| var bestStreak: Int = 0 | ||
|
|
||
| /// Daily word counts to calculate trends and streaks | ||
| /// Key is date string in "yyyy-MM-dd" format | ||
| var dailyWordCounts: [String: Int] = [:] | ||
|
|
||
| /// Daily duration to calculate WPM history | ||
| var dailyDurationSeconds: [String: Double] = [:] | ||
|
|
||
| /// Calculated average Words Per Minute (WPM) | ||
| var averageWPM: Double { | ||
| guard totalAudioDurationSeconds > 0 else { return 0 } | ||
| let minutes = totalAudioDurationSeconds / 60.0 | ||
| return Double(totalWords) / minutes | ||
| } | ||
| } | ||
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,126 @@ | ||
| // StatsManager.swift | ||
| // VocaMac | ||
| // | ||
| // Manages the persistence and updating of user statistics. | ||
|
|
||
| import Foundation | ||
| import Combine | ||
|
|
||
| @MainActor | ||
| class StatsManager: StatsManaging, ObservableObject { | ||
| @Published private(set) var stats: UserStats = UserStats() | ||
|
|
||
| var objectWillChangePublisher: AnyPublisher<Void, Never> { | ||
| objectWillChange.eraseToAnyPublisher() | ||
| } | ||
|
|
||
| private let fileManager = FileManager.default | ||
| private let statsFileName = "stats.json" | ||
|
|
||
| init() { | ||
| loadStats() | ||
| } | ||
|
|
||
| private var statsFileURL: URL { | ||
| let appSupport = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! | ||
| let vMacDir = appSupport.appendingPathComponent("VocaMac", isDirectory: true) | ||
|
|
||
| // Ensure directory exists | ||
| if !fileManager.fileExists(atPath: vMacDir.path) { | ||
| try? fileManager.createDirectory(at: vMacDir, withIntermediateDirectories: true) | ||
| } | ||
|
|
||
| return vMacDir.appendingPathComponent(statsFileName) | ||
| } | ||
|
|
||
| private func loadStats() { | ||
| do { | ||
| if fileManager.fileExists(atPath: statsFileURL.path) { | ||
| let data = try Data(contentsOf: statsFileURL) | ||
| stats = try JSONDecoder().decode(UserStats.self, from: data) | ||
| VocaLogger.debug(.general, "User stats loaded from disk") | ||
| } else { | ||
| VocaLogger.info(.general, "No stats file found, starting fresh") | ||
| } | ||
| } catch { | ||
| VocaLogger.error(.general, "Failed to load stats: \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| private func saveStats() { | ||
| do { | ||
| let data = try JSONEncoder().encode(stats) | ||
| try data.write(to: statsFileURL, options: .atomic) | ||
| VocaLogger.debug(.general, "User stats saved to disk") | ||
| } catch { | ||
| VocaLogger.error(.general, "Failed to save stats: \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| func recordTranscription(_ transcription: VocaTranscription) { | ||
| let text = transcription.text.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard !text.isEmpty else { return } | ||
|
|
||
| // Estimate word count | ||
| let words = text.components(separatedBy: .whitespacesAndNewlines) | ||
| .filter { !$0.isEmpty } | ||
| .count | ||
|
|
||
| let dateKey = formatDate(transcription.timestamp) | ||
|
Mr-Sunglasses marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Update basic counts | ||
| stats.totalWords += words | ||
| stats.totalTranscriptions += 1 | ||
| stats.totalAudioDurationSeconds += transcription.audioLengthSeconds | ||
|
|
||
| // Update daily stats | ||
| stats.dailyWordCounts[dateKey, default: 0] += words | ||
| stats.dailyDurationSeconds[dateKey, default: 0] += transcription.audioLengthSeconds | ||
|
|
||
| // Update streaks | ||
| updateStreaks(currentDate: transcription.timestamp) | ||
|
|
||
| stats.lastUsageDate = transcription.timestamp | ||
|
|
||
| saveStats() | ||
| } | ||
|
|
||
| func resetStats() { | ||
| stats = UserStats() | ||
| saveStats() | ||
| } | ||
|
|
||
| private func updateStreaks(currentDate: Date) { | ||
| guard let lastDate = stats.lastUsageDate else { | ||
| // First time usage | ||
| stats.currentStreak = 1 | ||
| stats.bestStreak = 1 | ||
| return | ||
| } | ||
|
|
||
| let calendar = Calendar.current | ||
|
|
||
| // Check if last usage was yesterday | ||
| if calendar.isDateInYesterday(lastDate) { | ||
|
Mr-Sunglasses marked this conversation as resolved.
|
||
| // Continuation of streak | ||
| if !calendar.isDate(lastDate, inSameDayAs: currentDate) { | ||
| stats.currentStreak += 1 | ||
| } | ||
| } else if calendar.isDate(lastDate, inSameDayAs: currentDate) { | ||
| // Already used today, streak remains same | ||
| } else { | ||
| // Streak broken | ||
| stats.currentStreak = 1 | ||
| } | ||
|
|
||
| if stats.currentStreak > stats.bestStreak { | ||
| stats.bestStreak = stats.currentStreak | ||
| } | ||
| } | ||
|
|
||
| private func formatDate(_ date: Date) -> String { | ||
|
Mr-Sunglasses marked this conversation as resolved.
Outdated
|
||
| let formatter = DateFormatter() | ||
| formatter.dateFormat = "yyyy-MM-dd" | ||
| return formatter.string(from: date) | ||
| } | ||
| } | ||
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
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.