-
Notifications
You must be signed in to change notification settings - Fork 8
/
db_utils.py
83 lines (66 loc) · 2.5 KB
/
db_utils.py
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
#!/usr/bin/env python3
import json
import os
from dateutil import parser
import psycopg2.extras
SCHEMA = """
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS tsm_system_rows;
CREATE TABLE IF NOT EXISTS streams (
id TEXT UNIQUE PRIMARY KEY,
time INTEGER NOT NULL DEFAULT extract(epoch from now() at time zone 'utc'),
viewer_count INTEGER,
title_block TEXT,
game TEXT,
generation INTEGER,
streamstart TIMESTAMP,
data TEXT
);
ALTER TABLE streams ADD COLUMN IF NOT EXISTS title_block text;
ALTER TABLE streams ADD COLUMN IF NOT EXISTS generation INTEGER;
CREATE INDEX IF NOT EXISTS viewer_count ON streams (viewer_count);
CREATE INDEX IF NOT EXISTS lowercase_title_block ON streams (lower(title_block));
CREATE INDEX IF NOT EXISTS title_block_trgm ON streams USING gin (lower(title_block) gin_trgm_ops);
"""
conn = psycopg2.connect(
f"dbname='{os.environ.get('NOBODY_DATABASE')}' user='{os.environ.get('NOBODY_USER')}' "
f"host='{os.environ.get('NOBODY_HOST')}' password='{os.environ.get('NOBODY_PASSWORD')}'"
)
conn.autocommit = True
def migrate():
with conn.cursor() as cursor:
print("Migrating schema")
cursor.execute(SCHEMA)
def bulk_insert_streams(streams, generation):
if streams:
formatted_rows = []
for stream in streams:
# sometimes tags=None
joined_tags = ""
if stream["tags"]:
joined_tags = " ".join(stream["tags"]).strip()
formatted_rows.append(
(
stream["id"],
stream["game_name"],
f"{stream['game_name']} {joined_tags}",
stream["viewer_count"],
generation,
parser.parse(stream["started_at"]),
json.dumps(stream),
)
)
insert_query = """
INSERT INTO streams (id, game, title_block, viewer_count, generation, streamstart, data) values %s
ON CONFLICT(id) DO UPDATE
SET time=extract(epoch from now() at time zone 'utc');"""
with conn.cursor() as cursor:
psycopg2.extras.execute_values(
cursor, insert_query, formatted_rows, template=None, page_size=100
)
def prune_all_but_generation(generation):
delete_query = """
DELETE FROM streams
WHERE generation != %s OR generation IS NULL;"""
with conn.cursor() as cursor:
cursor.execute(delete_query, [generation])