Skip to content

Commit

Permalink
add stats
Browse files Browse the repository at this point in the history
  • Loading branch information
yamader committed Oct 17, 2022
1 parent 73eda8f commit 1d753a1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 19 deletions.
11 changes: 11 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ async function main() {

app.use(express.static(join(__dirname, "pub")))

app.get("/api/dump", async (req, res) => {
const rows = await Activity.findAll({
attributes: ["code", "event", "date"],
})
const data = rows.map(({ code, event, date }) => ({
code, event,
date: date.getTime(),
}))
res.json(data)
})

const send = body => {
const data = JSON.stringify({
...body,
Expand Down
29 changes: 15 additions & 14 deletions pub/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ const { useEffect, useRef, useState } = React

const sleep = async ms => new Promise(res => setTimeout(res, ms))

const timeDiffFmt = (dt, now) => {
const ds = (now - dt) / 1000
if(ds < 0) return "未来"

const h = Math.trunc(ds / 3600)
if(h) return `${h}時間前`

const m = Math.trunc(ds / 60)
if(m) return `${m}分前`

const s = Math.trunc(ds)
if(s) return `さっき(${s}秒前)`
}

const App = () => {
const [code, setCode] = useState("-")
const [msg, setMsg] = useState("")
Expand Down Expand Up @@ -37,20 +51,6 @@ const App = () => {
}
}

const timeDiffFmt = (dt, now) => {
const ds = (now - dt) / 1000
if(ds < 0) return "未来"

const h = Math.trunc(ds / 3600)
if(h) return `${h}時間前`

const m = Math.trunc(ds / 60)
if(m) return `${m}分前`

const s = Math.trunc(ds)
if(s) return `さっき(${s}秒前)`
}

useEffect(() => {
async function handler({ data }) {
const { code, msg, dt, now } = JSON.parse(data)
Expand Down Expand Up @@ -93,6 +93,7 @@ const App = () => {
/>
<header>
<h1>食堂管理システム</h1>
<a href="/stats/">統計情報</a>
</header>
<main>
<div
Expand Down
10 changes: 5 additions & 5 deletions pub/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>食堂</title>
<script src="assets/react.js"></script><!-- <script crossorigin src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script> -->
<script src="assets/react-dom.js"></script><!-- <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script> -->
<script src="assets/babel.js"></script><!-- <script src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script> -->
<script src="/assets/react.js"></script><!-- <script crossorigin src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script> -->
<script src="/assets/react-dom.js"></script><!-- <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script> -->
<script src="/assets/babel.js"></script><!-- <script src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script> -->
<link rel="stylesheet" href="https://fonts.xz.style/serve/inter.css" />
<link rel="stylesheet" href="assets/new.css" /><!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@exampledev/[email protected]/new.min.css"> -->
<link rel="stylesheet" href="/assets/new.css" /><!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@exampledev/[email protected]/new.min.css"> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel" src="app.jsx"></script>
<script type="text/babel" src="./app.jsx"></script>
</body>
67 changes: 67 additions & 0 deletions pub/stats/app.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { useEffect, useState } = React

const timeDiffFmt = (dt, now) => {
const ds = (now - dt) / 1000
if(ds < 0) return "未来"

const h = Math.trunc(ds / 3600)
const m = Math.trunc((ds - 3600 * h) / 60)

const date = new Date(dt)
return `${h}時間と${m}分前 (${date.toLocaleString("ja")})`
}

const Stats = () => {
const [tab, setTab] = useState([])

useEffect(async () => {
const dump = await fetch("/api/dump").then(res => res.json())
dump.sort((a, b) => b.date - a.date)
setTab(dump)
}, [])

return (
<>
<header>
<h1>食堂管理システム - 統計情報</h1>
<a href="/">メインUIに戻る</a>
</header>
<main>
<table>
<thead>
<tr>
<th>時刻</th>
<th>学籍番号</th>
{/*<th>イベント</th>*/}
</tr>
</thead>
<tbody>
{tab.map(({ code, event, date }, i) => (
<tr key={i}>
<td>{timeDiffFmt(date, Date.now())}</td>
<td>{code}</td>
{/*<td>{event}</td>*/}
</tr>
))}
</tbody>
</table>
</main>
<footer>
<p
style={{
marginTop: "4rem",
color: "gray",
fontSize: "1.2em",
fontWeight: "bolder",
textAlign: "center",
}}>
https://github.com/KCCTdensan/syokudou
<br />
&copy; 2022 神戸高専電算部
</p>
</footer>
</>
)
}

ReactDOM.render(<Stats />, document.getElementById("root"))
1 change: 1 addition & 0 deletions pub/stats/index.html

0 comments on commit 1d753a1

Please sign in to comment.