-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add cron job to delete events and create events stats
- Loading branch information
1 parent
a956004
commit f96c147
Showing
15 changed files
with
345 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
orcid-persistence/src/main/java/org/orcid/persistence/dao/EventStatsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventStatsDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventStatsEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
orcid-persistence/src/main/resources/db/updates/create_event_stats_table.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
orcid-scheduler-web/src/main/java/org/orcid/scheduler/web/event/EventStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.