-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppLog.cs
More file actions
213 lines (174 loc) · 6 KB
/
Copy pathAppLog.cs
File metadata and controls
213 lines (174 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace PrimeDictate;
internal enum AppLogLevel
{
Info,
Error
}
internal sealed record AppLogEntry(
DateTime TimestampUtc,
AppLogLevel Level,
string Message,
Guid? ThreadId = null,
int RepeatCount = 1)
{
public string DisplayMessage =>
this.RepeatCount > 1 ? $"{this.Message} (x{this.RepeatCount})" : this.Message;
}
internal static class AppLog
{
public static event Action<AppLogEntry>? EntryWritten;
public static void Info(string message, Guid? threadId = null) =>
Write(AppLogLevel.Info, message, threadId);
public static void Error(string message, Guid? threadId = null) =>
Write(AppLogLevel.Error, message, threadId);
public static void Write(AppLogLevel level, string message, Guid? threadId = null)
{
EntryWritten?.Invoke(new AppLogEntry(DateTime.UtcNow, level, message, threadId));
}
}
internal sealed class DictationThreadViewModel : INotifyPropertyChanged
{
private string latestTranscript = string.Empty;
public DictationThreadViewModel(Guid id)
{
this.Id = id;
this.Title = $"Session {id.ToString()[..8]}";
this.StartedAtUtc = DateTime.UtcNow;
this.Entries = new ObservableCollection<AppLogEntry>();
}
public event PropertyChangedEventHandler? PropertyChanged;
public Guid Id { get; }
public string Title { get; }
public DateTime StartedAtUtc { get; }
public DateTime? EndedAtUtc { get; private set; }
public ObservableCollection<AppLogEntry> Entries { get; }
public string LatestTranscript
{
get => this.latestTranscript;
set
{
if (value == this.latestTranscript)
{
return;
}
this.latestTranscript = value;
this.OnPropertyChanged();
}
}
public string Summary =>
this.EndedAtUtc is null
? $"{this.StartedAtUtc.ToLocalTime():t} - active"
: $"{this.StartedAtUtc.ToLocalTime():t} - {this.EndedAtUtc.Value.ToLocalTime():t}";
public void MarkCompleted()
{
this.EndedAtUtc = DateTime.UtcNow;
this.OnPropertyChanged(nameof(this.Summary));
}
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
internal sealed class DictationWorkspaceViewModel : INotifyPropertyChanged
{
private const int MaxGlobalEntries = 600;
private const int MaxThreadEntries = 300;
private const int MaxThreads = 100;
private DictationThreadViewModel? selectedThread;
private string runtimeStatusText = "Backend: Whisper | Model: Default";
public DictationWorkspaceViewModel()
{
this.Threads = new ObservableCollection<DictationThreadViewModel>();
this.GlobalEntries = new ObservableCollection<AppLogEntry>();
}
public event PropertyChangedEventHandler? PropertyChanged;
public ObservableCollection<DictationThreadViewModel> Threads { get; }
public ObservableCollection<AppLogEntry> GlobalEntries { get; }
public string RuntimeStatusText
{
get => this.runtimeStatusText;
private set
{
if (string.Equals(value, this.runtimeStatusText, StringComparison.Ordinal))
{
return;
}
this.runtimeStatusText = value;
this.OnPropertyChanged();
}
}
public DictationThreadViewModel? SelectedThread
{
get => this.selectedThread;
set
{
if (ReferenceEquals(value, this.selectedThread))
{
return;
}
this.selectedThread = value;
this.OnPropertyChanged();
}
}
public DictationThreadViewModel StartThread(Guid threadId)
{
var thread = new DictationThreadViewModel(threadId);
this.Threads.Insert(0, thread);
while (this.Threads.Count > MaxThreads)
{
this.Threads.RemoveAt(this.Threads.Count - 1);
}
this.SelectedThread = thread;
return thread;
}
public DictationThreadViewModel? GetThread(Guid threadId) =>
this.Threads.FirstOrDefault(t => t.Id == threadId);
public void AppendEntry(AppLogEntry entry)
{
InsertWithAggregation(this.GlobalEntries, entry, MaxGlobalEntries);
if (entry.ThreadId is Guid threadId && this.GetThread(threadId) is { } thread)
{
InsertWithAggregation(thread.Entries, entry, MaxThreadEntries);
}
}
public void MarkThreadCompleted(Guid threadId)
{
var thread = this.GetThread(threadId);
thread?.MarkCompleted();
}
public void SetRuntimeStatus(string backendLabel, string modelLabel)
{
this.RuntimeStatusText = $"Backend: {backendLabel} | Model: {modelLabel}";
}
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private static void InsertWithAggregation(ObservableCollection<AppLogEntry> target, AppLogEntry entry, int maxEntries)
{
if (target.Count > 0 && CanAggregate(target[0], entry))
{
var currentTop = target[0];
target[0] = currentTop with
{
TimestampUtc = entry.TimestampUtc,
RepeatCount = currentTop.RepeatCount + 1
};
}
else
{
target.Insert(0, entry);
while (target.Count > maxEntries)
{
target.RemoveAt(target.Count - 1);
}
}
}
private static bool CanAggregate(AppLogEntry existing, AppLogEntry incoming) =>
existing.Level == incoming.Level &&
existing.ThreadId == incoming.ThreadId &&
string.Equals(existing.Message, incoming.Message, StringComparison.Ordinal);
}