forked from bot/app
✨ Add sign status for Lagrange.Core
This commit is contained in:
parent
205b69e5cb
commit
f22f8f772a
@ -47,7 +47,7 @@ sign_status = on_alconna(Alconna(
|
||||
"sign",
|
||||
Subcommand(
|
||||
"chart",
|
||||
Args["limit", int, 60]
|
||||
Args["limit", int, 10000]
|
||||
),
|
||||
Subcommand(
|
||||
"count"
|
||||
@ -57,6 +57,8 @@ sign_status = on_alconna(Alconna(
|
||||
)
|
||||
))
|
||||
|
||||
cache_img: bytes = None
|
||||
|
||||
|
||||
@sign_status.assign("count")
|
||||
async def _():
|
||||
@ -85,19 +87,26 @@ async def _():
|
||||
|
||||
@sign_status.assign("chart")
|
||||
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)
|
||||
await sign_status.send(UniMessage.image(raw=img))
|
||||
|
||||
|
||||
@scheduler.scheduled_job("interval", seconds=SIGN_COUNT_DURATION, next_run_time=datetime.datetime.now())
|
||||
async def update_sign_count():
|
||||
global cache_img
|
||||
if not SIGN_COUNT_URLS:
|
||||
return
|
||||
data = await get_now_sign()
|
||||
for name, count in data.items():
|
||||
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]]:
|
||||
"""
|
||||
@ -129,7 +138,8 @@ async def save_sign_count(timestamp: float, count: int, sid: str):
|
||||
async def generate_chart(limit):
|
||||
data = []
|
||||
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(
|
||||
{
|
||||
"name" : name,
|
||||
@ -138,6 +148,7 @@ async def generate_chart(limit):
|
||||
"counts": [row.count for row in count_rows]
|
||||
}
|
||||
)
|
||||
print(len(count_rows))
|
||||
|
||||
img = await template2image(
|
||||
template=get_path("templates/sign_status.html", debug=True),
|
||||
|
@ -24,7 +24,7 @@ data.forEach((item) => {
|
||||
},
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: item["times"],
|
||||
data: item["times"].map(timestampToTime),
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
@ -38,4 +38,16 @@ 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user