Skip to content

Commit

Permalink
Merge pull request #513 from Bot-detector/develop
Browse files Browse the repository at this point in the history
Release sql improve statement
  • Loading branch information
extreme4all authored Feb 1, 2024
2 parents d8b830b + 1f6e948 commit 9fa83e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 83 deletions.
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

0 comments on commit 9fa83e5

Please sign in to comment.