Added support for view exports

This commit is contained in:
retoor 2024-11-24 07:38:17 +01:00
parent 403ac07661
commit aeb6b1a635
12 changed files with 24 additions and 112 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -2,6 +2,7 @@ from drstats import db
import functools import functools
import sys import sys
import pathlib import pathlib
import json
printr = functools.partial(print, file=sys.stderr) printr = functools.partial(print, file=sys.stderr)
@ -35,7 +36,7 @@ def dump():
"```", "```",
) )
printr(text) printr(text)
with pathlib.Path("export/rants-" + user + ".txt").open("w") as f: with pathlib.Path("export/rants-" + user + ".txt").open("w+") as f:
f.write(user) f.write(user)
f.write(" said ") f.write(" said ")
f.write(text) f.write(text)
@ -53,7 +54,7 @@ def dump():
"```", "```",
) )
printr(text) printr(text)
with pathlib.Path("export/posts-" + user + ".txt").open("w") as f: with pathlib.Path("export/posts-" + user + ".txt").open("w+") as f:
f.write(user) f.write(user)
f.write(" said ") f.write(" said ")
f.write(text) f.write(text)
@ -68,3 +69,8 @@ def dump():
printr(line) printr(line)
print(line) print(line)
print("```") print("```")
for view in db.get_views():
print(f"export/view-f{view['name']}.json")
with pathlib.Path(f"export/view-f{view['name']}.json").open("w+") as f:
json.dump(view, db.query(view['sql']))

View File

@ -205,6 +205,9 @@ def get_db():
db.query( db.query(
"CREATE VIEW posts_of_user AS SELECT user_username as username, GROUP_CONCAT(body) as text FROM comments" "CREATE VIEW posts_of_user AS SELECT user_username as username, GROUP_CONCAT(body) as text FROM comments"
) )
db.query("DROP VIEW IF EXISTS contributions_extended_ranked")
db.query("CREATE VIEW contributions_extended_ranked AS SELECT ROW_NUMBER() OVER (ORDER BY upvote_ratio DESC) as rank, * FROM contributions_extended ORDER BY upvote_ratio DESC")
db.query("CREATE VIEW IF NOT EXISTS views AS SELECT sql, name FROM sqlite_schema WHERE type='view' AND name != 'views';")
return db return db
@ -239,10 +242,14 @@ class Db:
self.db = None self.db = None
def get_views():
with Db() as db:
return list(db.query("SELECT * FROM views;"))
def get_contributions(): def get_contributions():
with Db() as db: with Db() as db:
contributions = db.query( contributions = db.query(
"SELECT ROW_NUMBER() OVER (ORDER BY upvote_ratio DESC) as rank, * FROM contributions_extended ORDER BY upvote_ratio DESC" "SELECT * FROM contributions_extended_ranked"
) )
return list(contributions) return list(contributions)
@ -252,8 +259,15 @@ def get_upvote_average():
def get_users(): def get_users():
return list(set([user["username"] for user in get_contributions()])) """
Retrieve a list of distinct usernames from the contributions table.
Returns:
list: A list of usernames in ascending order.
"""
with Db() as db:
return [user["username"] for user in db.query("SELECT DISTINCT username FROM contributions ORDER BY username")]
def get_user_count(): def get_user_count():
return len(get_users()) return len(get_users())