Skip to content

Commit

Permalink
optimize query
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Feb 1, 2024
1 parent fe209cd commit 8a6dcdc
Showing 1 changed file with 35 additions and 83 deletions.
118 changes: 35 additions & 83 deletions src/api/v1/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ async def get_reports(

sql = select(Report)

if not reportedID is None:
if reportedID is not None:
sql = sql.where(Report.reportedID == reportedID)

if not reportingID is None:
if reportingID is not None:
sql = sql.where(Report.reportingID == reportingID)

if not timestamp is None:
if timestamp is not None:
sql = sql.where(func.date(Report.timestamp) == timestamp)

if not regionID is None:
if regionID is not None:
sql = sql.where(Report.region_id == regionID)

# execute query
Expand Down Expand Up @@ -199,8 +199,9 @@ async def update_reports(

return {"detail": f"{data.rowcount} rows updated to reportingID = {new_user_id}."}


async def insert_active_reporter(reporter: str):
if 'anonymoususer' in reporter:
if "anonymoususer" in reporter:
return
try:
sql: Text = text("INSERT INTO activeReporters (name) VALUES (:reporter)")
Expand All @@ -211,7 +212,7 @@ async def insert_active_reporter(reporter: str):
await session.execute(sql, {"reporter": reporter})
except Exception as e:
e = str(e)
if 'Duplicate entry' in e:
if "Duplicate entry" in e:
return
logger.error(str(e))

Expand Down Expand Up @@ -340,70 +341,57 @@ async def insert_report(

@router.get("/report/count", tags=["Report"])
async def get_report_count_v1(name: str):
""" """
voter: Player = aliased(Player, name="voter")
subject: Player = aliased(Player, name="subject")

name = await functions.to_jagex_name(name)

sql: Select = select(
func.count(Report.reportedID.distinct()),
subject.confirmed_ban,
subject.possible_ban,
subject.confirmed_player,
)
sql = sql.join(voter, Report.reportingID == voter.id)
sql = sql.join(subject, Report.reportedID == subject.id)
sql = sql.where(voter.name == name)
sql = sql.where(Report.manual_detect == 0)
sql = sql.group_by(
subject.confirmed_ban, subject.possible_ban, subject.confirmed_player
)

keys = ["count", "confirmed_ban", "possible_ban", "confirmed_player"]
# execute query
async with PLAYERDATA_ENGINE.get_session() as session:
session: AsyncSession = session
async with session.begin():
data = await session.execute(sql)
data = [{k: v for k, v in zip(keys, d)} for d in data]

return data


@router.get("/v2/report/count", tags=["Report"])
async def get_report_count_v2(name: str):
"""
Get the calculated player report count
objective is to make this query
SELECT COUNT(*) AS `count_1`,
`subject` . `confirmed_ban`,
`subject` . `possible_ban`,
`subject` . `confirmed_player`
FROM (
SELECT DISTINCT `Reports` . `reportedID`
FROM `Reports`
INNER JOIN `Players` AS `voter` ON `Reports` . `reportingID` = `voter` . `id`
WHERE `voter` . `name` = ? AND `Reports` . `manual_detect` = ?
) AS DistinctReports
INNER JOIN `Players` AS `subject` ON DistinctReports.`reportedID` = `subject` . `id`
GROUP BY `subject` . `confirmed_ban`, `subject` . `possible_ban`, `subject` . `confirmed_player`;
"""
# query
name = await functions.to_jagex_name(name)

voter: Player = aliased(Player, name="voter")
subject: Player = aliased(Player, name="subject")

name = await functions.to_jagex_name(name)
sub_query: Select = select(Report.reportedID.distinct().label("reportedID"))
sub_query = sub_query.join(voter, Report.reportingID == voter.id)
sub_query = sub_query.where(voter.name == name)
sub_query = sub_query.where(Report.manual_detect == 0)

# Create an alias for the subquery
sub_query_alias = sub_query.alias("DistinctReports")

sql: Select = select(
func.count(playerReports.reported_id.distinct()),
func.count(subject.id),
subject.confirmed_ban,
subject.possible_ban,
subject.confirmed_player,
)
sql = sql.join(voter, playerReports.reporting_id == voter.id)
sql = sql.join(subject, playerReports.reported_id == subject.id)
sql = sql.where(voter.name == name)
sql = sql.select_from(sub_query_alias)
sql = sql.join(
subject, sub_query_alias.c.reportedID == subject.id
) # Use c to access columns
sql = sql.group_by(
subject.confirmed_ban, subject.possible_ban, subject.confirmed_player
)

keys = ["count", "confirmed_ban", "possible_ban", "confirmed_player"]
PLAYERDATA_ENGINE.engine.echo = True
# execute query
async with PLAYERDATA_ENGINE.get_session() as session:
session: AsyncSession = session
async with session.begin():
data = await session.execute(sql)
data = [{k: v for k, v in zip(keys, d)} for d in data]

PLAYERDATA_ENGINE.engine.echo = False
return data


Expand Down Expand Up @@ -442,39 +430,3 @@ async def get_report_manual_count_v1(name: str):
data = [{k: v for k, v in zip(keys, d)} for d in data]

return data


@router.get("/v2/report/manual/count", tags=["Report"])
async def get_report_manual_count_v2(name: str):
"""
Get the calculated player report count
"""
# query

voter: Player = aliased(Player, name="voter")
subject: Player = aliased(Player, name="subject")

name = await functions.to_jagex_name(name)

sql: Select = select(
func.count(playerReportsManual.reported_id.distinct()),
subject.confirmed_ban,
subject.possible_ban,
subject.confirmed_player,
)
sql = sql.join(voter, playerReportsManual.reporting_id == voter.id)
sql = sql.join(subject, playerReportsManual.reported_id == subject.id)
sql = sql.where(voter.name == name)
sql = sql.group_by(
subject.confirmed_ban, subject.possible_ban, subject.confirmed_player
)

keys = ["count", "confirmed_ban", "possible_ban", "confirmed_player"]
# execute query
async with PLAYERDATA_ENGINE.get_session() as session:
session: AsyncSession = session
async with session.begin():
data = await session.execute(sql)
data = [{k: v for k, v in zip(keys, d)} for d in data]

return data

0 comments on commit 8a6dcdc

Please sign in to comment.