PLJ-TIF_E41253311/backend/app/routes/dashboard.py

89 lines
1.8 KiB
Python

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from ..database import get_db
from ..models import Komentar
router = APIRouter(
prefix="/dashboard",
tags=["Dashboard"]
)
# =========================================================
# RINGKASAN DASHBOARD
# =========================================================
@router.get("/summary")
def get_summary(
db: Session = Depends(get_db)
):
total = db.query(Komentar).count()
positif = db.query(Komentar).filter(
Komentar.sentiment == "Positif"
).count()
negatif = db.query(Komentar).filter(
Komentar.sentiment == "Negatif"
).count()
if total > 0:
persentase_positif = (
positif / total
) * 100
persentase_negatif = (
negatif / total
) * 100
else:
persentase_positif = 0
persentase_negatif = 0
return {
"total": total,
"positif": positif,
"negatif": negatif,
"persentase_positif": round(
persentase_positif,
2
),
"persentase_negatif": round(
persentase_negatif,
2
)
}
# =========================================================
# DATA DASHBOARD
# =========================================================
@router.get("/data")
def get_dashboard_data(
db: Session = Depends(get_db)
):
data = db.query(Komentar).all()
hasil = []
for item in data:
hasil.append({
"id": item.id,
"komentar": item.komentar,
"text_preprocessed":
item.text_preprocessed,
"sentiment":
item.sentiment,
"probability":
item.probability
})
return hasil