farmap 0.9.1

A library for working with Farcaster label datasets
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
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
use crate::cast_meta::CastMeta;
use crate::spam_score::SpamEntries;
use crate::spam_score::SpamEntry;
use crate::spam_score::SpamScore;
use crate::UnprocessedUserLine;
use chrono::DateTime;
use chrono::Datelike;
use chrono::Local;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use itertools::*;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct User {
    fid: usize,
    #[serde(rename = "entries")]
    labels: Option<SpamEntries>,

    /// Some(Empty vec): has been checked and there were no cast records.
    /// None: Has not been checked.
    cast_records: Option<Vec<CastMeta>>,
    reaction_times: Option<Vec<NaiveDateTime>>,
    latest_reaction_time_update_date: Option<NaiveDateTime>,
    latest_cast_record_check_date: Option<NaiveDate>,
}

type SpamRecord = (SpamScore, NaiveDate);

impl User {
    /// This method only takes a single SpamRecord as input. Therefore it cannot fail. Add more
    /// SpamRecords with add_spam_record. This function is mostly used for testing.
    #[deprecated(since = "0.9.1", note = "use new_without_labels instead")]
    pub fn new(fid: usize, labels: SpamRecord) -> Self {
        let entry = SpamEntry::WithoutSourceCommit(labels);
        let entries = SpamEntries::new(entry);
        Self {
            fid,
            labels: Some(entries),
            cast_records: None,
            latest_cast_record_check_date: None,
            reaction_times: None,
            latest_reaction_time_update_date: None,
        }
    }

    pub fn new_without_labels(fid: usize) -> Self {
        Self {
            fid,
            labels: None,
            cast_records: None,
            latest_cast_record_check_date: None,
            reaction_times: None,
            latest_reaction_time_update_date: None,
        }
    }

    pub fn add_cast_records(&mut self, records: Vec<CastMeta>, check_date: NaiveDate) {
        self.cast_records = Some(records);
        self.latest_cast_record_check_date = Some(check_date);
    }

    pub fn update_reaction_times(
        &mut self,
        reaction_times: Vec<NaiveDateTime>,
    ) -> Option<Vec<NaiveDateTime>> {
        self.latest_reaction_time_update_date = Some(Local::now().naive_utc());
        self.reaction_times.replace(reaction_times)
    }

    pub fn latest_reaction_time(&self) -> Option<&NaiveDateTime> {
        if let Some(reaction_times) = &self.reaction_times {
            Some(reaction_times.iter().max()?)
        } else {
            None
        }
    }

    /// Returns the fid of the user
    /// # Examples
    /// ```rust
    /// use farmap::User;
    /// use chrono::NaiveDate;
    /// use farmap::SpamScore;
    ///
    /// let user = User::new(1, (SpamScore::Zero, NaiveDate::from_ymd_opt(2020,1,1).unwrap()) );
    /// assert_eq!(user.fid(), 1);
    /// ````
    pub fn fid(&self) -> usize {
        self.fid
    }

    pub fn latest_spam_record(&self) -> Option<SpamRecord> {
        Some(self.labels.as_ref()?.last_spam_entry().record())
    }

    pub fn earliest_spam_record(&self) -> Option<SpamRecord> {
        Some(self.labels.as_ref()?.earliest_spam_entry().record())
    }

    pub fn all_spam_records_with_opt(&self) -> Option<Vec<SpamRecord>> {
        let records = self
            .labels
            .as_ref()?
            .all_spam_entries()
            .iter()
            .cloned()
            .map(|x| x.record())
            .collect_vec();
        Some(records)
    }

    /// None: there is no spam_score data in the dataset.
    pub fn created_at_or_after_date_with_opt(&self, date: NaiveDate) -> Option<bool> {
        Some(self.earliest_spam_record()?.1 >= date)
    }

    pub fn created_at_or_before_date_with_opt(&self, date: NaiveDate) -> Option<bool> {
        Some(self.earliest_spam_record()?.1 <= date)
    }

    pub fn latest_reaction_time_update_date(&self) -> Option<NaiveDateTime> {
        self.latest_reaction_time_update_date
    }

    /// Adds a new spam record to a user. There are three scenarios that may happen:
    ///
    /// The user already has a spam record with the same date and the same record. The method
    /// returns Ok without doing anything.
    ///
    /// The user already has a spam record with a different spam score at the same date.
    /// The method does not change the struct and returns an error.
    ///
    /// There is no collision and the list is updated while remaining sorted.
    ///
    pub fn add_spam_record(&mut self, new_record: SpamRecord) -> Result<(), UserError> {
        let new_entry = SpamEntry::WithoutSourceCommit(new_record);
        if let Some(labels) = &mut self.labels {
            labels
                .add_spam_entry(new_entry)
                .map_err(|_| UserError::SpamScoreCollision {
                    fid: self.fid(),
                    date: new_entry.date(),
                    old_spam_score: self
                        .spam_score_at_date_with_owned(&new_entry.date())
                        .unwrap(),
                    new_spam_score: new_entry.score(),
                })
        } else {
            self.labels = Some(SpamEntries::new(new_entry));
            Ok(())
        }
    }

