summaryrefslogtreecommitdiffstats
path: root/src/db.rs
blob: 27737cbf163359775371a256c583d40848f4c1fe (plain) (blame)
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
use lastfm;

use failure::Fail;

const SCHEMA: &'static str = "
    CREATE TABLE `tracks` (
        artist varchar(1024) NOT NULL,
        album varchar(1024) DEFAULT NULL,
        name varchar(1024) NOT NULL,
        timestamp integer(11) NOT NULL
    );
    CREATE VIEW `yearly_tracks` as
        SELECT *
        FROM tracks
        WHERE strftime('%s') - timestamp < 60*60*24*365;
    CREATE VIEW `monthly_tracks` as
        SELECT *
        FROM tracks
        WHERE strftime('%s') - timestamp < 60*60*24*30;
    CREATE VIEW `weekly_tracks` as
        SELECT *
        FROM tracks
        WHERE strftime('%s') - timestamp < 60*60*24*7;
";

#[derive(Eq, PartialEq, Copy, Clone)]
pub enum TimeWindow {
    All,
    Yearly,
    Monthly,
    Weekly,
    None,
}

pub struct DB {
    conn: rusqlite::Connection,
}

impl DB {
    pub fn new<P: AsRef<std::path::Path>>(path: &P) -> failure::Fallible<DB> {
        let conn = if path.as_ref().exists() {
            rusqlite::Connection::open(path).map_err(|e| {
                let msg = format!(
                    "couldn't open db at {}",
                    path.as_ref().display()
                );
                e.context(msg)
            })?
        } else {
            Self::create(path)?
        };

        return Ok(DB { conn });
    }

    fn create<P: AsRef<std::path::Path>>(
        path: &P,
    ) -> failure::Fallible<rusqlite::Connection> {
        eprintln!(
            "Initializing database at {}",
            path.as_ref().to_string_lossy(),
        );

        if let Some(parent) = path.as_ref().parent() {
            std::fs::create_dir_all(parent)?;
            let conn = rusqlite::Connection::open(path).map_err(|e| {
                let msg = format!(
                    "couldn't create db at {}",
                    path.as_ref().display()
                );
                e.context(msg)
            })?;
            conn.execute(SCHEMA, rusqlite::NO_PARAMS)
                .map_err(|e| e.context("failed to execute schema"))?;
            Ok(conn)
        } else {
            unimplemented!();
        }
    }

    pub fn most_recent_timestamp(&self) -> failure::Fallible<Option<i64>> {
        Ok(self
            .conn
            .query_row(
                "SELECT timestamp FROM tracks ORDER BY timestamp DESC LIMIT 1",
                rusqlite::NO_PARAMS,
                |row| Some(row.get(0)),
            )
            .or_else(|e| match e {
                rusqlite::Error::QueryReturnedNoRows => Ok(None),
                _ => Err(e),
            })?)
    }

    pub fn insert_tracks(
        &self,
        tracks: impl Iterator<Item = lastfm::Track>,
    ) -> failure::Fallible<()> {
        let mut sth = self
            .conn
            .prepare("INSERT INTO tracks VALUES (?, ?, ?, ?)")?;
        for track in tracks {
            sth.execute(&[
                &track.artist as &rusqlite::types::ToSql,
                &track.album,
                &track.name,
                &track.timestamp,
            ])
            .map(|_| ())?;
        }
        Ok(())
    }

    pub fn query<F: FnMut(&rusqlite::Row)>(
        &self,
        query: &str,
        mut f: F,
    ) -> failure::Fallible<Vec<String>> {
        let mut sth = self.conn.prepare(query)?;

        let cols = sth.column_names().iter().map(|s| s.to_string()).collect();

        let rows = sth.query_and_then(rusqlite::NO_PARAMS, |row| {
            f(row);
            Ok(())
        })?;
        // this call to collect() forces it to actually consume the iterator
        // (and therefore call the callbacks). what i really want here is for
        // there to be a query_for_each or something like that, but the weird
        // way lifetimes work for rows makes it difficult to emulate this any
        // other way
        let errs: failure::Fallible<Vec<()>> = rows.collect();

        errs.map(|_| cols)
    }

    pub fn recommend_artists(
        &self,
        count: u64,
        random: bool,
        include: TimeWindow,
        exclude: TimeWindow,
    ) -> failure::Fallible<Vec<String>> {
        let exclude = if exclude != TimeWindow::None {
            format!(
                "
                WHERE artist NOT IN (
                    SELECT DISTINCT(artist)
                    FROM {}
                )
                ",
                timewindow_table(&exclude)
            )
        } else {
            "".to_string()
        };
        let order = if random {
            "ORDER BY random()"
        } else {
            "ORDER BY count(artist) * (strftime('%s') - max(timestamp)) DESC"
        };

        let sql = format!(
            "
            SELECT artist
            FROM {}
            {}
            GROUP BY artist
            {}
            LIMIT {}
            ",
            timewindow_table(&include),
            exclude,
            order,
            count
        );
        let mut sth = self.conn.prepare(&sql)?;
        let artists = sth
            .query_and_then(rusqlite::NO_PARAMS, |row| {
                Ok(row.get_checked(0)?)
            })?
            .collect::<failure::Fallible<Vec<String>>>()?;

        Ok(artists)
    }

    pub fn recommend_album(
        &self,
        artist: &str,
        random: bool,
        include: TimeWindow,
        exclude: TimeWindow,
    ) -> failure::Fallible<String> {
        let mut params = vec![artist];
        let exclude = if exclude != TimeWindow::None {
            params.push(artist);
            format!(
                "
                AND album NOT IN (
                    SELECT DISTINCT(album)
                    FROM {}
                    WHERE artist = ?
                )
                ",
                timewindow_table(&exclude)
            )
        } else {
            "".to_string()
        };
        let order = if random {
            "ORDER BY random()"
        } else {
            "ORDER BY count(album) * (strftime('%s') - max(timestamp)) DESC"
        };

        let sql = format!(
            "
            SELECT album
            FROM {}
            WHERE artist = ?
            {}
            GROUP BY album
            {}
            LIMIT 1
            ",
            timewindow_table(&include),
            exclude,
            order
        );
        let mut sth = self.conn.prepare(&sql)?;
        let artists = sth.query_row::<failure::Fallible<String>, _, _>(
            &params,
            |row| Ok(row.get_checked(0)?),
        )??;

        Ok(artists)
    }
}

pub fn parse_timewindow(s: &str) -> TimeWindow {
    match s {
        "all" => TimeWindow::All,
        "yearly" => TimeWindow::Yearly,
        "monthly" => TimeWindow::Monthly,
        "weekly" => TimeWindow::Weekly,
        "none" => TimeWindow::None,
        _ => unreachable!(),
    }
}

fn timewindow_table(tw: &TimeWindow) -> String {
    match tw {
        TimeWindow::All => "tracks".to_string(),
        TimeWindow::Yearly => "yearly_tracks".to_string(),
        TimeWindow::Monthly => "monthly_tracks".to_string(),
        TimeWindow::Weekly => "weekly_tracks".to_string(),
        _ => unreachable!(),
    }
}