提交
This commit is contained in:
5
app/blueprints/bag_data/__init__.py
Normal file
5
app/blueprints/bag_data/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# from routes import Blueprint
|
||||
|
||||
# posts_bp = Blueprint('posts', __name__)
|
||||
|
||||
# from . import routes
|
||||
55
app/blueprints/bag_data/routes.py
Normal file
55
app/blueprints/bag_data/routes.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
from flask_jwt_extended import create_access_token, get_jwt, get_jwt_identity, jwt_required
|
||||
from sqlalchemy import and_, asc, desc, exists, func, or_
|
||||
from app.models import BagFile, Fst, Role, User, UserRole
|
||||
from app.utils.fst_tree import build_tree,build_sub_tree
|
||||
from app import db
|
||||
from app.utils.log_record import log_operation
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
# 1.创建蓝图实例,指定名称和导入名称
|
||||
data_bp = Blueprint('bag', __name__)
|
||||
|
||||
@data_bp.route('/fstlist',methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_fstlist():
|
||||
res=Fst.query.with_entities(Fst.id,Fst.name,Fst.parent_id).all()
|
||||
response=build_tree(res)
|
||||
return jsonify(response), 200
|
||||
|
||||
|
||||
|
||||
@data_bp.route('/fstlevel1', methods=['GET'])
|
||||
def get_level1_fst():
|
||||
# 查询 level=1 的记录,仅返回 id, name, level 字段
|
||||
results = Fst.query.with_entities(
|
||||
Fst.id,
|
||||
Fst.name,
|
||||
Fst.level
|
||||
).filter_by(level=1).all()
|
||||
|
||||
# 转换为字典列表(方便 JSON 序列化)
|
||||
data = [{
|
||||
"id": r.id,
|
||||
"name": r.name,
|
||||
"level": r.level
|
||||
} for r in results]
|
||||
|
||||
return jsonify(data),200
|
||||
|
||||
# 跨表查询所有的bag
|
||||
|
||||
@data_bp.route('/getnode', methods=['GET'])
|
||||
@jwt_required()
|
||||
def query_sub_node():
|
||||
'''
|
||||
根据一级标签查询对应的所有子标签
|
||||
'''
|
||||
tagid=request.args.get('tagid')
|
||||
res=Fst.query.with_entities(Fst.id,Fst.name,Fst.parent_id).all()
|
||||
response=build_sub_tree(res,tagid)
|
||||
return jsonify(response), 200
|
||||
|
||||
|
||||
73
app/blueprints/bag_data/services.py
Normal file
73
app/blueprints/bag_data/services.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# from datetime import datetime,timedelta
|
||||
# from app import db
|
||||
# from app.models import BagRecord
|
||||
# from sqlalchemy.exc import SQLAlchemyError
|
||||
# from sqlalchemy import func
|
||||
|
||||
# def add():
|
||||
# new_record = BagRecord(
|
||||
# bag_name="sensor_data_2025.bag",
|
||||
# collection_time=datetime(2025, 5, 1, 12, 1, 1),
|
||||
# bag_state=0,
|
||||
# frames=120,
|
||||
# car_state=1,
|
||||
# address="beijing",
|
||||
# collect_month=5
|
||||
# )
|
||||
# db.session.add(new_record)
|
||||
# db.session.commit() # 提交事务 [1,7](@ref)
|
||||
|
||||
|
||||
# def create_record(data):
|
||||
# """创建新记录"""
|
||||
# try:
|
||||
# # 转换日期类型字段
|
||||
# if 'collection_time' in data:
|
||||
# data['collection_time'] = datetime.fromisoformat(data['collection_time'])
|
||||
# if 'parse_time' in data:
|
||||
# data['parse_time'] = datetime.fromisoformat(data['parse_time'])
|
||||
|
||||
# record = BagRecord(**data)
|
||||
# db.session.add(record)
|
||||
# db.session.commit()
|
||||
# return record
|
||||
# except SQLAlchemyError as e:
|
||||
# db.session.rollback()
|
||||
# raise e
|
||||
|
||||
# def get_record_by_id(record_id):
|
||||
# """根据ID获取记录"""
|
||||
# return BagRecord.query.get(record_id)
|
||||
|
||||
|
||||
# def get_all_records(**filters):
|
||||
# """获取所有记录(支持过滤)"""
|
||||
# query = BagRecord.query
|
||||
# if filters:
|
||||
# query = query.filter_by(**filters)
|
||||
# return query.all()
|
||||
|
||||
|
||||
# def get_daily_counts(start_date, end_date):
|
||||
# # 执行 SQL 查询,按日期分组并统计数量
|
||||
# query = (
|
||||
# db.session.query(
|
||||
# db.func.date(BagRecord.collection_time).label('date'),
|
||||
# db.func.count(BagRecord.id).label('count')
|
||||
# )
|
||||
# .filter(BagRecord.collection_time >= start_date)
|
||||
# .filter(BagRecord.collection_time <= end_date + timedelta(days=1)) # 包含结束日期
|
||||
# .group_by(db.func.date(BagRecord.collection_time))
|
||||
# .order_by(db.func.date(BagRecord.collection_time))
|
||||
# )
|
||||
|
||||
# print(query.all())
|
||||
# # 将结果转换为字典列表
|
||||
# results = []
|
||||
# for date, count in query.all():
|
||||
# results.append({
|
||||
# 'date': str(date), # 将日期对象转换为字符串
|
||||
# 'count': count
|
||||
# })
|
||||
|
||||
# return results
|
||||
Reference in New Issue
Block a user