upload
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,79 @@
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import requests
|
||||
from flask import Flask, jsonify, render_template, request
|
||||
|
||||
TIMEOUT = 15 # 单次请求超时秒数
|
||||
MAX_WORKERS = 20 # 并发线程数
|
||||
PORT = 5000 # 前端服务端口
|
||||
BASE_URL = "http://ktl-001.dsfdl-henan.xyz/openApi"
|
||||
USER = "thzn-datagram0001"
|
||||
PWD = "thzn-datagram0001"
|
||||
VENDOR = "mt-datagram"
|
||||
|
||||
CITY_LIST = [ # 查询城市,重复的会去重
|
||||
'福州', '合肥', '北京', '重庆', '杭州', '兰州', '广州', '佛山',
|
||||
'上海', '南宁', '贵阳', '海口', '石家庄', '郑州', '哈尔滨',
|
||||
'武汉', '长沙', '长春', '南京', '南昌', '沈阳', '呼和浩特',
|
||||
'济南', '太原', '西安', '成都', '天津', '拉萨', '乌鲁木齐',
|
||||
'深圳', '西宁', '银川', '昆明', '苏州', '青岛', '东莞',
|
||||
'厦门', '无锡', '大连',
|
||||
]
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def _auth(username, password):
|
||||
token = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
return {"Authorization": f"Basic {token}"}
|
||||
|
||||
|
||||
def get_stats(stat_date, vendor_name, username, password, client_id=None):
|
||||
url = f"{BASE_URL}/getStats"
|
||||
params = {"date": stat_date, "vendorName": vendor_name}
|
||||
if client_id:
|
||||
params["clientId"] = client_id
|
||||
resp = requests.get(url, headers=_auth(username, password), params=params, timeout=TIMEOUT)
|
||||
resp.raise_for_status()
|
||||
return resp.json().get("data", {})
|
||||
|
||||
|
||||
def run_one(stat_date, clientid):
|
||||
try:
|
||||
result = get_stats(stat_date, VENDOR, USER, PWD, clientid)
|
||||
item = {"city": clientid, "success": result["success"], "reject": result["reject"], "error": None}
|
||||
except Exception as e:
|
||||
print(clientid, '请求失败', repr(e))
|
||||
item = {"city": clientid, "success": None, "reject": None, "error": f"{type(e).__name__}: {e}"}
|
||||
print(clientid, item)
|
||||
return item
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/api/stats")
|
||||
def api_stats():
|
||||
stat_date = request.args.get("date", "").strip()
|
||||
if not stat_date:
|
||||
return jsonify({"error": "date 不能为空"}), 400
|
||||
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as pool:
|
||||
items = list(pool.map(lambda city: run_one(stat_date, city), dict.fromkeys(CITY_LIST)))
|
||||
total_success = sum(item["success"] for item in items if item["success"] is not None)
|
||||
total_reject = sum(item["reject"] for item in items if item["reject"] is not None)
|
||||
failed_cities = [item["city"] for item in items if item["error"]]
|
||||
return jsonify({
|
||||
"date": stat_date,
|
||||
"total_success": total_success,
|
||||
"total_reject": total_reject,
|
||||
"failed_cities": failed_cities,
|
||||
"items": items,
|
||||
})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"前端地址: http://127.0.0.1:{PORT}")
|
||||
app.run(host="0.0.0.0", port=PORT, debug=False)
|
||||
@@ -0,0 +1,211 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>酒店查询 - 成功/失败条数</title>
|
||||
<style>
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 24px;
|
||||
font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
|
||||
background: #f4f6f9;
|
||||
color: #1f2937;
|
||||
}
|
||||
.wrap { max-width: 860px; margin: 0 auto; }
|
||||
h1 { font-size: 20px; margin: 0 0 16px; font-weight: 600; }
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
background: #fff;
|
||||
padding: 16px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,.08);
|
||||
}
|
||||
.toolbar label { font-size: 14px; color: #4b5563; }
|
||||
input[type=date] {
|
||||
padding: 8px 10px;
|
||||
font-size: 14px;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 6px;
|
||||
outline: none;
|
||||
}
|
||||
input[type=date]:focus { border-color: #2563eb; }
|
||||
button {
|
||||
padding: 9px 22px;
|
||||
font-size: 14px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: #2563eb;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
}
|
||||
button:hover:not(:disabled) { background: #1d4ed8; }
|
||||
button:disabled { background: #93b4ee; cursor: not-allowed; }
|
||||
.hint { font-size: 12px; color: #9ca3af; margin-left: auto; }
|
||||
.cards { display: flex; gap: 16px; margin-top: 16px; flex-wrap: wrap; }
|
||||
.card {
|
||||
flex: 1 1 160px;
|
||||
background: #fff;
|
||||
border-radius: 10px;
|
||||
padding: 16px;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,.08);
|
||||
}
|
||||
.card .label { font-size: 13px; color: #6b7280; }
|
||||
.card .value { font-size: 28px; font-weight: 600; margin-top: 6px; font-variant-numeric: tabular-nums; }
|
||||
.card.success .value { color: #059669; }
|
||||
.card.reject .value { color: #dc2626; }
|
||||
.card.fail .value { color: #d97706; }
|
||||
#status { margin-top: 16px; font-size: 13px; min-height: 18px; color: #6b7280; }
|
||||
#status.error { color: #dc2626; }
|
||||
table {
|
||||
width: 100%;
|
||||
margin-top: 8px;
|
||||
border-collapse: collapse;
|
||||
background: #fff;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,.08);
|
||||
opacity: 0;
|
||||
transition: opacity .2s;
|
||||
}
|
||||
table.show { opacity: 1; }
|
||||
th, td { padding: 10px 14px; font-size: 14px; text-align: left; border-bottom: 1px solid #eef1f5; }
|
||||
th { background: #f9fafb; color: #6b7280; font-weight: 500; }
|
||||
td.num { font-variant-numeric: tabular-nums; }
|
||||
td.num.reject { color: #dc2626; }
|
||||
tr:last-child td { border-bottom: none; }
|
||||
tr.failed td { color: #d97706; background: #fffbeb; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<h1>酒店查询 · 成功/失败条数</h1>
|
||||
|
||||
<div class="toolbar">
|
||||
<label for="date">查询日期</label>
|
||||
<input type="date" id="date">
|
||||
<button id="btn" type="button">查询</button>
|
||||
<span class="hint" id="hint">并发 8 线程,逐城市汇总</span>
|
||||
</div>
|
||||
|
||||
<div class="cards">
|
||||
<div class="card success">
|
||||
<div class="label">总成功条数</div>
|
||||
<div class="value" id="totalSuccess">-</div>
|
||||
</div>
|
||||
<div class="card reject">
|
||||
<div class="label">总失败条数</div>
|
||||
<div class="value" id="totalReject">-</div>
|
||||
</div>
|
||||
<div class="card fail">
|
||||
<div class="label">请求失败城市</div>
|
||||
<div class="value" id="failedCount">-</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="status"></div>
|
||||
|
||||
<table id="table">
|
||||
<thead>
|
||||
<tr><th>城市</th><th>成功</th><th>失败</th></tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const dateInput = document.getElementById('date');
|
||||
const btn = document.getElementById('btn');
|
||||
const statusEl = document.getElementById('status');
|
||||
const table = document.getElementById('table');
|
||||
const tbody = table.querySelector('tbody');
|
||||
|
||||
// 默认今天
|
||||
const now = new Date();
|
||||
const pad = n => String(n).padStart(2, '0');
|
||||
dateInput.value = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
||||
|
||||
function setStatus(text, isError) {
|
||||
statusEl.textContent = text;
|
||||
statusEl.className = isError ? 'error' : '';
|
||||
}
|
||||
|
||||
function fmt(n) {
|
||||
return n === null || n === undefined ? '-' : Number(n).toLocaleString('zh-CN');
|
||||
}
|
||||
|
||||
function render(data) {
|
||||
tbody.innerHTML = '';
|
||||
data.items.forEach(item => {
|
||||
const tr = document.createElement('tr');
|
||||
if (item.error) tr.className = 'failed';
|
||||
|
||||
const cityTd = document.createElement('td');
|
||||
cityTd.textContent = item.city;
|
||||
tr.appendChild(cityTd);
|
||||
|
||||
['success', 'reject'].forEach(key => {
|
||||
const td = document.createElement('td');
|
||||
td.className = 'num' + (key === 'reject' && item.reject ? ' reject' : '');
|
||||
if (item.error) {
|
||||
td.textContent = '请求失败';
|
||||
} else {
|
||||
td.textContent = fmt(item[key]);
|
||||
}
|
||||
tr.appendChild(td);
|
||||
});
|
||||
|
||||
tr.title = item.error || '';
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
|
||||
document.getElementById('totalSuccess').textContent = fmt(data.total_success);
|
||||
document.getElementById('totalReject').textContent = fmt(data.total_reject);
|
||||
document.getElementById('failedCount').textContent = fmt(data.failed_cities.length);
|
||||
table.classList.add('show');
|
||||
|
||||
let text = `date=${data.date},共 ${data.items.length} 个城市`;
|
||||
if (data.failed_cities.length) {
|
||||
text += `,请求失败:${data.failed_cities.join('、')}`;
|
||||
}
|
||||
setStatus(text, data.failed_cities.length > 0);
|
||||
}
|
||||
|
||||
async function query() {
|
||||
const value = dateInput.value;
|
||||
if (!value) {
|
||||
setStatus('请先选择日期', true);
|
||||
return;
|
||||
}
|
||||
const statDate = value; // 原样传 2026-11-01
|
||||
|
||||
btn.disabled = true;
|
||||
btn.textContent = '查询中…';
|
||||
setStatus(`正在查询 ${statDate} …`);
|
||||
try {
|
||||
const resp = await fetch(`/api/stats?date=${encodeURIComponent(statDate)}`);
|
||||
const data = await resp.json();
|
||||
if (!resp.ok) {
|
||||
setStatus(data.error || `请求失败 HTTP ${resp.status}`, true);
|
||||
return;
|
||||
}
|
||||
render(data);
|
||||
} catch (e) {
|
||||
setStatus(`请求异常:${e.message}`, true);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
btn.textContent = '查询';
|
||||
}
|
||||
}
|
||||
|
||||
btn.addEventListener('click', query);
|
||||
dateInput.addEventListener('keydown', e => {
|
||||
if (e.key === 'Enter') query();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user