termitype 0.0.11

Terminal-based typing test inspired by a certain typing test you might know.
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
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
use crate::{
    common::filesystem::config_dir,
    config::{Config, Setting},
    constants::db_file,
    error::{AppError, AppResult},
    log_debug, log_info,
    tracker::Tracker,
};
use chrono::{DateTime, Local};
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};

const SCHEMA_VERSION: i32 = 3;
const DEFAULT_LEADERBOARD_LIMIT: usize = 25;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LeaderboardResult {
    pub id: Option<i64>,
    pub mode_kind: String,
    pub mode_value: i32,
    pub language: String,
    pub wpm: u16,
    pub raw_wpm: u16,
    pub accuracy: u16,
    pub consistency: u16,
    pub error_count: u32,
    pub numbers: bool,
    pub symbols: bool,
    pub punctuation: bool,
    pub created_at: DateTime<Local>,
}

#[derive(Debug, Clone)]
pub enum LeaderboardColumn {
    ModeKind,
    ModeValue,
    Language,
    Wpm,
    RawWpm,
    Accuracy,
    Consistency,
    ErrorCount,
    Numbers,
    Symbols,
    Punctuation,
    CreatedAt,
}

impl LeaderboardColumn {
    pub fn to_value(&self) -> &'static str {
        match self {
            LeaderboardColumn::ModeKind => "mode_kind",
            LeaderboardColumn::ModeValue => "mode_value",
            LeaderboardColumn::Language => "language",
            LeaderboardColumn::Wpm => "wpm",
            LeaderboardColumn::RawWpm => "raw_wpm",
            LeaderboardColumn::Accuracy => "accuracy",
            LeaderboardColumn::Consistency => "consistency",
            LeaderboardColumn::ErrorCount => "error_count",
            LeaderboardColumn::Numbers => "numbers",
            LeaderboardColumn::Symbols => "symbols",
            LeaderboardColumn::Punctuation => "punctuation",
            LeaderboardColumn::CreatedAt => "created_at",
        }
    }
}

#[derive(Debug, Clone)]
pub enum SortOrder {
    Ascending,
    Descending,
}

impl SortOrder {
    pub fn to_value(&self) -> &'static str {
        match self {
            SortOrder::Ascending => "ASC",
            SortOrder::Descending => "DESC",
        }
    }
}

#[derive(Debug, Clone)]
pub struct LeaderboardState {
    pub count: usize,
    pub has_more: bool,
    pub data: Vec<LeaderboardResult>,
}

#[derive(Debug, Clone)]
pub struct LeaderboardQuery {
    pub limit: usize,
    pub offset: usize,
    pub sort_by: LeaderboardColumn, //  TODO: was `sort_col` must be an enum
    pub sort_order: SortOrder,
}

impl Default for LeaderboardQuery {
    fn default() -> Self {
        Self {
            limit: DEFAULT_LEADERBOARD_LIMIT,
            offset: 0,
            sort_by: LeaderboardColumn::CreatedAt,
            sort_order: SortOrder::Descending,
        }
    }
}

pub struct Db {
    conn: Connection,
}

impl Db {
    pub fn new(filename: &str) -> AppResult<Self> {
        let dir = config_dir()?;
        if !dir.exists() {
            std::fs::create_dir_all(&dir)?;
        }

        let path = dir.join(filename);
        let connection = Connection::open(&path)?;
        let mut db = Self { conn: connection };

        db.init()?;

        Ok(db)
    }

    #[cfg(test)]
    pub fn new_in_memory() -> AppResult<Self> {
        let connection = Connection::open_in_memory()?;
        let mut db = Self { conn: connection };

        db.init()?;

        Ok(db)
    }

