Skip to content

Commit

Permalink
GH-405 Handle memory more efficiently
Browse files Browse the repository at this point in the history
Instead of loading all attempt data into an array, iterate over a record set and just keep the necessary attempt data stored in an array.
  • Loading branch information
davidszkiba committed Jun 18, 2024
1 parent 6499873 commit aa8dc3d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
3 changes: 1 addition & 2 deletions classes/catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -1729,8 +1729,7 @@ public static function get_attempts(
'endtime' => $endtime,
];

$records = $DB->get_records_sql($sql, $params);
return $records;
return $DB->get_recordset_sql($sql, $params);
}

/**
Expand Down
47 changes: 32 additions & 15 deletions classes/output/catquizstatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,24 @@ private function get_attempts(): array {
return $this->attempts;
}

$attempts = catquiz::get_attempts(
$attempts = [];
foreach (catquiz::get_attempts(
null,
$this->scaleid,
$this->courseid,
$this->testid,
$this->contextid,
$this->starttime,
$this->endtime
);
) as $record) {
$json = json_decode($record->json);
$prunedrecord = $record;
$prunedrecord->json = json_encode((object) [
'personabilities_abilities' => $json->personabilities_abilities ?? null,
'personabilities' => $json->personabilities ?? null,
]);
$attempts[] = $prunedrecord;
}
$this->attempts = $attempts;
return $attempts;
}
Expand All @@ -665,14 +674,24 @@ private function get_attempts_by_timerange(bool $allowempty = false): array {
return $this->attemptsbytimerange[$allowempty];
}

$records = catquiz::get_attempts(
$records = [];
foreach (catquiz::get_attempts(
null,
$this->rootscaleid,
$this->courseid,
$this->testid,
$this->contextid,
$this->starttime,
$this->endtime);
$this->endtime) as $record) {
// Store a subset of the json to save memory.
$json = json_decode($record->json);
$prunedrecord = $record;
$prunedrecord->json = json_encode((object) [
'personabilities' => $json->personabilities,
]);
$records[] = $prunedrecord;
}

if (count($records) < 2) {
return [];
}
Expand Down Expand Up @@ -1090,17 +1109,15 @@ public function get_export_data(): array {
$this->endtime
);

$records = $DB->get_records_sql($sql, $params);
$enriched = array_map(
function ($r) {
$r->starttime = userdate($r->starttime, get_string('strftimedatetime', 'core_langconfig'));
$r->endtime = userdate($r->endtime, get_string('strftimedatetime', 'core_langconfig'));
$r->teststrategy = $this->get_teststrategy_name($r->teststrategy);
return $r;
},
$records
);
return $enriched;
$data = [];
foreach ($DB->get_recordset_sql($sql, $params) as $r) {
unset($r->json);
$r->starttime = userdate($r->starttime, get_string('strftimedatetime', 'core_langconfig'));
$r->endtime = userdate($r->endtime, get_string('strftimedatetime', 'core_langconfig'));
$r->teststrategy = $this->get_teststrategy_name($r->teststrategy);
$data[] = $r;
}
return $data;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions classes/teststrategy/feedbackgenerator/learningprogress.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,24 @@ public function render_abilityprogress(array $initialcontext, $primarycatscale)

// Compare to other courses.
// Find all courses before the end of the day of this attempt.
$records = catquiz::get_attempts(
$records = [];
foreach (catquiz::get_attempts(
null,
$initialcontext['catscaleid'],
$courseid,
$initialcontext['testid'],
$initialcontext['contextid'],
null,
$end);
$end
) as $record) {
$json = json_decode($record->json);
$prunedrecord = $record;
$prunedrecord->json = json_encode((object) [
'personabilities_abilities' => $json->personabilities_abilities ?? null,
'personabilities' => $json->personabilities ?? null,
]);
$records[] = $prunedrecord;
}
// Compare records to define range for average.
// Minimum 3 records required to display progress charts.
if (count($records) < 3) {
Expand Down

0 comments on commit aa8dc3d

Please sign in to comment.