Skip to content

Commit

Permalink
Chunk data to reduce memory on large datasets when updating `next_aud…
Browse files Browse the repository at this point in the history
…it_date`

Signed-off-by: snipe <[email protected]>
  • Loading branch information
snipe committed Feb 5, 2024
1 parent 7b4020c commit b499357
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,18 +639,18 @@ public function postAlerts(Request $request)
// Be careful - this could be a negative number
$audit_diff_months = ((int)$request->input('audit_interval') - (int)($setting->audit_interval));

// Grab all of the assets that have an existing next_audit_date
$assets = Asset::whereNotNull('next_audit_date')->get();

// Update all of the assets' next_audit_date values
foreach ($assets as $asset) {

if ($asset->next_audit_date != '') {
$old_next_audit = new \DateTime($asset->next_audit_date);
$asset->next_audit_date = $old_next_audit->modify($audit_diff_months.' month')->format('Y-m-d');
$asset->forceSave();
// Grab all assets that have an existing next_audit_date, chunking to handle very large datasets
Asset::whereNotNull('next_audit_date')->chunk(20, function ($assets) use ($audit_diff_months) {

// Update assets' next_audit_date values
foreach ($assets as $asset) {
if ($asset->next_audit_date != '') {
$old_next_audit = new \DateTime($asset->next_audit_date);
$asset->next_audit_date = $old_next_audit->modify($audit_diff_months . ' month')->format('Y-m-d');
$asset->forceSave();
}
}
}
});
}

$alert_email = rtrim($request->input('alert_email'), ',');
Expand Down

0 comments on commit b499357

Please sign in to comment.