1차완료
This commit is contained in:
@@ -542,22 +542,52 @@ def stats_top_chargers(limit: int = 10):
|
||||
db.close()
|
||||
|
||||
|
||||
@app.get("/api/stats/charger-error-codes")
|
||||
def stats_charger_error_codes(code_limit: int = 10):
|
||||
"""에러코드별 누적 건수 Top N (단순 순위)."""
|
||||
@app.get("/api/stats/top-stations")
|
||||
def stats_top_stations(limit: int = 10):
|
||||
"""충전소별 누적 고장 신고 건수 Top N."""
|
||||
from database import SessionLocal
|
||||
from sqlalchemy import text
|
||||
db = SessionLocal()
|
||||
try:
|
||||
rows = db.execute(text("""
|
||||
SELECT TRIM(error_code) AS error_code, COUNT(*) AS cnt
|
||||
SELECT COALESCE(c.station_name, rep.charger_id) AS station_name,
|
||||
COUNT(*) AS total,
|
||||
COUNT(*) FILTER (WHERE rep.status = 'done') AS done_cnt,
|
||||
COUNT(*) FILTER (WHERE rep.status != 'done') AS active_cnt
|
||||
FROM reports rep
|
||||
LEFT JOIN chargers c ON c.id = rep.charger_id
|
||||
GROUP BY COALESCE(c.station_name, rep.charger_id)
|
||||
ORDER BY total DESC
|
||||
LIMIT :lim
|
||||
"""), {"lim": limit}).fetchall()
|
||||
return [
|
||||
{
|
||||
"station_name": row[0],
|
||||
"total": int(row[1]),
|
||||
"done": int(row[2]),
|
||||
"active": int(row[3]),
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@app.get("/api/stats/charger-error-codes")
|
||||
def stats_charger_error_codes(code_limit: int = 10):
|
||||
"""에러코드별 누적 건수 Top N (에러코드 없음 포함)."""
|
||||
from database import SessionLocal
|
||||
from sqlalchemy import text
|
||||
db = SessionLocal()
|
||||
try:
|
||||
rows = db.execute(text("""
|
||||
SELECT COALESCE(NULLIF(TRIM(COALESCE(error_code, '')), ''), '에러코드 없음') AS error_code,
|
||||
COUNT(*) AS cnt
|
||||
FROM reports
|
||||
WHERE error_code IS NOT NULL AND TRIM(error_code) != ''
|
||||
GROUP BY TRIM(error_code)
|
||||
GROUP BY COALESCE(NULLIF(TRIM(COALESCE(error_code, '')), ''), '에러코드 없음')
|
||||
ORDER BY cnt DESC
|
||||
LIMIT :limit
|
||||
"""), {"limit": code_limit}).fetchall()
|
||||
# 역순: 차트 Y축에서 1위가 맨 위
|
||||
result = [{"error_code": r[0], "total": int(r[1])} for r in reversed(rows)]
|
||||
return {"error_codes": result}
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user