Code refactor: use only one cursor for every port (to prevent cursor/connection spam)

pull/1/head
Captain Arepa 2 years ago
parent 27117ab80e
commit 082249c87e

@ -92,118 +92,167 @@ class BotInstance:
# Database Queries
def get_all_blocks(self):
conn, cursor = self.get_db_cursor()
sql = "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and this.host is null;".format(self.mk_user)
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows
def daily_blocks_query(self):
return "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '1 day' and current_date " \
"and this.host is null;".format(self.mk_user)
def weekly_blocks_query(self):
return "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '7 day' and current_date " \
"and this.host is null;".format(self.mk_user)
def monthly_blocks_query(self):
return "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '1 month' and current_date " \
"and this.host is null;".format(self.mk_user)
def all_blocks_query(self):
return "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and this.host is null;".format(self.mk_user)
def get_yesterdays_blocks(self):
def latest_block_query(self):
return "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and this.host is null " \
"order by b.\"createdAt\" desc " \
"limit 1;".format(self.mk_user)
# Database Reports
def daily_blocks_report(self):
conn, cursor = self.get_db_cursor()
sql = "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '1 day' and current_date " \
"and this.host is null;".format(self.mk_user)
cursor.execute(sql)
rows = cursor.fetchall()
sql_all_time = self.all_blocks_query()
sql_latest = self.latest_block_query()
sql_daily = self.daily_blocks_query()
# get all time blocks
cursor.execute(sql_all_time)
all_blocks = cursor.fetchall()
# get latest block
cursor.execute(sql_latest)
latest_block = cursor.fetchall()
# get daily blocks
cursor.execute(sql_daily)
daily_blocks = cursor.fetchall()
# close cursor and conn
cursor.close()
conn.close()
return rows
def get_this_weeks_blocks(self):
return all_blocks, latest_block, daily_blocks
def weekly_blocks_report(self):
conn, cursor = self.get_db_cursor()
sql = "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '7 day' and current_date " \
"and this.host is null;".format(self.mk_user)
cursor.execute(sql)
rows = cursor.fetchall()
sql_all_time = self.all_blocks_query()
sql_latest = self.latest_block_query()
sql_weekly = self.weekly_blocks_query()
# get all time blocks
cursor.execute(sql_all_time)
all_blocks = cursor.fetchall()
# get latest block
cursor.execute(sql_latest)
latest_block = cursor.fetchall()
# get daily blocks
cursor.execute(sql_weekly)
weekly_blocks = cursor.fetchall()
# close cursor and conn
cursor.close()
conn.close()
return rows
def get_this_months_blocks(self):
return all_blocks, latest_block, weekly_blocks
def monthly_blocks_report(self):
conn, cursor = self.get_db_cursor()
sql = "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and b.\"createdAt\" between current_date - interval '1 month' and current_date " \
"and this.host is null;".format(self.mk_user)
cursor.execute(sql)
rows = cursor.fetchall()
sql_all_time = self.all_blocks_query()
sql_latest = self.latest_block_query()
sql_monthly = self.monthly_blocks_query()
# get all time blocks
cursor.execute(sql_all_time)
all_blocks = cursor.fetchall()
# get latest block
cursor.execute(sql_latest)
latest_block = cursor.fetchall()
# get daily blocks
cursor.execute(sql_monthly)
monthly_blocks = cursor.fetchall()
# close cursor and conn
cursor.close()
conn.close()
return rows
def get_latest_block(self):
return all_blocks, latest_block, monthly_blocks
def all_time_blocks_report(self):
conn, cursor = self.get_db_cursor()
sql = "select concat(that.username,'@',that.host ) as blocker, " \
"this.username as blockee, " \
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
"from blocking b " \
"join \"user\" this on this.id = b.\"blockeeId\" " \
"join \"user\" that on that.id = b.\"blockerId\" " \
"where this.\"username\" like \'{0}\' " \
"and this.host is null " \
"order by b.\"createdAt\" desc " \
"limit 1;".format(self.mk_user)
cursor.execute(sql)
rows = cursor.fetchall()
sql_all_time = self.all_blocks_query()
sql_latest = self.latest_block_query()
# All Time
cursor.execute(sql_all_time)
all_time = cursor.fetchall()
# Latest
cursor.execute(sql_latest)
latest = cursor.fetchall()
cursor.close()
conn.close()
return rows
# Reports
def parse_block_list(self, result, bullet=""):
block_line = ""
if len(result) == 0:
block_line += self.msg_no_block + "\n"
else:
for row in result:
blocker = row["blocker"]
block_date = row["block_date"]
block_line += "%s@%s (%s)\n" % (bullet, blocker, block_date)
return block_line
return all_time, latest, all_time
def block_reports(self, report_type, log_file):
all_time_blocks = self.get_all_blocks()
latest_block = self.get_latest_block()
all_time_blocks = []
latest_block = []
match report_type:
case "DAILY":
blocks = self.get_yesterdays_blocks()
all_time_blocks, latest_block, blocks = self.daily_blocks_report()
report_header = "\nYesterday's blocks: %d\n" % len(blocks)
case "WEEKLY":
blocks = self.get_this_weeks_blocks()
all_time_blocks, latest_block, blocks = self.weekly_blocks_report()
report_header = "\nThis week's blocks: %d\n" % len(blocks)
case "MONTHLY":
blocks = self.get_this_months_blocks()
all_time_blocks, latest_block, blocks = self.monthly_blocks_report()
report_header = "\nThis month's blocks: %d\n" % len(blocks)
case "ALL_TIME":
blocks = all_time_blocks
all_time_blocks, latest_block, blocks = self.all_time_blocks_report()
report_header = "\nAll time blocks: %d\n" % len(blocks)
case "STATISTICS":
blocks = None
@ -220,8 +269,20 @@ class BotInstance:
msg_content = report_header + content + msg_latest + msg_all_time
self.post_note(msg_content, log_file)
def parse_block_list(self, result, bullet=""):
block_line = ""
if len(result) == 0:
block_line += self.msg_no_block + "\n"
else:
for row in result:
blocker = row["blocker"]
block_date = row["block_date"]
block_line += "%s@%s (%s)\n" % (bullet, blocker, block_date)
return block_line
# Misskey Post
def post_note(self, content="", log_file=None):
print("ENTER!")
note_content = "%s%s" % (self.msg_text, content)
payload = {
"visibility": self.msg_visibility,
@ -229,6 +290,7 @@ class BotInstance:
"localOnly": self.msg_local_only,
"i": self.mk_token
}
print(note_content)
create_note_request = requests.post(self.mk_url + "notes/create", json=payload)
if create_note_request.status_code != 200:
print("Misskey error: " + create_note_request.json()["error"]["message"], file=log_file)

Loading…
Cancel
Save