    /// Merges two user objects into one. Both must have the same FID and no contradictory spam
    /// records (i.e same date but different scores)
    #[deprecated(note = "use add spam record instead")]
    pub fn merge_user(&mut self, other: Self) -> Result<(), UserError> {
        if self.fid != other.fid() {
            return Err(UserError::DifferentFidMerge {
                fid_1: self.fid,
                fid_2: other.fid,
            });
        };

        for spam_record in other.all_spam_records_with_opt().unwrap() {
            self.add_spam_record(spam_record)?
        }

        Ok(())
    }

    pub fn cast_count(&self) -> Option<u64> {
        Some(self.cast_records.as_ref()?.len() as u64)
    }

    pub fn reaction_times(&self) -> &Option<Vec<NaiveDateTime>> {
        &self.reaction_times
    }

    pub fn average_monthly_cast_rate(&self) -> Option<f32> {
        let [sum, count] = self
            .monthly_cast_counts()?
            .iter()
            .fold([0, 0], |acc, (x, _)| [acc[0] + x, acc[1] + 1]);

        Some(sum as f32 / count as f32)
    }

    pub fn monthly_cast_counts(&self) -> Option<Vec<(usize, NaiveDate)>> {
        let cast_records = if let Some(cast_records) = &self.cast_records {
            cast_records
        } else {
            return None;
        };

        Some(
            cast_records
                .iter()
                .map(|x| {
                    NaiveDate::from_ymd_opt(x.cast_date().year(), x.cast_date().month(), 1)
                        .expect("date parsing inside monthly_cast_count should work")
                })
                .sorted()
                .dedup_with_count()
                .collect::<Vec<_>>(),
        )
    }

    pub fn has_cast_data(&self) -> bool {
        self.cast_records.is_some()
    }

    pub fn latest_cast_record_check_date(&self) -> Option<NaiveDate> {
        self.latest_cast_record_check_date
    }

    pub fn latest_spam_score_update_date_with_opt(&self) -> Option<NaiveDate> {
        Some(self.labels.as_ref()?.last_spam_entry().date())
    }

    pub fn earliest_spam_score_date_with_opt(&self) -> Option<NaiveDate> {
        Some(self.labels.as_ref()?.earliest_spam_entry().date())
    }

    pub fn latest_spam_score_date_with_opt(&self) -> Option<NaiveDate> {
        Some(self.labels.as_ref()?.last_spam_entry().date())
    }

    /// If the user didn't exist at the date, the function returns none.
    pub fn spam_score_at_date_with_owned(&self, date: &NaiveDate) -> Option<SpamScore> {
        if date < &self.earliest_spam_record()?.1 {
            return None;
        };
        Some(self.labels.as_ref()?.spam_score_at_date(*date)?)
    }
}

#[derive(Error, Debug, PartialEq)]
pub enum UserError {
    #[error("User {0} already has a spam record for date {1}. The existing spam score at the date is {} but a spamscore of {} is now trying to be set.", .fid, . date) ]
    SpamScoreCollision {
        fid: usize,
        date: NaiveDate,
        old_spam_score: SpamScore,
        new_spam_score: SpamScore,
    },
    #[error("Trying to merge users with different fids. For merge_user to work both input users must have the same fid. provided fid_1: {} and provided fid_2 {}", .fid_1, .fid_2)]
    DifferentFidMerge { fid_1: usize, fid_2: usize },
}

impl TryFrom<UnprocessedUserLine> for User {
    type Error = InvalidInputError;

    fn try_from(value: UnprocessedUserLine) -> Result<Self, Self::Error> {
        let label_value = SpamScore::try_from(value.label_value())?;
        let fid = value.fid();
        let date = if let Some(date) =
            DateTime::from_timestamp(value.timestamp().try_into().unwrap(), 0)
        {
            date.date_naive()
        } else {
            return Err(InvalidInputError::DateError {
                timestamp: value.timestamp(),
            });
        };
        let record: SpamRecord = (label_value, date);
        let entries = SpamEntries::new(SpamEntry::WithoutSourceCommit(record));

        Ok(Self {
            fid,
            labels: Some(entries),
            cast_records: None,
            latest_cast_record_check_date: None,
            reaction_times: None,
            latest_reaction_time_update_date: None,
        })
    }
}

