-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveGoalManager.cs
More file actions
567 lines (491 loc) · 21 KB
/
AdaptiveGoalManager.cs
File metadata and controls
567 lines (491 loc) · 21 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Windows;
namespace TimeTask
{
public enum GoalAdjustmentType
{
ExtendDeadline,
IncreasePriority,
DecomposeTask,
ReduceScope,
SuggestAlternative,
RequestReview
}
public class GoalAdjustmentSuggestion
{
public string SuggestionId { get; set; }
public string GoalId { get; set; }
public string GoalDescription { get; set; }
public GoalAdjustmentType AdjustmentType { get; set; }
public string Reason { get; set; }
public string Suggestion { get; set; }
public DateTime GeneratedAt { get; set; }
public bool IsAccepted { get; set; }
public DateTime? AcceptedAt { get; set; }
public bool IsDismissed { get; set; }
public DateTime? DismissedAt { get; set; }
public double Urgency { get; set; }
}
public class GoalProgressTracker
{
public string GoalId { get; set; }
public DateTime LastCheckDate { get; set; }
public int TotalTasks { get; set; }
public int CompletedTasks { get; set; }
public int ActiveTasks { get; set; }
public int StuckTasks { get; set; }
public double CompletionRate { get; set; }
public TimeSpan TimeElapsed { get; set; }
public TimeSpan TimeRemaining { get; set; }
public double ExpectedProgress { get; set; }
public double ProgressDeviation { get; set; }
public bool IsBehindSchedule { get; set; }
public bool IsAtRisk { get; set; }
}
public class AdaptiveGoalManager
{
private readonly string _dataPath;
private readonly string _suggestionsPath;
private readonly string _progressHistoryPath;
private readonly object _lock = new object();
private List<GoalAdjustmentSuggestion> _suggestions;
private List<GoalProgressTracker> _progressHistory;
private UserProfileManager _userProfileManager;
private UserBehaviorObserver _behaviorObserver;
private const int SuggestionRetentionDays = 30;
private const int ProgressHistoryRetentionDays = 90;
public event Action<GoalAdjustmentSuggestion> AdjustmentSuggested;
public event Action<GoalProgressTracker> GoalAtRisk;
public AdaptiveGoalManager(string appDataPath, UserProfileManager userProfileManager, UserBehaviorObserver behaviorObserver)
{
_dataPath = appDataPath;
_suggestionsPath = Path.Combine(appDataPath, "adaptive", "adjustment_suggestions.json");
_progressHistoryPath = Path.Combine(appDataPath, "adaptive", "progress_history.json");
Directory.CreateDirectory(Path.GetDirectoryName(_suggestionsPath));
_suggestions = new List<GoalAdjustmentSuggestion>();
_progressHistory = new List<GoalProgressTracker>();
_userProfileManager = userProfileManager;
_behaviorObserver = behaviorObserver;
LoadData();
}
public void CheckAndAdjustGoals()
{
lock (_lock)
{
var goals = LoadActiveGoals();
if (goals == null || !goals.Any()) return;
foreach (var goal in goals)
{
var tracker = AnalyzeGoalProgress(goal);
SaveProgressTracker(tracker);
if (tracker.IsAtRisk)
{
GoalAtRisk?.Invoke(tracker);
GenerateAdjustmentSuggestions(goal, tracker);
}
}
CleanupOldData();
}
}
private List<LongTermGoal> LoadActiveGoals()
{
string goalsPath = Path.Combine(_dataPath, "long_term_goals.csv");
if (!File.Exists(goalsPath)) return null;
var goals = HelperClass.ReadLongTermGoalsCsv(goalsPath);
return goals?.Where(g => g.IsActive).ToList();
}
private GoalProgressTracker AnalyzeGoalProgress(LongTermGoal goal)
{
var tracker = new GoalProgressTracker
{
GoalId = goal.Id,
LastCheckDate = DateTime.Now
};
var relatedTasks = LoadRelatedTasks(goal.Id);
tracker.TotalTasks = relatedTasks.Count;
tracker.CompletedTasks = relatedTasks.Count(t => !t.IsActive);
tracker.ActiveTasks = relatedTasks.Count(t => t.IsActive);
tracker.StuckTasks = relatedTasks.Count(t => t.IsActive &&
t.LastModifiedDate < DateTime.Now.AddDays(-3) &&
t.InactiveWarningCount >= 1);
tracker.CompletionRate = tracker.TotalTasks > 0
? (double)tracker.CompletedTasks / tracker.TotalTasks
: 0;
if (goal.StartDate.HasValue)
{
tracker.TimeElapsed = DateTime.Now - goal.StartDate.Value;
}
if (goal.EndDate.HasValue)
{
tracker.TimeRemaining = goal.EndDate.Value - DateTime.Now;
}
if (goal.StartDate.HasValue && goal.EndDate.HasValue)
{
var totalDuration = goal.EndDate.Value - goal.StartDate.Value;
tracker.ExpectedProgress = totalDuration.TotalDays > 0
? tracker.TimeElapsed.TotalDays / totalDuration.TotalDays
: 0;
tracker.ProgressDeviation = tracker.ExpectedProgress - tracker.CompletionRate;
tracker.IsBehindSchedule = tracker.ProgressDeviation > 0.2;
}
tracker.IsAtRisk = DetermineIfAtRisk(tracker, goal);
return tracker;
}
private List<ItemGrid> LoadRelatedTasks(string goalId)
{
var relatedTasks = new List<ItemGrid>();
string[] quadrantFiles = { "1.csv", "2.csv", "3.csv", "4.csv" };
foreach (var file in quadrantFiles)
{
string filePath = Path.Combine(_dataPath, file);
if (File.Exists(filePath))
{
var tasks = HelperClass.ReadCsv(filePath);
if (tasks != null)
{
relatedTasks.AddRange(tasks.Where(t => t.LongTermGoalId == goalId));
}
}
}
return relatedTasks;
}
private bool DetermineIfAtRisk(GoalProgressTracker tracker, LongTermGoal goal)
{
if (tracker.IsBehindSchedule && tracker.ProgressDeviation > 0.3)
{
return true;
}
if (tracker.TimeRemaining.TotalDays < 7 && tracker.CompletionRate < 0.5)
{
return true;
}
if (tracker.StuckTasks > tracker.ActiveTasks * 0.5)
{
return true;
}
if (goal.LastReviewDate < DateTime.Now.AddDays(-14) && tracker.CompletionRate < 0.3)
{
return true;
}
return false;
}
private void GenerateAdjustmentSuggestions(LongTermGoal goal, GoalProgressTracker tracker)
{
var existingSuggestions = _suggestions
.Where(s => s.GoalId == goal.Id &&
!s.IsAccepted &&
!s.IsDismissed &&
s.GeneratedAt > DateTime.Now.AddDays(-7))
.ToList();
if (existingSuggestions.Any()) return;
var suggestions = new List<GoalAdjustmentSuggestion>();
if (tracker.IsBehindSchedule && tracker.ProgressDeviation > 0.3)
{
suggestions.Add(new GoalAdjustmentSuggestion
{
SuggestionId = Guid.NewGuid().ToString("N"),
GoalId = goal.Id,
GoalDescription = goal.Description,
AdjustmentType = GoalAdjustmentType.ExtendDeadline,
Reason = $"目标进度落后预期{tracker.ProgressDeviation:P1}",
Suggestion = $"考虑将目标截止日期从{goal.EndDate?.ToString("yyyy-MM-dd")}延长1-2周,以匹配当前进度",
GeneratedAt = DateTime.Now,
Urgency = 0.8
});
}
if (tracker.StuckTasks > tracker.ActiveTasks * 0.5)
{
suggestions.Add(new GoalAdjustmentSuggestion
{
SuggestionId = Guid.NewGuid().ToString("N"),
GoalId = goal.Id,
GoalDescription = goal.Description,
AdjustmentType = GoalAdjustmentType.DecomposeTask,
Reason = $"有{tracker.StuckTasks}个任务卡住超过3天",
Suggestion = "使用任务拆解功能,将卡住的任务分解为更小的可执行步骤",
GeneratedAt = DateTime.Now,
Urgency = 0.9
});
}
if (tracker.TimeRemaining.TotalDays < 7 && tracker.CompletionRate < 0.5)
{
suggestions.Add(new GoalAdjustmentSuggestion
{
SuggestionId = Guid.NewGuid().ToString("N"),
GoalId = goal.Id,
GoalDescription = goal.Description,
AdjustmentType = GoalAdjustmentType.ReduceScope,
Reason = $"距离截止日期仅剩{tracker.TimeRemaining.Days}天,但完成率仅{tracker.CompletionRate:P0}",
Suggestion = "考虑减少目标范围,优先完成最核心的部分",
GeneratedAt = DateTime.Now,
Urgency = 1.0
});
suggestions.Add(new GoalAdjustmentSuggestion
{
SuggestionId = Guid.NewGuid().ToString("N"),
GoalId = goal.Id,
GoalDescription = goal.Description,
AdjustmentType = GoalAdjustmentType.IncreasePriority,
Reason = "目标即将到期但进度不足",
Suggestion = "将相关任务提升到高优先级,集中精力完成",
GeneratedAt = DateTime.Now,
Urgency = 1.0
});
}
if (goal.LastReviewDate < DateTime.Now.AddDays(-14))
{
suggestions.Add(new GoalAdjustmentSuggestion
{
SuggestionId = Guid.NewGuid().ToString("N"),
GoalId = goal.Id,
GoalDescription = goal.Description,
AdjustmentType = GoalAdjustmentType.RequestReview,
Reason = $"目标已{Math.Floor((DateTime.Now - goal.LastReviewDate).TotalDays)}天未回顾",
Suggestion = "建议立即回顾目标进展,重新评估任务优先级和时间安排",
GeneratedAt = DateTime.Now,
Urgency = 0.7
});
}
foreach (var suggestion in suggestions)
{
_suggestions.Add(suggestion);
AdjustmentSuggested?.Invoke(suggestion);
}
SaveSuggestions();
}
public void AcceptSuggestion(string suggestionId)
{
lock (_lock)
{
var suggestion = _suggestions.FirstOrDefault(s => s.SuggestionId == suggestionId);
if (suggestion != null)
{
suggestion.IsAccepted = true;
suggestion.AcceptedAt = DateTime.Now;
SaveSuggestions();
ApplyAdjustment(suggestion);
_behaviorObserver.RecordSystemEvent("goal_adjustment_accepted",
$"Goal: {suggestion.GoalDescription}, Type: {suggestion.AdjustmentType}");
}
}
}
public void DismissSuggestion(string suggestionId)
{
lock (_lock)
{
var suggestion = _suggestions.FirstOrDefault(s => s.SuggestionId == suggestionId);
if (suggestion != null)
{
suggestion.IsDismissed = true;
suggestion.DismissedAt = DateTime.Now;
SaveSuggestions();
_behaviorObserver.RecordSystemEvent("goal_adjustment_dismissed",
$"Goal: {suggestion.GoalDescription}, Type: {suggestion.AdjustmentType}");
}
}
}
private void ApplyAdjustment(GoalAdjustmentSuggestion suggestion)
{
var goals = LoadActiveGoals();
if (goals == null) return;
var goal = goals.FirstOrDefault(g => g.Id == suggestion.GoalId);
if (goal == null) return;
switch (suggestion.AdjustmentType)
{
case GoalAdjustmentType.ExtendDeadline:
if (goal.EndDate.HasValue)
{
goal.EndDate = goal.EndDate.Value.AddDays(14);
SaveGoal(goal);
}
break;
case GoalAdjustmentType.IncreasePriority:
IncreaseGoalTaskPriority(goal.Id);
break;
case GoalAdjustmentType.DecomposeTask:
var stuckTasks = LoadRelatedTasks(goal.Id)
.Where(t => t.IsActive &&
t.LastModifiedDate < DateTime.Now.AddDays(-3))
.ToList();
foreach (var task in stuckTasks.Take(3))
{
_behaviorObserver.RecordSystemEvent("suggest_task_decomposition",
$"Task: {task.Task}");
}
break;
case GoalAdjustmentType.ReduceScope:
var lowPriorityTasks = LoadRelatedTasks(goal.Id)
.Where(t => t.IsActive &&
string.Equals(t.Importance, "Low", StringComparison.OrdinalIgnoreCase))
.ToList();
foreach (var task in lowPriorityTasks)
{
task.IsActiveInQuadrant = false;
}
SaveGoalTasks(goal.Id, lowPriorityTasks);
break;
case GoalAdjustmentType.RequestReview:
goal.LastReviewDate = DateTime.Now;
SaveGoal(goal);
break;
}
}
private void IncreaseGoalTaskPriority(string goalId)
{
var tasks = LoadRelatedTasks(goalId);
foreach (var task in tasks.Where(t => t.IsActive))
{
if (string.Equals(task.Importance, "Low", StringComparison.OrdinalIgnoreCase))
{
task.Importance = "Medium";
}
else if (string.Equals(task.Urgency, "Low", StringComparison.OrdinalIgnoreCase))
{
task.Urgency = "Medium";
}
}
SaveGoalTasks(goalId, tasks);
}
private void SaveGoal(LongTermGoal goal)
{
string goalsPath = Path.Combine(_dataPath, "long_term_goals.csv");
var goals = HelperClass.ReadLongTermGoalsCsv(goalsPath) ?? new List<LongTermGoal>();
var existingGoal = goals.FirstOrDefault(g => g.Id == goal.Id);
if (existingGoal != null)
{
goals.Remove(existingGoal);
}
goals.Add(goal);
var csvLines = new List<string>
{
"id,description,startDate,endDate,isActive,lastReviewDate"
};
foreach (var g in goals)
{
csvLines.Add($"{g.Id},{g.Description},{g.StartDate?.ToString("o") ?? ""},{g.EndDate?.ToString("o") ?? ""},{g.IsActive},{g.LastReviewDate:o}");
}
File.WriteAllLines(goalsPath, csvLines);
}
private void SaveGoalTasks(string goalId, List<ItemGrid> tasks)
{
string[] quadrantFiles = { "1.csv", "2.csv", "3.csv", "4.csv" };
foreach (var file in quadrantFiles)
{
string filePath = Path.Combine(_dataPath, file);
if (File.Exists(filePath))
{
var allTasks = HelperClass.ReadCsv(filePath);
if (allTasks != null)
{
foreach (var task in allTasks.Where(t => t.LongTermGoalId == goalId))
{
var updatedTask = tasks.FirstOrDefault(t =>
t.Task == task.Task &&
t.CreatedDate == task.CreatedDate);
if (updatedTask != null)
{
task.Importance = updatedTask.Importance;
task.Urgency = updatedTask.Urgency;
task.IsActiveInQuadrant = updatedTask.IsActiveInQuadrant;
task.LastModifiedDate = DateTime.Now;
}
}
HelperClass.WriteCsv(allTasks, filePath);
}
}
}
}
public List<GoalAdjustmentSuggestion> GetActiveSuggestions()
{
lock (_lock)
{
return _suggestions
.Where(s => !s.IsAccepted && !s.IsDismissed)
.OrderByDescending(s => s.Urgency)
.ThenByDescending(s => s.GeneratedAt)
.ToList();
}
}
public List<GoalProgressTracker> GetProgressHistory(string goalId = null)
{
lock (_lock)
{
var query = _progressHistory.AsEnumerable();
if (!string.IsNullOrWhiteSpace(goalId))
{
query = query.Where(p => p.GoalId == goalId);
}
return query.OrderByDescending(p => p.LastCheckDate).ToList();
}
}
private void SaveProgressTracker(GoalProgressTracker tracker)
{
_progressHistory.Add(tracker);
SaveProgressHistory();
}
private void CleanupOldData()
{
var suggestionCutoff = DateTime.Now.AddDays(-SuggestionRetentionDays);
_suggestions = _suggestions.Where(s => s.GeneratedAt >= suggestionCutoff).ToList();
var progressCutoff = DateTime.Now.AddDays(-ProgressHistoryRetentionDays);
_progressHistory = _progressHistory.Where(p => p.LastCheckDate >= progressCutoff).ToList();
SaveSuggestions();
SaveProgressHistory();
}
private void LoadData()
{
try
{
if (File.Exists(_suggestionsPath))
{
string json = File.ReadAllText(_suggestionsPath);
var serializer = new JavaScriptSerializer();
_suggestions = serializer.Deserialize<List<GoalAdjustmentSuggestion>>(json) ?? new List<GoalAdjustmentSuggestion>();
}
if (File.Exists(_progressHistoryPath))
{
string json = File.ReadAllText(_progressHistoryPath);
var serializer = new JavaScriptSerializer();
_progressHistory = serializer.Deserialize<List<GoalProgressTracker>>(json) ?? new List<GoalProgressTracker>();
}
CleanupOldData();
}
catch (Exception ex)
{
Console.WriteLine($"[AdaptiveGoalManager] Failed to load data: {ex.Message}");
}
}
private void SaveSuggestions()
{
try
{
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(_suggestions);
File.WriteAllText(_suggestionsPath, json);
}
catch (Exception ex)
{
Console.WriteLine($"[AdaptiveGoalManager] Failed to save suggestions: {ex.Message}");
}
}
private void SaveProgressHistory()
{
try
{
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(_progressHistory);
File.WriteAllText(_progressHistoryPath, json);
}
catch (Exception ex)
{
Console.WriteLine($"[AdaptiveGoalManager] Failed to save progress history: {ex.Message}");
}
}
}
}