(20241226) Started working on the server management system.
This commit is contained in:
+128
-11
@@ -36,13 +36,17 @@ sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
# For making data behaviour_models:
|
||||
from pydantic import BaseModel, Field, field_validator, PastDatetime, model_validator, AwareDatetime
|
||||
from pydantic import BaseModel, Field, field_validator, PastDatetime, model_validator, AwareDatetime, computed_field
|
||||
from typing import Optional, Literal, Union
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.date_time import date_time
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
@@ -76,6 +80,14 @@ 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"
|
||||
)
|
||||
|
||||
hostname: str = Field(
|
||||
description = "the hostname to identify the server",
|
||||
frozen = True
|
||||
@@ -106,8 +118,9 @@ class CoreServerInfoModel(BaseModel):
|
||||
frozen = True
|
||||
)
|
||||
|
||||
portNo: str | None = Field(
|
||||
portNo: int = Field(
|
||||
description = "the port no. that this service is running on",
|
||||
default = None,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
@@ -121,6 +134,12 @@ class CoreServerInfoModel(BaseModel):
|
||||
frozen = True
|
||||
)
|
||||
|
||||
description: str = Field(
|
||||
description = "a brief description about this service",
|
||||
max_length = 350,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
healthCheckUrl: str = Field(
|
||||
description = "a get request will be sent to this url to see if the server is up",
|
||||
frozen = True
|
||||
@@ -143,7 +162,7 @@ class CoreServerInfoModel(BaseModel):
|
||||
frozen = False
|
||||
)
|
||||
|
||||
batchId: str | None = Field(
|
||||
batchId: ObjectId | 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
|
||||
@@ -157,23 +176,26 @@ class CoreServerInfoModel(BaseModel):
|
||||
|
||||
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 = Field(
|
||||
lastCheckTs: AwareDatetime | None = Field(
|
||||
description = "the last time (utc) this server was checked for being online",
|
||||
default = None,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
checkAfterTs: AwareDatetime = Field(
|
||||
description = "the time (utc) after which this server needs to be checked again for being online",
|
||||
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)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
@@ -182,6 +204,7 @@ class CoreServerInfoModel(BaseModel):
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
@@ -200,7 +223,12 @@ class CoreServerInfoModel(BaseModel):
|
||||
value = date_time.parse_date_time(
|
||||
input_value = value,
|
||||
timezone = date_time.TIMEZONE_UTC,
|
||||
date_formats = ["%Y-%m-%d"]
|
||||
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,
|
||||
@@ -217,13 +245,77 @@ class CoreServerInfoModel(BaseModel):
|
||||
@field_validator(
|
||||
"batchTs",
|
||||
"firstRegTs", "lastRegTs",
|
||||
"lastCheckTs", "checkAfterTs",
|
||||
"lastCheckTs",
|
||||
mode = "before"
|
||||
)
|
||||
def parse_given_date_time(cls, value):
|
||||
return cls.parse_date_time(value)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class CoreServerCheckLogModel(BaseModel):
|
||||
|
||||
serverCheckId: ObjectId | None = Field(
|
||||
description = "the id of the document in mongodb that holds the info. about this check",
|
||||
default = None,
|
||||
frozen = True,
|
||||
exclude = True,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
serverId: ObjectId = Field(
|
||||
description = "the id of the server that we checked",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
ts: AwareDatetime = Field(
|
||||
description = "the time at which this check was made",
|
||||
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False),
|
||||
frozen = True
|
||||
)
|
||||
|
||||
latency: float | None = Field(
|
||||
description = "the time the server took to respond to the health check",
|
||||
default = None,
|
||||
frozen = False
|
||||
)
|
||||
|
||||
online: bool = Field(
|
||||
description = "whether, or not, the server was online during this check",
|
||||
default = False,
|
||||
frozen = False
|
||||
)
|
||||
|
||||
alertRaised: bool | None = Field(
|
||||
description = "whether, or not, the alert url was successfully called (200 response)",
|
||||
default = None,
|
||||
frozen = False
|
||||
)
|
||||
|
||||
message: str | None = Field(
|
||||
description = "any additional message about this check",
|
||||
default = None,
|
||||
frozen = False
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
@@ -233,4 +325,29 @@ class CoreServerInfoModel(BaseModel):
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
pass
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user