From 8a78b6144df8436d4d386bd1e8a1f0fc8377d095 Mon Sep 17 00:00:00 2001 From: Riedler Date: Sun, 12 Oct 2025 23:11:25 +0200 Subject: [PATCH 1/3] fix: dramatically increase performance for get_my_slots --- lbplanner/classes/helpers/slot_helper.php | 23 +++++++++++++++++++++++ lbplanner/services/slots/get_my_slots.php | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/slot_helper.php b/lbplanner/classes/helpers/slot_helper.php index 16a146d2..0e7bfaca 100644 --- a/lbplanner/classes/helpers/slot_helper.php +++ b/lbplanner/classes/helpers/slot_helper.php @@ -107,6 +107,29 @@ public static function get_all_slots(): array { return $slotsobj; } + /** + * Returns a list of all slots relevant for a vintage. + * + * @param string $vintage the vintage to filter for + * @return slot[] An array of the slots. + */ + public static function get_vintage_slots(string $vintage): array { + global $DB; + $slots = $DB->get_records_sql( + 'SELECT slot.* FROM {' . self::TABLE_SLOTS . '} as slot ' . + 'INNER JOIN {'. self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' . + 'WHERE filter.vintage=? OR filter.vintage=NULL', + [$vintage] + ); + + $slotsobj = []; + foreach ($slots as $slot) { + array_push($slotsobj, slot::from_db($slot)); + } + + return $slotsobj; + } + /** * Returns a list of all slots belonging to a supervisor. * @param int $supervisorid userid of the supervisor in question diff --git a/lbplanner/services/slots/get_my_slots.php b/lbplanner/services/slots/get_my_slots.php index 469a6cbc..46a35196 100644 --- a/lbplanner/services/slots/get_my_slots.php +++ b/lbplanner/services/slots/get_my_slots.php @@ -43,7 +43,7 @@ public static function get_my_slots_parameters(): external_function_parameters { public static function get_my_slots(): array { global $USER; - $allslots = slot_helper::get_all_slots(); + $allslots = slot_helper::get_vintage_slots($USER->address); $myslots = slot_helper::filter_slots_for_user($allslots, $USER); From 396e94cfaad7ecbf30ea9b0fdda90ad6818e569f Mon Sep 17 00:00:00 2001 From: Riedler Date: Sun, 12 Oct 2025 23:15:23 +0200 Subject: [PATCH 2/3] style: minor fix --- lbplanner/classes/helpers/slot_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/slot_helper.php b/lbplanner/classes/helpers/slot_helper.php index 0e7bfaca..ffed0202 100644 --- a/lbplanner/classes/helpers/slot_helper.php +++ b/lbplanner/classes/helpers/slot_helper.php @@ -117,7 +117,7 @@ public static function get_vintage_slots(string $vintage): array { global $DB; $slots = $DB->get_records_sql( 'SELECT slot.* FROM {' . self::TABLE_SLOTS . '} as slot ' . - 'INNER JOIN {'. self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' . + 'INNER JOIN {' . self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' . 'WHERE filter.vintage=? OR filter.vintage=NULL', [$vintage] ); From 99f0a90eb4b1fdc200fe35ed89be6956c43ef36c Mon Sep 17 00:00:00 2001 From: Riedler Date: Mon, 13 Oct 2025 00:17:06 +0200 Subject: [PATCH 3/3] fix: even more perf --- lbplanner/classes/enums/WEEKDAY.php | 5 ++--- lbplanner/classes/helpers/slot_helper.php | 22 +++++++++++++++++++--- lbplanner/services/slots/get_my_slots.php | 12 ++++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lbplanner/classes/enums/WEEKDAY.php b/lbplanner/classes/enums/WEEKDAY.php index 145cbbf9..7ee2e6db 100644 --- a/lbplanner/classes/enums/WEEKDAY.php +++ b/lbplanner/classes/enums/WEEKDAY.php @@ -31,9 +31,8 @@ use local_lbplanner\polyfill\Enum; /** - * All the days of the week. - * All seven of them. - * Yup. + * ISO 8601 numeric representation of the day of the week. + * Same as `(int)DateTime::format('N');` */ class WEEKDAY extends Enum { /** diff --git a/lbplanner/classes/helpers/slot_helper.php b/lbplanner/classes/helpers/slot_helper.php index ffed0202..eefe7460 100644 --- a/lbplanner/classes/helpers/slot_helper.php +++ b/lbplanner/classes/helpers/slot_helper.php @@ -108,17 +108,30 @@ public static function get_all_slots(): array { } /** - * Returns a list of all slots relevant for a vintage. + * Returns a list of all slots relevant for a vintage and range of weekdays. * * @param string $vintage the vintage to filter for + * @param int $today the starting day to filter for + * @param int $range the range in days * @return slot[] An array of the slots. */ - public static function get_vintage_slots(string $vintage): array { + public static function get_vintage_time_slots(string $vintage, int $today, int $range): array { global $DB; + + if ($range < 7) { + $valid = []; + for ($i = $today; $i < ($today + $range); $i++) { + array_push($valid, (($i - 1) % 7) + 1); + } + $insert = " AND slot.weekday IN (" . implode(',', $valid) . ")"; + } else { + $insert = ''; + } + $slots = $DB->get_records_sql( 'SELECT slot.* FROM {' . self::TABLE_SLOTS . '} as slot ' . 'INNER JOIN {' . self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' . - 'WHERE filter.vintage=? OR filter.vintage=NULL', + 'WHERE (filter.vintage=? OR filter.vintage=NULL)' . $insert, [$vintage] ); @@ -329,6 +342,9 @@ public static function filter_slots_for_user(array $allslots, \stdClass $user): * @return slot[] the filtered slot array */ public static function filter_slots_for_time(array $allslots, int $range): array { + if ($range === 7) { + return $allslots; + } $utctz = new DateTimeZone('UTC'); $now = new DateTimeImmutable('now', $utctz); $slots = []; diff --git a/lbplanner/services/slots/get_my_slots.php b/lbplanner/services/slots/get_my_slots.php index 46a35196..dc71bc5c 100644 --- a/lbplanner/services/slots/get_my_slots.php +++ b/lbplanner/services/slots/get_my_slots.php @@ -17,6 +17,7 @@ namespace local_lbplanner_services; use core_external\{external_api, external_function_parameters, external_multiple_structure}; +use DateTimeImmutable; use local_lbplanner\helpers\{config_helper, slot_helper}; use local_lbplanner\model\slot; @@ -43,13 +44,16 @@ public static function get_my_slots_parameters(): external_function_parameters { public static function get_my_slots(): array { global $USER; - $allslots = slot_helper::get_vintage_slots($USER->address); + $dayofweek = (int)(new DateTimeImmutable('today'))->format('N'); + $allslots = slot_helper::get_vintage_time_slots( + $USER->address, + $dayofweek, + config_helper::get_slot_futuresight() + ); $myslots = slot_helper::filter_slots_for_user($allslots, $USER); - $returnslots = slot_helper::filter_slots_for_time($myslots, config_helper::get_slot_futuresight()); - - return array_map(fn(slot $slot) => $slot->prepare_for_api(), $returnslots); + return array_map(fn(slot $slot) => $slot->prepare_for_api(), $myslots); } /**