backend-dev #1
@ -1,13 +1,38 @@
|
||||
from conf.util import get_config_object
|
||||
from kafka import KafkaConsumer
|
||||
from confluent_kafka import Consumer
|
||||
import json, re
|
||||
|
||||
conf = get_config_object()
|
||||
|
||||
|
||||
# Kafka
|
||||
def get_KafkaConsumer() -> KafkaConsumer:
|
||||
""" 返回KafkaConsumer对象 """
|
||||
consumer = KafkaConsumer(
|
||||
bootstrap_servers=conf.get("kafka", "bootstrap_servers"),
|
||||
group_id=conf.get("kafka", "group_id")
|
||||
)
|
||||
def get_KafkaConsumer() -> Consumer:
|
||||
"""返回KafkaConsumer对象"""
|
||||
kafka_conf = {
|
||||
"bootstrap.servers": conf.get("kafka", "bootstrap_servers"),
|
||||
"group.id": conf.get("kafka", "group_id"),
|
||||
"auto.offset.reset": "earliest",
|
||||
}
|
||||
consumer = Consumer(kafka_conf)
|
||||
return consumer
|
||||
|
||||
|
||||
def raw_Data_to_jsonstr(data: str) -> str:
|
||||
"""
|
||||
将原始数据切分转换为json字符串
|
||||
"""
|
||||
# 清理转义字符
|
||||
data = re.sub(r'\\', '', data)
|
||||
|
||||
# 去除多余的空格和换行符
|
||||
data = data.strip()
|
||||
data_list = data.split("\t")
|
||||
|
||||
return {
|
||||
"order_id": data_list[0],
|
||||
"order_category": data_list[1],
|
||||
"order_name": data_list[2],
|
||||
"order_quantity": data_list[3],
|
||||
"date": data_list[4],
|
||||
"is_valid": data_list[5],
|
||||
}
|
||||
|
@ -1,4 +1,39 @@
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint, jsonify
|
||||
from dao.kafka.util import get_KafkaConsumer, raw_Data_to_jsonstr
|
||||
from confluent_kafka import KafkaError
|
||||
|
||||
api_bp = Blueprint('api', __name__)
|
||||
|
||||
@api_bp.route('/readkafka', methods=['GET'])
|
||||
def readKafka():
|
||||
consumer = get_KafkaConsumer()
|
||||
# 订阅Kafka主题
|
||||
consumer.subscribe(["orders"])
|
||||
|
||||
# 读取Kafka消息
|
||||
messages = []
|
||||
try:
|
||||
while True:
|
||||
msg = consumer.poll(timeout=1.0)
|
||||
if msg is None:
|
||||
break
|
||||
if msg.error():
|
||||
if msg.error().code() == KafkaError._PARTITION_EOF:
|
||||
continue
|
||||
else:
|
||||
print(msg.error())
|
||||
break
|
||||
messages.append(raw_Data_to_jsonstr(msg.value().decode('utf-8')))
|
||||
# 这里只读取最新的10条消息
|
||||
if len(messages) >= 100:
|
||||
break
|
||||
finally:
|
||||
# 取消订阅并关闭消费者
|
||||
consumer.unsubscribe()
|
||||
consumer.close()
|
||||
|
||||
return jsonify(messages)
|
||||
|
||||
@api_bp.route('/test', methods=['GET'])
|
||||
def test():
|
||||
return jsonify({"message": "Hello, World!"})
|
||||
|
@ -6,3 +6,7 @@ page_bp = Blueprint('page', __name__)
|
||||
@page_bp.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@page_bp.route('/show')
|
||||
def test():
|
||||
return render_template('show.html')
|
||||
|
105
templates/show.html
Normal file
105
templates/show.html
Normal file
@ -0,0 +1,105 @@
|
||||
<!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>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f4f4f9;
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
color: #333;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 20px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
th, td {
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
th {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
tr:nth-child(even) {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
.valid {
|
||||
color: green;
|
||||
font-weight: bold;
|
||||
}
|
||||
.invalid {
|
||||
color: red;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>订单信息</h1>
|
||||
<table id="orders-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>订单ID</th>
|
||||
<th>订单分类</th>
|
||||
<th>订单名称</th>
|
||||
<th>数量</th>
|
||||
<th>日期</th>
|
||||
<th>是否有效</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<!-- 这里将通过 JavaScript 动态填充订单数据 -->
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 定义一个函数用于请求数据并更新表格
|
||||
function fetchAndUpdateOrders() {
|
||||
fetch('/readkafka') // Flask API 路径
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const tableBody = document.querySelector('#orders-table tbody');
|
||||
// 清空现有的表格内容
|
||||
tableBody.innerHTML = '';
|
||||
// 填充新的数据
|
||||
data.forEach(order => {
|
||||
const row = document.createElement('tr');
|
||||
row.innerHTML = `
|
||||
<td>${order.order_id}</td>
|
||||
<td>${order.order_category}</td>
|
||||
<td>${order.order_name}</td>
|
||||
<td>${order.order_quantity}</td>
|
||||
<td>${order.date}</td>
|
||||
<td class="${order.is_valid === 'Y' ? 'valid' : 'invalid'}">${order.is_valid === 'Y' ? '有效' : '无效'}</td>
|
||||
`;
|
||||
tableBody.appendChild(row);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('获取订单数据失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载时,立即调用一次更新数据的函数
|
||||
fetchAndUpdateOrders();
|
||||
|
||||
// 设置轮询:每 5 秒请求一次数据并更新表格
|
||||
setInterval(fetchAndUpdateOrders, 5000); // 5000 毫秒 = 5 秒
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user