Skip to content

Commit 62c6be2

Browse files
feat: alter unsaved search deletion and add option for last used lists
This commit alters the deletion of unsaved searches to be carried out each time the cron script runs. It also adds the option for libraries to choose to have stored information about the last list a user has accessed deleted after 14 days.
1 parent 063e933 commit 62c6be2

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

code/cron/src/com/turning_leaf_technologies/cron/DatabaseCleanup.java

+55-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void doCronProcess(String servername, Ini configIni, Section processSetti
2727
removeOldCachedObjects(dbConn, logger, processLog);
2828
removeOldIndexingData(dbConn, logger, processLog);
2929
removeOldExternalRequests(dbConn, logger, processLog);
30+
removeOldLastListUsed(dbConn, logger, processLog);
3031
optimizeSearchTable(dbConn, logger, processLog);
3132
optimizeSessionsTable(dbConn, logger, processLog);
3233

@@ -420,15 +421,15 @@ private void removeOldSearches(Connection dbConn, Logger logger, CronProcessLogE
420421
//Remove old searches
421422
try {
422423
int rowsRemoved = 0;
423-
ResultSet numSearchesRS = dbConn.prepareStatement("SELECT count(id) from search where created < (CURDATE() - INTERVAL 2 DAY) and saved = 0").executeQuery();
424+
ResultSet numSearchesRS = dbConn.prepareStatement("SELECT count(id) from search where saved = 0").executeQuery();
424425
numSearchesRS.next();
425426
long numSearches = numSearchesRS.getLong(1);
426427
long batchSize = 100000;
427428
long numBatches = (numSearches / batchSize) + 1;
428429
processLog.addNote("Found " + numSearches + " expired searches that need to be removed. Will process in " + numBatches + " batches");
429430
processLog.saveResults();
430431
for (int i = 0; i < numBatches; i++){
431-
PreparedStatement searchesToRemove = dbConn.prepareStatement("SELECT id from search where created < (CURDATE() - INTERVAL 2 DAY) and saved = 0 LIMIT 0, " + batchSize, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
432+
PreparedStatement searchesToRemove = dbConn.prepareStatement("SELECT id from search where saved = 0 LIMIT 0, " + batchSize, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
432433
PreparedStatement removeSearchStmt = dbConn.prepareStatement("DELETE from search where id = ?");
433434

434435
ResultSet searchesToRemoveRs = searchesToRemove.executeQuery();
@@ -447,4 +448,56 @@ private void removeOldSearches(Connection dbConn, Logger logger, CronProcessLogE
447448
}
448449
}
449450

451+
private void removeOldLastListUsed(Connection dbConn, Logger logger, CronProcessLogEntry processLog) {
452+
//Remove old last list used
453+
try {
454+
//Get list of libraries that want last used list cleared
455+
PreparedStatement librariesListStmt = dbConn.prepareStatement("SELECT libraryId, deleteOldLastListUsedEntries from library where deleteOldLastListUsedEntries > 0");
456+
PreparedStatement libraryLocationsStmt = dbConn.prepareStatement("SELECT locationId from location where libraryId = ?");
457+
PreparedStatement deleteLastListUsedStmt = dbConn.prepareStatement("DELETE from user where lastListUsed = ?");
458+
459+
ResultSet librariesListRS = librariesListStmt.executeQuery();
460+
461+
long numDeletions = 0;
462+
while (librariesListRS.next()) {
463+
long libraryId = librariesListRS.getLong("libraryId");
464+
long daysToPreserve = librariesListRS.getLong("deleteLastUsedListEntries");
465+
466+
libraryLocationsStmt.setLong(1, libraryId);
467+
468+
ResultSet libraryLocationsRS = libraryLocationsStmt.executeQuery();
469+
StringBuilder libraryLocations = new StringBuilder();
470+
471+
while (libraryLocationsRS.next()) {
472+
if (libraryLocations.length() > 0) {
473+
libraryLocations.append(", ");
474+
}
475+
libraryLocations.append(libraryLocationsRS.getString("locationId"));
476+
}
477+
if (libraryLocations.length() > 0) {
478+
long now = new Date().getTime() /1000;
479+
480+
long earliestDateToPreserve = now - (14 * 24 * 60 * 60);
481+
482+
PreparedStatement lastListUsedEntriesToDeleteStmt = dbConn.prepareStatement("SELECT lastListUsed from user where user.homeLocationId IN (" + libraryLocations + ") and lastListused < ?");
483+
lastListUsedEntriesToDeleteStmt.setLong(1, earliestDateToPreserve);
484+
485+
ResultSet lastListUsedEntriesToDeleteRS = lastListUsedEntriesToDeleteStmt.executeQuery();
486+
while (lastListUsedEntriesToDeleteRS.next()) {
487+
deleteLastListUsedStmt.setLong(1, lastListUsedEntriesToDeleteRS.getLong(1));
488+
int numUpdates = deleteLastListUsedStmt.executeUpdate();
489+
processLog.addUpdates(numUpdates);
490+
numDeletions += numUpdates;
491+
}
492+
lastListUsedEntriesToDeleteRS.close();
493+
lastListUsedEntriesToDeleteStmt.close();
494+
}
495+
}
496+
librariesListRS.close();
497+
librariesListStmt.close();
498+
processLog.addNote("Removed " + numDeletions + " expired last list used entries");
499+
} catch (SQLException e) {
500+
processLog.incErrors("Unable to remove expired last used list entries.", e);
501+
}
502+
}
450503
}

code/web/release_notes/24.06.00.MD

+11
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,21 @@
5252
- Added new Documentation links to several settings pages (*MKD*)
5353
- Updates to default user roles: removed testing roles and a couple uncommonly used roles; updated role titles (*MKD*)
5454

55+
//alexander
56+
### New Settings
57+
- Added option to delete stored information for lastListUsed after 14 days. Primary Configuration > Library Systems.
58+
59+
### Database Cleanup Script Updates
60+
- Update DatabaseCleanup.java to delete unsaved searches each time the cron runs.
61+
- Add function to remove last list used after 14 days for libraries who have selected this option.
62+
5563
## This release includes code contributions from
5664
- ByWater Solutions
5765
- Mark Noble (MDN)
5866
- Kirstin Kroeger (KK)
5967
- Kodi Lein (KL)
6068
- Liz Rea (LR)
6169
- Morgan Daigneault (MKD)
70+
71+
- PTFS Europe
72+
- Alexander Blanchard (AB)

code/web/sys/DBMaintenance/version_updates/24.06.00.php

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ function getUpdates24_06_00(): array {
6464
],
6565
], //full_text_limiter
6666

67+
//alexander - PTFS Europe
68+
'library_delete_last_list_used_entries' => [
69+
'title' => 'Library delete last list used history',
70+
'description' => 'Add an option to delete lastListUsed',
71+
'continueOnError' => true,
72+
'sql' => [
73+
'ALTER TABLE library ADD COLUMN deleteLastListUsedEntries TINYINT(1) DEFAULT 0',
74+
],
75+
],
6776
//other
6877

6978

code/web/sys/LibraryLocation/Library.php

+9
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class Library extends DataObject {
113113
public $showUserContactInformation;
114114
public $inSystemPickupsOnly;
115115
public $validPickupSystems;
116+
public $deleteLastListUsedEntries;
116117
/** @noinspection PhpUnused */
117118
public $pTypes; //This is used as part of the indexing process
118119
public $facetLabel;
@@ -2837,6 +2838,14 @@ static function getObjectStructure($context = ''): array {
28372838
'forcesListReindex' => true,
28382839
'default' => 4,
28392840
],
2841+
'deleteLastListUsedEntries' => [
2842+
'property' => 'deleteLastListUsedEntries',
2843+
'type' => 'checkbox',
2844+
'label' => 'Delete Last Used List After 14 Days',
2845+
'description' => 'Whether to delete the last used list information for users after 14 days',
2846+
'hideInLists' => true,
2847+
'default' => 0,
2848+
],
28402849
'allowAutomaticSearchReplacements' => [
28412850
'property' => 'allowAutomaticSearchReplacements',
28422851
'type' => 'checkbox',

0 commit comments

Comments
 (0)