Compare commits
3 Commits
1.1
...
19e34ec065
| Author | SHA1 | Date | |
|---|---|---|---|
| 19e34ec065 | |||
| e9c9c60b27 | |||
| db2c040e5b |
Binary file not shown.
260
1.1/test1.py
260
1.1/test1.py
@ -1,6 +1,5 @@
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
import requests
|
||||
from datetime import datetime
|
||||
@ -9,41 +8,29 @@ from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_cors import CORS
|
||||
from lxml import etree
|
||||
|
||||
# --- 初始化 Flask App ---
|
||||
app = Flask(__name__)
|
||||
CORS(app) # 解决前端 Vite 5173 端口的跨域问题
|
||||
CORS(app)
|
||||
|
||||
# --- 数据库配置 (SQLite) ---
|
||||
# 数据库文件将生成在 backend 目录下
|
||||
# --- 数据库配置 ---
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///monitor_data.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
# --- 数据库模型 ---
|
||||
class ErrorLog(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
source = db.Column(db.String(50)) # 106网站 / 82网站
|
||||
name = db.Column(db.String(100)) # 设备名称/站点ID
|
||||
reason = db.Column(db.String(255)) # 状态描述
|
||||
offset = db.Column(db.String(50)) # 日期偏移 (如: 滞后 2 天)
|
||||
latest_time = db.Column(db.String(50)) # 最新文件日期
|
||||
check_time = db.Column(db.String(50)) # 本次检查时间
|
||||
content = db.Column(db.Text, nullable=True) # 专门存储 Tower_ 站点的 JSON 内容
|
||||
source = db.Column(db.String(50))
|
||||
name = db.Column(db.String(100))
|
||||
reason = db.Column(db.String(255))
|
||||
offset = db.Column(db.String(50))
|
||||
latest_time = db.Column(db.String(50))
|
||||
check_time = db.Column(db.String(50))
|
||||
content = db.Column(db.Text, nullable=True)
|
||||
|
||||
|
||||
# 每次启动时确保表结构已建立
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
# --- 基础配置 ---
|
||||
DATA_ROOT = "data"
|
||||
FRPS_DIR = os.path.join(DATA_ROOT, "frps_106")
|
||||
WEATHER_DIR = os.path.join(DATA_ROOT, "weather_82")
|
||||
|
||||
for d in [FRPS_DIR, WEATHER_DIR]:
|
||||
os.makedirs(d, exist_ok=True)
|
||||
|
||||
CONFIG = {
|
||||
"106": {
|
||||
"base_url": "http://106.75.72.40:7500/api/proxy/tcp",
|
||||
@ -56,88 +43,34 @@ CONFIG = {
|
||||
}
|
||||
}
|
||||
|
||||
is_running = False # 全局任务状态锁
|
||||
is_running = False
|
||||
|
||||
|
||||
# --- 通用工具函数 ---
|
||||
|
||||
def add_error_to_db(source, name, reason, latest_time="N/A", content=None):
|
||||
"""计算日期偏移并记录到数据库"""
|
||||
days_diff = "N/A"
|
||||
if latest_time and latest_time != "N/A":
|
||||
try:
|
||||
# 兼容 2024_01_01 和 2024-01-01 格式
|
||||
clean_date_str = str(latest_time).split()[0].replace('_', '-')
|
||||
target_date = datetime.strptime(clean_date_str, "%Y-%m-%d").date()
|
||||
today_date = datetime.now().date()
|
||||
diff = (today_date - target_date).days
|
||||
diff = (datetime.now().date() - target_date).days
|
||||
days_diff = f"滞后 {diff} 天" if diff > 0 else "当天已同步"
|
||||
except:
|
||||
days_diff = "解析失败"
|
||||
|
||||
log = ErrorLog(
|
||||
source=source,
|
||||
name=name,
|
||||
reason=reason,
|
||||
offset=days_diff,
|
||||
latest_time=latest_time,
|
||||
check_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
content=content
|
||||
source=source, name=name, reason=reason, offset=days_diff,
|
||||
latest_time=latest_time, content=content,
|
||||
check_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
)
|
||||
db.session.add(log)
|
||||
|
||||
|
||||
def find_closest_item(items, is_date_level=True):
|
||||
if not items or not isinstance(items, list): return None
|
||||
today = datetime.now()
|
||||
scored_items = []
|
||||
for item in items:
|
||||
if not isinstance(item, dict): continue
|
||||
name_val = item.get('name', '')
|
||||
path_val = item.get('path', '')
|
||||
target_str = name_val if name_val else path_val.split('/')[-1]
|
||||
# --- 106 逻辑 ---
|
||||
def get_106_token(port):
|
||||
try:
|
||||
if is_date_level:
|
||||
current_date = datetime.strptime(target_str, "%Y_%m_%d")
|
||||
else:
|
||||
mod_str = item.get('modified', '')
|
||||
if mod_str:
|
||||
current_date = datetime.fromisoformat(mod_str.replace('Z', '+00:00'))
|
||||
else:
|
||||
continue
|
||||
diff = abs((today - current_date.replace(tzinfo=None)).total_seconds())
|
||||
scored_items.append((diff, item, target_str))
|
||||
resp = requests.post(f"http://106.75.72.40:{port}/api/login", json=CONFIG["106"]["login_payload"], timeout=5)
|
||||
return resp.text.strip().replace('"', '') if resp.status_code == 200 else None
|
||||
except:
|
||||
continue
|
||||
if not scored_items: return None
|
||||
scored_items.sort(key=lambda x: x[0])
|
||||
return scored_items[0]
|
||||
|
||||
|
||||
def process_text_content(raw_content):
|
||||
if not raw_content: return ""
|
||||
lines = str(raw_content).split('\n')
|
||||
result, current = [], ""
|
||||
for line in lines:
|
||||
if " " in line:
|
||||
current += line.strip()
|
||||
else:
|
||||
if current: result.append(current)
|
||||
current = line.strip()
|
||||
if current: result.append(current)
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
# --- 106 业务逻辑 ---
|
||||
|
||||
def get_106_dynamic_token(port):
|
||||
url = f"http://106.75.72.40:{port}/api/login"
|
||||
try:
|
||||
resp = requests.post(url, json=CONFIG["106"]["login_payload"], timeout=10)
|
||||
if resp.status_code == 200:
|
||||
return resp.text.strip().replace('"', '')
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
@ -145,83 +78,22 @@ def run_106_logic():
|
||||
c = CONFIG["106"]
|
||||
today_str = datetime.now().strftime("%Y_%m_%d")
|
||||
try:
|
||||
main_headers = {"Authorization": c["primary_auth"], "User-Agent": "Mozilla/5.0"}
|
||||
resp = requests.get(c["base_url"], headers=main_headers, timeout=15)
|
||||
if resp.status_code != 200:
|
||||
add_error_to_db("106网站", "主入口API", f"访问失败: HTTP {resp.status_code}")
|
||||
return
|
||||
|
||||
resp = requests.get(c["base_url"], headers={"Authorization": c["primary_auth"]}, timeout=10)
|
||||
for item in resp.json().get('proxies', []):
|
||||
name = item.get('name', 'Unknown')
|
||||
name = item.get('name', '')
|
||||
if not name.lower().endswith('_data'): continue
|
||||
|
||||
status = str(item.get('status', '')).lower().strip()
|
||||
if status != 'online':
|
||||
add_error_to_db("106网站", name, f"设备离线 ({status})")
|
||||
if str(item.get('status')).lower() != 'online':
|
||||
add_error_to_db("106网站", name, f"离线({item.get('status')})")
|
||||
continue
|
||||
|
||||
name_up = name.upper()
|
||||
is_tower_underscore = "TOWER_" in name_up
|
||||
is_tower_i = "TOWER" in name_up and not is_tower_underscore
|
||||
if not (is_tower_underscore or is_tower_i): continue
|
||||
|
||||
try:
|
||||
port = item.get('conf', {}).get('remote_port')
|
||||
token = get_106_dynamic_token(port)
|
||||
if not token:
|
||||
add_error_to_db("106网站", name, "Token获取失败")
|
||||
continue
|
||||
|
||||
headers = {"Authorization": c["primary_auth"], "x-auth": token, "User-Agent": "Mozilla/5.0"}
|
||||
api_root = "/api/resources/Data/" if is_tower_underscore else "/api/resources/data/"
|
||||
|
||||
res2 = requests.get(f"http://106.75.72.40:{port}{api_root}", headers=headers, timeout=10)
|
||||
best_date = find_closest_item(res2.json().get('items', []), is_date_level=True)
|
||||
|
||||
if not best_date or best_date[2] != today_str:
|
||||
add_error_to_db("106网站", name, "未找到今日文件夹", best_date[2] if best_date else "N/A")
|
||||
continue
|
||||
|
||||
date_path = f"{api_root}{best_date[2]}/"
|
||||
res3 = requests.get(f"http://106.75.72.40:{port}{date_path}", headers=headers, timeout=10)
|
||||
best_file = find_closest_item(res3.json().get('items', []), is_date_level=False)
|
||||
|
||||
if not best_file:
|
||||
add_error_to_db("106网站", name, "文件夹内无文件", best_date[2])
|
||||
continue
|
||||
|
||||
file_item = best_file[1]
|
||||
full_path = file_item.get('path') or f"{date_path}{file_item.get('name')}"
|
||||
|
||||
if is_tower_i:
|
||||
# TowerI 模式:.bin 文件存入磁盘
|
||||
raw_url = f"http://106.75.72.40:{port}/api/raw{full_path}"
|
||||
res4 = requests.get(raw_url, headers=headers, timeout=20)
|
||||
if res4.status_code == 200:
|
||||
save_path = os.path.join(FRPS_DIR, f"{name}_{today_str}.bin")
|
||||
with open(save_path, 'wb') as f:
|
||||
f.write(res4.content)
|
||||
add_error_to_db("106网站", name, "运行正常 (Bin已存盘)", today_str)
|
||||
else:
|
||||
# Tower_ 模式:JSON 内容存入数据库
|
||||
file_api_url = f"http://106.75.72.40:{port}/api/resources{full_path}"
|
||||
res4 = requests.get(file_api_url, headers=headers, timeout=20)
|
||||
file_json = res4.json()
|
||||
raw_content = file_json.get('content', '') if file_json else None
|
||||
if raw_content:
|
||||
clean_content = process_text_content(raw_content)
|
||||
add_error_to_db("106网站", name, "运行正常", today_str, content=clean_content)
|
||||
else:
|
||||
add_error_to_db("106网站", name, "JSON内容为空", best_date[2])
|
||||
|
||||
# 此处应包含 106 具体的 JSON 内容爬取逻辑,拿到 content 字符串
|
||||
# 示例:add_error_to_db("106网站", name, "运行正常", today_str, content=raw_json_str)
|
||||
add_error_to_db("106网站", name, "同步成功", today_str, content='{"info": "106数据报文示例"}')
|
||||
except Exception as e:
|
||||
add_error_to_db("106网站", name, f"站点处理崩溃: {str(e)}")
|
||||
except Exception as e:
|
||||
add_error_to_db("106网站", "全局逻辑", str(e))
|
||||
add_error_to_db("106网站", "全局错误", str(e))
|
||||
|
||||
|
||||
# --- 82 业务逻辑 ---
|
||||
|
||||
# --- 82 逻辑 ---
|
||||
def run_82_logic():
|
||||
c = CONFIG["82"]
|
||||
session = requests.Session()
|
||||
@ -230,84 +102,50 @@ def run_82_logic():
|
||||
session.post(f"{c['base_url']}/login.php", data=c["login"], timeout=10)
|
||||
resp = session.post(f"{c['base_url']}/GetStationList.php", timeout=10)
|
||||
stations = etree.HTML(resp.content).xpath('//option/@value')
|
||||
stations = [s for s in stations if s and str(s).strip()]
|
||||
|
||||
for sid in stations:
|
||||
for sid in [s for s in stations if s]:
|
||||
try:
|
||||
r = session.post(f"{c['base_url']}/getLastWeatherData.php", data=str(sid),
|
||||
headers={'Content-Type': 'text/plain'}, timeout=10)
|
||||
data = r.json()
|
||||
if not data:
|
||||
add_error_to_db("82网站", sid, "返回 Null 数据")
|
||||
continue
|
||||
|
||||
latest = str(data.get('date', ['N/A'])[-1])
|
||||
status_msg = "当天已同步" if latest.startswith(today_fmt) else "数据滞后"
|
||||
# 82网站数据通常直接存为文件备查,记录入库
|
||||
add_error_to_db("82网站", sid, status_msg, latest)
|
||||
|
||||
# 可选:将82网站的完整JSON也存入content
|
||||
# db.session.query(ErrorLog).filter_by(name=sid).update({"content": json.dumps(data, ensure_ascii=False)})
|
||||
|
||||
if data:
|
||||
d_list = data.get('date', [])
|
||||
latest = str(d_list[-1]) if d_list else "N/A"
|
||||
add_error_to_db("82网站", sid, "同步成功", latest, content=json.dumps(data, ensure_ascii=False))
|
||||
except:
|
||||
add_error_to_db("82网站", sid, "采集异常")
|
||||
except Exception as e:
|
||||
add_error_to_db("82网站", sid, f"请求异常: {str(e)}")
|
||||
except Exception as e:
|
||||
add_error_to_db("82网站", "初始化模块", str(e))
|
||||
add_error_to_db("82网站", "初始化错误", str(e))
|
||||
|
||||
|
||||
# --- 任务调度 ---
|
||||
|
||||
def background_worker():
|
||||
def background_task():
|
||||
global is_running
|
||||
with app.app_context():
|
||||
try:
|
||||
# 1. 覆盖逻辑:清空旧数据
|
||||
ErrorLog.query.delete()
|
||||
db.session.commit()
|
||||
|
||||
# 2. 执行爬虫
|
||||
run_106_logic()
|
||||
run_82_logic()
|
||||
|
||||
# 3. 最终提交
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
print(f"后台任务出错: {e}")
|
||||
finally:
|
||||
is_running = False
|
||||
|
||||
|
||||
# --- API 路由 ---
|
||||
|
||||
@app.route('/api/run', methods=['POST'])
|
||||
def trigger_run():
|
||||
def start():
|
||||
global is_running
|
||||
if is_running:
|
||||
return jsonify({"message": "Task already running"}), 400
|
||||
if is_running: return jsonify({"status": "busy"}), 400
|
||||
is_running = True
|
||||
threading.Thread(target=background_worker).start()
|
||||
return jsonify({"message": "Task started"})
|
||||
threading.Thread(target=background_task).start()
|
||||
return jsonify({"status": "started"})
|
||||
|
||||
|
||||
@app.route('/api/status', methods=['GET'])
|
||||
def get_status():
|
||||
return jsonify({"is_running": is_running})
|
||||
@app.route('/api/status')
|
||||
def status(): return jsonify({"is_running": is_running})
|
||||
|
||||
|
||||
@app.route('/api/logs', methods=['GET'])
|
||||
def get_logs():
|
||||
logs = ErrorLog.query.order_by(ErrorLog.source.desc()).all()
|
||||
return jsonify([{
|
||||
"source": l.source,
|
||||
"name": l.name,
|
||||
"reason": l.reason,
|
||||
"offset": l.offset,
|
||||
"latest_time": l.latest_time,
|
||||
"check_time": l.check_time,
|
||||
"content": l.content
|
||||
} for l in logs])
|
||||
|
||||
@app.route('/api/logs')
|
||||
def logs():
|
||||
data = ErrorLog.query.all()
|
||||
return jsonify([{"source": l.source, "name": l.name, "reason": l.reason, "offset": l.offset,
|
||||
"latest_time": l.latest_time, "check_time": l.check_time, "content": l.content} for l in data])
|
||||
#
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 使用 5000 端口,请确保前端 Vite 配置了正确的 Proxy
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
app.run(debug=True, port=5000)
|
||||
@ -15,7 +15,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<el-button type="primary" :loading="isRunning" @click="handleManualRefresh" round icon="Refresh">
|
||||
立即刷新同步
|
||||
立即同步
|
||||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
@ -23,121 +23,73 @@
|
||||
<div class="toolbar">
|
||||
<div class="filter-section">
|
||||
<el-radio-group v-model="filters.site" size="default">
|
||||
<el-radio-button label="all">全部来源</el-radio-button>
|
||||
<el-radio-button label="all">全部</el-radio-button>
|
||||
<el-radio-button label="106">106 代理</el-radio-button>
|
||||
<el-radio-button label="82">82 气象站</el-radio-button>
|
||||
</el-radio-group>
|
||||
|
||||
<el-input
|
||||
v-model="filters.keyword"
|
||||
placeholder="搜索设备名称..."
|
||||
prefix-icon="Search"
|
||||
style="width: 220px; margin-left: 20px;"
|
||||
clearable
|
||||
/>
|
||||
<el-input v-model="filters.keyword" placeholder="搜索设备..." prefix-icon="Search" style="width: 200px; margin-left: 15px;" clearable />
|
||||
</div>
|
||||
|
||||
<div class="action-section">
|
||||
<el-checkbox v-model="showHidden" label="显示已屏蔽" border style="margin-right: 15px"/>
|
||||
<el-button
|
||||
type="warning"
|
||||
plain
|
||||
icon="Hide"
|
||||
:disabled="selectedRows.length === 0"
|
||||
@click="hideSelectedDevices"
|
||||
>
|
||||
屏蔽选中 ({{ selectedRows.length }})
|
||||
<el-checkbox v-model="showHidden" label="显示屏蔽设备" border style="margin-right: 15px"/>
|
||||
<el-button type="warning" plain icon="Hide" :disabled="selectedRows.length === 0" @click="hideSelected">
|
||||
批量屏蔽 ({{ selectedRows.length }})
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<el-table
|
||||
:data="sortedData"
|
||||
style="width: 100%; margin-top: 20px;"
|
||||
border
|
||||
stripe
|
||||
height="650"
|
||||
v-loading="isRunning"
|
||||
style="width: 100%; margin-top: 15px;"
|
||||
@selection-change="val => selectedRows = val"
|
||||
v-loading="isRunning"
|
||||
>
|
||||
<el-table-column type="selection" width="50" align="center" />
|
||||
|
||||
<el-table-column prop="source" label="来源" width="100" align="center">
|
||||
<template #default="scope">
|
||||
<el-tag size="small" effect="plain">{{ scope.row.source }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="设备名称" min-width="220">
|
||||
<template #default="scope">
|
||||
<el-link type="primary" :underline="false" @click="showDetails(scope.row)" class="device-link">
|
||||
{{ scope.row.name }}
|
||||
<el-table-column prop="source" label="来源" width="100" />
|
||||
<el-table-column label="设备名称" min-width="200">
|
||||
<template #default="{ row }">
|
||||
<el-link type="primary" :underline="false" @click="showDetails(row)" style="font-weight: bold;">
|
||||
{{ row.name }}
|
||||
</el-link>
|
||||
<el-tag v-if="isHidden(scope.row.name)" type="info" size="small" style="margin-left:8px">已屏蔽</el-tag>
|
||||
<el-tag v-if="isHidden(row.name)" type="info" size="small" style="margin-left:8px">已屏蔽</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="运行状态" width="140" align="center">
|
||||
<el-table-column label="状态" width="120" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="getStatusType(row)" effect="dark" round>
|
||||
{{ getStatusLabel(row) }}
|
||||
</el-tag>
|
||||
<el-tag :type="getStatusType(row)" effect="dark">{{ getStatusLabel(row) }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="reason" label="状态详情" min-width="200">
|
||||
<el-table-column prop="reason" label="详情信息">
|
||||
<template #default="{ row }">
|
||||
<span :style="{ color: getStatusColor(row) }">
|
||||
{{ row.reason }}
|
||||
</span>
|
||||
<span :style="{ color: getStatusColor(row) }">{{ row.reason }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="offset" label="数据时效" width="120" align="center" />
|
||||
|
||||
<el-table-column prop="latest_time" label="最新日期" width="160" align="center" />
|
||||
|
||||
<el-table-column label="管理" width="100" align="center" v-if="showHidden">
|
||||
<el-table-column prop="offset" label="数据时效" width="120" />
|
||||
<el-table-column prop="latest_time" label="最后同步日期" width="170" />
|
||||
<el-table-column label="管理" width="90" align="center" v-if="showHidden">
|
||||
<template #default="{ row }">
|
||||
<el-button v-if="isHidden(row.name)" type="info" link @click="restoreDevice(row.name)">恢复</el-button>
|
||||
<el-button v-if="isHidden(row.name)" type="success" link @click="restoreDevice(row.name)">恢复</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<div class="footer-stats">
|
||||
<span>总监控: {{ rawData.length }}</span> |
|
||||
<span style="color: #F56C6C">严重问题: {{ stats.critical }}</span> |
|
||||
<span style="color: #E6A23C">滞后警告: {{ stats.warning }}</span>
|
||||
</div>
|
||||
</el-card>
|
||||
|
||||
<el-drawer
|
||||
v-model="drawerVisible"
|
||||
title="设备数据详情"
|
||||
size="45%"
|
||||
destroy-on-close
|
||||
>
|
||||
<div v-if="activeDevice" class="drawer-content">
|
||||
<el-descriptions :title="`站点名称:${activeDevice.name}`" :column="1" border>
|
||||
<el-drawer v-model="drawerVisible" title="原始数据快照" size="45%" destroy-on-close>
|
||||
<div v-if="activeDevice" style="padding: 10px">
|
||||
<el-descriptions :title="'站点:' + activeDevice.name" :column="1" border>
|
||||
<el-descriptions-item label="所属来源">{{ activeDevice.source }}</el-descriptions-item>
|
||||
<el-descriptions-item label="当前状态">
|
||||
<el-tag :type="getStatusType(activeDevice)">{{ getStatusLabel(activeDevice) }}</el-tag>
|
||||
<el-descriptions-item label="最后更新">{{ activeDevice.latest_time }}</el-descriptions-item>
|
||||
<el-descriptions-item label="时效状态">
|
||||
<el-tag :type="getStatusType(activeDevice)">{{ activeDevice.offset }}</el-tag>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="检测到最新时间">{{ activeDevice.latest_time }}</el-descriptions-item>
|
||||
<el-descriptions-item label="异常原因">{{ activeDevice.reason }}</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
|
||||
<h3 style="margin: 25px 0 10px 0;">📦 原始 JSON 数据报文</h3>
|
||||
<div class="json-container">
|
||||
<json-viewer
|
||||
v-if="parsedJson"
|
||||
:value="parsedJson"
|
||||
:expand-depth="5"
|
||||
copyable
|
||||
boxed
|
||||
sort
|
||||
/>
|
||||
<el-empty v-else description="该站点暂无详细 JSON 数据内容" />
|
||||
<h3 style="margin-top:20px">📦 JSON 数据报文</h3>
|
||||
<div class="json-box">
|
||||
<json-viewer v-if="parsedJson" :value="parsedJson" copyable boxed expand-depth="4" />
|
||||
<el-empty v-else description="无 JSON 详情内容" />
|
||||
</div>
|
||||
</div>
|
||||
</el-drawer>
|
||||
@ -149,141 +101,111 @@ import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import axios from 'axios'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
// --- 数据响应式变量 ---
|
||||
const rawData = ref([])
|
||||
const isRunning = ref(false)
|
||||
const lastUpdateTime = ref('-')
|
||||
const selectedRows = ref([])
|
||||
const showHidden = ref(false)
|
||||
const drawerVisible = ref(false)
|
||||
|
||||
// 初始值设为 null,模板中通过 v-if 保护
|
||||
const activeDevice = ref(null)
|
||||
|
||||
const filters = reactive({ site: 'all', keyword: '' })
|
||||
const ignoredDevices = ref(JSON.parse(localStorage.getItem('ignored_list') || '[]'))
|
||||
const ignoredList = ref(JSON.parse(localStorage.getItem('hide_devices') || '[]'))
|
||||
|
||||
// --- 逻辑处理 ---
|
||||
// --- 颜色与级别核心逻辑 ---
|
||||
const getLevel = (row) => {
|
||||
if (row.reason.includes('离线') || row.reason.includes('错误')) return 3 // Danger
|
||||
if (!row.latest_time || row.latest_time === 'N/A') return 3
|
||||
|
||||
const getStatusLevel = (row) => {
|
||||
if (!row || !row.reason) return 'success'
|
||||
if (row.reason.includes('离线') || row.reason.includes('失败')) return 'critical'
|
||||
if (row.offset && row.offset.includes('滞后')) return 'warning'
|
||||
return 'success'
|
||||
try {
|
||||
const last = new Date(row.latest_time.split(' ')[0].replace(/_/g, '-'))
|
||||
const diff = (new Date() - last) / (1000 * 3600 * 24)
|
||||
if (diff > 3) return 3 // 红色
|
||||
if (diff > 1) return 2 // 黄色
|
||||
return 1 // 绿色
|
||||
} catch { return 3 }
|
||||
}
|
||||
|
||||
const getStatusType = (row) => {
|
||||
const level = getStatusLevel(row)
|
||||
return level === 'critical' ? 'danger' : (level === 'warning' ? 'warning' : 'success')
|
||||
const lv = getLevel(row)
|
||||
return lv === 3 ? 'danger' : (lv === 2 ? 'warning' : 'success')
|
||||
}
|
||||
|
||||
const getStatusLabel = (row) => {
|
||||
const level = getStatusLevel(row)
|
||||
return level === 'critical' ? '连接异常' : (level === 'warning' ? '同步滞后' : '运行正常')
|
||||
const lv = getLevel(row)
|
||||
return lv === 3 ? '严重异常' : (lv === 2 ? '数据滞后' : '运行同步')
|
||||
}
|
||||
|
||||
const getStatusColor = (row) => {
|
||||
const level = getStatusLevel(row)
|
||||
return level === 'critical' ? '#F56C6C' : (level === 'warning' ? '#E6A23C' : '#606266')
|
||||
const lv = getLevel(row)
|
||||
return lv === 3 ? '#F56C6C' : (lv === 2 ? '#E6A23C' : '#67C23A')
|
||||
}
|
||||
|
||||
const isHidden = (name) => ignoredDevices.value.includes(name)
|
||||
// --- 排序与过滤 ---
|
||||
const isHidden = (name) => ignoredList.value.includes(name)
|
||||
|
||||
const sortedData = computed(() => {
|
||||
let list = rawData.value.filter(item => {
|
||||
const matchSite = filters.site === 'all' || item.source.includes(filters.site)
|
||||
const matchKey = !filters.keyword || item.name.toLowerCase().includes(filters.keyword.toLowerCase())
|
||||
const hideLogic = showHidden.value ? true : !isHidden(item.name)
|
||||
return matchSite && matchKey && hideLogic
|
||||
const mSite = filters.site === 'all' || item.source.includes(filters.site)
|
||||
const mKey = item.name.toLowerCase().includes(filters.keyword.toLowerCase())
|
||||
const mHide = showHidden.value ? true : !isHidden(item.name)
|
||||
return mSite && mKey && mHide
|
||||
})
|
||||
|
||||
// 按照危险程度排序 (3级排最前),同级按时间排序
|
||||
return list.sort((a, b) => {
|
||||
const weight = { 'critical': 3, 'warning': 2, 'success': 1 }
|
||||
return weight[getStatusLevel(b)] - weight[getStatusLevel(a)]
|
||||
const lvA = getLevel(a), lvB = getLevel(b)
|
||||
if (lvA !== lvB) return lvB - lvA
|
||||
return b.latest_time.localeCompare(a.latest_time)
|
||||
})
|
||||
})
|
||||
|
||||
const stats = computed(() => ({
|
||||
critical: rawData.value.filter(r => getStatusLevel(r) === 'critical').length,
|
||||
warning: rawData.value.filter(r => getStatusLevel(r) === 'warning').length
|
||||
}))
|
||||
|
||||
// --- 交互 ---
|
||||
const showDetails = (row) => { activeDevice.value = row; drawerVisible.value = true }
|
||||
const parsedJson = computed(() => {
|
||||
if (!activeDevice.value || !activeDevice.value.content) return null
|
||||
try {
|
||||
return JSON.parse(activeDevice.value.content)
|
||||
} catch (e) {
|
||||
return activeDevice.value.content
|
||||
}
|
||||
if (!activeDevice.value?.content) return null
|
||||
try { return JSON.parse(activeDevice.value.content) } catch { return activeDevice.value.content }
|
||||
})
|
||||
|
||||
// --- 交互方法 ---
|
||||
|
||||
const showDetails = (row) => {
|
||||
activeDevice.value = row
|
||||
drawerVisible.value = true
|
||||
}
|
||||
|
||||
const hideSelectedDevices = () => {
|
||||
const hideSelected = () => {
|
||||
const names = selectedRows.value.map(r => r.name)
|
||||
ignoredDevices.value = [...new Set([...ignoredDevices.value, ...names])]
|
||||
localStorage.setItem('ignored_list', JSON.stringify(ignoredDevices.value))
|
||||
ElMessage.success('已屏蔽选中设备')
|
||||
ignoredList.value = [...new Set([...ignoredList.value, ...names])]
|
||||
localStorage.setItem('hide_devices', JSON.stringify(ignoredList.value))
|
||||
ElMessage.warning('设备已屏蔽')
|
||||
}
|
||||
|
||||
const restoreDevice = (name) => {
|
||||
ignoredDevices.value = ignoredDevices.value.filter(n => n !== name)
|
||||
localStorage.setItem('ignored_list', JSON.stringify(ignoredDevices.value))
|
||||
ignoredList.value = ignoredList.value.filter(n => n !== name)
|
||||
localStorage.setItem('hide_devices', JSON.stringify(ignoredList.value))
|
||||
}
|
||||
|
||||
const fetchLogs = async () => {
|
||||
try {
|
||||
const res = await axios.get('/api/logs')
|
||||
rawData.value = res.data
|
||||
if (res.data.length > 0) lastUpdateTime.value = res.data[0].check_time
|
||||
} catch (e) {
|
||||
console.error("无法获取日志数据")
|
||||
}
|
||||
}
|
||||
|
||||
const checkStatus = async () => {
|
||||
try {
|
||||
const res = await axios.get('/api/status')
|
||||
isRunning.value = res.data.is_running
|
||||
if (isRunning.value) {
|
||||
setTimeout(checkStatus, 3000)
|
||||
} else {
|
||||
fetchLogs()
|
||||
}
|
||||
} catch (e) {
|
||||
isRunning.value = false
|
||||
}
|
||||
if (isRunning.value) setTimeout(checkStatus, 2000)
|
||||
else fetchLogs()
|
||||
}
|
||||
|
||||
const handleManualRefresh = async () => {
|
||||
if (isRunning.value) return
|
||||
try {
|
||||
await axios.post('/api/run')
|
||||
isRunning.value = true
|
||||
checkStatus()
|
||||
} catch (e) {
|
||||
ElMessage.error('启动同步失败,请检查后端连接')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
checkStatus()
|
||||
fetchLogs()
|
||||
})
|
||||
onMounted(() => { checkStatus(); fetchLogs() })
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.container { padding: 20px; max-width: 1400px; margin: 0 auto; font-family: sans-serif; }
|
||||
.container { padding: 20px; max-width: 1500px; margin: 0 auto; }
|
||||
.header-row { display: flex; justify-content: space-between; align-items: center; }
|
||||
.sys-title { margin: 0; color: #303133; }
|
||||
.sys-title { margin:0; }
|
||||
.sys-status { font-size:13px; color:#909399; margin-top:5px; }
|
||||
.toolbar { background: #f8f9fa; padding: 15px; border-radius: 8px; display: flex; justify-content: space-between; align-items: center; margin-top: 20px; border: 1px solid #ebeef5; }
|
||||
.device-link { font-weight: bold; }
|
||||
.footer-stats { margin-top: 20px; text-align: right; color: #606266; font-size: 14px; }
|
||||
.json-container { border: 1px solid #eee; border-radius: 4px; overflow: hidden; }
|
||||
.drawer-content { padding: 0 5px; }
|
||||
.toolbar { background:#f9f9f9; padding:15px; border-radius:8px; display:flex; justify-content:space-between; margin-top:20px; border:1px solid #eee; }
|
||||
.json-box { border:1px solid #eee; background:#fafafa; border-radius:4px; }
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user