Skip to content

Commit

Permalink
✨ 新增numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kalicyh committed Aug 6, 2024
1 parent 703d994 commit 8025f95
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
30 changes: 29 additions & 1 deletion api/crud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy.orm import Session
from .database import Record, Info
from .database import Record, Info, Number, NumberInfo
import datetime

def get_records(db: Session):
Expand Down Expand Up @@ -28,3 +28,31 @@ def update_info(db: Session, total_rows: int):
)
db.add(new_info)
db.commit()


def get_numbers(db: Session):
return db.query(Number).all()

def get_numbers_info(db: Session):
return db.query(NumberInfo).first()

def clear_numbers(db: Session):
db.query(Number).delete()
db.commit()

def add_numbers(db: Session, numbers):
db.add_all(numbers)
db.commit()

def update_numbers_info(db: Session, total_rows: int):
info_record = db.query(NumberInfo).first()
if info_record:
info_record.last_updated = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
info_record.total_rows = total_rows
else:
new_info = NumberInfo(
last_updated=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
total_rows=total_rows
)
db.add(new_info)
db.commit()
11 changes: 11 additions & 0 deletions api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ class Info(Base):
last_updated = Column(String(20))
total_rows = Column(Integer)

class Number(Base):
__tablename__ = 'numbers'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
category = Column(String(255), nullable=True)
text = Column(String(5000))

class NumberInfo(Base):
__tablename__ = 'numbers_info'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
text = Column(String(5000))

Base.metadata.create_all(bind=engine)
2 changes: 2 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from .routes.talking_points import router as talking_points_router
from .routes.numbers import router as numbers_router

app = FastAPI()

Expand All @@ -17,6 +18,7 @@
app.mount("/dist", StaticFiles(directory="dist"), name="dist")

app.include_router(talking_points_router, prefix="/talking_points")
app.include_router(numbers_router, prefix="/numbers")

@app.get("/")
def root():
Expand Down
Empty file removed api/routes/moments.py
Empty file.
146 changes: 146 additions & 0 deletions api/routes/numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from fastapi import APIRouter, File, UploadFile, HTTPException, BackgroundTasks
from fastapi.responses import JSONResponse
from io import BytesIO
import pandas as pd
import uuid
from sqlalchemy.orm import Session
from ..database import SessionLocal, Number,NumberInfo
from ..crud import clear_numbers, add_numbers, get_numbers, get_numbers_info, update_numbers_info

router = APIRouter()

upload_progress = {}

@router.post("/upload")
async def upload_file(file: UploadFile = File(...), background_tasks: BackgroundTasks = BackgroundTasks()):
upload_id = str(uuid.uuid4())
upload_progress[upload_id] = {'progress': 0}

file_content = await file.read()
file_buffer = BytesIO(file_content)

background_tasks.add_task(process_file, file_buffer, upload_id)

return {"upload_id": upload_id, "message": "Upload started"}

@router.post("/overwrite_upload")
async def overwrite_upload_file(file: UploadFile = File(...), background_tasks: BackgroundTasks = BackgroundTasks()):
upload_id = str(uuid.uuid4())
upload_progress[upload_id] = {'progress': 0}

db = SessionLocal()
try:
clear_numbers(db)
except Exception as e:
db.rollback()
upload_progress[upload_id]['message'] = f"清空数据表错误: {str(e)}"
db.close()
return {"upload_id": upload_id, "message": "清空数据表错误"}
finally:
db.close()

file_content = await file.read()
file_buffer = BytesIO(file_content)

background_tasks.add_task(process_file, file_buffer, upload_id)

return {"upload_id": upload_id, "message": "Overwrite upload started"}

@router.get("/progress/{upload_id}")
async def get_progress(upload_id: str):
progress = upload_progress.get(upload_id)
if not progress:
raise HTTPException(status_code=404, detail="Upload ID not found")
return progress

def process_file(file_buffer: BytesIO, upload_id: str):
df = pd.read_excel(file_buffer)
df.columns = df.columns.str.strip()

total_rows = len(df)
numbers = []
chunk_size = 50

for index, row in df.iterrows():
numbers.append(
Number(
category=row['工作站'],
text=row['好友ID']
)
)

if (index + 1) % chunk_size == 0 or (index + 1) == total_rows:
db = SessionLocal()
try:
add_numbers(db, numbers)
progress = round((index + 1) / total_rows * 100, 2)
upload_progress[upload_id] = {
'progress': progress,
'current_row': index + 1,
'total_rows': total_rows
}
numbers = []
except Exception as e:
db.rollback()
upload_progress[upload_id]['message'] = f"上传错误: {str(e)}"
raise e
finally:
db.close()

if numbers:
db = SessionLocal()
try:
add_numbers(db, numbers)
upload_progress[upload_id]['progress'] = 100
upload_progress[upload_id]['message'] = "文件已上传,数据已添加到数据库"
except Exception as e:
db.rollback()
upload_progress[upload_id]['message'] = f"上传错误: {str(e)}"
raise e
finally:
db.close()

db = SessionLocal()
try:
update_numbers_info(db, total_rows)
except Exception as e:
db.rollback()
raise e
finally:
db.close()

@router.get("/data")
async def get_data():
db = SessionLocal()
numbers = get_numbers(db)
db.close()

return JSONResponse(content={
"total_numbers": len(numbers),
"numbers": [
{
"序号": number.id,
"分类": number.category,
"联系方式": number.text
}
for number in numbers
]
})

@router.get("/info")
async def get_numbers_infos():
db = SessionLocal()
info_number = get_numbers_info(db)
if not info_number:
db.close()
return JSONResponse(content={
"total_rows": "0",
"last_updated": "暂无数据",
}, status_code=200)

db.close()

return JSONResponse(content={
"last_updated": info_number.last_updated,
"total_rows": info_number.total_rows
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knowledge_server_vue",
"version": "1.3.1",
"version": "1.4.0",
"private": true,
"scripts": {
"setup": "yarn install && pip3 install poetry && poetry install",
Expand Down

0 comments on commit 8025f95

Please sign in to comment.