    fn init(&mut self) -> AppResult<()> {
        self.conn.execute("PRAGMA foreign_keys = ON", [])?;

        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY)",
            [],
        )?;

        let current_version: i32 = self
            .conn
            .query_row(
                "SELECT version FROM schema_version ORDER BY version DESC LIMIT 1",
                [],
                |row| row.get(0),
            )
            .unwrap_or(0);

        if current_version < SCHEMA_VERSION {
            self.create()?;
            self.conn.execute(
                "INSERT OR REPLACE INTO schema_version (version) VALUES (?1)",
                params![SCHEMA_VERSION],
            )?;
            log_info!("DB: schema updated to version {}", SCHEMA_VERSION);
        }

        Ok(())
    }

    fn create(&mut self) -> AppResult<()> {
        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS results (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                mode_kind TEXT NOT NULL,
                mode_value INTEGER NOT NULL,
                language TEXT NOT NULL,
                wpm REAL NOT NULL,
                raw_wpm REAL DEFAULT 0,
                accuracy INTEGER NOT NULL,
                consistency INTEGER NOT NULL,
                error_count INTEGER NOT NULL,
                numbers BOOLEAN NOT NULL,
                punctuation BOOLEAN NOT NULL,
                symbols BOOLEAN NOT NULL,
                created_at TEXT NOT NULL
            )",
            [],
        )?;

        self.create_indexes()?;
        log_debug!("DB: tables and indexes created successfully");

        Ok(())
    }

    fn create_indexes(&mut self) -> AppResult<()> {
        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_filters ON results (
                mode_kind, mode_value, language, numbers, punctuation, symbols
            )",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_wpm ON results (wpm DESC)",
            [],
        )?;
        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_accuracy ON results (accuracy DESC)",
            [],
        )?;
        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_consistency ON results (consistency DESC)",
            [],
        )?;
        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_created_at ON results (created_at DESC)",
            [],
        )?;

        Ok(())
    }

    pub fn write(&mut self, config: &Config, tracker: &Tracker) -> AppResult<i64> {
        let current_mode = config.current_mode();
        let summary = tracker.summary();
        let result = LeaderboardResult {
            id: None,
            mode_kind: current_mode.kind().to_display(),
            mode_value: current_mode.value() as i32,
            language: config.current_language(),
            wpm: summary.wpm.round() as u16,
            raw_wpm: summary.raw_wpm().round() as u16,
            accuracy: (summary.accuracy * 100.0) as u16,
            consistency: summary.consistency as u16,
            error_count: summary.total_errors as u32,
            numbers: config.is_enabled(Setting::Numbers),
            symbols: config.is_enabled(Setting::Symbols),
            punctuation: config.is_enabled(Setting::Punctuation),
            created_at: Local::now(),
        };

        self.conn.execute(
            "INSERT INTO results (
                mode_kind,
                mode_value,
                language,
                wpm,
                raw_wpm,
                accuracy,
                consistency,
                error_count,
                numbers,
                symbols,
                punctuation,
                created_at
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
            params![
                result.mode_kind,
                result.mode_value,
                result.language,
                result.wpm,
                result.raw_wpm,
                result.accuracy,
                result.consistency,
                result.error_count,
                result.numbers,
                result.symbols,
                result.punctuation,
                result.created_at
            ],
        )?;

        let id = self.conn.last_insert_rowid();

        log_debug!("DB: saved test result to database with ID: '{id}'");

        Ok(id)
    }

    pub fn reset(&self) -> AppResult<usize> {
        let affected_rows = self.conn.execute("DELETE FROM results", [])?;
        log_info!("DB: reset database, deleted {affected_rows} results");
        Ok(affected_rows)
    }

    pub fn insert_dummy_result(&mut self, result: LeaderboardResult) -> AppResult<i64> {
        self.conn.execute(
            "INSERT INTO results (
                mode_kind,
                mode_value,
                language,
                wpm,
                raw_wpm,
                accuracy,
                consistency,
                error_count,
                numbers,
                symbols,
                punctuation,
                created_at
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
            params![
                result.mode_kind,
                result.mode_value,
                result.language,
                result.wpm,
                result.raw_wpm,
                result.accuracy,
                result.consistency,
                result.error_count,
                result.numbers,
                result.symbols,
                result.punctuation,
                result.created_at
            ],
        )?;

        Ok(self.conn.last_insert_rowid())
    }

    #[cfg(test)]
    pub fn insert_test_result(
        &mut self,
        mode_kind: &str,
        mode_value: i32,
        language: &str,
        wpm: u16,
        accuracy: u16,
    ) {
        let created_at_str = "2023-10-18T12:00:00+00:00";
        self.conn.execute(
            "INSERT INTO results (mode_kind, mode_value, language, wpm, raw_wpm, accuracy, consistency, error_count, numbers, symbols, punctuation, created_at)
             VALUES (?, ?, ?, ?, 0, ?, 100, 0, 0, 0, 0, ?)",
            params![mode_kind, mode_value, language, wpm, accuracy, created_at_str],
        ).unwrap();
    }

    pub fn query_data(&self, query: &LeaderboardQuery) -> AppResult<LeaderboardState> {
        if !self.is_valid_column(&query.sort_by) {
            return Err(AppError::TermiDB(format!(
                "Invalid sort column: {}",
                query.sort_by.to_value()
            )));
        }
        let sort_direction = query.sort_order.to_value();
        let sort_col = query.sort_by.to_value();
        let count: usize = self
            .conn
            .query_row("SELECT COUNT(*) FROM results", [], |row| row.get(0))?;

        let sql_payload = format!(
            "SELECT
                id,
                mode_kind,
                mode_value,
                language,
                wpm,
                raw_wpm,
                accuracy,
                consistency,
                error_count,
                numbers,
                symbols,
                punctuation,
                created_at
              FROM results
             ORDER BY {} {}
             LIMIT {} OFFSET {}",
            sort_col, sort_direction, query.limit, query.offset
        );

        let mut statement = self.conn.prepare(&sql_payload)?;

        let results: Result<Vec<LeaderboardResult>, rusqlite::Error> = statement
            .query_map([], |row| {
                let created_at: DateTime<Local> = row.get(12)?;

                Ok(LeaderboardResult {
                    id: Some(row.get(0)?),
                    mode_kind: row.get(1)?,
                    mode_value: row.get(2)?,
                    language: row.get(3)?,
                    wpm: row.get::<_, f64>(4)?.round() as u16,
                    raw_wpm: row.get::<_, f64>(5).unwrap_or(0.0).round() as u16,
                    accuracy: row.get(6)?,
                    consistency: row.get::<_, f64>(7)?.round() as u16,
                    error_count: row.get(8)?,
                    numbers: row.get(9)?,
                    symbols: row.get(10)?,
                    punctuation: row.get(11)?,
                    created_at,
                })
            })?
            .collect();
        let results = results?;
        let has_more = query.offset + results.len() < count;

        Ok(LeaderboardState {
            count,
            has_more,
            data: results,
        })
    }

    fn is_valid_column(&self, column: &LeaderboardColumn) -> bool {
        let col = column.to_value();
        let valid_cols = [
            "mode_kind",
            "mode_value",
            "language",
            "wpm",
            "raw_wpm",
            "accuracy",
            "consistency",
            "error_count",
            "numbers",
            "punctuation",
            "symbols",
            "created_at",
        ];

        valid_cols.contains(&col)
    }
}

