Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/dispatch/cost_model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CostModelActivity(Base):
id = Column(Integer, primary_key=True)
plugin_event_id = Column(Integer, ForeignKey(PluginEvent.id, ondelete="CASCADE"))
plugin_event = relationship(PluginEvent, backref="plugin_event")
response_time_seconds = Column(Integer, default=300)
response_time_seconds = Column(Integer, default=0)
enabled = Column(Boolean, default=True)


Expand All @@ -69,7 +69,7 @@ class CostModelActivityBase(DispatchBase):
"""Base class for cost model activity resources"""

plugin_event: PluginEventRead
response_time_seconds: int | None = 300
response_time_seconds: int | None = None
enabled: bool | None = Field(True, nullable=True)


Expand Down
28 changes: 10 additions & 18 deletions src/dispatch/cost_model/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
def has_unique_plugin_event(cost_model_in: CostModelRead) -> bool:
seen = set()
for activity in cost_model_in.activities:
if activity.plugin_event.id in seen:
if activity.response_time_seconds in seen:
log.warning(
f"Duplicate plugin event id detected. Please ensure all plugin events are unique for each cost model. Duplicate id: {activity.plugin_event.id}"
f"Duplicate response time detected. Please ensure all response times are unique for each cost model. Duplicate time: {activity.response_time_seconds}"
)
return False
seen.add(activity.plugin_event.id)
seen.add(activity.response_time_seconds)
return True


Expand All @@ -34,16 +34,14 @@ def get_default(*, db_session, project_id: int) -> CostModel:
return (
db_session.query(CostModel)
.filter(CostModel.project_id == project_id)
.order_by(CostModel.created_at.desc())
.order_by(CostModel.created_at.asc())
.first()
)


def get_all(*, db_session, project_id: int) -> list[CostModel]:
"""Returns all cost models."""
if project_id:
return db_session.query(CostModel).filter(CostModel.project_id == project_id)
return db_session.query(CostModel)
return db_session.query(CostModel).all()


def get_cost_model_activity_by_id(*, db_session, cost_model_activity_id: int) -> CostModelActivity:
Expand All @@ -70,7 +68,7 @@ def update_cost_model_activity(*, db_session, cost_model_activity_in: CostModelA
db_session=db_session, cost_model_activity_id=cost_model_activity_in.id
)

cost_model_activity.response_time_seconds = cost_model_activity_in.response_time_seconds
cost_model_activity.response_time_seconds = 0
cost_model_activity.enabled = cost_model_activity_in.enabled
cost_model_activity.plugin_event_id = cost_model_activity_in.plugin_event.id

Expand Down Expand Up @@ -104,12 +102,10 @@ def delete(*, db_session, cost_model_id: int):
db_session.commit()


def update(*, db_session, cost_model_in: CostModelUpdate) -> CostModel:
def update(*, db_session, cost_model_in: CostModelUpdate, cost_model_id: int) -> CostModel:
"""Updates a cost model."""
if not has_unique_plugin_event(cost_model_in):
raise KeyError("Unable to update cost model. Duplicate plugin event ids detected.")

cost_model = get_cost_model_by_id(db_session=db_session, cost_model_id=cost_model_in.id)
cost_model = get_cost_model_by_id(db_session=db_session, cost_model_id=cost_model_id)
if not cost_model:
raise ValueError("Unable to update cost model. No cost model found with that id.")

Expand All @@ -121,6 +117,8 @@ def update(*, db_session, cost_model_in: CostModelUpdate) -> CostModel:
cost_model_in.updated_at if cost_model_in.updated_at else datetime.utcnow()
)

cost_model.updated_at = cost_model.created_at

# Update all recognized activities. Delete all removed activities.
update_activities = []
delete_activities = []
Expand All @@ -132,7 +130,6 @@ def update(*, db_session, cost_model_in: CostModelUpdate) -> CostModel:
update_activities.append((activity, activity_in))
cost_model_in.activities.pop(idx_in)
updated = True
break
if updated:
continue

Expand All @@ -142,9 +139,6 @@ def update(*, db_session, cost_model_in: CostModelUpdate) -> CostModel:
for activity, activity_in in update_activities:
activity.response_time_seconds = activity_in.response_time_seconds
activity.enabled = activity_in.enabled
activity.plugin_event = plugin_service.get_plugin_event_by_id(
db_session=db_session, plugin_event_id=activity_in.plugin_event.id
)

for activity in delete_activities:
cost_model_service.delete_cost_model_activity(
Expand All @@ -169,8 +163,6 @@ def update(*, db_session, cost_model_in: CostModelUpdate) -> CostModel:

def create(*, db_session, cost_model_in: CostModelCreate) -> CostModel:
"""Creates a new cost model."""
if not has_unique_plugin_event(cost_model_in):
raise KeyError("Unable to update cost model. Duplicate plugin event ids detected.")

project = project_service.get_by_name_or_raise(
db_session=db_session, project_in=cost_model_in.project
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/cost_model/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_cost_model(

@router.put(
"/{cost_model_id}",
summary="Modifies an existing cost model.",
summary="Creates a new cost model.",
response_model=CostModelRead,
dependencies=[Depends(PermissionsDependency([SensitiveProjectActionPermission]))],
)
Expand Down