Implemented rank + formatted
This commit is contained in:
parent
d2007a731a
commit
aef307bbac
@ -2,14 +2,17 @@ db_path = "./drstats.db"
|
|||||||
import dataset
|
import dataset
|
||||||
from drstats.duration import Duration
|
from drstats.duration import Duration
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = dataset.connect(f"sqlite:///{db_path}")
|
db = dataset.connect(f"sqlite:///{db_path}")
|
||||||
|
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
DROP VIEW IF EXISTS score_ignored_most_last_7_days
|
DROP VIEW IF EXISTS score_ignored_most_last_7_days
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW score_ignored_most_last_7_days AS SELECT
|
CREATE VIEW score_ignored_most_last_7_days AS SELECT
|
||||||
user_username AS username,
|
user_username AS username,
|
||||||
COUNT(score) AS userscore
|
COUNT(score) AS userscore
|
||||||
@ -18,20 +21,24 @@ WHERE score = 0
|
|||||||
AND created >= DATE('now', '-7 days')
|
AND created >= DATE('now', '-7 days')
|
||||||
GROUP BY username
|
GROUP BY username
|
||||||
ORDER BY userscore DESC
|
ORDER BY userscore DESC
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS score_last_7_days")
|
db.query("DROP VIEW IF EXISTS score_last_7_days")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW score_last_7_days AS SELECT
|
CREATE VIEW score_last_7_days AS SELECT
|
||||||
user_username AS username,
|
user_username AS username,
|
||||||
SUM(score) AS userscore
|
SUM(score) AS userscore
|
||||||
FROM comments
|
FROM comments
|
||||||
GROUP BY user_username
|
GROUP BY user_username
|
||||||
ORDER BY userscore DESC
|
ORDER BY userscore DESC
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS rant_stats_per_day")
|
db.query("DROP VIEW IF EXISTS rant_stats_per_day")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW rant_stats_per_day AS SELECT
|
CREATE VIEW rant_stats_per_day AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
DATE(created) AS created_date,
|
DATE(created) AS created_date,
|
||||||
@ -47,10 +54,12 @@ CREATE VIEW rant_stats_per_day AS SELECT
|
|||||||
FROM rants
|
FROM rants
|
||||||
GROUP BY created_date
|
GROUP BY created_date
|
||||||
ORDER BY created_date
|
ORDER BY created_date
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS comment_stats_per_day")
|
db.query("DROP VIEW IF EXISTS comment_stats_per_day")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW comment_stats_per_day AS SELECT
|
CREATE VIEW comment_stats_per_day AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
DATE(created) AS created_date,
|
DATE(created) AS created_date,
|
||||||
@ -66,10 +75,12 @@ CREATE VIEW comment_stats_per_day AS SELECT
|
|||||||
FROM comments
|
FROM comments
|
||||||
GROUP BY created_date
|
GROUP BY created_date
|
||||||
ORDER BY created_date
|
ORDER BY created_date
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS rant_stats_per_weekday")
|
db.query("DROP VIEW IF EXISTS rant_stats_per_weekday")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW rant_stats_per_weekday AS SELECT
|
CREATE VIEW rant_stats_per_weekday AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
DATE(created) AS created_date,
|
DATE(created) AS created_date,
|
||||||
@ -85,10 +96,12 @@ CREATE VIEW rant_stats_per_weekday AS SELECT
|
|||||||
FROM rants
|
FROM rants
|
||||||
GROUP BY weekday
|
GROUP BY weekday
|
||||||
ORDER BY created_date
|
ORDER BY created_date
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS comment_stats_per_weekday")
|
db.query("DROP VIEW IF EXISTS comment_stats_per_weekday")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW comment_stats_per_weekday AS SELECT
|
CREATE VIEW comment_stats_per_weekday AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
DATE(created) AS created_date,
|
DATE(created) AS created_date,
|
||||||
@ -104,38 +117,50 @@ CREATE VIEW comment_stats_per_weekday AS SELECT
|
|||||||
FROM comments
|
FROM comments
|
||||||
GROUP BY weekday
|
GROUP BY weekday
|
||||||
ORDER BY created_date
|
ORDER BY created_date
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS comment_stats_per_hour")
|
db.query("DROP VIEW IF EXISTS comment_stats_per_hour")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW comment_stats_per_hour AS SELECT
|
CREATE VIEW comment_stats_per_hour AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
strftime('%H', created) AS hour
|
strftime('%H', created) AS hour
|
||||||
FROM comments
|
FROM comments
|
||||||
GROUP BY hour
|
GROUP BY hour
|
||||||
ORDER BY hour
|
ORDER BY hour
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("DROP VIEW IF EXISTS rant_stats_per_hour")
|
db.query("DROP VIEW IF EXISTS rant_stats_per_hour")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW rant_stats_per_hour AS SELECT
|
CREATE VIEW rant_stats_per_hour AS SELECT
|
||||||
COUNT(0) AS count,
|
COUNT(0) AS count,
|
||||||
strftime('%H', created) AS hour
|
strftime('%H', created) AS hour
|
||||||
FROM rants
|
FROM rants
|
||||||
GROUP BY hour
|
GROUP BY hour
|
||||||
ORDER BY hour
|
ORDER BY hour
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
DROP VIEW IF EXISTS user_stats
|
DROP VIEW IF EXISTS user_stats
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW user_stats AS
|
CREATE VIEW user_stats AS
|
||||||
SELECT
|
SELECT
|
||||||
user_username AS username,
|
user_username AS username,
|
||||||
COUNT(0) AS post_count,
|
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,
|
DATE(comments.created) AS date,
|
||||||
(SELECT COUNT(0)
|
(SELECT COUNT(0)
|
||||||
FROM comments AS comments2
|
FROM comments AS comments2
|
||||||
@ -152,25 +177,34 @@ ORDER BY hour
|
|||||||
FROM comments
|
FROM comments
|
||||||
GROUP BY username, DATE(comments.created)
|
GROUP BY username, DATE(comments.created)
|
||||||
ORDER BY username ASC, date ASC;
|
ORDER BY username ASC, date ASC;
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
db.query("DROP VIEW IF EXISTS contributions")
|
db.query("DROP VIEW IF EXISTS contributions")
|
||||||
db.query("""
|
db.query(
|
||||||
|
"""
|
||||||
CREATE VIEW contributions AS
|
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
|
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
|
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
|
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
|
group by username
|
||||||
order by contributions desc, username asc
|
order by contributions desc, username asc
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
db.query("DROP VIEW IF EXISTS contributions_extended")
|
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
|
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
|
FROM contributions
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
db.query("DROP VIEW IF EXISTS rants_of_user")
|
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("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
|
return db
|
||||||
|
|
||||||
@ -188,12 +222,10 @@ class Db:
|
|||||||
with Duration("DB Query {}".format(str[:80])):
|
with Duration("DB Query {}".format(str[:80])):
|
||||||
return self.db.query(str)
|
return self.db.query(str)
|
||||||
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
self.db = None
|
self.db = None
|
||||||
|
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
self.db = get_db()
|
self.db = get_db()
|
||||||
return self
|
return self
|
||||||
@ -202,7 +234,6 @@ class Db:
|
|||||||
with Duration("DB Query {}".format(str[:80])):
|
with Duration("DB Query {}".format(str[:80])):
|
||||||
return self.db.query(str)
|
return self.db.query(str)
|
||||||
|
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
self.db = None
|
self.db = None
|
||||||
@ -210,33 +241,43 @@ class Db:
|
|||||||
|
|
||||||
def get_contributions():
|
def get_contributions():
|
||||||
with Db() as db:
|
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)
|
return list(contributions)
|
||||||
|
|
||||||
|
|
||||||
def get_upvote_average():
|
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():
|
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():
|
def get_user_count():
|
||||||
return len(get_users())
|
return len(get_users())
|
||||||
|
|
||||||
|
|
||||||
def get_contribution_count():
|
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():
|
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):
|
def get_all_rants_of_user(username):
|
||||||
with Db() as db:
|
with Db() as db:
|
||||||
try:
|
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:
|
except TypeError:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_all_posts_of_user(username):
|
def get_all_posts_of_user(username):
|
||||||
with Db() as db:
|
with Db() as db:
|
||||||
try:
|
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:
|
except TypeError:
|
||||||
return ""
|
return ""
|
Loading…
Reference in New Issue
Block a user