Update API service to use raw_waiting_time table
- Change RawOpdCheckpoint model to RawWaitingTime - Update schema from FeedCheckpointIn to FeedWaitingTimeIn - Switch to rawdata.raw_waiting_time table - Keep existing /feed/checkpoint endpoint - Add new fields: vn, txn, name, doctor_code, doctor_name, location_code, location_name, step_name, time - Update permission to feed.waiting-time:write
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.v1.schemas import FeedCheckpointIn
|
||||
from app.api.v1.schemas import FeedWaitingTimeIn
|
||||
from app.core.config import settings
|
||||
from app.db.models import RawOpdCheckpoint
|
||||
from app.db.models import RawWaitingTime
|
||||
from app.security.dependencies import get_db, require_permission
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/v1")
|
||||
|
||||
PERM_FEED_CHECKPOINT_WRITE = "feed.checkpoint:write"
|
||||
PERM_FEED_WAITING_TIME_WRITE = "feed.waiting-time:write"
|
||||
|
||||
|
||||
def _to_tz(dt):
|
||||
@@ -28,8 +29,8 @@ def _to_tz(dt):
|
||||
|
||||
@router.post("/feed/checkpoint")
|
||||
def upsert_feed_checkpoint(
|
||||
payload: list[FeedCheckpointIn],
|
||||
_: Annotated[object, Depends(require_permission(PERM_FEED_CHECKPOINT_WRITE))],
|
||||
payload: list[FeedWaitingTimeIn],
|
||||
_: Annotated[object, Depends(require_permission(PERM_FEED_WAITING_TIME_WRITE))],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
):
|
||||
rows = []
|
||||
@@ -37,30 +38,36 @@ def upsert_feed_checkpoint(
|
||||
rows.append(
|
||||
{
|
||||
"id": item.id,
|
||||
"hn": item.hn,
|
||||
"vn": item.vn,
|
||||
"location": item.location,
|
||||
"type": item.type,
|
||||
"timestamp_in": _to_tz(item.timestamp_in),
|
||||
"timestamp_out": _to_tz(item.timestamp_out),
|
||||
"waiting_time": item.waiting_time,
|
||||
"bu": item.bu,
|
||||
"txn": item.txn,
|
||||
"hn": item.hn,
|
||||
"name": item.name,
|
||||
"doctor_code": item.doctor_code,
|
||||
"doctor_name": item.doctor_name,
|
||||
"location_code": item.location_code,
|
||||
"location_name": item.location_name,
|
||||
"step_name": item.step_name,
|
||||
"time": _to_tz(item.time),
|
||||
"updated_at": datetime.now(ZoneInfo(settings.TIMEZONE)),
|
||||
}
|
||||
)
|
||||
|
||||
stmt = insert(RawOpdCheckpoint).values(rows)
|
||||
stmt = insert(RawWaitingTime).values(rows)
|
||||
update_cols = {
|
||||
"hn": stmt.excluded.hn,
|
||||
"vn": stmt.excluded.vn,
|
||||
"location": stmt.excluded.location,
|
||||
"type": stmt.excluded.type,
|
||||
"timestamp_in": stmt.excluded.timestamp_in,
|
||||
"timestamp_out": stmt.excluded.timestamp_out,
|
||||
"waiting_time": stmt.excluded.waiting_time,
|
||||
"bu": stmt.excluded.bu,
|
||||
"txn": stmt.excluded.txn,
|
||||
"hn": stmt.excluded.hn,
|
||||
"name": stmt.excluded.name,
|
||||
"doctor_code": stmt.excluded.doctor_code,
|
||||
"doctor_name": stmt.excluded.doctor_name,
|
||||
"location_code": stmt.excluded.location_code,
|
||||
"location_name": stmt.excluded.location_name,
|
||||
"step_name": stmt.excluded.step_name,
|
||||
"time": stmt.excluded.time,
|
||||
"updated_at": stmt.excluded.updated_at,
|
||||
}
|
||||
|
||||
stmt = stmt.on_conflict_do_update(index_elements=[RawOpdCheckpoint.id], set_=update_cols)
|
||||
stmt = stmt.on_conflict_do_update(index_elements=[RawWaitingTime.id], set_=update_cols)
|
||||
result = db.execute(stmt)
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -3,13 +3,15 @@ from datetime import datetime
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FeedCheckpointIn(BaseModel):
|
||||
class FeedWaitingTimeIn(BaseModel):
|
||||
id: int
|
||||
hn: int
|
||||
vn: int
|
||||
location: str
|
||||
type: str
|
||||
timestamp_in: datetime
|
||||
timestamp_out: datetime | None = None
|
||||
waiting_time: int | None = None
|
||||
bu: str | None = None
|
||||
vn: int | None = None
|
||||
txn: int | None = None
|
||||
hn: str | None = None
|
||||
name: str | None = None
|
||||
doctor_code: str | None = None
|
||||
doctor_name: str | None = None
|
||||
location_code: str | None = None
|
||||
location_name: str | None = None
|
||||
step_name: str | None = None
|
||||
time: datetime
|
||||
|
||||
@@ -9,19 +9,23 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.db.base import Base
|
||||
|
||||
|
||||
class RawOpdCheckpoint(Base):
|
||||
__tablename__ = "raw_opd_checkpoint"
|
||||
__table_args__ = {"schema": "operationbi"}
|
||||
class RawWaitingTime(Base):
|
||||
__tablename__ = "raw_waiting_time"
|
||||
__table_args__ = {"schema": "rawdata"}
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
hn: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
vn: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
location: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
timestamp_in: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
timestamp_out: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
waiting_time: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
bu: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
vn: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
|
||||
txn: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
|
||||
hn: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
doctor_code: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
doctor_name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
location_code: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
location_name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
step_name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
time: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
|
||||
|
||||
class ApiClient(Base):
|
||||
|
||||
Reference in New Issue
Block a user