feat: add view export functionality and refactor contributions query to use named view
Introduce a new `get_views()` function in db.py that queries the sqlite_schema for user-defined views, and add a loop in dataset.py to export each view's query results as a JSON file. Also create a persistent `contributions_extended_ranked` view to replace the inline subquery in `get_contributions()`, and refactor `get_users()` to use a direct DISTINCT query instead of deduplicating in Python.
This commit is contained in:
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
BIN
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 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,6 +2,7 @@ from drstats import db
|
||||
import functools
|
||||
import sys
|
||||
import pathlib
|
||||
import json
|
||||
|
||||
printr = functools.partial(print, file=sys.stderr)
|
||||
|
||||
@@ -35,7 +36,7 @@ def dump():
|
||||
"```",
|
||||
)
|
||||
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(" said ")
|
||||
f.write(text)
|
||||
@@ -53,7 +54,7 @@ def dump():
|
||||
"```",
|
||||
)
|
||||
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(" said ")
|
||||
f.write(text)
|
||||
@@ -68,3 +69,8 @@ def dump():
|
||||
printr(line)
|
||||
print(line)
|
||||
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']))
|
||||
+16
-2
@@ -205,6 +205,9 @@ def get_db():
|
||||
db.query(
|
||||
"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
|
||||
|
||||
@@ -239,10 +242,14 @@ class Db:
|
||||
self.db = None
|
||||
|
||||
|
||||
def get_views():
|
||||
with Db() as db:
|
||||
return list(db.query("SELECT * FROM views;"))
|
||||
|
||||
def get_contributions():
|
||||
with Db() as db:
|
||||
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)
|
||||
|
||||
@@ -252,8 +259,15 @@ def get_upvote_average():
|
||||
|
||||
|
||||
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():
|
||||
return len(get_users())
|
||||
|
||||
Reference in New Issue
Block a user