#[derive(Error, Debug, PartialEq)]
pub enum InvalidInputError {
    #[error("SpamScore was {0}, not zero, one or two.", .label)]
    SpamScoreError { label: usize },
    #[error("Timestamp was {0}, which is invalid.", . timestamp)]
    DateError { timestamp: usize },
}

#[cfg(test)]
pub mod tests {
    use chrono::NaiveTime;
    use std::path::PathBuf;

    use super::*;
    use crate::user_collection::UserCollection;

    fn check_created_at_or_before_date(user: &User, year: u32, month: u32, date: u32) -> bool {
        user.created_at_or_before_date_with_opt(
            NaiveDate::from_ymd_opt(year as i32, month, date).unwrap(),
        )
        .unwrap()
    }

    fn check_created_at_or_after_date(user: &User, year: u32, month: u32, date: u32) -> bool {
        user.created_at_or_after_date_with_opt(
            NaiveDate::from_ymd_opt(year as i32, month, date).unwrap(),
        )
        .unwrap()
    }

    fn check_spam_score_at_date(
        user: &User,
        year: u32,
        month: u32,
        date: u32,
        spam_score: Option<SpamScore>,
    ) {
        assert_eq!(
            user.spam_score_at_date_with_owned(
                &NaiveDate::from_ymd_opt(year as i32, month, date).unwrap()
            ),
            spam_score
        )
    }

    fn check_latest_reaction_time(user: &User, time: NaiveDateTime) {
        assert_eq!(*user.latest_reaction_time().unwrap(), time);
    }

    fn dummy_data_users_with_reaction_times() -> UserCollection {
        let mut users = UserCollection::default();
        let label = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
        let reaction_date = NaiveDate::from_ymd_opt(2025, 2, 1).unwrap();
        let reaction_time = NaiveTime::from_hms_opt(10, 1, 10).unwrap();
        let reaction_datetime = NaiveDateTime::new(reaction_date, reaction_time);
        let check_date = NaiveDate::from_ymd_opt(2025, 4, 1).unwrap();
        let check_time = NaiveTime::from_hms_opt(10, 1, 10).unwrap();
        let check_datetime = NaiveDateTime::new(check_date, check_time);
        let labels = SpamEntries::new(SpamEntry::WithoutSourceCommit((SpamScore::Zero, label)));
        let user = User {
            fid: 1,
            labels: Some(labels),
            reaction_times: Some(vec![reaction_datetime]),
            latest_reaction_time_update_date: Some(check_datetime),
            cast_records: None,
            latest_cast_record_check_date: None,
        };
        #[allow(deprecated)]
        users.push_with_res(user).unwrap();
        let label = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
        let first_reaction_date = NaiveDate::from_ymd_opt(2025, 4, 1).unwrap();
        let first_reaction_time = NaiveTime::from_hms_opt(10, 1, 10).unwrap();
        let first_reaction_datetime = NaiveDateTime::new(first_reaction_date, first_reaction_time);
        let second_reaction_date = NaiveDate::from_ymd_opt(2025, 5, 2).unwrap();
        let second_reaction_time = NaiveTime::from_hms_opt(10, 1, 10).unwrap();
        let second_reaction_datetime =
            NaiveDateTime::new(second_reaction_date, second_reaction_time);
        let check_date = NaiveDate::from_ymd_opt(2025, 4, 1).unwrap();
        let check_time = NaiveTime::from_hms_opt(10, 1, 10).unwrap();
        let check_datetime = NaiveDateTime::new(check_date, check_time);
        let labels = SpamEntries::new(SpamEntry::WithoutSourceCommit((SpamScore::Zero, label)));

        let user = User {
            fid: 2,
            labels: Some(labels),
            reaction_times: Some(vec![first_reaction_datetime, second_reaction_datetime]),
            latest_reaction_time_update_date: Some(check_datetime),
            cast_records: None,
            latest_cast_record_check_date: None,
        };
        #[allow(deprecated)]
        users.push_with_res(user).unwrap();
        users
    }

