From 83dbf0fcb82026f5ce1c27d5b23a51061511deed Mon Sep 17 00:00:00 2001 From: clearml <> Date: Thu, 5 Dec 2024 22:27:52 +0200 Subject: [PATCH] Add age_sec field to loading serving models Return serving instance charts sorted by instance name --- apiserver/bll/project/project_bll.py | 3 ++- apiserver/bll/serving/__init__.py | 22 ++++++++-------------- apiserver/bll/serving/stats.py | 6 +++--- apiserver/schema/services/serving.conf | 4 ++++ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/apiserver/bll/project/project_bll.py b/apiserver/bll/project/project_bll.py index 2a6036c..95a8661 100644 --- a/apiserver/bll/project/project_bll.py +++ b/apiserver/bll/project/project_bll.py @@ -40,7 +40,8 @@ from .sub_projects import ( _ids_with_children, _ids_with_parents, _get_project_depth, - ProjectsChildren, _get_writable_project_from_name, + ProjectsChildren, + _get_writable_project_from_name, ) log = config.logger(__file__) diff --git a/apiserver/bll/serving/__init__.py b/apiserver/bll/serving/__init__.py index 9d815b6..a8e80b2 100644 --- a/apiserver/bll/serving/__init__.py +++ b/apiserver/bll/serving/__init__.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from enum import Enum, auto from operator import attrgetter from time import time @@ -75,7 +75,7 @@ class ServingBLL: """ Register a serving container """ - now = datetime.utcnow() + now = datetime.now(timezone.utc) key = self._get_container_key(company_id, request.container_id) entry = ServingContainerEntry( **request.to_struct(), @@ -120,7 +120,7 @@ class ServingBLL: Serving container status report """ container_id = report.container_id - now = datetime.utcnow() + now = datetime.now(timezone.utc) entry = self._get_serving_container_entry(company_id, container_id) if entry: ip = ip or entry.ip @@ -252,7 +252,7 @@ class ServingBLL: "instances": len(entries), **{counter.name: counter() for counter in counters}, } - ret["last_update"] = self._naive_time(ret.get("last_update")) + ret["last_update"] = ret.get("last_update") return ret def get_endpoints(self, company_id: str): @@ -307,18 +307,12 @@ class ServingBLL: "input_type": entry.input_type, "input_size": entry.input_size, "uptime_sec": entry.uptime_sec, - "last_update": self._naive_time(entry.last_activity_time), + "age_sec": int((datetime.now(timezone.utc) - entry.register_time).total_seconds()), + "last_update": entry.last_activity_time, } for entry in entries ] - @staticmethod - def _naive_time(input_: datetime) -> datetime: - if not isinstance(input_, datetime): - return input_ - - return input_.replace(tzinfo=None) - def get_endpoint_details(self, company_id, endpoint_url: str) -> dict: entries = self._get_endpoint_entries(company_id, endpoint_url) if not entries: @@ -346,7 +340,7 @@ class ServingBLL: "model_source": first_entry.model_source, "model_version": first_entry.model_version, "uptime_sec": max(e.uptime_sec for e in entries), - "last_update": self._naive_time(max(e.last_activity_time for e in entries)), + "last_update": max(e.last_activity_time for e in entries), "instances": [ { "id": entry.container_id, @@ -354,7 +348,7 @@ class ServingBLL: "requests": entry.requests_num, "requests_min": entry.requests_min, "latency_ms": entry.latency_ms, - "last_update": self._naive_time(entry.last_activity_time), + "last_update": entry.last_activity_time, "reference": [ref.to_struct() for ref in entry.reference] if isinstance(entry.reference, list) else entry.reference, diff --git a/apiserver/bll/serving/stats.py b/apiserver/bll/serving/stats.py index 47aba59..6238151 100644 --- a/apiserver/bll/serving/stats.py +++ b/apiserver/bll/serving/stats.py @@ -1,5 +1,5 @@ from collections import defaultdict -from datetime import datetime +from datetime import datetime, timezone from enum import Enum from typing import Tuple, Optional, Sequence @@ -35,7 +35,7 @@ class ServingStats: @staticmethod def _get_es_index_suffix(): """Get the index name suffix for storing current month data""" - return datetime.utcnow().strftime("%Y-%m") + return datetime.now(timezone.utc).strftime("%Y-%m") @staticmethod def _get_average_value(value) -> Tuple[Optional[float], Optional[int]]: @@ -292,7 +292,7 @@ class ServingStats: hist_ret["total"]["values"] = cls.round_series(total) hist_ret["instances"] = { key: {"title": key, "dates": dates_, "values": cls.round_series(values)} - for key, values in instances.items() + for key, values in sorted(instances.items(), key=lambda p: p[0]) } return hist_ret diff --git a/apiserver/schema/services/serving.conf b/apiserver/schema/services/serving.conf index be990a9..86fb68a 100644 --- a/apiserver/schema/services/serving.conf +++ b/apiserver/schema/services/serving.conf @@ -190,6 +190,10 @@ _definitions { format: "date-time" description: The latest time when the container instance sent update } + age_sec { + type: integer + description: Amount of seconds since the container registration + } } } metrics_history_series {