Implemented rank + formatted
This commit is contained in:
parent
d2007a731a
commit
aef307bbac
@ -2,176 +2,210 @@ 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("""
|
)
|
||||||
CREATE VIEW score_ignored_most_last_7_days AS SELECT
|
db.query(
|
||||||
user_username AS username,
|
"""
|
||||||
COUNT(score) AS userscore
|
CREATE VIEW score_ignored_most_last_7_days AS SELECT
|
||||||
FROM comments
|
user_username AS username,
|
||||||
WHERE score = 0
|
COUNT(score) AS userscore
|
||||||
AND created >= DATE('now', '-7 days')
|
FROM comments
|
||||||
GROUP BY username
|
WHERE score = 0
|
||||||
ORDER BY userscore DESC
|
AND created >= DATE('now', '-7 days')
|
||||||
""")
|
GROUP BY username
|
||||||
|
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
|
"""
|
||||||
user_username AS username,
|
CREATE VIEW score_last_7_days AS SELECT
|
||||||
SUM(score) AS userscore
|
user_username AS username,
|
||||||
FROM comments
|
SUM(score) AS userscore
|
||||||
GROUP BY user_username
|
FROM comments
|
||||||
ORDER BY userscore DESC
|
GROUP BY user_username
|
||||||
""")
|
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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW rant_stats_per_day AS SELECT
|
||||||
DATE(created) AS created_date,
|
COUNT(0) AS count,
|
||||||
CASE strftime('%w', DATE(created))
|
DATE(created) AS created_date,
|
||||||
WHEN '0' THEN 'Sunday'
|
CASE strftime('%w', DATE(created))
|
||||||
WHEN '1' THEN 'Monday'
|
WHEN '0' THEN 'Sunday'
|
||||||
WHEN '2' THEN 'Tuesday'
|
WHEN '1' THEN 'Monday'
|
||||||
WHEN '3' THEN 'Wednesday'
|
WHEN '2' THEN 'Tuesday'
|
||||||
WHEN '4' THEN 'Thursday'
|
WHEN '3' THEN 'Wednesday'
|
||||||
WHEN '5' THEN 'Friday'
|
WHEN '4' THEN 'Thursday'
|
||||||
WHEN '6' THEN 'Saturday'
|
WHEN '5' THEN 'Friday'
|
||||||
END AS weekday
|
WHEN '6' THEN 'Saturday'
|
||||||
FROM rants
|
END AS weekday
|
||||||
GROUP BY created_date
|
FROM rants
|
||||||
ORDER BY created_date
|
GROUP 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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW comment_stats_per_day AS SELECT
|
||||||
DATE(created) AS created_date,
|
COUNT(0) AS count,
|
||||||
CASE strftime('%w', DATE(created))
|
DATE(created) AS created_date,
|
||||||
WHEN '0' THEN 'Sunday'
|
CASE strftime('%w', DATE(created))
|
||||||
WHEN '1' THEN 'Monday'
|
WHEN '0' THEN 'Sunday'
|
||||||
WHEN '2' THEN 'Tuesday'
|
WHEN '1' THEN 'Monday'
|
||||||
WHEN '3' THEN 'Wednesday'
|
WHEN '2' THEN 'Tuesday'
|
||||||
WHEN '4' THEN 'Thursday'
|
WHEN '3' THEN 'Wednesday'
|
||||||
WHEN '5' THEN 'Friday'
|
WHEN '4' THEN 'Thursday'
|
||||||
WHEN '6' THEN 'Saturday'
|
WHEN '5' THEN 'Friday'
|
||||||
END AS weekday
|
WHEN '6' THEN 'Saturday'
|
||||||
FROM comments
|
END AS weekday
|
||||||
GROUP BY created_date
|
FROM comments
|
||||||
ORDER BY created_date
|
GROUP 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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW rant_stats_per_weekday AS SELECT
|
||||||
DATE(created) AS created_date,
|
COUNT(0) AS count,
|
||||||
CASE strftime('%w', DATE(created))
|
DATE(created) AS created_date,
|
||||||
WHEN '0' THEN 'Sunday'
|
CASE strftime('%w', DATE(created))
|
||||||
WHEN '1' THEN 'Monday'
|
WHEN '0' THEN 'Sunday'
|
||||||
WHEN '2' THEN 'Tuesday'
|
WHEN '1' THEN 'Monday'
|
||||||
WHEN '3' THEN 'Wednesday'
|
WHEN '2' THEN 'Tuesday'
|
||||||
WHEN '4' THEN 'Thursday'
|
WHEN '3' THEN 'Wednesday'
|
||||||
WHEN '5' THEN 'Friday'
|
WHEN '4' THEN 'Thursday'
|
||||||
WHEN '6' THEN 'Saturday'
|
WHEN '5' THEN 'Friday'
|
||||||
END AS weekday
|
WHEN '6' THEN 'Saturday'
|
||||||
FROM rants
|
END AS weekday
|
||||||
GROUP BY weekday
|
FROM rants
|
||||||
ORDER BY created_date
|
GROUP BY weekday
|
||||||
""")
|
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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW comment_stats_per_weekday AS SELECT
|
||||||
DATE(created) AS created_date,
|
COUNT(0) AS count,
|
||||||
CASE strftime('%w', DATE(created))
|
DATE(created) AS created_date,
|
||||||
WHEN '0' THEN 'Sunday'
|
CASE strftime('%w', DATE(created))
|
||||||
WHEN '1' THEN 'Monday'
|
WHEN '0' THEN 'Sunday'
|
||||||
WHEN '2' THEN 'Tuesday'
|
WHEN '1' THEN 'Monday'
|
||||||
WHEN '3' THEN 'Wednesday'
|
WHEN '2' THEN 'Tuesday'
|
||||||
WHEN '4' THEN 'Thursday'
|
WHEN '3' THEN 'Wednesday'
|
||||||
WHEN '5' THEN 'Friday'
|
WHEN '4' THEN 'Thursday'
|
||||||
WHEN '6' THEN 'Saturday'
|
WHEN '5' THEN 'Friday'
|
||||||
END AS weekday
|
WHEN '6' THEN 'Saturday'
|
||||||
FROM comments
|
END AS weekday
|
||||||
GROUP BY weekday
|
FROM comments
|
||||||
ORDER BY created_date
|
GROUP BY weekday
|
||||||
""")
|
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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW comment_stats_per_hour AS SELECT
|
||||||
strftime('%H', created) AS hour
|
COUNT(0) AS count,
|
||||||
FROM comments
|
strftime('%H', created) AS hour
|
||||||
GROUP BY hour
|
FROM comments
|
||||||
ORDER BY hour
|
GROUP 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
|
"""
|
||||||
COUNT(0) AS count,
|
CREATE VIEW rant_stats_per_hour AS SELECT
|
||||||
strftime('%H', created) AS hour
|
COUNT(0) AS count,
|
||||||
FROM rants
|
strftime('%H', created) AS hour
|
||||||
GROUP BY hour
|
FROM rants
|
||||||
ORDER BY hour
|
GROUP 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
|
"""
|
||||||
SELECT
|
CREATE VIEW user_stats AS
|
||||||
user_username AS username,
|
SELECT
|
||||||
COUNT(0) AS post_count,
|
user_username AS username,
|
||||||
(select count(0) from rants where rants.id = comments.rant_id and date(rants.created) = date(comments.created)) as rant_count,
|
COUNT(0) AS post_count,
|
||||||
DATE(comments.created) AS date,
|
(
|
||||||
(SELECT COUNT(0)
|
select count(0) from rants where
|
||||||
FROM comments AS comments2
|
rants.id = comments.rant_id
|
||||||
WHERE comments2.user_username = comments.user_username
|
and date(rants.created) = date(comments.created)
|
||||||
AND comments2.score = 0 and date(comments2.created) = date(comments.created)) AS ignore_count,
|
) as rant_count,
|
||||||
(SELECT COUNT(0)
|
DATE(comments.created) AS date,
|
||||||
FROM comments AS comments2
|
(SELECT COUNT(0)
|
||||||
WHERE comments2.user_username = comments.user_username
|
FROM comments AS comments2
|
||||||
AND comments2.score > 0 and date(comments2.created) = date(comments.created)) AS upvote_times,
|
WHERE comments2.user_username = comments.user_username
|
||||||
(SELECT SUM(score)
|
AND comments2.score = 0 and date(comments2.created) = date(comments.created)) AS ignore_count,
|
||||||
FROM comments AS comments2
|
(SELECT COUNT(0)
|
||||||
WHERE comments2.user_username = comments.user_username
|
FROM comments AS comments2
|
||||||
AND comments2.score > 0 and date(comments2.created) = date(comments.created)) AS upvote_total
|
WHERE comments2.user_username = comments.user_username
|
||||||
FROM comments
|
AND comments2.score > 0 and date(comments2.created) = date(comments.created)) AS upvote_times,
|
||||||
GROUP BY username, DATE(comments.created)
|
(SELECT SUM(score)
|
||||||
ORDER BY username ASC, date ASC;
|
FROM comments AS comments2
|
||||||
""")
|
WHERE comments2.user_username = comments.user_username
|
||||||
|
AND comments2.score > 0 and date(comments2.created) = date(comments.created)) AS upvote_total
|
||||||
|
FROM comments
|
||||||
|
GROUP BY username, DATE(comments.created)
|
||||||
|
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
|
"""
|
||||||
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
|
CREATE VIEW contributions AS
|
||||||
union
|
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, sum(length(body)) / count(0) as post_length_average, sum(length(body)) as content_length from comments
|
union
|
||||||
group by username
|
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
|
||||||
order by contributions desc, username asc
|
group by username
|
||||||
""")
|
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
|
"""
|
||||||
FROM contributions
|
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("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
|
||||||
|
|
||||||
|
|
||||||
@ -183,25 +217,22 @@ class Db:
|
|||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.db = get_db()
|
self.db = get_db()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def query(self, str):
|
def query(self, str):
|
||||||
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
|
||||||
|
|
||||||
def query(self, str):
|
def query(self, str):
|
||||||
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()
|
||||||
@ -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