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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
//! Study statistics and problem card detection.
//!
//! This module provides analytics workflows for understanding study
//! patterns and identifying cards that need attention.
use std::collections::HashMap;
use crate::Result;
use ankit::AnkiClient;
use serde::Serialize;
/// Summary of study activity.
#[derive(Debug, Clone, Default, Serialize)]
pub struct StudySummary {
/// Total number of reviews in the period.
pub total_reviews: usize,
/// Number of unique cards reviewed.
pub unique_cards: usize,
/// Total time spent studying in seconds.
pub total_time_seconds: u64,
/// Average reviews per day.
pub avg_reviews_per_day: f64,
/// Daily breakdown.
pub daily: Vec<DailyStats>,
}
/// Study statistics for a single day.
#[derive(Debug, Clone, Default, Serialize)]
pub struct DailyStats {
/// Date in YYYY-MM-DD format.
pub date: String,
/// Number of reviews.
pub reviews: usize,
/// Time spent in seconds.
pub time_seconds: u64,
}
/// A card identified as problematic.
#[derive(Debug, Clone, Serialize)]
pub struct ProblemCard {
/// The card ID.
pub card_id: i64,
/// The note ID.
pub note_id: i64,
/// Number of lapses (times forgotten).
pub lapses: i64,
/// Total number of reviews.
pub reps: i64,
/// Current ease factor (percentage * 10).
pub ease: i64,
/// Current interval in days.
pub interval: i64,
/// The deck name.
pub deck_name: String,
/// Front field content (first field).
pub front: String,
/// Reason this card was flagged.
pub reason: ProblemReason,
}
/// Reason a card was flagged as problematic.
#[derive(Debug, Clone, Serialize)]
pub enum ProblemReason {
/// Card has been forgotten many times.
HighLapseCount(i64),
/// Card has very low ease factor.
LowEase(i64),
/// Card has been reviewed many times but still has short interval.
PoorRetention { reps: i64, interval: i64 },
}
/// Criteria for finding problem cards.
#[derive(Debug, Clone)]
pub struct ProblemCriteria {
/// Minimum lapse count to flag.
pub min_lapses: i64,
/// Maximum ease factor to flag (e.g., 2000 = 200%).
pub max_ease: i64,
/// Minimum reps with max interval for poor retention.
pub min_reps_for_retention: i64,
/// Maximum interval with high reps for poor retention.
pub max_interval_for_retention: i64,
}
impl Default for ProblemCriteria {
fn default() -> Self {
Self {
min_lapses: 5,
max_ease: 2000, // 200%
min_reps_for_retention: 10,
max_interval_for_retention: 7,
}
}
}
/// Analysis workflow engine.
#[derive(Debug)]
pub struct AnalyzeEngine<'a> {
client: &'a AnkiClient,
}
impl<'a> AnalyzeEngine<'a> {
pub(crate) fn new(client: &'a AnkiClient) -> Self {
Self { client }
}
/// Get a summary of study activity.
///
/// # Arguments
///
/// * `deck` - Deck to analyze (use "*" for all decks)
/// * `days` - Number of days to include
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
/// let stats = engine.analyze().study_summary("Japanese", 30).await?;
/// println!("Reviewed {} cards", stats.total_reviews);
/// # Ok(())
/// # }
/// ```
pub async fn study_summary(&self, deck: &str, days: u32) -> Result<StudySummary> {
let daily_reviews = self.client.statistics().cards_reviewed_by_day().await?;
let mut summary = StudySummary::default();
let take_days = days as usize;
// Take last N days
let recent: Vec<_> = daily_reviews.into_iter().take(take_days).collect();
for (date, count) in &recent {
summary.total_reviews += *count as usize;
summary.daily.push(DailyStats {
date: date.clone(),
reviews: *count as usize,
time_seconds: 0, // Would need review data for this
});
}
if !recent.is_empty() {
summary.avg_reviews_per_day = summary.total_reviews as f64 / recent.len() as f64;
}
// Get unique cards reviewed
if deck != "*" {
let query = format!("deck:\"{}\" rated:{}", deck, days);
let cards = self.client.cards().find(&query).await?;
summary.unique_cards = cards.len();
}
Ok(summary)
}
/// Find problem cards (leeches).
///
/// # Arguments
///
/// * `query` - Anki search query to filter cards
/// * `criteria` - Criteria for identifying problems
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # use ankit_engine::analyze::ProblemCriteria;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
/// let problems = engine.analyze()
/// .find_problems("deck:Japanese", ProblemCriteria::default())
/// .await?;
/// for card in problems {
/// println!("Problem card: {} - {:?}", card.front, card.reason);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn find_problems(
&self,
query: &str,
criteria: ProblemCriteria,
) -> Result<Vec<ProblemCard>> {
let card_ids = self.client.cards().find(query).await?;
if card_ids.is_empty() {
return Ok(Vec::new());
}
let cards = self.client.cards().info(&card_ids).await?;
let mut problems = Vec::new();
for card in cards {
let reason = if card.lapses >= criteria.min_lapses {
Some(ProblemReason::HighLapseCount(card.lapses))
} else if card.ease_factor > 0 && card.ease_factor <= criteria.max_ease {
Some(ProblemReason::LowEase(card.ease_factor))
} else if card.reps >= criteria.min_reps_for_retention
&& card.interval <= criteria.max_interval_for_retention
{
Some(ProblemReason::PoorRetention {
reps: card.reps,
interval: card.interval,
})
} else {
None
};
if let Some(reason) = reason {
// Get the note to get the front field
let note_info = self.client.notes().info(&[card.note_id]).await?;
let front = note_info
.first()
.and_then(|n| n.fields.values().next())
.map(|f| f.value.clone())
.unwrap_or_default();
problems.push(ProblemCard {
card_id: card.card_id,
note_id: card.note_id,
lapses: card.lapses,
reps: card.reps,
ease: card.ease_factor,
interval: card.interval,
deck_name: card.deck_name.clone(),
front,
reason,
});
}
}
Ok(problems)
}
/// Get retention statistics for a deck.
///
/// # Arguments
///
/// * `deck` - Deck to analyze
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
/// let retention = engine.analyze().retention_stats("Japanese").await?;
/// println!("Average ease: {}%", retention.avg_ease / 10);
/// # Ok(())
/// # }
/// ```
pub async fn retention_stats(&self, deck: &str) -> Result<RetentionStats> {
let query = format!("deck:\"{}\" is:review", deck);
let card_ids = self.client.cards().find(&query).await?;
if card_ids.is_empty() {
return Ok(RetentionStats::default());
}
let cards = self.client.cards().info(&card_ids).await?;
let ease_factors = self.client.cards().get_ease(&card_ids).await?;
let total_lapses: i64 = cards.iter().map(|c| c.lapses).sum();
let total_reps: i64 = cards.iter().map(|c| c.reps).sum();
let avg_ease: i64 = if !ease_factors.is_empty() {
ease_factors.iter().sum::<i64>() / ease_factors.len() as i64
} else {
0
};
let avg_interval: i64 = if !cards.is_empty() {
cards.iter().map(|c| c.interval).sum::<i64>() / cards.len() as i64
} else {
0
};
Ok(RetentionStats {
total_cards: cards.len(),
total_reviews: total_reps as usize,
total_lapses: total_lapses as usize,
avg_ease,
avg_interval,
retention_rate: if total_reps > 0 {
1.0 - (total_lapses as f64 / total_reps as f64)
} else {
0.0
},
})
}
/// Perform a comprehensive audit of a deck.
///
/// Returns detailed information about deck contents including card counts,
/// tag distribution, empty fields, duplicates, and scheduling state.
///
/// # Arguments
///
/// * `deck` - Deck name to audit
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
/// let audit = engine.analyze().deck_audit("Japanese").await?;
///
/// println!("Deck: {}", audit.deck);
/// println!("Total cards: {}", audit.total_cards);
/// println!("Total notes: {}", audit.total_notes);
/// println!("Leeches: {}", audit.leech_count);
/// println!("Suspended: {}", audit.suspended_count);
/// println!("New: {}, Learning: {}, Review: {}",
/// audit.new_cards, audit.learning_cards, audit.review_cards);
///
/// for (model, count) in &audit.cards_by_model {
/// println!(" {}: {} cards", model, count);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn deck_audit(&self, deck: &str) -> Result<DeckAudit> {
let mut audit = DeckAudit {
deck: deck.to_string(),
..Default::default()
};
let query = format!("deck:\"{}\"", deck);
// Get all cards in deck
let card_ids = self.client.cards().find(&query).await?;
audit.total_cards = card_ids.len();
if card_ids.is_empty() {
return Ok(audit);
}
// Get card info for scheduling and model analysis
let cards = self.client.cards().info(&card_ids).await?;
// Count by model and scheduling state
let mut ease_sum: i64 = 0;
let mut ease_count: usize = 0;
for card in &cards {
// Count by model
*audit
.cards_by_model
.entry(card.model_name.clone())
.or_insert(0) += 1;
// Count by scheduling state (card_type: 0=new, 1=learning, 2=review, 3=relearning)
match card.card_type {
0 => audit.new_cards += 1,
1 | 3 => audit.learning_cards += 1,
2 => audit.review_cards += 1,
_ => {}
}
// Check suspended (queue == -1)
if card.queue == -1 {
audit.suspended_count += 1;
}
// Check leech (high lapses, default threshold 8)
if card.lapses >= 8 {
audit.leech_count += 1;
}
// Accumulate ease for average
if card.ease_factor > 0 {
ease_sum += card.ease_factor;
ease_count += 1;
}
}
// Calculate average ease
if ease_count > 0 {
audit.average_ease = ease_sum as f64 / ease_count as f64;
}
// Get all notes in deck
let note_ids = self.client.notes().find(&query).await?;
audit.total_notes = note_ids.len();
if !note_ids.is_empty() {
let notes = self.client.notes().info(¬e_ids).await?;
// Tag distribution and untagged count
for note in ¬es {
if note.tags.is_empty() {
audit.untagged_notes += 1;
} else {
for tag in ¬e.tags {
*audit.tag_distribution.entry(tag.clone()).or_insert(0) += 1;
}
}
}
// Empty field analysis - collect all field names and check which are empty
let mut field_names: HashMap<String, bool> = HashMap::new();
for note in ¬es {
for (field_name, field_value) in ¬e.fields {
field_names.insert(field_name.clone(), true);
if field_value.value.trim().is_empty() {
*audit
.empty_field_counts
.entry(field_name.clone())
.or_insert(0) += 1;
}
}
}
// Duplicate detection - use first field as key
let mut seen_values: HashMap<String, usize> = HashMap::new();
for note in ¬es {
// Get the first field value (sorted by order)
if let Some(first_field) = note
.fields
.values()
.min_by_key(|f| f.order)
.map(|f| f.value.trim().to_lowercase())
{
if !first_field.is_empty() {
*seen_values.entry(first_field).or_insert(0) += 1;
}
}
}
// Count duplicates (values that appear more than once)
audit.duplicate_count = seen_values.values().filter(|&&count| count > 1).count();
}
Ok(audit)
}
/// Generate a comprehensive study report.
///
/// Combines multiple statistics into a single overview including activity summary,
/// performance metrics, problem cards, and upcoming workload.
///
/// # Arguments
///
/// * `deck` - Deck to analyze (use "*" for all decks)
/// * `days` - Number of days to include in the report
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
/// let report = engine.analyze().study_report("Japanese", 7).await?;
///
/// println!("Study Report for {}", report.deck);
/// println!("Reviews: {} ({:.1}/day)", report.total_reviews, report.average_reviews_per_day);
/// println!("Retention: {:.1}%", report.retention_rate * 100.0);
/// println!("Study streak: {} days", report.study_streak);
/// println!("Leeches: {}", report.leeches.len());
/// println!("Due tomorrow: {}", report.due_tomorrow);
/// # Ok(())
/// # }
/// ```
pub async fn study_report(&self, deck: &str, days: u32) -> Result<StudyReport> {
let mut report = StudyReport {
deck: deck.to_string(),
period_days: days,
..Default::default()
};
// Get daily review counts
let daily_reviews = self.client.statistics().cards_reviewed_by_day().await?;
let take_days = days as usize;
let recent: Vec<_> = daily_reviews.into_iter().take(take_days).collect();
// Calculate activity metrics
for (date, count) in &recent {
report.total_reviews += *count as usize;
report.daily_stats.push(ReportDailyStats {
date: date.clone(),
reviews: *count as usize,
});
}
if !recent.is_empty() {
report.average_reviews_per_day = report.total_reviews as f64 / recent.len() as f64;
}
// Calculate study streak (consecutive days with reviews from most recent)
report.study_streak = recent.iter().take_while(|(_, count)| *count > 0).count() as u32;
// Build query for deck-specific stats
let review_query = if deck == "*" {
"is:review".to_string()
} else {
format!("deck:\"{}\" is:review", deck)
};
let review_card_ids = self.client.cards().find(&review_query).await?;
if !review_card_ids.is_empty() {
let cards = self.client.cards().info(&review_card_ids).await?;
// Calculate retention and ease
let total_lapses: i64 = cards.iter().map(|c| c.lapses).sum();
let total_reps: i64 = cards.iter().map(|c| c.reps).sum();
if total_reps > 0 {
report.retention_rate = 1.0 - (total_lapses as f64 / total_reps as f64);
}
let ease_values: Vec<i64> = cards
.iter()
.filter(|c| c.ease_factor > 0)
.map(|c| c.ease_factor)
.collect();
if !ease_values.is_empty() {
report.average_ease =
ease_values.iter().sum::<i64>() as f64 / ease_values.len() as f64;
}
// Find problem cards
for card in &cards {
// Leeches: 8+ lapses (Anki default)
if card.lapses >= 8 {
report.leeches.push(card.card_id);
}
// Low ease: below 200% (2000)
if card.ease_factor > 0 && card.ease_factor < 2000 {
report.low_ease_cards.push(card.card_id);
}
}
// Count relearning cards
report.relearning_cards = cards.iter().filter(|c| c.card_type == 3).count();
}
// Get cards studied in period (rated:N query)
if deck != "*" {
let rated_query = format!("deck:\"{}\" rated:{}", deck, days);
let rated_cards = self.client.cards().find(&rated_query).await?;
if !rated_cards.is_empty() {
let card_infos = self.client.cards().info(&rated_cards).await?;
// Count by type
for card in &card_infos {
match card.card_type {
0 => report.new_cards_studied += 1,
2 => report.review_cards_studied += 1,
_ => {}
}
}
}
}
// Get upcoming workload
let due_tomorrow_query = if deck == "*" {
"prop:due=1".to_string()
} else {
format!("deck:\"{}\" prop:due=1", deck)
};
let due_tomorrow_cards = self.client.cards().find(&due_tomorrow_query).await?;
report.due_tomorrow = due_tomorrow_cards.len();
let due_week_query = if deck == "*" {
"prop:due<=7".to_string()
} else {
format!("deck:\"{}\" prop:due<=7", deck)
};
let due_week_cards = self.client.cards().find(&due_week_query).await?;
report.due_this_week = due_week_cards.len();
Ok(report)
}
/// Compare two decks for overlap and differences.
///
/// Analyzes notes in both decks based on a key field, identifying:
/// - Notes unique to each deck
/// - Exact matches (identical key field values)
/// - Similar notes (fuzzy matching above threshold)
///
/// # Arguments
///
/// * `deck_a` - Name of the first deck
/// * `deck_b` - Name of the second deck
/// * `options` - Comparison options (key field and similarity threshold)
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # use ankit_engine::analyze::CompareOptions;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
///
/// let comparison = engine.analyze()
/// .compare_decks("Japanese::Core", "Japanese::Extra", CompareOptions {
/// key_field: "Front".to_string(),
/// similarity_threshold: 0.85,
/// })
/// .await?;
///
/// println!("Only in Core: {}", comparison.only_in_a.len());
/// println!("Only in Extra: {}", comparison.only_in_b.len());
/// println!("Exact matches: {}", comparison.exact_matches.len());
/// println!("Similar: {}", comparison.similar.len());
///
/// for pair in &comparison.similar {
/// println!(" {:.0}% similar: '{}' vs '{}'",
/// pair.similarity * 100.0,
/// pair.note_a.key_value,
/// pair.note_b.key_value);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn compare_decks(
&self,
deck_a: &str,
deck_b: &str,
options: CompareOptions,
) -> Result<DeckComparison> {
let mut comparison = DeckComparison {
deck_a: deck_a.to_string(),
deck_b: deck_b.to_string(),
key_field: options.key_field.clone(),
similarity_threshold: options.similarity_threshold,
..Default::default()
};
// Get notes from both decks
let query_a = format!("deck:\"{}\"", deck_a);
let query_b = format!("deck:\"{}\"", deck_b);
let note_ids_a = self.client.notes().find(&query_a).await?;
let note_ids_b = self.client.notes().find(&query_b).await?;
if note_ids_a.is_empty() && note_ids_b.is_empty() {
return Ok(comparison);
}
// Get note info
let notes_a = if note_ids_a.is_empty() {
Vec::new()
} else {
self.client.notes().info(¬e_ids_a).await?
};
let notes_b = if note_ids_b.is_empty() {
Vec::new()
} else {
self.client.notes().info(¬e_ids_b).await?
};
// Extract key field values
let extract_key = |note: &ankit::NoteInfo| -> Option<(i64, String, Vec<String>)> {
note.fields
.get(&options.key_field)
.map(|f| (note.note_id, f.value.trim().to_string(), note.tags.clone()))
};
let keys_a: Vec<_> = notes_a.iter().filter_map(extract_key).collect();
let keys_b: Vec<_> = notes_b.iter().filter_map(extract_key).collect();
// Build lookup map for deck B (for exact matching from A)
let map_b: HashMap<String, (i64, Vec<String>)> = keys_b
.iter()
.map(|(id, key, tags)| (key.to_lowercase(), (*id, tags.clone())))
.collect();
// Track which notes have been matched
let mut matched_in_a: std::collections::HashSet<i64> = std::collections::HashSet::new();
let mut matched_in_b: std::collections::HashSet<i64> = std::collections::HashSet::new();
// Find exact matches
for (note_id_a, key_a, tags_a) in &keys_a {
let key_lower = key_a.to_lowercase();
if let Some((note_id_b, tags_b)) = map_b.get(&key_lower) {
matched_in_a.insert(*note_id_a);
matched_in_b.insert(*note_id_b);
comparison.exact_matches.push((
ComparisonNote {
note_id: *note_id_a,
key_value: key_a.clone(),
tags: tags_a.clone(),
},
ComparisonNote {
note_id: *note_id_b,
key_value: key_a.clone(), // Same value
tags: tags_b.clone(),
},
));
}
}
// Find similar matches (only for unmatched notes)
if options.similarity_threshold < 1.0 {
for (note_id_a, key_a, tags_a) in &keys_a {
if matched_in_a.contains(note_id_a) {
continue;
}
for (note_id_b, key_b, tags_b) in &keys_b {
if matched_in_b.contains(note_id_b) {
continue;
}
let similarity = string_similarity(key_a, key_b);
if similarity >= options.similarity_threshold {
matched_in_a.insert(*note_id_a);
matched_in_b.insert(*note_id_b);
comparison.similar.push(SimilarPair {
note_a: ComparisonNote {
note_id: *note_id_a,
key_value: key_a.clone(),
tags: tags_a.clone(),
},
note_b: ComparisonNote {
note_id: *note_id_b,
key_value: key_b.clone(),
tags: tags_b.clone(),
},
similarity,
});
break; // Move to next note in A
}
}
}
}
// Collect unmatched notes
for (note_id_a, key_a, tags_a) in &keys_a {
if !matched_in_a.contains(note_id_a) {
comparison.only_in_a.push(ComparisonNote {
note_id: *note_id_a,
key_value: key_a.clone(),
tags: tags_a.clone(),
});
}
}
for (note_id_b, key_b, tags_b) in &keys_b {
if !matched_in_b.contains(note_id_b) {
comparison.only_in_b.push(ComparisonNote {
note_id: *note_id_b,
key_value: key_b.clone(),
tags: tags_b.clone(),
});
}
}
Ok(comparison)
}
/// Generate a study plan with recommendations.
///
/// Creates a plan for a study session based on due cards, new cards,
/// and target study time. Provides recommendations for optimizing
/// the session.
///
/// # Arguments
///
/// * `deck` - Deck to plan for
/// * `options` - Planning options (target time, new card ratio, etc.)
///
/// # Example
///
/// ```no_run
/// # use ankit_engine::Engine;
/// # use ankit_engine::analyze::PlanOptions;
/// # async fn example() -> ankit_engine::Result<()> {
/// let engine = Engine::new();
///
/// let plan = engine.analyze()
/// .study_plan("Japanese", PlanOptions {
/// target_time_minutes: 30,
/// new_card_ratio: 0.2,
/// prioritize_leeches: true,
/// ..PlanOptions::default()
/// })
/// .await?;
///
/// println!("Estimated time: {} minutes", plan.estimated_time);
/// println!("Reviews: {}, New: {}", plan.review_count, plan.new_count);
///
/// for rec in &plan.recommendations {
/// println!("- {}", rec);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn study_plan(&self, deck: &str, options: PlanOptions) -> Result<StudyPlan> {
let mut plan = StudyPlan {
deck: deck.to_string(),
..Default::default()
};
// Get due cards
let due_query = format!("deck:\"{}\" is:due -is:suspended", deck);
let due_card_ids = self.client.cards().find(&due_query).await?;
plan.total_due = due_card_ids.len();
// Get new cards
let new_query = format!("deck:\"{}\" is:new -is:suspended", deck);
let new_card_ids = self.client.cards().find(&new_query).await?;
plan.total_new_available = new_card_ids.len();
if due_card_ids.is_empty() && new_card_ids.is_empty() {
plan.recommendations
.push("No cards to study! Consider adding new material.".to_string());
return Ok(plan);
}
// Get card info for prioritization
let due_cards = if due_card_ids.is_empty() {
Vec::new()
} else {
self.client.cards().info(&due_card_ids).await?
};
// Calculate target card counts based on time
let total_seconds = options.target_time_minutes * 60;
// First, identify leeches and calculate how many we can fit
let mut leech_ids: Vec<i64> = Vec::new();
let mut regular_review_ids: Vec<i64> = Vec::new();
for card in &due_cards {
if card.lapses >= options.leech_threshold {
leech_ids.push(card.card_id);
} else {
regular_review_ids.push(card.card_id);
}
}
plan.leech_count = leech_ids.len();
// Calculate card allocation
let mut remaining_seconds = total_seconds;
let mut selected_reviews: Vec<i64> = Vec::new();
let mut selected_new: Vec<i64> = Vec::new();
// If prioritizing leeches, add them first
if options.prioritize_leeches && !leech_ids.is_empty() {
let leech_time = leech_ids.len() as u32 * options.seconds_per_review_card;
if leech_time <= remaining_seconds {
selected_reviews.extend(&leech_ids);
remaining_seconds -= leech_time;
} else {
// Can only fit some leeches
let max_leeches = (remaining_seconds / options.seconds_per_review_card) as usize;
selected_reviews.extend(leech_ids.iter().take(max_leeches));
remaining_seconds = 0;
}
}
// Calculate remaining time split between new and review cards
if remaining_seconds > 0 {
// Target ratio of new cards
let new_time_budget = (remaining_seconds as f64 * options.new_card_ratio) as u32;
let review_time_budget = remaining_seconds - new_time_budget;
// How many of each can we fit?
let max_new = (new_time_budget / options.seconds_per_new_card) as usize;
let max_reviews = (review_time_budget / options.seconds_per_review_card) as usize;
// Select regular reviews (excluding already-selected leeches)
let available_reviews: Vec<i64> = if options.prioritize_leeches {
regular_review_ids.clone()
} else {
due_card_ids.clone()
};
let reviews_to_add = available_reviews.iter().take(max_reviews);
selected_reviews.extend(reviews_to_add);
// Select new cards
let new_to_add = new_card_ids.iter().take(max_new);
selected_new.extend(new_to_add);
}
// Build the suggested order
// Order: Leeches first (if prioritized), then reviews, then new
let mut ordered_cards: Vec<(i64, CardPriority)> = Vec::new();
if options.prioritize_leeches {
for &id in &leech_ids {
if selected_reviews.contains(&id) {
ordered_cards.push((id, CardPriority::Leech));
}
}
}
for &id in &selected_reviews {
if !leech_ids.contains(&id) || !options.prioritize_leeches {
ordered_cards.push((id, CardPriority::DueReview));
}
}
for &id in &selected_new {
ordered_cards.push((id, CardPriority::New));
}
// Sort by priority
ordered_cards.sort_by_key(|(_, priority)| *priority);
plan.suggested_order = ordered_cards.into_iter().map(|(id, _)| id).collect();
plan.review_count = selected_reviews.len();
plan.new_count = selected_new.len();
// Calculate estimated time
let review_time = plan.review_count as u32 * options.seconds_per_review_card;
let new_time = plan.new_count as u32 * options.seconds_per_new_card;
plan.estimated_time = (review_time + new_time) / 60;
// Generate recommendations
if plan.leech_count > 0 {
plan.recommendations.push(format!(
"You have {} leech cards that need extra attention.",
plan.leech_count
));
}
if plan.total_due > plan.review_count {
plan.recommendations.push(format!(
"Only {} of {} due cards fit in your target time.",
plan.review_count, plan.total_due
));
}
if plan.total_new_available > 0 && plan.new_count == 0 {
plan.recommendations
.push("No time for new cards today. Consider increasing study time.".to_string());
} else if plan.new_count > 0 {
plan.recommendations.push(format!(
"Introducing {} new cards ({:.0}% of session).",
plan.new_count,
(plan.new_count as f64 / (plan.review_count + plan.new_count) as f64) * 100.0
));
}
if plan.review_count + plan.new_count == 0 {
plan.recommendations
.push("No cards fit the target time. Try increasing study time.".to_string());
}
let actual_ratio = if plan.review_count + plan.new_count > 0 {
plan.new_count as f64 / (plan.review_count + plan.new_count) as f64
} else {
0.0
};
if actual_ratio < options.new_card_ratio * 0.5 && plan.total_new_available > 0 {
plan.recommendations.push(
"New card ratio is below target. You may be accumulating a review backlog."
.to_string(),
);
}
Ok(plan)
}
}
/// Calculate string similarity using normalized Levenshtein distance.
///
/// Returns a value between 0.0 (completely different) and 1.0 (identical).
fn string_similarity(a: &str, b: &str) -> f64 {
let a_lower = a.to_lowercase();
let b_lower = b.to_lowercase();
if a_lower == b_lower {
return 1.0;
}
if a_lower.is_empty() || b_lower.is_empty() {
return 0.0;
}
let distance = levenshtein_distance(&a_lower, &b_lower);
let max_len = a_lower.chars().count().max(b_lower.chars().count());
1.0 - (distance as f64 / max_len as f64)
}
/// Calculate the Levenshtein distance between two strings.
fn levenshtein_distance(a: &str, b: &str) -> usize {
let a_chars: Vec<char> = a.chars().collect();
let b_chars: Vec<char> = b.chars().collect();
let m = a_chars.len();
let n = b_chars.len();
if m == 0 {
return n;
}
if n == 0 {
return m;
}
// Use two rows instead of full matrix for memory efficiency
let mut prev: Vec<usize> = (0..=n).collect();
let mut curr = vec![0; n + 1];
for i in 1..=m {
curr[0] = i;
for j in 1..=n {
let cost = if a_chars[i - 1] == b_chars[j - 1] {
0
} else {
1
};
curr[j] = (prev[j] + 1) // deletion
.min(curr[j - 1] + 1) // insertion
.min(prev[j - 1] + cost); // substitution
}
std::mem::swap(&mut prev, &mut curr);
}
prev[n]
}
/// Comprehensive study report combining multiple statistics.
///
/// Provides a complete overview of study activity, performance, problem areas,
/// and upcoming workload for a deck over a specified time period.
#[derive(Debug, Clone, Default, Serialize)]
pub struct StudyReport {
/// The deck name (or "*" for all decks).
pub deck: String,
/// Number of days covered by this report.
pub period_days: u32,
// Activity summary
/// Total number of reviews in the period.
pub total_reviews: usize,
/// Total time spent studying in minutes.
pub total_time_minutes: u64,
/// Average reviews per day.
pub average_reviews_per_day: f64,
/// Consecutive days with at least one review.
pub study_streak: u32,
// Performance metrics
/// Estimated retention rate (0.0 - 1.0).
pub retention_rate: f64,
/// Average ease factor (percentage * 10, e.g., 2500 = 250%).
pub average_ease: f64,
// Cards reviewed breakdown
/// Number of new cards studied in the period.
pub new_cards_studied: usize,
/// Number of review cards studied in the period.
pub review_cards_studied: usize,
/// Number of cards in relearning state.
pub relearning_cards: usize,
// Problem areas (card IDs)
/// Card IDs flagged as leeches (high lapses).
pub leeches: Vec<i64>,
/// Card IDs with low ease factor (below 200%).
pub low_ease_cards: Vec<i64>,
// Upcoming workload
/// Number of cards due tomorrow.
pub due_tomorrow: usize,
/// Number of cards due within the next 7 days.
pub due_this_week: usize,
// Daily breakdown
/// Statistics for each day in the period.
pub daily_stats: Vec<ReportDailyStats>,
}
/// Daily statistics for a study report.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ReportDailyStats {
/// Date in YYYY-MM-DD format.
pub date: String,
/// Number of reviews on this day.
pub reviews: usize,
}
/// Options for comparing two decks.
#[derive(Debug, Clone)]
pub struct CompareOptions {
/// Field name to use as the comparison key (e.g., "Front").
pub key_field: String,
/// Similarity threshold for fuzzy matching (0.0 - 1.0).
/// Cards with similarity >= this value are considered similar.
/// Set to 1.0 for exact matches only.
pub similarity_threshold: f64,
}
impl Default for CompareOptions {
fn default() -> Self {
Self {
key_field: "Front".to_string(),
similarity_threshold: 0.9,
}
}
}
/// Result of comparing two decks.
#[derive(Debug, Clone, Default, Serialize)]
pub struct DeckComparison {
/// Name of the first deck.
pub deck_a: String,
/// Name of the second deck.
pub deck_b: String,
/// Field used for comparison.
pub key_field: String,
/// Similarity threshold used.
pub similarity_threshold: f64,
/// Notes only in deck A (not in B).
pub only_in_a: Vec<ComparisonNote>,
/// Notes only in deck B (not in A).
pub only_in_b: Vec<ComparisonNote>,
/// Notes with exact matching key field values.
pub exact_matches: Vec<(ComparisonNote, ComparisonNote)>,
/// Notes with similar (but not exact) key field values.
pub similar: Vec<SimilarPair>,
}
/// A note in a comparison result.
#[derive(Debug, Clone, Serialize)]
pub struct ComparisonNote {
/// The note ID.
pub note_id: i64,
/// The value of the key field.
pub key_value: String,
/// The note's tags.
pub tags: Vec<String>,
}
/// A pair of similar notes from two decks.
#[derive(Debug, Clone, Serialize)]
pub struct SimilarPair {
/// Note from deck A.
pub note_a: ComparisonNote,
/// Note from deck B.
pub note_b: ComparisonNote,
/// Similarity score (0.0 - 1.0).
pub similarity: f64,
}
/// Retention statistics for a deck.
#[derive(Debug, Clone, Default, Serialize)]
pub struct RetentionStats {
/// Total number of review cards.
pub total_cards: usize,
/// Total number of reviews.
pub total_reviews: usize,
/// Total number of lapses.
pub total_lapses: usize,
/// Average ease factor (percentage * 10).
pub avg_ease: i64,
/// Average interval in days.
pub avg_interval: i64,
/// Estimated retention rate (0.0 - 1.0).
pub retention_rate: f64,
}
/// Comprehensive audit of a deck's contents and health.
///
/// Combines multiple analyses into a single report including card counts,
/// tag distribution, empty fields, duplicates, and scheduling state.
#[derive(Debug, Clone, Default, Serialize)]
pub struct DeckAudit {
/// The deck name.
pub deck: String,
/// Total number of cards.
pub total_cards: usize,
/// Total number of notes.
pub total_notes: usize,
// Card counts by model
/// Number of cards per note type (model).
pub cards_by_model: HashMap<String, usize>,
// Tag coverage
/// Number of notes per tag.
pub tag_distribution: HashMap<String, usize>,
/// Number of notes without any tags.
pub untagged_notes: usize,
// Field analysis
/// Number of notes with each field empty (field name -> count).
pub empty_field_counts: HashMap<String, usize>,
// Duplicates
/// Number of potential duplicate notes detected.
pub duplicate_count: usize,
// Problem cards
/// Number of leech cards (high lapses).
pub leech_count: usize,
/// Number of suspended cards.
pub suspended_count: usize,
// Scheduling summary
/// Number of new cards (never reviewed).
pub new_cards: usize,
/// Number of cards in learning phase.
pub learning_cards: usize,
/// Number of review cards.
pub review_cards: usize,
/// Average ease factor (percentage * 10, e.g., 2500 = 250%).
pub average_ease: f64,
}
/// Options for generating a study plan.
#[derive(Debug, Clone)]
pub struct PlanOptions {
/// Target study time in minutes.
pub target_time_minutes: u32,
/// Ratio of new cards (0.0 - 1.0). E.g., 0.2 means 20% new cards.
pub new_card_ratio: f64,
/// Whether to prioritize leech cards (cards with high lapses).
pub prioritize_leeches: bool,
/// Estimated seconds per new card.
pub seconds_per_new_card: u32,
/// Estimated seconds per review card.
pub seconds_per_review_card: u32,
/// Leech threshold (minimum lapses to consider a card a leech).
pub leech_threshold: i64,
}
impl Default for PlanOptions {
fn default() -> Self {
Self {
target_time_minutes: 30,
new_card_ratio: 0.2,
prioritize_leeches: true,
seconds_per_new_card: 30, // 30 seconds for new cards
seconds_per_review_card: 8, // 8 seconds for reviews
leech_threshold: 8,
}
}
}
/// A generated study plan with recommendations.
#[derive(Debug, Clone, Default, Serialize)]
pub struct StudyPlan {
/// The deck name.
pub deck: String,
/// Estimated time to complete the plan in minutes.
pub estimated_time: u32,
/// Number of review cards in the plan.
pub review_count: usize,
/// Number of new cards in the plan.
pub new_count: usize,
/// Number of leech cards in the plan.
pub leech_count: usize,
/// Total cards due today.
pub total_due: usize,
/// Total new cards available.
pub total_new_available: usize,
/// Recommendations for the study session.
pub recommendations: Vec<String>,
/// Suggested card IDs in study order.
pub suggested_order: Vec<i64>,
}
/// Priority category for a card in the study plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum CardPriority {
/// Leech cards should be studied first when prioritize_leeches is true.
Leech,
/// Review cards due today.
DueReview,
/// New cards.
New,
}