pub fn reset_database() -> anyhow::Result<()> {
    match Db::new(db_file()) {
        Ok(db) => {
            db.reset()?;
            Ok(())
        }
        _ => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::config::Mode;

    fn create_test_db() -> Db {
        Db::new_in_memory().expect("Failed to create test database")
    }

    fn insert_test_result(
        db: &mut Db,
        mode_kind: &str,
        mode_value: i32,
        language: &str,
        wpm: u16,
        accuracy: u16,
    ) {
        let created_at_str = "2023-10-18T12:00:00+00:00";
        db.conn.execute(
            "INSERT INTO results (mode_kind, mode_value, language, wpm, raw_wpm, accuracy, consistency, error_count, numbers, symbols, punctuation, created_at)
             VALUES (?, ?, ?, ?, 0, ?, 100, 0, 0, 0, 0, ?)",
            params![mode_kind, mode_value, language, wpm, accuracy, created_at_str],
        ).unwrap();
    }

    #[test]
    fn test_save_results() {
        let mut db = create_test_db();
        let config = Config::default();
        let mut tracker = Tracker::new("test".to_string(), Mode::with_words(1));
        tracker.start_typing();
        for c in "test".chars() {
            tracker.type_char(c).unwrap()
        }

        tracker.complete();

        let id = db.write(&config, &tracker).unwrap();

        assert!(id > 0)
    }

    #[test]
    fn test_query_data() {
        let mut db = create_test_db();
        db.reset().unwrap();
        insert_test_result(&mut db, "time", 60, "english", 80, 95);
        insert_test_result(&mut db, "words", 25, "english", 70, 90);

        let query = LeaderboardQuery::default();
        let state = db.query_data(&query).unwrap();

        assert_eq!(state.count, 2);
        assert_eq!(state.data.len(), 2);
        assert!(!state.has_more);
    }

    #[test]
    fn test_query_data_sorting() {
        let mut db = create_test_db();
        db.reset().unwrap();
        insert_test_result(&mut db, "words", 25, "english", 70, 90);
        insert_test_result(&mut db, "time", 60, "english", 80, 95);

        let query = LeaderboardQuery {
            sort_by: LeaderboardColumn::Wpm,
            sort_order: SortOrder::Descending,
            ..Default::default()
        };
        let state = db.query_data(&query).unwrap();

        assert_eq!(state.data[0].wpm, 80);
        assert_eq!(state.data[1].wpm, 70);
    }

    #[test]
    fn test_query_data_limit_offset() {
        let mut db = create_test_db();
        // db.conn.execute("DELETE FROM results", []).unwrap();
        db.reset().unwrap();
        for i in 0..5 {
            insert_test_result(&mut db, "time", 60, "english", (50 + i) as u16, 90);
        }

        let query = LeaderboardQuery {
            limit: 2,
            offset: 0,
            ..Default::default()
        };
        let state = db.query_data(&query).unwrap();

        assert_eq!(state.count, 5);
        assert_eq!(state.data.len(), 2);
        assert!(state.has_more);

        let query2 = LeaderboardQuery {
            limit: 2,
            offset: 2,
            ..Default::default()
        };
        let state2 = db.query_data(&query2).unwrap();
        assert_eq!(state2.data.len(), 2);
        assert!(state2.has_more);
    }
}