mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-19 19:04:23 +08:00
chore: 增加主页积分操作与事件同步诊断日志
This commit is contained in:
@@ -5,6 +5,7 @@ use sea_orm::{
|
|||||||
TransactionTrait,
|
TransactionTrait,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tauri::State;
|
use tauri::State;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -173,7 +174,32 @@ pub async fn event_create(
|
|||||||
active.update(&txn).await.map_err(|e| e.to_string())?;
|
active.update(&txn).await.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
txn.commit().await.map_err(|e| e.to_string())?;
|
txn.commit().await.map_err(|e| e.to_string())?;
|
||||||
|
{
|
||||||
|
let state_guard = state.read();
|
||||||
|
let logger = state_guard.logger.read();
|
||||||
|
logger.info_with_meta(
|
||||||
|
"event_create:committed",
|
||||||
|
json!({
|
||||||
|
"student_name": student_name,
|
||||||
|
"delta": data.delta,
|
||||||
|
"val_prev": val_prev,
|
||||||
|
"val_curr": val_curr,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
realtime_dual_write_sync(state.inner()).await?;
|
realtime_dual_write_sync(state.inner()).await?;
|
||||||
|
{
|
||||||
|
let state_guard = state.read();
|
||||||
|
let logger = state_guard.logger.read();
|
||||||
|
logger.info_with_meta(
|
||||||
|
"event_create:sync_done",
|
||||||
|
json!({
|
||||||
|
"student_name": student_name,
|
||||||
|
"delta": data.delta,
|
||||||
|
"val_curr": val_curr,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
Ok(IpcResponse::success(inserted.id))
|
Ok(IpcResponse::success(inserted.id))
|
||||||
}
|
}
|
||||||
Ok(None) => Ok(IpcResponse::error("Student not found")),
|
Ok(None) => Ok(IpcResponse::error("Student not found")),
|
||||||
|
|||||||
@@ -105,6 +105,18 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
window.dispatchEvent(new CustomEvent("ss:data-updated", { detail: { category } }))
|
window.dispatchEvent(new CustomEvent("ss:data-updated", { detail: { category } }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logHome = (message: string, meta?: Record<string, unknown>) => {
|
||||||
|
try {
|
||||||
|
if (meta) {
|
||||||
|
console.info(`[Home] ${message}`, meta)
|
||||||
|
} else {
|
||||||
|
console.info(`[Home] ${message}`)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
void 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const getSurname = (name: string) => {
|
const getSurname = (name: string) => {
|
||||||
if (!name) return ""
|
if (!name) return ""
|
||||||
return name.charAt(0)
|
return name.charAt(0)
|
||||||
@@ -121,6 +133,7 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
const fetchData = useCallback(async (silent = false) => {
|
const fetchData = useCallback(async (silent = false) => {
|
||||||
if (!(window as any).api) return
|
if (!(window as any).api) return
|
||||||
const requestId = ++fetchRequestIdRef.current
|
const requestId = ++fetchRequestIdRef.current
|
||||||
|
logHome("fetchData:start", { requestId, silent })
|
||||||
if (!silent) setLoading(true)
|
if (!silent) setLoading(true)
|
||||||
const [stuRes, reaRes] = await Promise.all([
|
const [stuRes, reaRes] = await Promise.all([
|
||||||
(window as any).api.queryStudents({}),
|
(window as any).api.queryStudents({}),
|
||||||
@@ -128,6 +141,15 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
])
|
])
|
||||||
if (requestId !== fetchRequestIdRef.current) return
|
if (requestId !== fetchRequestIdRef.current) return
|
||||||
|
|
||||||
|
logHome("fetchData:response", {
|
||||||
|
requestId,
|
||||||
|
silent,
|
||||||
|
studentsSuccess: Boolean(stuRes?.success),
|
||||||
|
studentsCount: Array.isArray(stuRes?.data) ? stuRes.data.length : 0,
|
||||||
|
reasonsSuccess: Boolean(reaRes?.success),
|
||||||
|
reasonsCount: Array.isArray(reaRes?.data) ? reaRes.data.length : 0,
|
||||||
|
})
|
||||||
|
|
||||||
if (stuRes.success) {
|
if (stuRes.success) {
|
||||||
const enrichedStudents = (stuRes.data as student[]).map((s) => ({
|
const enrichedStudents = (stuRes.data as student[]).map((s) => ({
|
||||||
...s,
|
...s,
|
||||||
@@ -148,6 +170,12 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
if (res.success) {
|
if (res.success) {
|
||||||
const latest = Array.isArray(res.data) && res.data.length > 0 ? (res.data[0] as scoreEvent) : null
|
const latest = Array.isArray(res.data) && res.data.length > 0 ? (res.data[0] as scoreEvent) : null
|
||||||
setLatestEvent(latest)
|
setLatestEvent(latest)
|
||||||
|
logHome("fetchLatestEvent:response", {
|
||||||
|
hasLatest: Boolean(latest),
|
||||||
|
latestStudent: latest?.student_name,
|
||||||
|
latestDelta: latest?.delta,
|
||||||
|
latestUuid: latest?.uuid,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -156,6 +184,7 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
fetchLatestEvent()
|
fetchLatestEvent()
|
||||||
const onDataUpdated = (e: any) => {
|
const onDataUpdated = (e: any) => {
|
||||||
const category = e?.detail?.category
|
const category = e?.detail?.category
|
||||||
|
logHome("event:ss:data-updated", { detail: e?.detail })
|
||||||
if (
|
if (
|
||||||
category === "events" ||
|
category === "events" ||
|
||||||
category === "students" ||
|
category === "students" ||
|
||||||
@@ -448,12 +477,25 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
}
|
}
|
||||||
|
|
||||||
setSubmitLoading(true)
|
setSubmitLoading(true)
|
||||||
|
logHome("performSubmit:start", {
|
||||||
|
student: student.name,
|
||||||
|
delta,
|
||||||
|
content,
|
||||||
|
localScoreBefore: student.score,
|
||||||
|
})
|
||||||
const res = await (window as any).api.createEvent({
|
const res = await (window as any).api.createEvent({
|
||||||
student_name: student.name,
|
student_name: student.name,
|
||||||
reason_content: content,
|
reason_content: content,
|
||||||
delta: delta,
|
delta: delta,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
logHome("performSubmit:createEvent:response", {
|
||||||
|
student: student.name,
|
||||||
|
delta,
|
||||||
|
success: Boolean(res?.success),
|
||||||
|
message: (res as any)?.message,
|
||||||
|
})
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
messageApi.success(
|
messageApi.success(
|
||||||
delta > 0
|
delta > 0
|
||||||
@@ -464,6 +506,10 @@ export const Home: React.FC<HomeProps> = ({ canEdit, isPortraitMode = false }) =
|
|||||||
fetchData(true)
|
fetchData(true)
|
||||||
fetchLatestEvent()
|
fetchLatestEvent()
|
||||||
emitDataUpdated("events")
|
emitDataUpdated("events")
|
||||||
|
logHome("performSubmit:afterSuccessRefreshDispatched", {
|
||||||
|
student: student.name,
|
||||||
|
delta,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
messageApi.error(res.message || t("home.submitFailed"))
|
messageApi.error(res.message || t("home.submitFailed"))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user