mdvault-core 0.7.2

Core library for mdvault - markdown vault management
Documentation
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
use chrono::{Datelike, Duration, NaiveDate, Utc};
use std::collections::HashMap;

use crate::index::{IndexDb, IndexedNote, NoteType};

use super::helpers::{
    extract_project_info, get_frontmatter_date, get_frontmatter_str, normalise_status,
    parse_review_interval, task_matches_project,
};
use super::{
    ActivityReport, CompletedTask, DashboardOptions, DayActivity, FlaggedTask,
    ProjectReport, ReviewDueProject, StaleNote, TaskCounts, VaultSummary, Velocity,
};

pub(super) fn build_vault_summary(
    all_notes: &[IndexedNote],
    tasks: &[&IndexedNote],
    projects: &[&IndexedNote],
    project_filter: &Option<String>,
) -> VaultSummary {
    let mut notes_by_type: HashMap<String, usize> = HashMap::new();
    for note in all_notes {
        *notes_by_type.entry(note.note_type.as_str().to_string()).or_default() += 1;
    }

    let scoped_tasks: Vec<&&IndexedNote> = if let Some(pf) = project_filter {
        tasks.iter().filter(|t| task_matches_project(t, pf)).collect()
    } else {
        tasks.iter().collect()
    };

    let mut tasks_by_status: HashMap<String, usize> = HashMap::new();
    for task in &scoped_tasks {
        let status =
            get_frontmatter_str(task, "status").unwrap_or_else(|| "todo".to_string());
        let normalised = normalise_status(&status);
        *tasks_by_status.entry(normalised).or_default() += 1;
    }

    let active_projects = projects
        .iter()
        .filter(|p| {
            let (_, status, _) = extract_project_info(p);
            !matches!(status.as_str(), "archived" | "done" | "completed")
        })
        .count();

    VaultSummary {
        total_notes: all_notes.len(),
        notes_by_type,
        total_tasks: scoped_tasks.len(),
        tasks_by_status,
        total_projects: projects.len(),
        active_projects,
    }
}

pub(super) fn build_project_report(
    project: &IndexedNote,
    all_tasks: &[&IndexedNote],
) -> ProjectReport {
    let (id, status, kind) = extract_project_info(project);
    let project_folder = project.path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
    let title = if project.title.is_empty() {
        project_folder.to_string()
    } else {
        project.title.clone()
    };

    let project_tasks: Vec<&&IndexedNote> =
        all_tasks.iter().filter(|t| task_matches_project(t, project_folder)).collect();

    let mut counts = TaskCounts::default();
    for task in &project_tasks {
        let s = get_frontmatter_str(task, "status").unwrap_or_else(|| "todo".to_string());
        match normalise_status(&s).as_str() {
            "todo" => counts.todo += 1,
            "in_progress" => counts.in_progress += 1,
            "blocked" => counts.blocked += 1,
            "done" => counts.done += 1,
            "cancelled" => counts.cancelled += 1,
            _ => counts.todo += 1,
        }
    }
    counts.total = project_tasks.len();

    let active_total = counts.total - counts.cancelled;
    let progress_percent = if active_total > 0 {
        (counts.done as f64 / active_total as f64) * 100.0
    } else {
        0.0
    };

    let velocity = calculate_velocity(&project_tasks);
    let completions = recent_completions(&project_tasks, &id, 7, 10);

    ProjectReport {
        id,
        title,
        kind,
        status,
        tasks: counts,
        progress_percent,
        velocity,
        recent_completions: completions,
    }
}

pub(super) fn build_activity_report(
    db: &IndexDb,
    all_notes: &[IndexedNote],
    tasks: &[&IndexedNote],
    options: &DashboardOptions,
) -> Result<ActivityReport, String> {
    let today = chrono::Local::now().date_naive();
    let start = today - Duration::days(options.activity_days as i64);

    let mut daily_activity: Vec<DayActivity> = Vec::new();
    let mut current = start;
    while current <= today {
        let date_str = current.format("%Y-%m-%d").to_string();

        let completed = tasks
            .iter()
            .filter(|t| get_frontmatter_date(t, "completed_at") == Some(current))
            .count();

        let created = tasks
            .iter()
            .filter(|t| get_frontmatter_date(t, "created_at") == Some(current))
            .count();

        let modified =
            all_notes.iter().filter(|n| n.modified.date_naive() == current).count();

        daily_activity.push(DayActivity {
            date: date_str,
            weekday: format!("{:?}", current.weekday()),
            tasks_completed: completed,
            tasks_created: created,
            notes_modified: modified,
        });

        current += Duration::days(1);
    }

    let stale_notes = db
        .get_stale_notes(options.stale_threshold, None, Some(options.stale_limit))
        .map_err(|e| format!("Failed to query stale notes: {e}"))?
        .into_iter()
        .filter(|(note, _)| matches!(note.note_type, NoteType::Task | NoteType::Project))
        .map(|(note, score)| {
            let last_seen = db
                .get_activity_summary(note.id.unwrap_or(0))
                .ok()
                .flatten()
                .and_then(|s| s.last_seen.map(|d| d.format("%Y-%m-%d").to_string()));

            StaleNote {
                title: note.title.clone(),
                path: note.path.to_string_lossy().to_string(),
                note_type: note.note_type.as_str().to_string(),
                staleness_score: score,
                last_seen,
            }
        })
        .collect();

    Ok(ActivityReport { period_days: options.activity_days, daily_activity, stale_notes })
}

