Skip to content

Commit

Permalink
feature: Add cron job to delete events and create events stats
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielPalafox committed Jan 4, 2024
1 parent a956004 commit f96c147
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 6 deletions.
5 changes: 4 additions & 1 deletion orcid-core/src/main/java/org/orcid/core/togglz/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public enum Features implements Feature {
SEND_ALL_VERIFICATION_EMAILS,

@Label("Send add works emails for 7, 28 and 90 days.")
SEND_ADD_WORKS_EMAILS;
SEND_ADD_WORKS_EMAILS,

@Label("Delete events older than 90 days from the DB ")
DELETE_EVENTS;

public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.orcid.persistence.jpa.entities.EventEntity;

import java.util.List;

/**
*
* @author Daniel Palafox
Expand All @@ -12,4 +14,8 @@ public interface EventDao {
void createEvent(EventEntity eventEntity);

EventEntity find(long id);

List<EventEntity> findAll();

void deleteEventsByDate(Integer numberOfDays);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.orcid.persistence.dao;

import org.orcid.persistence.jpa.entities.EventStatsEntity;

import java.util.List;

/**
*
* @author Daniel Palafox
*
*/
public interface EventStatsDao {

void createEventStats();

List<EventStatsEntity> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.orcid.persistence.dao.EventDao;
import org.orcid.persistence.jpa.entities.EventEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Daniel Palafox
*/
Expand All @@ -29,5 +33,19 @@ public void createEvent(EventEntity eventEntity) {
public EventEntity find(long id) {
return entityManager.find(EventEntity.class, id);
}


@Override
public List<EventEntity> findAll() {
TypedQuery<EventEntity> query = entityManager.createQuery("from EventEntity", EventEntity.class);
return query.getResultList();
}

@Override
@Transactional
public void deleteEventsByDate(Integer numberOfDays) {
String query = "DELETE FROM event where CAST(date_created as date) < CAST(now() - (CAST('1' AS INTERVAL DAY) * 1) as date) * :numberOfDays)";
Query queryDelete = entityManager.createNativeQuery(query);
queryDelete.setParameter("numberOfDays", numberOfDays);
queryDelete.executeUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.orcid.persistence.dao.impl;

import org.orcid.persistence.dao.EventStatsDao;
import org.orcid.persistence.jpa.entities.EventStatsEntity;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.List;

/**
* @author Daniel Palafox
*/
public class EventStatsDaoImpl implements EventStatsDao {

@Resource(name = "entityManager")
protected EntityManager entityManager;

@Override
@Transactional
public void createEventStats() {
String query =
"INSERT INTO event_stats (event_type, client_id, count, date, date_created, last_modified) " +
"SELECT event_type, client_id, COUNT(id), CAST(e.date_created as date), now(), now() " +
"FROM event as e " +
"WHERE event_type != 'Public-Page' " +
"AND CAST(e.date_created as date) = CAST(now() - (CAST('1' AS INTERVAL DAY) * 1) as date) " +
"GROUP BY event_type, client_id, CAST(e.date_created as date) " +
"ORDER BY CAST(e.date_created as date) DESC;";
Query insertQuery = entityManager.createNativeQuery(query);
insertQuery.executeUpdate();
}

@Override
public List<EventStatsEntity> findAll() {
TypedQuery<EventStatsEntity> query = entityManager.createQuery("from EventStatsEntity", EventStatsEntity.class);
return query.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.orcid.persistence.jpa.entities;

import java.util.Date;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
*
* @author Daniel Palafox
*
*/
@Entity
@Table(name = "event_stats")
public class EventStatsEntity extends BaseEntity<Long>{
private Long id;
private String eventType;
private String clientId;
private Integer count;
private Date date;

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "event_seq")
@SequenceGenerator(name = "event_seq", sequenceName = "event_seq", allocationSize = 1)
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name = "event_type")
public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

@Column(name = "client_id")
public String getClientId() {
return clientId;
}

public void setClientId(String client_id) {
this.clientId = client_id;
}

@Column(name = "count")
public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

@Column(name = "date")
public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EventStatsEntity that = (EventStatsEntity) o;
return Objects.equals(getId(), that.getId()) &&
Objects.equals(getEventType(), that.getEventType()) &&
Objects.equals(getClientId(), that.getClientId()) &&
Objects.equals(getCount(), that.getCount()) &&
Objects.equals(getDate(), that.getDate());
}

@Override
public int hashCode() {
return Objects.hash(getId(), getEventType(), getClientId(), getCount(), getDate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<class>org.orcid.persistence.jpa.entities.FindMyStuffHistoryEntity</class>
<class>org.orcid.persistence.jpa.entities.SpamEntity</class>
<class>org.orcid.persistence.jpa.entities.EventEntity</class>
<class>org.orcid.persistence.jpa.entities.EventStatsEntity</class>
<class>org.orcid.persistence.jpa.entities.EmailDomainEntity</class>

<!-- OAuth entities -->
Expand Down
1 change: 1 addition & 0 deletions orcid-persistence/src/main/resources/db-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,5 @@
<include file="/db/updates/dw_alter_event_2.xml" />
<include file="/db/updates/grant_select_to_dw_user_on_dw_event.xml" />
<include file="/db/updates/identifier-types/update-ol-to-be-case-sensitive.xml" />
<include file="/db/updates/create_event_stats_table.xml" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<changeSet id="CREATE-EVENT-STATS-TABLE" author="Daniel Palafox">
<createTable tableName="event_stats">
<column name="id" type="bigint" autoIncrement="true">
<constraints nullable="false" primaryKey="true"
primaryKeyName="event_stats_pkey"/>
</column>
<column name="event_type" type="VARCHAR(20)"/>
<column name="client_id" type="VARCHAR(255)"/>
<column name="count" type="bigint"/>
<column name="date" type="TIMESTAMP"/>
<column name="date_created" type="TIMESTAMP WITH TIME ZONE"/>
<column name="last_modified" type="TIMESTAMP WITH TIME ZONE"/>
</createTable>
</changeSet>

<changeSet id="CREATE-EVENT-STATS-SEQUENCES" author="Daniel Palafox">
<preConditions onFail="MARK_RAN">
<not>
<sequenceExists sequenceName="event_stats_seq"/>
</not>
</preConditions>
<createSequence sequenceName="event_stats_seq"/>
</changeSet>

<changeSet id="CREATE-EVENT-STATS-AUTOCOLS" author="Daniel Palafox" dbms="hsqldb">
<addAutoIncrement tableName="event_stats" columnName="id" columnDataType="bigint"/>
</changeSet>

<changeSet id="GRANT-READ-PERMISSIONS-TO-ORCIDRO-ON-EVENT-STATS" author="Daniel Palafox" dbms="postgresql">
<preConditions>
<sqlCheck expectedResult="1">SELECT 1 FROM pg_roles WHERE rolname='orcidro'</sqlCheck>
</preConditions>
<sql>GRANT SELECT ON event_stats to orcidro;</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@

<bean id="eventDao" class="org.orcid.persistence.dao.impl.EventDaoImpl" />


<bean id="eventStatsDao" class="org.orcid.persistence.dao.impl.EventStatsDaoImpl" />

<bean id="emailDomainDao" class="org.orcid.persistence.dao.impl.EmailDomainDaoImpl" />

<bean id="emailDomainDaoReadOnly" class="org.orcid.persistence.dao.impl.EmailDomainDaoImpl">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.orcid.persistence;
package org.orcid.persistence.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

Expand All @@ -14,7 +15,6 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.orcid.persistence.dao.EventDao;
import org.orcid.persistence.jpa.entities.EventEntity;
import org.orcid.test.DBUnitTest;
import org.orcid.test.OrcidJUnit4ClassRunner;
Expand All @@ -39,6 +39,21 @@ public static void removeDBUnitData() throws Exception {
removeDBUnitData(Arrays.asList("/data/EventEntityData.xml", "/data/ProfileEntityData.xml", "/data/SourceClientDetailsEntityData.xml"));
}

@Test
public void deleteEventsByDate() {
List<EventEntity> eventEntityList = eventDao.findAll();

assertNotNull(eventEntityList);
assertEquals(3, eventEntityList.size());

eventDao.deleteEventsByDate(90);

eventEntityList = eventDao.findAll();

assertNotNull(eventEntityList);
assertEquals(0, eventEntityList.size());
}

@Test
public void testWriteEvent() throws IllegalAccessException {
EventEntity eventEntity = new EventEntity();
Expand All @@ -63,5 +78,4 @@ public void testWriteEvent() throws IllegalAccessException {
assertEquals(eventEntity.getLabel(), fromDb.getLabel());
assertNotNull(fromDb.getDateCreated());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.orcid.persistence.dao;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.orcid.persistence.jpa.entities.EventEntity;
import org.orcid.persistence.jpa.entities.EventStatsEntity;
import org.orcid.test.OrcidJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(OrcidJUnit4ClassRunner.class)
@ContextConfiguration(inheritInitializers = false, inheritLocations = false, locations = {"classpath:test-orcid-persistence-context.xml"})
public class EventStatsDaoTest {

@Resource(name = "eventDao")
private EventDao eventDao;

@Resource(name = "eventStatsDao")
private EventStatsDao eventStatsDao;

@Test
public void createEventStats() {
createEvents();

eventStatsDao.createEventStats();

List<EventStatsEntity> eventStatsEntityList = eventStatsDao.findAll();

assertNotNull(eventStatsEntityList);
assertEquals(1, eventStatsEntityList.size());
}

private void createEvents() {
for (int i = 0; i < 20; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType("Sign-In");
eventEntity.setClientId("Client " + 1);
eventEntity.setDateCreated(new Date());
eventDao.createEvent(eventEntity);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.orcid.scheduler.web.event;

public interface EventStats {

void saveEventStats();

void deleteEvents();
}
Loading

0 comments on commit f96c147

Please sign in to comment.