stats board

This commit is contained in:
Michael Zhang 2023-06-11 15:08:16 -05:00
parent afd50c1f0e
commit 3080602727
6 changed files with 112 additions and 9 deletions

View file

@ -1,4 +1,4 @@
CREATE TABLE [SrsEntrySet] ( CREATE TABLE IF NOT EXISTS [SrsEntrySet] (
[ID] integer NOT NULL PRIMARY KEY AUTOINCREMENT, [ID] integer NOT NULL PRIMARY KEY AUTOINCREMENT,
[CreationDate] bigint NOT NULL DEFAULT CURRENT_TIMESTAMP, [CreationDate] bigint NOT NULL DEFAULT CURRENT_TIMESTAMP,
[NextAnswerDate] bigint, [NextAnswerDate] bigint,
@ -15,4 +15,5 @@ CREATE TABLE [SrsEntrySet] (
[Tags] [nvarchar(300)], [Tags] [nvarchar(300)],
[LastUpdateDate] BIGINT, [LastUpdateDate] BIGINT,
[IsDeleted] BOOLEAN NOT NULL DEFAULT false, [IsDeleted] BOOLEAN NOT NULL DEFAULT false,
[ServerId] integer); [ServerId] integer
);

View file

@ -1,9 +1,42 @@
use sqlx::SqlitePool; use sqlx::{Row, SqlitePool};
use tauri::State; use tauri::State;
pub struct SrsDb(pub SqlitePool); pub struct SrsDb(pub SqlitePool);
#[tauri::command] #[derive(Debug, Serialize, Deserialize)]
pub async fn get_srs_stats(db: State<'_, SrsDb>) -> Result<(), ()> { pub struct SrsStats {
Ok(()) reviews_available: u32,
reviews_today: u32,
total_items: u32,
total_reviews: u32,
/// Used to calculate average success
num_success: u32,
num_failure: u32,
}
#[tauri::command]
pub async fn get_srs_stats(db: State<'_, SrsDb>) -> Result<SrsStats, String> {
let row = sqlx::query(
r#"
SELECT
COUNT(*) AS total_items,
SUM(SuccessCount) AS num_success,
SUM(FailureCount) AS num_failure
FROM SrsEntrySet
"#,
)
.fetch_one(&db.0)
.await
.map_err(|err| err.to_string())?;
Ok(SrsStats {
reviews_available: 0,
reviews_today: 0,
total_items: row.get("total_items"),
total_reviews: 0,
num_success: row.get("num_success"),
num_failure: row.get("num_failure"),
})
} }

View file

@ -0,0 +1,7 @@
.top-row {
}
.reviews-stats {
background-color: gray;
color: white;
}

View file

@ -1,3 +1,58 @@
export default function DashboardReviewStats() { import {
return <></>; Grid,
GridItem,
Stat,
StatArrow,
StatGroup,
StatHelpText,
StatLabel,
StatNumber,
} from "@chakra-ui/react";
import styles from "./DashboardReviewStats.module.scss";
import useSWR from "swr";
import { invoke } from "@tauri-apps/api/tauri";
interface SrsStats {
reviews_available: number;
reviews_today: number;
total_items: number;
total_reviews: number;
/// Used to calculate average success
num_success: number;
num_failure: number;
}
export default function DashboardReviewStats() {
const {
data: srsStats,
error,
isLoading,
} = useSWR(["get_srs_stats"], ([command]) => invoke<SrsStats>(command));
if (!srsStats) return <>Loading...</>;
const averageSuccess = srsStats.num_success / (srsStats.num_success + srsStats.num_failure);
const averageSuccessStr = `${Math.round(averageSuccess * 10000) / 100}%`;
const generateStat = (stat) => {
return (
<Stat>
<StatLabel>{stat.label}</StatLabel>
<StatNumber>{stat.value}</StatNumber>
</Stat>
);
};
return (
<>
{/* JSON.stringify([srsStats, error, isLoading]) */}
<StatGroup>
{generateStat({ label: "total items", value: srsStats.total_items })}
{generateStat({ label: "average success", value: averageSuccessStr })}
</StatGroup>
</>
);
} }

View file

@ -4,6 +4,7 @@ $kanjiDisplaySize: 80px;
border: 1px solid rgb(87, 87, 210); border: 1px solid rgb(87, 87, 210);
width: $kanjiDisplaySize; width: $kanjiDisplaySize;
height: $kanjiDisplaySize; height: $kanjiDisplaySize;
line-height: $kanjiDisplaySize;
font-size: $kanjiDisplaySize * 0.8; font-size: $kanjiDisplaySize * 0.8;
text-align: center; text-align: center;
vertical-align: middle; vertical-align: middle;

View file

@ -1,3 +1,9 @@
import DashboardReviewStats from "../components/DashboardReviewStats";
export default function SrsPane() { export default function SrsPane() {
return <></>; return (
<main>
<DashboardReviewStats />
</main>
);
} }