Skip to content

Commit b8c97b7

Browse files
authored
fix: Dramatically increase performance for get_my_slots (#95)
1 parent c84deaa commit b8c97b7

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

lbplanner/classes/enums/WEEKDAY.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
use local_lbplanner\polyfill\Enum;
3232

3333
/**
34-
* All the days of the week.
35-
* All seven of them.
36-
* Yup.
34+
* ISO 8601 numeric representation of the day of the week.
35+
* Same as `(int)DateTime::format('N');`
3736
*/
3837
class WEEKDAY extends Enum {
3938
/**

lbplanner/classes/helpers/slot_helper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,42 @@ public static function get_all_slots(): array {
107107
return $slotsobj;
108108
}
109109

110+
/**
111+
* Returns a list of all slots relevant for a vintage and range of weekdays.
112+
*
113+
* @param string $vintage the vintage to filter for
114+
* @param int $today the starting day to filter for
115+
* @param int $range the range in days
116+
* @return slot[] An array of the slots.
117+
*/
118+
public static function get_vintage_time_slots(string $vintage, int $today, int $range): array {
119+
global $DB;
120+
121+
if ($range < 7) {
122+
$valid = [];
123+
for ($i = $today; $i < ($today + $range); $i++) {
124+
array_push($valid, (($i - 1) % 7) + 1);
125+
}
126+
$insert = " AND slot.weekday IN (" . implode(',', $valid) . ")";
127+
} else {
128+
$insert = '';
129+
}
130+
131+
$slots = $DB->get_records_sql(
132+
'SELECT slot.* FROM {' . self::TABLE_SLOTS . '} as slot ' .
133+
'INNER JOIN {' . self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' .
134+
'WHERE (filter.vintage=? OR filter.vintage=NULL)' . $insert,
135+
[$vintage]
136+
);
137+
138+
$slotsobj = [];
139+
foreach ($slots as $slot) {
140+
array_push($slotsobj, slot::from_db($slot));
141+
}
142+
143+
return $slotsobj;
144+
}
145+
110146
/**
111147
* Returns a list of all slots belonging to a supervisor.
112148
* @param int $supervisorid userid of the supervisor in question
@@ -306,6 +342,9 @@ public static function filter_slots_for_user(array $allslots, \stdClass $user):
306342
* @return slot[] the filtered slot array
307343
*/
308344
public static function filter_slots_for_time(array $allslots, int $range): array {
345+
if ($range === 7) {
346+
return $allslots;
347+
}
309348
$utctz = new DateTimeZone('UTC');
310349
$now = new DateTimeImmutable('now', $utctz);
311350
$slots = [];

lbplanner/services/slots/get_my_slots.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace local_lbplanner_services;
1818

1919
use core_external\{external_api, external_function_parameters, external_multiple_structure};
20+
use DateTimeImmutable;
2021
use local_lbplanner\helpers\{config_helper, slot_helper};
2122
use local_lbplanner\model\slot;
2223

@@ -43,13 +44,16 @@ public static function get_my_slots_parameters(): external_function_parameters {
4344
public static function get_my_slots(): array {
4445
global $USER;
4546

46-
$allslots = slot_helper::get_all_slots();
47+
$dayofweek = (int)(new DateTimeImmutable('today'))->format('N');
48+
$allslots = slot_helper::get_vintage_time_slots(
49+
$USER->address,
50+
$dayofweek,
51+
config_helper::get_slot_futuresight()
52+
);
4753

4854
$myslots = slot_helper::filter_slots_for_user($allslots, $USER);
4955

50-
$returnslots = slot_helper::filter_slots_for_time($myslots, config_helper::get_slot_futuresight());
51-
52-
return array_map(fn(slot $slot) => $slot->prepare_for_api(), $returnslots);
56+
return array_map(fn(slot $slot) => $slot->prepare_for_api(), $myslots);
5357
}
5458

5559
/**

0 commit comments

Comments
 (0)