Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
env
__pycache__
articles
articles
app.log
10 changes: 9 additions & 1 deletion app/api/v1/endpoints/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from app.schemas.note import NoteCreate, NoteUpdate, NoteFind
from app.utils.get_db import get_db
from app.curd.note import create_note_in_db, delete_note_in_db, update_note_in_db, find_notes_in_db, find_notes_title_in_db
from app.curd.note import create_note_in_db, delete_note_in_db, update_note_in_db, find_notes_in_db, find_notes_title_in_db, find_recent_notes_in_db
from typing import Optional

router = APIRouter()
Expand Down Expand Up @@ -51,4 +51,12 @@ async def get_notes_title(note_find: NoteFind = Depends(), db: AsyncSession = De
"page_size": note_find.page_size
},
"notes": notes
}


@router.get("/recent", response_model=dict)
async def get_recent_notes(db: AsyncSession = Depends(get_db)):
notes = await find_recent_notes_in_db(db)
return {
"notes": notes
}
34 changes: 33 additions & 1 deletion app/curd/note.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy import func
from sqlalchemy import func, cast, Date
from datetime import datetime, timedelta
from app.models.model import Note
from app.schemas.note import NoteCreate, NoteUpdate, NoteFind, NoteResponse

Expand Down Expand Up @@ -73,3 +74,34 @@ async def find_notes_title_in_db(note_find: NoteFind, db: AsyncSession):
result = await db.execute(stmt)
notes = [row[0] for row in result.fetchall()]
return notes, total_count

async def find_recent_notes_in_db(db: AsyncSession):
"""
返回近7天内创建的笔记的数目和对应日期
"""
# 获取当前日期和7天前的日期
today = datetime.now().date()
seven_days_ago = today - timedelta(days=6)

# 查询近7天内的笔记数目,按日期分组
stmt = (
select(
cast(Note.create_time, Date).label("date"), # 按日期分组
func.count(Note.id).label("count") # 统计每日期的笔记数
)
.where(
Note.create_time >= seven_days_ago, # 筛选近7天的笔记
Note.create_time <= today # 包括今天
)
.group_by(cast(Note.create_time, Date)) # 按日期分组
.order_by(cast(Note.create_time, Date)) # 按日期排序
)

# 执行查询
result = await db.execute(stmt)
data = result.fetchall()

# 格式化结果为字典列表
recent_notes = [{"date": row.date, "count": row.count} for row in data]

return recent_notes
15 changes: 13 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI
from fastapi import FastAPI, Request
from app.routers.router import include_routers
from fastapi_pagination import add_pagination
from loguru import logger

app = FastAPI()

Expand All @@ -16,4 +17,14 @@ def read_item(item_id: int, q: str = None):
include_routers(app)

# 注册分页功能
add_pagination(app)
add_pagination(app)

# 设置日志配置
logger.add("app.log", rotation="1 MB", retention="7 days", level="INFO")

@app.middleware("http")
async def log_requests(request: Request, call_next):
logger.info(f"Request: {request.method} {request.url}")
response = await call_next(request)
logger.info(f"Response status: {response.status_code}")
return response
Binary file modified requirements.txt
Binary file not shown.