Skip to content

Commit

Permalink
fix: rse name is unique (#223)
Browse files Browse the repository at this point in the history
Co-authored-by: 99cloud <qiao.lei@99cloud.net>
  • Loading branch information
qiaolei1 and 99cloud committed May 6, 2023
1 parent 611d290 commit 5b913b4
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 49 deletions.
44 changes: 44 additions & 0 deletions dandelion/alembic/versions/b1f155a1aeae_rse_name_not_repeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""rse_name_not_repeat
Revision ID: b1f155a1aeae
Revises: cb43f864daea
Create Date: 2023-05-05 10:31:53.774217
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "b1f155a1aeae"
down_revision = "cb43f864daea"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index("ix_camera_name", table_name="camera")
op.create_index(op.f("ix_camera_name"), "camera", ["name"], unique=True)
op.drop_index("ix_lidar_name", table_name="lidar")
op.create_index(op.f("ix_lidar_name"), "lidar", ["name"], unique=True)
op.drop_index("ix_radar_name", table_name="radar")
op.create_index(op.f("ix_radar_name"), "radar", ["name"], unique=True)
op.drop_index("ix_rsu_rsu_name", table_name="rsu")
op.create_index(op.f("ix_rsu_rsu_name"), "rsu", ["rsu_name"], unique=True)
op.drop_index("ix_spat_name", table_name="spat")
op.create_index(op.f("ix_spat_name"), "spat", ["name"], unique=True)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_spat_name"), table_name="spat")
op.create_index("ix_spat_name", "spat", ["name"], unique=False)
op.drop_index(op.f("ix_rsu_rsu_name"), table_name="rsu")
op.create_index("ix_rsu_rsu_name", "rsu", ["rsu_name"], unique=False)
op.drop_index(op.f("ix_radar_name"), table_name="radar")
op.create_index("ix_radar_name", "radar", ["name"], unique=False)
op.drop_index(op.f("ix_lidar_name"), table_name="lidar")
op.create_index("ix_lidar_name", "lidar", ["name"], unique=False)
op.drop_index(op.f("ix_camera_name"), table_name="camera")
op.create_index("ix_camera_name", "camera", ["name"], unique=False)
# ### end Alembic commands ###
4 changes: 2 additions & 2 deletions dandelion/api/api_v1/endpoints/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create(
try:
camera_in_db = crud.camera.create(db, obj_in=camera_in)
except (sql_exc.IntegrityError, sql_exc.DataError) as ex:
raise error_handle(ex, "sn", camera_in.sn)
raise error_handle(ex, ["sn", "name"], [camera_in.sn, camera_in.name])
return camera_in_db.to_dict()


Expand Down Expand Up @@ -159,5 +159,5 @@ def update(
try:
new_camera_in_db = crud.camera.update(db, db_obj=camera_in_db, obj_in=camera_in)
except (sql_exc.DataError, sql_exc.IntegrityError) as ex:
raise error_handle(ex, "sn", camera_in.sn)
raise error_handle(ex, ["sn", "name"], [camera_in.sn, camera_in.name])
return new_camera_in_db.to_dict()
4 changes: 2 additions & 2 deletions dandelion/api/api_v1/endpoints/lidars.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create(
try:
lidar_in_db = crud.lidar.create(db, obj_in=lidar_in)
except (sql_exc.IntegrityError, sql_exc.DataError) as ex:
raise error_handle(ex, "sn", lidar_in.sn)
raise error_handle(ex, ["sn", "name"], [lidar_in.sn, lidar_in.name])
return lidar_in_db.to_dict()


Expand Down Expand Up @@ -159,5 +159,5 @@ def update(
try:
new_lidar_in_db = crud.lidar.update(db, db_obj=lidar_in_db, obj_in=lidar_in)
except (sql_exc.DataError, sql_exc.IntegrityError) as ex:
raise error_handle(ex, "sn", lidar_in.sn)
raise error_handle(ex, ["sn", "name"], [lidar_in.sn, lidar_in.name])
return new_lidar_in_db.to_dict()
4 changes: 2 additions & 2 deletions dandelion/api/api_v1/endpoints/radar_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create(
try:
radar_in_db = crud.radar_camera.create(db, obj_in=radar_camera_in)
except (sql_exc.IntegrityError, sql_exc.DataError) as ex:
raise error_handle(ex, "sn or name", f"{radar_camera_in.sn} or {radar_camera_in.name}")
raise error_handle(ex, ["sn", "name"], [radar_camera_in.sn, radar_camera_in.name])
return radar_in_db.to_all_dict()


Expand Down Expand Up @@ -166,5 +166,5 @@ def update(
db, db_obj=radar_camera_in_db, obj_in=radar_camera_in
)
except (sql_exc.DataError, sql_exc.IntegrityError) as ex:
raise error_handle(ex, "sn or name", f"{radar_camera_in.sn} or {radar_camera_in.name}")
raise error_handle(ex, ["sn", "name"], [radar_camera_in.sn, radar_camera_in.name])
return new_radar_camera_in_db.to_all_dict()
4 changes: 2 additions & 2 deletions dandelion/api/api_v1/endpoints/radars.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create(
try:
radar_in_db = crud.radar.create(db, obj_in=radar_in)
except (sql_exc.IntegrityError, sql_exc.DataError) as ex:
raise error_handle(ex, "sn", radar_in.sn)
raise error_handle(ex, ["sn", "name"], [radar_in.sn, radar_in.name])
return radar_in_db.to_dict()


Expand Down Expand Up @@ -159,5 +159,5 @@ def update(
try:
new_radar_in_db = crud.radar.update(db, db_obj=radar_in_db, obj_in=radar_in)
except (sql_exc.DataError, sql_exc.IntegrityError) as ex:
raise error_handle(ex, "sn", radar_in.sn)
raise error_handle(ex, ["sn", "name"], [radar_in.sn, radar_in.name])
return new_radar_in_db.to_dict()
4 changes: 2 additions & 2 deletions dandelion/api/api_v1/endpoints/rsus.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def create(
try:
rsu_in_db = crud.rsu.create_rsu(db, obj_in=rsu_in, rsu_tmp_in_db=rsu_tmp)
except (sql_exc.IntegrityError, sql_exc.DataError) as ex:
raise error_handle(ex, "rsu_esn", rsu_in.rsu_esn)
raise error_handle(ex, ["rsu_esn", "rsu_name"], [rsu_in.rsu_esn, rsu_in.rsu_name])
return rsu_in_db.to_all_dict()


Expand Down Expand Up @@ -177,7 +177,7 @@ def update(
try:
new_rsu_in_db = crud.rsu.update_with_location(db, db_obj=rsu_in_db, obj_in=rsu_in)
except (sql_exc.DataError, sql_exc.IntegrityError) as ex:
raise error_handle(ex, "rsu_esn", rsu_in.rsu_esn)
raise error_handle(ex, ["rsu_esn", "rsu_name"], [rsu_in.rsu_esn, rsu_in.rsu_name])
return new_rsu_in_db.to_all_dict()


Expand Down
31 changes: 2 additions & 29 deletions dandelion/api/api_v1/endpoints/spats.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from __future__ import annotations

import re
from logging import LoggerAdapter
from typing import Optional

Expand Down Expand Up @@ -53,20 +52,7 @@ def create(
try:
spat_in_db = crud.spat.create(db, obj_in=spat_in)
except sql_exc.IntegrityError as ex:
error_code = eval(re.findall(r"\(pymysql.err.IntegrityError\) (.*)", ex.args[0])[0])[0]
LOG.error(ex.args[0])
detail = {
"code": error_code,
"msg": ex.args[0],
}
if error_code == 1062:
detail["detail"] = {
"intersection_id": spat_in.intersection_id,
"phase_id": spat_in.phase_id,
}
detail["code"] = 1116

raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
raise deps.spat_intersection_phase_unique(ex, spat_in)
except sql_exc.DataError as ex:
LOG.error(ex.args[0])
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ex.args[0])
Expand Down Expand Up @@ -180,20 +166,7 @@ def update(
try:
new_spat_in_db = crud.spat.update(db, db_obj=spat_in_db, obj_in=spat_in)
except sql_exc.IntegrityError as ex:
error_code = eval(re.findall(r"\(pymysql.err.IntegrityError\) (.*)", ex.args[0])[0])[0]
LOG.error(ex.args[0])
detail = {
"code": error_code,
"msg": ex.args[0],
}
if error_code == 1062:
detail["detail"] = {
"intersection_id": spat_in.intersection_id,
"phase_id": spat_in.phase_id,
}
detail["code"] = 1116

raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
raise deps.spat_intersection_phase_unique(ex, spat_in)
except sql_exc.DataError as ex:
LOG.error(ex.args[0])
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ex.args[0])
Expand Down
26 changes: 23 additions & 3 deletions dandelion/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

reusable_oauth2 = OAuth2PasswordBearer(tokenUrl=f"{constants.API_V1_STR}/login/access-token")


RESPONSE_ERROR: Dict = {
status.HTTP_400_BAD_REQUEST: {"model": schemas.ErrorMessage, "description": "Bad Request"},
status.HTTP_401_UNAUTHORIZED: {
Expand Down Expand Up @@ -143,12 +142,13 @@ def error_handle(
err_msg = err.args[0]
LOG.error(err_msg)
if isinstance(err, sqlalchemy.exc.IntegrityError):
code = err.orig.args[0]
detail = {
"code": eval(re.findall(r"\(pymysql.err.IntegrityError\) (.*)", err_msg)[0])[0],
"code": code,
"msg": err_msg,
"detail": {},
}
if detail["code"] == 1062: # 重复
if code == 1062: # 重复
if isinstance(field, list) and isinstance(field_data, list):
for index, value in enumerate(field):
detail["detail"][value] = field_data[index]
Expand Down Expand Up @@ -183,3 +183,23 @@ def get_gunicorn_port():
return port
except (AttributeError, ImportError, IndexError, ValueError):
return "28300"


def spat_intersection_phase_unique(
ex: sqlalchemy.exc.DatabaseError, spat_in: Union[schemas.SpatCreate, schemas.SpatUpdate]
):
match = re.search(r"Duplicate entry '.*' for key '(.*?)'", ex.orig.args[1])
if match and match.group(1) != "ix_spat_name":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": 1063,
"msg": ex.args[0],
"detail": {
"intersection_id": spat_in.intersection_id,
"phase_id": spat_in.phase_id,
},
},
)
else:
raise error_handle(ex, "name", spat_in.name)
1 change: 1 addition & 0 deletions dandelion/models/algos_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ def to_all_dict(self):
algo=self.algo_name.name,
module=self.algo_name.algo_module.module,
endpoint_id=self.endpoint_id,
endpoint_url=self.endpoint.url,
)
2 changes: 1 addition & 1 deletion dandelion/models/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Camera(Base, DandelionBase):
__tablename__ = "camera"

sn = Column(String(64), nullable=False, index=True, unique=True)
name = Column(String(64), nullable=False, index=True, default="")
name = Column(String(64), nullable=False, index=True, unique=True, default="")
stream_url = Column(String(1024), nullable=False, default="")
lng = Column(Float, nullable=False)
lat = Column(Float, nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion dandelion/models/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Lidar(Base, DandelionBase):
__tablename__ = "lidar"

sn = Column(String(64), nullable=False, index=True, unique=True)
name = Column(String(64), nullable=False, index=True, default="")
name = Column(String(64), nullable=False, index=True, unique=True, default="")
lidar_ip = Column(String(15), nullable=False, default="")
lng = Column(Float, nullable=False)
lat = Column(Float, nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion dandelion/models/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Radar(Base, DandelionBase):
__tablename__ = "radar"

sn = Column(String(64), nullable=False, index=True, unique=True)
name = Column(String(64), nullable=False, index=True, default="")
name = Column(String(64), nullable=False, index=True, unique=True, default="")
radar_ip = Column(String(15), nullable=False, default="")
lng = Column(Float, nullable=False)
lat = Column(Float, nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion dandelion/models/rsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RSU(Base, DandelionBase):
rsu_id = Column(String(64), nullable=False)
rsu_esn = Column(String(64), nullable=False, index=True, unique=True, comment="serial number")
rsu_ip = Column(String(64), nullable=False)
rsu_name = Column(String(64), index=True, nullable=False)
rsu_name = Column(String(64), index=True, unique=True, nullable=False)
version = Column(String(64), nullable=False)
rsu_status = Column(String(32), index=True, nullable=False)
enabled = Column(Boolean, nullable=True, default=True)
Expand Down
2 changes: 1 addition & 1 deletion dandelion/models/spat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Spat(Base, DandelionBase):
__tablename__ = "spat"

intersection_id = Column(String(64), nullable=False, index=True)
name = Column(String(64), nullable=False, index=True, default="")
name = Column(String(64), nullable=False, index=True, unique=True, default="")
spat_ip = Column(String(15), nullable=False, default="")
point = Column(String(15), nullable=False, default="")
online_status = Column(Boolean, nullable=False, default=False)
Expand Down
3 changes: 2 additions & 1 deletion dandelion/schemas/algo_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class AlgoVersionGET(AlgoVersionBase):
id: Optional[int] = Field(None, alias="id", description="Algo id")
module: str = Field(..., alias="module", description="Algo module")
algo: str = Field(..., alias="algo", description="Algo name")
version: str = Field(..., alias="version", description="Algo version list")
version: str = Field(..., alias="version", description="Algo version ")
endpoint_url: Optional[str] = Field(None, alias="endpointUrl", description="Algo endpoint url")


class AlgoVersions(BaseModel):
Expand Down
12 changes: 11 additions & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -11644,6 +11644,11 @@
"title": "Inuse",
"type": "string",
"description": "Algo in use"
},
"modulePath": {
"title": "Modulepath",
"type": "string",
"description": "Algo module path"
}
}
},
Expand Down Expand Up @@ -11770,7 +11775,12 @@
"version": {
"title": "Version",
"type": "string",
"description": "Algo version list"
"description": "Algo version "
},
"endpointUrl": {
"title": "Endpointurl",
"type": "string",
"description": "Algo endpoint url"
}
}
},
Expand Down

0 comments on commit 5b913b4

Please sign in to comment.