-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathrw_devtool_stats_dump.py
More file actions
87 lines (80 loc) · 2.14 KB
/
rw_devtool_stats_dump.py
File metadata and controls
87 lines (80 loc) · 2.14 KB
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
#!/usr/bin/env python
import argparse
import os
import sys
import libs.db
import libs.config
import libs.log
parser = argparse.ArgumentParser(
description="Dumps data to /tmp/rwstats in CSV format from SQL. Dangerous script in many, many ways - needs Postgres superuser permissions."
)
parser.add_argument("--config", default=None)
parser.add_argument("--sid", type=int)
args = parser.parse_args()
# Before running this script you're going to need pseudo_encrypt in the database:
"""
CREATE OR REPLACE FUNCTION pseudo_encrypt(VALUE int) returns int AS $$
DECLARE
l1 int;
l2 int;
r1 int;
r2 int;
i int:=0;
BEGIN
l1:= (VALUE >> 16) & 65535;
r1:= VALUE & 65535;
WHILE i < 3 LOOP
l2 := r1;
r2 := l1 # ((((1366 * r1 + 150889) % 714025) / 714025.0) * 32767)::int;
l1 := l2;
r1 := r2;
i := i + 1;
END LOOP;
RETURN ((r1 << 16) + l1);
END;
$$ LANGUAGE plpgsql strict immutable;
"""
libs.config.load(args.config)
libs.log.init()
libs.db.connect()
tables = [
"r4_album_ratings",
"r4_album_sid",
"r4_albums",
"r4_artists",
"r4_election_entries",
"r4_elections",
"r4_groups",
"r4_listeners",
"r4_listener_counts",
"r4_one_ups",
"r4_request_history",
"r4_request_line",
"r4_request_store",
"r4_schedule",
"r4_song_artist",
"r4_song_group",
"r4_song_ratings",
"r4_song_sid",
"r4_songs",
"r4_vote_history",
]
if not os.path.exists("/tmp/rwstats"):
print("Make sure /tmp/rwstats exists and is world writable.")
else:
for table in tables:
print(table)
sys.stdout.flush()
query = libs.db.c.fetch_var(
"SELECT "
"'SELECT ' || "
"ARRAY_TO_STRING(ARRAY(SELECT CASE WHEN COLUMN_NAME::VARCHAR(50)='user_id' THEN 'pseudo_encrypt(user_id) AS user_id' ELSE COLUMN_NAME::VARCHAR(50) END "
"FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_NAME='%s' AND "
"COLUMN_NAME NOT IN ('col2') "
"ORDER BY ORDINAL_POSITION "
"), ', ') || ' FROM %s'" % (table, table)
)
libs.db.c.update(
"COPY (%s) TO '/tmp/rwstats/%s.csv' WITH CSV HEADER" % (query, table)
)