Split intervals

This commit is contained in:
jude 2023-06-21 15:24:43 +01:00
parent 3d627b5bf0
commit 284bfcd9ad
4 changed files with 93 additions and 17 deletions

2
Cargo.lock generated
View File

@ -2217,7 +2217,7 @@ checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]] [[package]]
name = "reminder-rs" name = "reminder-rs"
version = "1.6.16" version = "1.6.17"
dependencies = [ dependencies = [
"base64 0.21.2", "base64 0.21.2",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder-rs" name = "reminder-rs"
version = "1.6.16" version = "1.6.17"
authors = ["Jude Southworth <judesouthworth@pm.me>"] authors = ["Jude Southworth <judesouthworth@pm.me>"]
edition = "2021" edition = "2021"
license = "AGPL-3.0 only" license = "AGPL-3.0 only"

View File

@ -51,16 +51,19 @@ pub async fn bot_data(cookies: &CookieJar<'_>, pool: &State<Pool<MySql>>) -> Jso
.await .await
.unwrap(); .unwrap();
let schedule = sqlx::query_as_unchecked!( let schedule_once = sqlx::query_as_unchecked!(
TimeFrame, TimeFrame,
"SELECT "SELECT
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 60) * 60) AS `time_key`, FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 600) * 600) AS `time_key`,
COUNT(1) AS `count` COUNT(1) AS `count`
FROM reminders FROM reminders
WHERE WHERE
`utc_time` < DATE_ADD(NOW(), INTERVAL 1 DAY) AND `utc_time` < DATE_ADD(NOW(), INTERVAL 1 DAY) AND
`utc_time` >= NOW() AND `utc_time` >= NOW() AND
`enabled` = 1 `enabled` = 1 AND
`interval_seconds` IS NULL AND
`interval_months` IS NULL AND
`interval_days` IS NULL
GROUP BY `time_key` GROUP BY `time_key`
ORDER BY `time_key`" ORDER BY `time_key`"
) )
@ -68,16 +71,40 @@ pub async fn bot_data(cookies: &CookieJar<'_>, pool: &State<Pool<MySql>>) -> Jso
.await .await
.unwrap(); .unwrap();
let schedule_long = sqlx::query_as_unchecked!( let schedule_interval = sqlx::query_as_unchecked!(
TimeFrame,
"SELECT
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 600) * 600) AS `time_key`,
COUNT(1) AS `count`
FROM reminders
WHERE
`utc_time` < DATE_ADD(NOW(), INTERVAL 1 DAY) AND
`utc_time` >= NOW() AND
`enabled` = 1 AND (
`interval_seconds` IS NOT NULL OR
`interval_months` IS NOT NULL OR
`interval_days` IS NOT NULL
)
GROUP BY `time_key`
ORDER BY `time_key`"
)
.fetch_all(pool.inner())
.await
.unwrap();
let schedule_once_long = sqlx::query_as_unchecked!(
TimeFrame, TimeFrame,
"SELECT "SELECT
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 86400) * 86400) AS `time_key`, FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 86400) * 86400) AS `time_key`,
COUNT(1) AS `count` COUNT(1) AS `count`
FROM reminders FROM reminders
WHERE WHERE
`utc_time` < DATE_ADD(NOW(), INTERVAL 31 DAY) AND `utc_time` < DATE_ADD(NOW(), INTERVAL 14 DAY) AND
`utc_time` >= NOW() AND `utc_time` >= NOW() AND
`enabled` = 1 `enabled` = 1 AND
`interval_seconds` IS NULL AND
`interval_months` IS NULL AND
`interval_days` IS NULL
GROUP BY `time_key` GROUP BY `time_key`
ORDER BY `time_key`" ORDER BY `time_key`"
) )
@ -85,5 +112,36 @@ pub async fn bot_data(cookies: &CookieJar<'_>, pool: &State<Pool<MySql>>) -> Jso
.await .await
.unwrap(); .unwrap();
Ok(json!({ "backlog": backlog.backlog, "schedule": schedule, "scheduleLong": schedule_long })) let schedule_interval_long = sqlx::query_as_unchecked!(
TimeFrame,
"SELECT
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`utc_time`) / 86400) * 86400) AS `time_key`,
COUNT(1) AS `count`
FROM reminders
WHERE
`utc_time` < DATE_ADD(NOW(), INTERVAL 14 DAY) AND
`utc_time` >= NOW() AND
`enabled` = 1 AND (
`interval_seconds` IS NOT NULL OR
`interval_months` IS NOT NULL OR
`interval_days` IS NOT NULL
)
GROUP BY `time_key`
ORDER BY `time_key`"
)
.fetch_all(pool.inner())
.await
.unwrap();
Ok(json!({
"backlog": backlog.backlog,
"scheduleShort": {
"once": schedule_once,
"interval": schedule_interval
},
"scheduleLong": {
"once": schedule_once_long,
"interval": schedule_interval_long,
}
}))
} }

View File

@ -7,13 +7,18 @@ document.addEventListener("DOMContentLoaded", () => {
new Chart(document.getElementById("schedule"), { new Chart(document.getElementById("schedule"), {
type: "bar", type: "bar",
data: { data: {
labels: data.schedule.map((row) => labels: [
luxon.DateTime.fromISO(row.time_key) ...data.scheduleShort.once,
), ...data.scheduleShort.interval,
].map((row) => luxon.DateTime.fromISO(row.time_key)),
datasets: [ datasets: [
{ {
label: "Reminders", label: "Reminders",
data: data.schedule.map((row) => row.count), data: data.scheduleShort.once.map((row) => row.count),
},
{
label: "Intervals",
data: data.scheduleShort.interval.map((row) => row.count),
}, },
], ],
}, },
@ -22,11 +27,15 @@ document.addEventListener("DOMContentLoaded", () => {
maintainAspectRatio: false, maintainAspectRatio: false,
scales: { scales: {
x: { x: {
stacked: true,
type: "time", type: "time",
time: { time: {
unit: "minute", unit: "minute",
}, },
}, },
y: {
stacked: true,
},
}, },
}, },
}); });
@ -34,13 +43,18 @@ document.addEventListener("DOMContentLoaded", () => {
new Chart(document.getElementById("scheduleLong"), { new Chart(document.getElementById("scheduleLong"), {
type: "bar", type: "bar",
data: { data: {
labels: data.scheduleLong.map((row) => labels: [
luxon.DateTime.fromISO(row.time_key) ...data.scheduleLong.once,
), ...data.scheduleLong.interval,
].map((row) => luxon.DateTime.fromISO(row.time_key)),
datasets: [ datasets: [
{ {
label: "Reminders", label: "Reminders",
data: data.scheduleLong.map((row) => row.count), data: data.scheduleLong.once.map((row) => row.count),
},
{
label: "Intervals",
data: data.scheduleLong.interval.map((row) => row.count),
}, },
], ],
}, },
@ -49,11 +63,15 @@ document.addEventListener("DOMContentLoaded", () => {
maintainAspectRatio: false, maintainAspectRatio: false,
scales: { scales: {
x: { x: {
stacked: true,
type: "time", type: "time",
time: { time: {
unit: "day", unit: "day",
}, },
}, },
y: {
stacked: true,
},
}, },
}, },
}); });