(20241226) Started working on the server management system.

This commit is contained in:
2024-12-25 19:48:25 +05:30
parent 7607a427a6
commit 5051f73725
5 changed files with 446 additions and 630 deletions
+5 -119
View File
@@ -78,15 +78,7 @@ import datetime
# *****************************************************************************************************************
class CoreServerInfoModel(BaseModel):
serverId: ObjectId | None = Field(
description = "the id of the document in mongodb that holds the info. about this server",
default = None,
frozen = True,
exclude = True,
alias = "_id"
)
class RegisterServerRequestData(BaseModel):
hostname: str = Field(
description = "the hostname to identify the server",
@@ -118,7 +110,7 @@ class CoreServerInfoModel(BaseModel):
frozen = True
)
portNo: int | None = Field(
portNo: int = Field(
description = "the port no. that this service is running on",
default = None,
frozen = True
@@ -156,100 +148,19 @@ class CoreServerInfoModel(BaseModel):
frozen = True
)
online: bool = Field(
description = "whether this service is online or not",
default = False,
frozen = False
)
batchId: str | None = Field(
description = "set some value here when checking the status of this server, set it to null once done",
default = None,
frozen = False
)
batchTs: AwareDatetime | None = Field(
description = "set the time (utc) when checking the status of this server, set it to null once done",
default = None,
frozen = False
)
firstRegTs: AwareDatetime = Field(
description = "the first time (utc) this server was registered in the database",
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False),
frozen = True
)
lastRegTs: AwareDatetime = Field(
description = "the last time (utc) this server was registered in the database",
default_factory = lambda: date_time.get_current_utc_date_time(as_string=False),
frozen = True
)
lastCheckTs: AwareDatetime | None = Field(
description = "the last time (utc) this server was checked for being online",
default = None,
frozen = True
)
@computed_field
def checkAfterTs(self) -> datetime.datetime:
if self.lastCheckTs is None: return date_time.get_current_utc_date_time(as_string = False)
else: return self.lastCheckTs + datetime.timedelta(seconds = self.healthCheckInterval)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
arbitrary_types_allowed = True
extra = "forbid"
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@staticmethod
def parse_date_time(value):
# If a null value was given,
# we can't do anything:
if not value: value = None
# If the input is a string:
if isinstance(value, str):
value = value.strip()
value = date_time.parse_date_time(
input_value = value,
timezone = date_time.TIMEZONE_UTC,
date_formats = [
"%Y-%m-%d",
"%Y-%m-%d %M:%H:%S",
"%Y%m%d",
"%Y%m%d %M:%H:%S",
]
)
# If the input is already a date-time object,
# we just normalize the timestamp:
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(
value,
timezone = date_time.TIMEZONE_UTC
)
# Done here:
return value
@field_validator(
"batchTs",
"firstRegTs", "lastRegTs",
"lastCheckTs",
mode = "before"
)
def parse_given_date_time(cls, value):
return cls.parse_date_time(value)
pass
# *****************************************************************************************************************
@@ -261,29 +172,4 @@ class CoreServerInfoModel(BaseModel):
if __name__ == "__main__":
test_json = {
"hostname": "kbprod",
"os": "Ubuntu 22.04.5 LTS",
"cpu": "x86_64 (x86_64)",
"pid": 456,
"ppid": 123,
"ipAddr": "127.0.0.1",
"portNo": 5000,
"project": "Bicree",
"service": "user",
"description": "This is Bicree's user module.",
"healthCheckUrl": "https://v2.api.bicree.com/user/metrics/memory",
"healthCheckInterval": 30,
"healthAlertUrl": (
"https://nexcom.ditscentre.in/wtt/webhook/telegram/out"
"?appKey=9999999"
"&chatId=1275560043"
"&message=%F0%9F%9A%A8%20Bicree%27s%20user%20module%20is%20down%21%20Please%20check%20it%20urgently%21"
),
"online": True,
"batchId": None,
"batchTs": None
}
test_model = CoreServerInfoModel(**test_json)
print("TEST MODEL:", json.to_string(test_model.model_dump(), default = str))
pass