pub(super) fn calculate_velocity(tasks: &[&&IndexedNote]) -> Velocity {
    let now = Utc::now();
    let two_weeks_ago = (now - Duration::weeks(2)).date_naive();
    let four_weeks_ago = (now - Duration::weeks(4)).date_naive();
    let seven_days_ago = (now - Duration::days(7)).date_naive();

    let completed_4w = tasks
        .iter()
        .filter(|t| {
            get_frontmatter_date(t, "completed_at")
                .map(|d| d >= four_weeks_ago)
                .unwrap_or(false)
        })
        .count();

    let completed_2w = tasks
        .iter()
        .filter(|t| {
            get_frontmatter_date(t, "completed_at")
                .map(|d| d >= two_weeks_ago)
                .unwrap_or(false)
        })
        .count();

    let completed_7d = tasks
        .iter()
        .filter(|t| {
            get_frontmatter_date(t, "completed_at")
                .map(|d| d >= seven_days_ago)
                .unwrap_or(false)
        })
        .count();

    let created_7d = tasks
        .iter()
        .filter(|t| {
            get_frontmatter_date(t, "created_at")
                .map(|d| d >= seven_days_ago)
                .unwrap_or(false)
        })
        .count();

    Velocity {
        tasks_per_week_4w: completed_4w as f64 / 4.0,
        tasks_per_week_2w: completed_2w as f64 / 2.0,
        created_last_7d: created_7d,
        completed_last_7d: completed_7d,
    }
}

pub(super) fn recent_completions(
    tasks: &[&&IndexedNote],
    project_id: &str,
    days: i64,
    limit: usize,
) -> Vec<CompletedTask> {
    let cutoff = (Utc::now() - Duration::days(days)).date_naive();

    let mut completions: Vec<CompletedTask> = tasks
        .iter()
        .filter_map(|t| {
            let completed_at = get_frontmatter_date(t, "completed_at")?;
            if completed_at < cutoff {
                return None;
            }
            Some(CompletedTask {
                id: get_frontmatter_str(t, "task-id").unwrap_or_default(),
                title: t.title.clone(),
                completed_at: completed_at.format("%Y-%m-%d").to_string(),
                project: project_id.to_string(),
            })
        })
        .collect();

    completions.sort_by(|a, b| b.completed_at.cmp(&a.completed_at));
    completions.truncate(limit);
    completions
}

