refactor(config): 重构配置模块并优化应用依赖注入
- 将配置相关类移动到model模块 - 实现依赖注入容器管理各组件依赖关系 - 重构配置加载逻辑支持多层级键值查找 - 更新主应用入口支持命令行参数解析 - 统一日志输出格式替换原有打印语句 - 引入钻井实时数据模型简化数据处理 - 移除硬编码字段映射改用动态配置方式 - 优化数据库写入逻辑基于新的数据模型
This commit is contained in:
41
db/config.py
Normal file
41
db/config.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from model import TdengineConfig
|
||||
|
||||
|
||||
def parse_taos_url(jdbc_url):
|
||||
if not jdbc_url:
|
||||
return "", ""
|
||||
raw = str(jdbc_url).strip()
|
||||
if raw.lower().startswith("jdbc:taos-rs://"):
|
||||
raw = "http://" + raw[len("jdbc:TAOS-RS://") :]
|
||||
elif "://" not in raw:
|
||||
raw = "http://" + raw
|
||||
parsed = urlparse(raw)
|
||||
base_url = f"{parsed.scheme or 'http'}://{parsed.hostname or '127.0.0.1'}:{parsed.port or 6041}"
|
||||
database = (parsed.path or "").strip("/")
|
||||
return base_url, database
|
||||
|
||||
|
||||
def load_tdengine_config(cfg_or_app, default_device_code="GJ-304-0088"):
|
||||
config = getattr(cfg_or_app, "tdengine", None)
|
||||
if isinstance(config, TdengineConfig):
|
||||
url = config.url
|
||||
_, db_from_url = parse_taos_url(url)
|
||||
if config.database:
|
||||
return config
|
||||
return TdengineConfig(
|
||||
url=config.url,
|
||||
username=config.username,
|
||||
password=config.password,
|
||||
database=db_from_url,
|
||||
stable=config.stable,
|
||||
device_code=config.device_code or default_device_code,
|
||||
pool_size=config.pool_size,
|
||||
timeout=config.timeout,
|
||||
)
|
||||
_, db_from_url = parse_taos_url("")
|
||||
return TdengineConfig(database=db_from_url, device_code=default_device_code)
|
||||
|
||||
|
||||
TDengineConfig = TdengineConfig
|
||||
41
db/orm.py
41
db/orm.py
@@ -1,5 +1,6 @@
|
||||
import re
|
||||
import time
|
||||
|
||||
from model import DrillingRealtimeData
|
||||
|
||||
|
||||
DB_COLUMNS = [
|
||||
@@ -50,7 +51,6 @@ DB_COLUMNS = [
|
||||
"space5",
|
||||
]
|
||||
|
||||
|
||||
INT_COLUMNS = {"stknum", "recid", "seqid", "actcod", "rpma", "spm1", "spm2", "spm3", "mfop", "stkc", "lagstks"}
|
||||
|
||||
|
||||
@@ -67,20 +67,6 @@ def sql_quote(value):
|
||||
return "'" + str(value).replace("'", "''") + "'"
|
||||
|
||||
|
||||
def to_int(value, default=0):
|
||||
try:
|
||||
return int(value)
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def to_float(value, default=0.0):
|
||||
try:
|
||||
return float(value)
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
class DrillingRealtimeORM:
|
||||
def __init__(self, database, stable="drilling_realtime_st", default_device_code="GJ-304-0088"):
|
||||
self.database = database
|
||||
@@ -90,30 +76,23 @@ class DrillingRealtimeORM:
|
||||
def build_insert_sql(self, payload):
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError("payload is not JSON object")
|
||||
entity = DrillingRealtimeData.from_payload(payload)
|
||||
meta = payload.get("meta") if isinstance(payload.get("meta"), dict) else {}
|
||||
data = payload.get("data") if isinstance(payload.get("data"), dict) else {}
|
||||
|
||||
equipment_code = (
|
||||
str(self.default_device_code).strip()
|
||||
or str(meta.get("equipment_code", "")).strip()
|
||||
or str(meta.get("equipment_sn", "")).strip()
|
||||
or entity.wellid
|
||||
or "GJ-304-0088"
|
||||
)
|
||||
table_name = sanitize_identifier(
|
||||
f"drilling_realtime_{equipment_code}",
|
||||
"drilling_realtime_default",
|
||||
)
|
||||
|
||||
table_name = sanitize_identifier(f"drilling_realtime_{equipment_code}", "drilling_realtime_default")
|
||||
values = []
|
||||
for col in DB_COLUMNS:
|
||||
if col == "ts":
|
||||
raw = data.get("ts", data.get("record_time", int(time.time() * 1000)))
|
||||
values.append(str(to_int(raw, int(time.time() * 1000))))
|
||||
elif col in INT_COLUMNS:
|
||||
values.append(str(to_int(data.get(col, 0), 0)))
|
||||
for column in DB_COLUMNS:
|
||||
raw = getattr(entity, column)
|
||||
if column in INT_COLUMNS or column == "ts":
|
||||
values.append(str(int(raw)))
|
||||
else:
|
||||
values.append(str(to_float(data.get(col, 0), 0.0)))
|
||||
|
||||
values.append(str(float(raw)))
|
||||
columns_sql = ", ".join([f"`{column}`" for column in DB_COLUMNS])
|
||||
values_sql = ", ".join(values)
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user