Implemented rank + formatted

This commit is contained in:
retoor 2024-11-24 07:13:53 +01:00
parent d2007a731a
commit aef307bbac

View File

@ -2,37 +2,44 @@ db_path = "./drstats.db"
import dataset
from drstats.duration import Duration
def get_db():
db = dataset.connect(f"sqlite:///{db_path}")
db.query("""
DROP VIEW IF EXISTS score_ignored_most_last_7_days
""")
db.query("""
CREATE VIEW score_ignored_most_last_7_days AS SELECT
db.query(
"""
DROP VIEW IF EXISTS score_ignored_most_last_7_days
"""
)
db.query(
"""
CREATE VIEW score_ignored_most_last_7_days AS SELECT
user_username AS username,
COUNT(score) AS userscore
FROM comments
WHERE score = 0
FROM comments
WHERE score = 0
AND created >= DATE('now', '-7 days')
GROUP BY username
ORDER BY userscore DESC
""")
GROUP BY username
ORDER BY userscore DESC
"""
)
db.query("DROP VIEW IF EXISTS score_last_7_days")
db.query("""
CREATE VIEW score_last_7_days AS SELECT
db.query(
"""
CREATE VIEW score_last_7_days AS SELECT
user_username AS username,
SUM(score) AS userscore
FROM comments
GROUP BY user_username
ORDER BY userscore DESC
""")
FROM comments
GROUP BY user_username
ORDER BY userscore DESC
"""
)
db.query("DROP VIEW IF EXISTS rant_stats_per_day")
db.query("""
CREATE VIEW rant_stats_per_day AS SELECT
db.query(
"""
CREATE VIEW rant_stats_per_day AS SELECT
COUNT(0) AS count,
DATE(created) AS created_date,
CASE strftime('%w', DATE(created))
@ -44,14 +51,16 @@ CREATE VIEW rant_stats_per_day AS SELECT
WHEN '5' THEN 'Friday'
WHEN '6' THEN 'Saturday'
END AS weekday
FROM rants
GROUP BY created_date
ORDER BY created_date
""")
FROM rants
GROUP BY created_date
ORDER BY created_date
"""
)
db.query("DROP VIEW IF EXISTS comment_stats_per_day")
db.query("""
CREATE VIEW comment_stats_per_day AS SELECT
db.query(
"""
CREATE VIEW comment_stats_per_day AS SELECT
COUNT(0) AS count,
DATE(created) AS created_date,
CASE strftime('%w', DATE(created))
@ -63,14 +72,16 @@ CREATE VIEW comment_stats_per_day AS SELECT
WHEN '5' THEN 'Friday'
WHEN '6' THEN 'Saturday'
END AS weekday
FROM comments
GROUP BY created_date
ORDER BY created_date
""")
FROM comments
GROUP BY created_date
ORDER BY created_date
"""
)
db.query("DROP VIEW IF EXISTS rant_stats_per_weekday")
db.query("""
CREATE VIEW rant_stats_per_weekday AS SELECT
db.query(
"""
CREATE VIEW rant_stats_per_weekday AS SELECT
COUNT(0) AS count,
DATE(created) AS created_date,
CASE strftime('%w', DATE(created))
@ -82,14 +93,16 @@ CREATE VIEW rant_stats_per_weekday AS SELECT
WHEN '5' THEN 'Friday'
WHEN '6' THEN 'Saturday'
END AS weekday
FROM rants
GROUP BY weekday
ORDER BY created_date
""")
FROM rants
GROUP BY weekday
ORDER BY created_date
"""
)
db.query("DROP VIEW IF EXISTS comment_stats_per_weekday")
db.query("""
CREATE VIEW comment_stats_per_weekday AS SELECT
db.query(
"""
CREATE VIEW comment_stats_per_weekday AS SELECT
COUNT(0) AS count,
DATE(created) AS created_date,
CASE strftime('%w', DATE(created))
@ -101,41 +114,53 @@ CREATE VIEW comment_stats_per_weekday AS SELECT
WHEN '5' THEN 'Friday'
WHEN '6' THEN 'Saturday'
END AS weekday
FROM comments
GROUP BY weekday
ORDER BY created_date
""")
FROM comments
GROUP BY weekday
ORDER BY created_date
"""
)
db.query("DROP VIEW IF EXISTS comment_stats_per_hour")
db.query("""
CREATE VIEW comment_stats_per_hour AS SELECT
db.query(
"""
CREATE VIEW comment_stats_per_hour AS SELECT
COUNT(0) AS count,
strftime('%H', created) AS hour
FROM comments
GROUP BY hour
ORDER BY hour
""")
FROM comments
GROUP BY hour
ORDER BY hour
"""
)
db.query("DROP VIEW IF EXISTS rant_stats_per_hour")
db.query("""
CREATE VIEW rant_stats_per_hour AS SELECT
db.query(
"""
CREATE VIEW rant_stats_per_hour AS SELECT
COUNT(0) AS count,
strftime('%H', created) AS hour
FROM rants
GROUP BY hour
ORDER BY hour
""")
FROM rants
GROUP BY hour
ORDER BY hour
"""
)
db.query("""
db.query(
"""
DROP VIEW IF EXISTS user_stats
""")
"""
)
db.query("""
db.query(
"""
CREATE VIEW user_stats AS
SELECT
user_username AS username,
COUNT(0) AS post_count,
(select count(0) from rants where rants.id = comments.rant_id and date(rants.created) = date(comments.created)) as rant_count,
(
select count(0) from rants where
rants.id = comments.rant_id
and date(rants.created) = date(comments.created)
) as rant_count,
DATE(comments.created) AS date,
(SELECT COUNT(0)
FROM comments AS comments2
@ -152,25 +177,34 @@ ORDER BY hour
FROM comments
GROUP BY username, DATE(comments.created)
ORDER BY username ASC, date ASC;
""")
"""
)
db.query("DROP VIEW IF EXISTS contributions")
db.query("""
db.query(
"""
CREATE VIEW contributions AS
select distinct user_username as username, count(0) as contributions,sum(score) as upvotes,avg(length(text)) as post_length_average, sum(length(text)) as content_length from rants
union
select distinct user_username as username, count(0) as contributions,sum(score) as upvotes, sum(length(body)) / count(0) as post_length_average, sum(length(body)) as content_length from comments
group by username
order by contributions desc, username asc
""")
"""
)
db.query("DROP VIEW IF EXISTS contributions_extended")
db.query("""
db.query(
"""
CREATE VIEW contributions_extended as SELECT username, contributions,ROUND(CAST(contributions AS REAL) / CAST((select contributions from contributions) AS REAL),2) as ownership, upvotes, ROUND(CAST(upvotes AS REAL) / CAST((SELECT SUM(upvotes) from contributions) AS REAL),2) upvotes_ownership, ROUND(CAST(upvotes AS REAL) / CAST(contributions AS REAL),2) upvote_ratio,content_length as post_length_total, ROUND(CAST(content_length AS REAL) / CAST((SELECT SUM(content_length) from contributions) AS REAL)) as ownership_content,post_length_average
FROM contributions
""")
"""
)
db.query("DROP VIEW IF EXISTS rants_of_user")
db.query("CREATE VIEW rants_of_user as SELECT user_username as username, GROUP_CONCAT(text) as text FROM rants")
db.query(
"CREATE VIEW rants_of_user as SELECT user_username as username, GROUP_CONCAT(text) as text FROM rants"
)
db.query("DROP VIEW IF EXISTS posts_of_user")
db.query("CREATE VIEW posts_of_user AS SELECT user_username as username, GROUP_CONCAT(body) as text FROM comments")
db.query(
"CREATE VIEW posts_of_user AS SELECT user_username as username, GROUP_CONCAT(body) as text FROM comments"
)
return db
@ -188,12 +222,10 @@ class Db:
with Duration("DB Query {}".format(str[:80])):
return self.db.query(str)
def __exit__(self, exc_type, exc_val, exc_tb):
self.db.close()
self.db = None
async def __aenter__(self):
self.db = get_db()
return self
@ -202,7 +234,6 @@ class Db:
with Duration("DB Query {}".format(str[:80])):
return self.db.query(str)
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.db.close()
self.db = None
@ -210,33 +241,43 @@ class Db:
def get_contributions():
with Db() as db:
contributions = db.query("SELECT ROW_NUMBER() OVER (ORDER BY upvote_ratio DESC) as popularity_postion, * FROM contributions_extended ORDER BY upvote_ratio DESC")
contributions = db.query(
"SELECT ROW_NUMBER() OVER (ORDER BY upvote_ratio DESC) as rank, * FROM contributions_extended ORDER BY upvote_ratio DESC"
)
return list(contributions)
def get_upvote_average():
return avg(contribution['upvote_ratio'] for contribution in get_contributions())
return avg(contribution["upvote_ratio"] for contribution in get_contributions())
def get_users():
return list(set([user['username'] for user in get_contributions()]))
return list(set([user["username"] for user in get_contributions()]))
def get_user_count():
return len(get_users())
def get_contribution_count():
return sum(user['contributions'] for user in get_contributions())
return sum(user["contributions"] for user in get_contributions())
def get_contribution_average_per_user():
return round(get_contribution_count() / get_user_count(),2)
return round(get_contribution_count() / get_user_count(), 2)
def get_all_rants_of_user(username):
with Db() as db:
try:
return db.db['rants_of_user'].find_one(username=username)['text']
return db.db["rants_of_user"].find_one(username=username)["text"]
except TypeError:
return ""
def get_all_posts_of_user(username):
with Db() as db:
try:
return db.db['posts_of_user'].find_one(username=username)['text']
return db.db["posts_of_user"].find_one(username=username)["text"]
except TypeError:
return ""