    #[test]
    pub fn test_spam_score_collision_error_for_invalid_record_add() {
        let date = NaiveDate::from_ymd_opt(2020, 1, 2).unwrap();
        let later_date = NaiveDate::from_ymd_opt(2020, 1, 3).unwrap();
        let earlier_date = NaiveDate::from_ymd_opt(2020, 1, 1).unwrap();

        let spam_record = (SpamScore::One, date);
        let entries = SpamEntries::new(SpamEntry::WithoutSourceCommit(spam_record));
        let mut user = User {
            fid: 1,
            labels: Some(entries),
            cast_records: None,
            latest_cast_record_check_date: None,
            reaction_times: None,
            latest_reaction_time_update_date: None,
        };

        assert!(user.add_spam_record((SpamScore::Zero, date)).is_err());
        assert!(user.add_spam_record((SpamScore::One, date)).is_ok());
        assert_eq!(user.all_spam_records_with_opt().unwrap().len(), 1);
        assert!(user.add_spam_record((SpamScore::Two, later_date)).is_ok());
        assert_eq!(user.all_spam_records_with_opt().unwrap().len(), 2);

        //make sure spam_records are sorted
        assert_eq!(
            user.all_spam_records_with_opt().unwrap().first().unwrap().1,
            date
        );
        assert_eq!(
            user.all_spam_records_with_opt().unwrap().last().unwrap().1,
            later_date
        );

        assert!(user
            .add_spam_record((SpamScore::Zero, earlier_date))
            .is_ok());
        assert_eq!(
            user.all_spam_records_with_opt().unwrap().first().unwrap(),
            &(SpamScore::Zero, earlier_date)
        );
        assert_eq!(
            user.all_spam_records_with_opt().unwrap()[1],
            (SpamScore::One, date)
        );
        assert_eq!(
            user.all_spam_records_with_opt().unwrap().last().unwrap(),
            &(SpamScore::Two, later_date)
        );
    }

    #[test]
    pub fn test_dummy_data_import_with_new() {
        let fid = 1;
        let db_path = PathBuf::from("data/dummy-data_db.json");
        let users = UserCollection::create_from_db(&db_path).unwrap();
        assert_eq!(users.spam_score_by_fid(fid).unwrap(), SpamScore::Zero);
        let user = users.user(fid).unwrap();
        assert_eq!(
            user.earliest_spam_score_date_with_opt().unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap()
        );

        assert_eq!(
            user.latest_spam_score_update_date_with_opt().unwrap(),
            NaiveDate::from_ymd_opt(2025, 1, 23).unwrap()
        );
    }

    #[test]
    pub fn test_user_created_after_date_on_dummy_data_with_new() {
        let db_path = PathBuf::from("data/dummy-data_db.json");
        let users = UserCollection::create_from_db(&db_path).unwrap();
        let user = users.user(1).unwrap();

        assert!(check_created_at_or_after_date(user, 2023, 1, 1));
        assert!(check_created_at_or_after_date(user, 2024, 1, 1));
        assert!(!check_created_at_or_after_date(user, 2024, 6, 1));
        assert!(!check_created_at_or_after_date(user, 2025, 1, 1));
    }

    #[test]
    pub fn test_spam_score_by_date_on_dummy_data_with_new() {
        let db_path = PathBuf::from("data/dummy-data_db.json");
        let users = UserCollection::create_from_db(&db_path).unwrap();
        let user = users.user(1).unwrap();
        check_spam_score_at_date(user, 2023, 1, 25, None);
        check_spam_score_at_date(user, 2024, 1, 25, Some(SpamScore::One));
        check_spam_score_at_date(user, 2025, 1, 20, Some(SpamScore::One));
        check_spam_score_at_date(user, 2025, 1, 23, Some(SpamScore::Zero));
        check_spam_score_at_date(user, 2025, 1, 25, Some(SpamScore::Zero));
    }

    #[test]
    fn test_created_by_before_date_with_new() {
        let db_path = PathBuf::from("data/dummy-data_db.json");
        let users = UserCollection::create_from_db(&db_path).unwrap();
        let user = users.user(1).unwrap();
        assert!(!check_created_at_or_before_date(user, 2023, 12, 31));
        assert!(check_created_at_or_before_date(user, 2024, 1, 1));
        assert!(check_created_at_or_before_date(user, 2024, 1, 2));
        assert!(check_created_at_or_before_date(user, 2024, 1, 2));
        assert!(check_created_at_or_before_date(user, 2025, 12, 31));

        let user = users.user(2).unwrap();
        assert!(!check_created_at_or_before_date(user, 2023, 1, 31));
        assert!(!check_created_at_or_before_date(user, 2024, 1, 1));
        assert!(!check_created_at_or_before_date(user, 2024, 1, 2));
        assert!(!check_created_at_or_before_date(user, 2025, 1, 22));
        assert!(check_created_at_or_before_date(user, 2025, 1, 23));
        assert!(check_created_at_or_before_date(user, 2025, 1, 24));
        assert!(check_created_at_or_before_date(user, 2025, 12, 31));
    }

    #[test]
    fn test_latest_reaction_time() {
        let users = dummy_data_users_with_reaction_times();
        let user = users.user(1).unwrap();
        check_latest_reaction_time(
            user,
            NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2025, 2, 1).unwrap(),
                NaiveTime::from_hms_opt(10, 1, 10).unwrap(),
            ),
        );

        let user = users.user(2).unwrap();
        check_latest_reaction_time(
            user,
            NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2025, 5, 2).unwrap(),
                NaiveTime::from_hms_opt(10, 1, 10).unwrap(),
            ),
        );
    }
}