···
···
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/popular'
13
-
if os.path.isdir('/dev/shm/'):
14
-
os.makedirs('/dev/shm/feedgens/', exist_ok=True)
15
-
db_fname = '/dev/shm/feedgens/popular.db'
17
-
db_fname = 'db/popular.db'
19
-
self.db_cnx = apsw.Connection(db_fname)
14
+
self.db_cnx = apsw.Connection('db/popular.db')
self.db_cnx.pragma('journal_mode', 'WAL')
21
-
self.db_cnx.pragma('synchronous', 'OFF')
self.db_cnx.pragma('wal_autocheckpoint', '0')
···
if collection != 'app.bsky.feed.like':
35
+
record = op['record']
39
+
ts = self.safe_timestamp(record['createdAt']).timestamp()
like_subject_uri = op['record']['subject']['uri']
45
-
self.db_cnx.execute((
46
-
"insert into posts (uri, create_ts, update_ts, temperature) "
47
-
"values (:uri, :ts, :ts, 1) "
48
-
"on conflict (uri) do update set temperature = temperature + 1, update_ts = :ts"
49
-
), dict(uri=like_subject_uri, ts=ts))
42
+
self.transaction_begin(self.db_cnx)
51
-
def run_tasks_minute(self):
52
-
self.logger.debug('running minute tasks')
44
+
self.db_cnx.execute("""
45
+
insert into posts (uri, create_ts, update_ts, temperature)
46
+
values (:uri, :ts, :ts, 1)
47
+
on conflict (uri) do update set temperature = temperature + 1, update_ts = :ts
48
+
""", dict(uri=like_subject_uri, ts=ts))
55
-
self.db_cnx.execute(
56
-
"delete from posts where temperature * exp( -1 * ( ( strftime( '%s', 'now' ) - strftime( '%s', create_ts ) ) / 1800.0 ) ) < 1.0 and strftime( '%s', create_ts ) < strftime( '%s', 'now', '-15 minutes' )"
50
+
def delete_old_posts(self):
51
+
self.db_cnx.execute("""
54
+
temperature * exp( -1 * ( ( unixepoch('now') - create_ts ) / 1800.0 ) ) < 1.0
55
+
and create_ts < unixepoch('now', '-15 minutes')
59
-
self.db_cnx.pragma('wal_checkpoint(TRUNCATE)')
58
+
def commit_changes(self):
59
+
self.logger.debug('committing changes')
60
+
self.delete_old_posts()
61
+
self.transaction_commit(self.db_cnx)
62
+
self.wal_checkpoint(self.db_cnx, 'RESTART')
def serve_feed(self, limit, offset, langs):
62
-
cur = self.db_cnx.execute((
63
-
"select uri from posts "
64
-
"order by temperature * exp( "
65
-
"-1 * ( ( strftime( '%s', 'now' ) - strftime( '%s', create_ts ) ) / 1800.0 ) "
66
-
") desc limit :limit offset :offset"
67
-
), dict(limit=limit, offset=offset))
65
+
cur = self.db_cnx.execute("""
66
+
select uri from posts
67
+
order by temperature * exp( -1 * ( ( unixepoch('now') - create_ts ) / 1800.0 ) )
68
+
desc limit :limit offset :offset
69
+
""", dict(limit=limit, offset=offset))
return [uri for (uri,) in cur]
72
+
def serve_feed_debug(self, limit, offset, langs):
76
+
unixepoch('now') - create_ts as age_seconds,
78
+
-1 * ( ( unixepoch('now') - create_ts ) / 1800.0 )
81
+
-1 * ( ( unixepoch('now') - create_ts ) / 1800.0 )
85
+
limit :limit offset :offset
87
+
bindings = dict(limit=limit, offset=offset)
88
+
return apsw.ext.format_query_table(
89
+
self.db_cnx, query, bindings,
90
+
string_sanitize=2, text_width=9999, use_unicode=True