Untitled

 avatar
unknown
plain_text
10 months ago
1.0 kB
3
Indexable
import psycopg2
conn = psycopg2.connect(user="netdb", password="csieproj", host="localhost", port="5432")
conn.autocommit = True
cur = conn.cursor()
getusagesql = f'''SELECT normal_usage FROM data_time WHERE device_uuid= %s AND generated_time=%s'''

def get_time(user: str, start_time: str, end_time: str,db: Session) -> str:
    user: User = (
        db.query(User).filter(User.username == user).first()
    )
    if user:
        cur.execute(getusagesql, (user.electricity_meter, start_time))
        start = cur.fetchall()
        if start is None:
            raise HTTPException(status_code=400, detail="Date wrong!")

        cur.execute(getusagesql, (user.electricity_meter, end_time))
        end = cur.fetchall()
        if end is None:
            raise HTTPException(status_code=400, detail="Date wrong!")
        start_usage = start[0][0]
        end_usage = end[0][0]

        usage_difference = end_usage - start_usage

        return usage_difference
    raise HTTPException(status_code=400, detail="User not found")

Editor is loading...
Leave a Comment