Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release sql improve statement #513

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/v1/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Prediction(BaseModel):
Herblore_bot: Optional[float] = 0
Zulrah_bot: Optional[float] = 0
Unknown_bot: Optional[float] = 0
Gauntlet_bot: Optional[float] = 0
Nex_bot: Optional[float] = 0


@router.get("/prediction", tags=["Prediction"])
Expand Down
103 changes: 20 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,58 +341,31 @@ 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
"""
# 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
)
Expand All @@ -403,7 +377,6 @@ async def get_report_count_v2(name: str):
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


Expand Down Expand Up @@ -442,39 +415,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
Loading