pub(super) fn build_flagged_tasks(
    tasks: &[&IndexedNote],
    target_projects: &[&IndexedNote],
    today: NaiveDate,
    zombie_days: u32,
) -> (Vec<FlaggedTask>, Vec<FlaggedTask>, Vec<FlaggedTask>, Vec<FlaggedTask>) {
    let folder_to_id: HashMap<String, String> = target_projects
        .iter()
        .filter_map(|p| {
            let folder = p.path.file_stem().and_then(|s| s.to_str())?.to_string();
            let (id, _, _) = extract_project_info(p);
            Some((folder, id))
        })
        .collect();

    let resolve_project = |task: &IndexedNote| -> String {
        let raw = get_frontmatter_str(task, "project").unwrap_or_default();
        folder_to_id.get(&raw).cloned().unwrap_or(raw)
    };

    let is_open =
        |status: &str| !matches!(normalise_status(status).as_str(), "done" | "cancelled");

    // Overdue
    let mut overdue: Vec<FlaggedTask> = tasks
        .iter()
        .filter_map(|t| {
            let status = get_frontmatter_str(t, "status").unwrap_or_default();
            if !is_open(&status) {
                return None;
            }
            let due = get_frontmatter_date(t, "due_date")?;
            if due >= today {
                return None;
            }
            Some(FlaggedTask {
                id: get_frontmatter_str(t, "task-id").unwrap_or_default(),
                title: t.title.clone(),
                project: resolve_project(t),
                due_date: Some(due.format("%Y-%m-%d").to_string()),
                priority: get_frontmatter_str(t, "priority"),
                status: normalise_status(&status),
                days_overdue: Some((today - due).num_days()),
            })
        })
        .collect();
    overdue.sort_by(|a, b| b.days_overdue.cmp(&a.days_overdue));

    // High priority
    let mut high_priority: Vec<FlaggedTask> = tasks
        .iter()
        .filter_map(|t| {
            let status = get_frontmatter_str(t, "status").unwrap_or_default();
            if !is_open(&status) {
                return None;
            }
            let priority = get_frontmatter_str(t, "priority")?;
            if priority != "high" {
                return None;
            }
            Some(FlaggedTask {
                id: get_frontmatter_str(t, "task-id").unwrap_or_default(),
                title: t.title.clone(),
                project: resolve_project(t),
                due_date: get_frontmatter_date(t, "due_date")
                    .map(|d| d.format("%Y-%m-%d").to_string()),
                priority: Some(priority),
                status: normalise_status(&status),
                days_overdue: None,
            })
        })
        .collect();
    high_priority.truncate(10);

    // Upcoming deadlines (next 14 days)
    let deadline_horizon = today + Duration::days(14);
    let mut upcoming_deadlines: Vec<FlaggedTask> = tasks
        .iter()
        .filter_map(|t| {
            let status = get_frontmatter_str(t, "status").unwrap_or_default();
            if !is_open(&status) {
                return None;
            }
            let due = get_frontmatter_date(t, "due_date")?;
            if due < today || due > deadline_horizon {
                return None;
            }
            Some(FlaggedTask {
                id: get_frontmatter_str(t, "task-id").unwrap_or_default(),
                title: t.title.clone(),
                project: resolve_project(t),
                due_date: Some(due.format("%Y-%m-%d").to_string()),
                priority: get_frontmatter_str(t, "priority"),
                status: normalise_status(&status),
                days_overdue: None,
            })
        })
        .collect();
    upcoming_deadlines.sort_by(|a, b| a.due_date.cmp(&b.due_date));

    // Zombie tasks
    let zombie_horizon = today - Duration::days(i64::from(zombie_days));
    let mut zombie: Vec<FlaggedTask> = tasks
        .iter()
        .filter_map(|t| {
            let status = get_frontmatter_str(t, "status").unwrap_or_default();
            if normalise_status(&status) != "todo" {
                return None;
            }
            let created = get_frontmatter_date(t, "created_at")?;
            if created > zombie_horizon {
                return None;
            }
            Some(FlaggedTask {
                id: get_frontmatter_str(t, "task-id").unwrap_or_default(),
                title: t.title.clone(),
                project: resolve_project(t),
                due_date: get_frontmatter_date(t, "due_date")
                    .map(|d| d.format("%Y-%m-%d").to_string()),
                priority: get_frontmatter_str(t, "priority"),
                status: normalise_status(&status),
                days_overdue: Some((today - created).num_days()),
            })
        })
        .collect();
    zombie.sort_by(|a, b| b.days_overdue.cmp(&a.days_overdue));

    (overdue, high_priority, upcoming_deadlines, zombie)
}

pub(super) fn build_review_due(
    projects: &[&IndexedNote],
    today: NaiveDate,
) -> Vec<ReviewDueProject> {
    let mut due: Vec<ReviewDueProject> = projects
        .iter()
        .filter_map(|p| {
            let (id, status, kind) = extract_project_info(p);
            if matches!(status.as_str(), "done" | "archived") {
                return None;
            }
            let interval_str = get_frontmatter_str(p, "review_interval")?;
            let interval_days = parse_review_interval(&interval_str)?;

            let last_reviewed = get_frontmatter_date(p, "last_reviewed")
                .or_else(|| get_frontmatter_date(p, "updated_at"))?;

            let days_since = (today - last_reviewed).num_days();
            let days_overdue = days_since - interval_days;

            if days_overdue <= 0 {
                return None;
            }

            Some(ReviewDueProject {
                id,
                title: p.title.clone(),
                kind,
                review_interval: interval_str,
                last_reviewed: Some(last_reviewed.format("%Y-%m-%d").to_string()),
                days_since_review: days_since,
                days_overdue,
            })
        })
        .collect();

    due.sort_by(|a, b| b.days_overdue.cmp(&a.days_overdue));
    due
}