Skip to content

Commit

Permalink
Delete offset, coding done, need unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Aug 14, 2018
1 parent 6dc70ff commit 5ba589d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
@@ -0,0 +1,15 @@
package org.orcid.core.cli;

import org.orcid.core.manager.v3.NotificationManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DeleteOffsetNotifications {
@SuppressWarnings("resource")
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("orcid-core-context.xml");
NotificationManager notificationManager = (NotificationManager) context.getBean("notificationManagerV3");
notificationManager.deleteOffsetNotifications();
System.exit(0);
}
}
Expand Up @@ -100,4 +100,6 @@ public interface NotificationManager {
Notification createPermissionNotification(String orcid, NotificationPermission notification);

Integer archiveOffsetNotifications();

Integer deleteOffsetNotifications();
}
Expand Up @@ -192,7 +192,13 @@ public class NotificationManagerImpl implements NotificationManager {
private EmailFrequencyManager emailFrequencyManager;

@Value("${org.orcid.notifications.archive.offset:100}")
private Integer notificationOffset;
private Integer notificationArchiveOffset;

@Value("${org.orcid.notifications.delete.offset:100}")
private Integer notificationDeleteOffset;

@Value("${org.orcid.notifications.delete.offset.records:10}")
private Integer recordsPerBatch;

private static final Logger LOGGER = LoggerFactory.getLogger(NotificationManagerImpl.class);

Expand Down Expand Up @@ -1213,6 +1219,26 @@ private String getAssetsUrl() {

@Override
public Integer archiveOffsetNotifications() {
return notificationDao.archiveOffsetNotifications(notificationOffset == null ? 100 : notificationOffset);
return notificationDao.archiveOffsetNotifications(notificationArchiveOffset == null ? 100 : notificationArchiveOffset);
}

@Override
public Integer deleteOffsetNotifications() {
List<Object[]> toDelete = new ArrayList<Object[]>();
Integer deleted = 0;
do {
toDelete = notificationDao.findNotificationsToDeleteByOffset((notificationDeleteOffset == null ? 10000 : notificationDeleteOffset), recordsPerBatch);
LOGGER.info("Got batch of {} notifications to delete", toDelete.size());
for(Object[] o : toDelete) {
Long id = (Long) o[0];
String orcid = (String) o[1];
LOGGER.info("About to delete old notification: id={}, orcid={}",
new Object[] { id, orcid });
notificationDao.remove(id);
}
deleted += toDelete.size();
} while(!toDelete.isEmpty());

return deleted;
}
}
Expand Up @@ -55,6 +55,8 @@ public interface NotificationDao extends GenericDao<NotificationEntity, Long> {

Integer archiveOffsetNotifications(Integer offset);

List<Object[]> findNotificationsToDeleteByOffset(Integer offset, Integer recordsPerBatch);

List<NotificationEntity> findNotificationsCreatedBefore(Date createdBefore, int batchSize);

List<NotificationEntity> findUnsentServiceAnnouncements(int batchSize);
Expand Down
@@ -1,5 +1,6 @@
package org.orcid.persistence.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -273,4 +274,27 @@ public Integer archiveOffsetNotifications(Integer offset) {
return result;
}

@Override
public List<Object[]> findNotificationsToDeleteByOffset(Integer offset, Integer recordsPerBatch) {
Query selectQuery = entityManager.createNativeQuery("SELECT orcid FROM notification group by orcid having count(*) > :offset order by count(*) desc limit :limit");
selectQuery.setParameter("offset", offset);
selectQuery.setParameter("limit", recordsPerBatch);
@SuppressWarnings("unchecked")
List<String> ids = selectQuery.getResultList();
if (ids.isEmpty()) {
return new ArrayList<Object[]>();
}

List<Object[]> results = new ArrayList<Object[]>();

for(String orcid : ids) {
Query archiveQuery = entityManager.createNativeQuery("SELECT id, orcid FROM notification WHERE orcid=:orcid order by date_created desc OFFSET :offset");
archiveQuery.setParameter("orcid", orcid);
archiveQuery.setParameter("offset", offset);
results.addAll(archiveQuery.getResultList());
}

return results;
}

}
Expand Up @@ -22,10 +22,9 @@
<task:scheduled ref="notificationManager" method="processOldNotificationsToAutoArchive" cron="${org.orcid.scheduler.web.processOldNotificationsToAutoArchive:06 06 1 * * *}"/>
<task:scheduled ref="notificationManager" method="processOldNotificationsToAutoDelete" cron="${org.orcid.scheduler.web.processOldNotificationsToAutoDelete:07 07 2 * * *}"/>
<task:scheduled ref="notificationManagerV3" method="processUnverifiedEmails7Days" cron="${org.orcid.scheduler.web.processUnverifiedEmails7Days:0 10 * * * *}"/>
<task:scheduled ref="notificationManagerV3" method="archiveOffsetNotifications" cron="${org.orcid.scheduler.web.processUnverifiedEmails7Days:08 08 3 * * *}"/>
<task:scheduled ref="notificationManagerV3" method="archiveOffsetNotifications" cron="${org.orcid.scheduler.web.archiveOffsetNotifications:08 08 3 * * *}"/>
<task:scheduled ref="notificationManagerV3" method="deleteOffsetNotifications" cron="${org.orcid.scheduler.web.deleteOffsetNotifications:09 09 4 * * *}"/>



<task:scheduled ref="orcidRecordIndexer" method="processProfilesWithPendingFlagAndAddToMessageQueue" fixed-delay="${org.orcid.scheduler.web.processProfilesPendingIndexingDelaySeconds:30}000"/>
<task:scheduled ref="orcidRecordIndexer" method="processProfilesWithReindexFlagAndAddToMessageQueue" fixed-delay="${org.orcid.scheduler.web.processProfilesPendingIndexingDelaySeconds:60}000"/>
<task:scheduled ref="orcidRecordIndexer" method="processProfilesWithFailedFlagAndAddToMessageQueue" cron="${org.orcid.scheduler.web.processProfilesThatMissedIndexing:20 20 0-2 * * *}000"/>
Expand Down

0 comments on commit 5ba589d

Please sign in to comment.