Add sign status for Lagrange.Core

This commit is contained in:
远野千束 2024-05-11 00:34:11 +08:00
parent 205b69e5cb
commit f22f8f772a
2 changed files with 28 additions and 5 deletions

View File

@ -47,7 +47,7 @@ sign_status = on_alconna(Alconna(
"sign", "sign",
Subcommand( Subcommand(
"chart", "chart",
Args["limit", int, 60] Args["limit", int, 10000]
), ),
Subcommand( Subcommand(
"count" "count"
@ -57,6 +57,8 @@ sign_status = on_alconna(Alconna(
) )
)) ))
cache_img: bytes = None
@sign_status.assign("count") @sign_status.assign("count")
async def _(): async def _():
@ -85,19 +87,26 @@ async def _():
@sign_status.assign("chart") @sign_status.assign("chart")
async def _(arp: CommandResult = AlconnaResult()): async def _(arp: CommandResult = AlconnaResult()):
limit = arp.result.main_args.get("limit", 60) limit = arp.result.subcommands.get("chart").args.get("limit")
if limit == 10000:
if cache_img:
await sign_status.send(UniMessage.image(raw=cache_img))
return
img = await generate_chart(limit) img = await generate_chart(limit)
await sign_status.send(UniMessage.image(raw=img)) await sign_status.send(UniMessage.image(raw=img))
@scheduler.scheduled_job("interval", seconds=SIGN_COUNT_DURATION, next_run_time=datetime.datetime.now()) @scheduler.scheduled_job("interval", seconds=SIGN_COUNT_DURATION, next_run_time=datetime.datetime.now())
async def update_sign_count(): async def update_sign_count():
global cache_img
if not SIGN_COUNT_URLS: if not SIGN_COUNT_URLS:
return return
data = await get_now_sign() data = await get_now_sign()
for name, count in data.items(): for name, count in data.items():
await save_sign_count(count[0], count[1], SIGN_COUNT_URLS[name]) await save_sign_count(count[0], count[1], SIGN_COUNT_URLS[name])
cache_img = await generate_chart(10000)
async def get_now_sign() -> dict[str, tuple[float, int]]: async def get_now_sign() -> dict[str, tuple[float, int]]:
""" """
@ -129,7 +138,8 @@ async def save_sign_count(timestamp: float, count: int, sid: str):
async def generate_chart(limit): async def generate_chart(limit):
data = [] data = []
for name, url in SIGN_COUNT_URLS.items(): for name, url in SIGN_COUNT_URLS.items():
count_rows = sign_db.all(SignCount(), "sid = ? LIMIT ?", url, limit) count_rows = sign_db.all(SignCount(), "sid = ? ORDER BY id DESC LIMIT ?", url, limit)
count_rows.reverse()
data.append( data.append(
{ {
"name" : name, "name" : name,
@ -138,6 +148,7 @@ async def generate_chart(limit):
"counts": [row.count for row in count_rows] "counts": [row.count for row in count_rows]
} }
) )
print(len(count_rows))
img = await template2image( img = await template2image(
template=get_path("templates/sign_status.html", debug=True), template=get_path("templates/sign_status.html", debug=True),

View File

@ -24,7 +24,7 @@ data.forEach((item) => {
}, },
xAxis: { xAxis: {
type: 'category', type: 'category',
data: item["times"], data: item["times"].map(timestampToTime),
}, },
yAxis: { yAxis: {
type: 'value', type: 'value',
@ -39,3 +39,15 @@ data.forEach((item) => {
} }
) )
}) })
function timestampToTime(timestamp) {
let date = new Date(timestamp * 1000)
let Y = date.getFullYear() + '-'
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
let D = date.getDate() + ' '
let h = date.getHours() + ':'
let m = date.getMinutes() + ':'
let s = date.getSeconds()
return M + D + h + m + s
}