diff --git a/apps/devops/app/common/models/deployment/deployment.py b/apps/devops/app/common/models/deployment/deployment.py index c29feeb..2bcbdda 100644 --- a/apps/devops/app/common/models/deployment/deployment.py +++ b/apps/devops/app/common/models/deployment/deployment.py @@ -5,14 +5,11 @@ from beanie import Document from bson import ObjectId from pydantic import Field, field_validator from pydantic import BaseModel +from pymongo import IndexModel class Deployment(Document): - deployment_id: str = Field(alias="_id") - @field_validator("deployment_id", mode="before") - @classmethod - def convert_object_id(cls, v): - return str(v) + deployment_id: str deployment_stage: str deployment_status: Literal["started", "failed", "succeeded", "aborted"] @@ -35,11 +32,9 @@ class Deployment(Document): class Settings: name = "deployment" indexes = [ - [("deployment_product_id", 1), ("created_at", 1)], # Compound index - [("deployment_id", 1), ("deployment_status", 1)], # Compound index - - # somehow combo + unique errors out - # {"keys": [("deployment_id", 1), ("deployment_stage", 1)], "unique": True} # Unique compound index + IndexModel([("deployment_product_id", 1), ("created_at", 1)]), + IndexModel([("deployment_id", 1), ("deployment_status", 1)]), + IndexModel([("deployment_id", 1), ("deployment_stage", 1)], unique=True) ] class InitDeploymentRequest(BaseModel): diff --git a/apps/devops/app/routes/deployment/service.py b/apps/devops/app/routes/deployment/service.py index 8a74d76..9093d77 100644 --- a/apps/devops/app/routes/deployment/service.py +++ b/apps/devops/app/routes/deployment/service.py @@ -1,3 +1,4 @@ +import uuid from collections import defaultdict from datetime import datetime, timedelta from typing import List @@ -32,12 +33,13 @@ class DeploymentService: project_name = "TODO" # retrieve product info - product_id = "TODO" + product_id = request.product_id product_name = "TODO" deployment = Deployment.model_construct( + deployment_id = str(uuid.uuid4()), deployment_stage = "init", deployment_status = "started", deployment_target_env = request.target_env, @@ -74,11 +76,11 @@ class DeploymentService: ).to_list() grouped = defaultdict(list) for deployment in deployment_records: - grouped[deployment.deployment_product_id].append(deployment) + grouped[deployment.deployment_id].append(deployment) for deployment_list in grouped.values(): deployment_list.sort(key=lambda d: (d.created_at, d.updated_at), reverse=True) - latest_deployments = [deployments[-1] for deployments in grouped.values()] + return latest_deployments async def update_deployment_status( @@ -117,7 +119,7 @@ class DeploymentService: Retrieve git url by product id """ # TODO implement this function - pass + return "TODO-git_url" async def _check_if_project_initialized( self, @@ -128,7 +130,7 @@ class DeploymentService: Check if the project has been initialized """ # TODO implement this function - pass + return True async def _init_product( self,