-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6960 from ORCID/8983-tech-cron-job-to-store-the-d…
…aily-stats-in-eventstats-and-remove-old-events feature: Add cron job to delete events and create events stats
- Loading branch information
Showing
17 changed files
with
389 additions
and
7 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
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
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 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> |
27 changes: 27 additions & 0 deletions
27
orcid-persistence/src/main/resources/db/updates/dw_event_stats.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,27 @@ | ||
<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="DROP-VIEW-FOR-EVENT" author="Daniel Palafox" dbms="postgresql"> | ||
<dropView viewName="dw_event"/> | ||
</changeSet> | ||
|
||
<changeSet id="CREATE-DW-EVENT-STATS-VIEW-GROUP-BY-DAY-CLIENT_ID-AND-EVENT_TYPE" author="Daniel Palafox" dbms="postgresql"> | ||
<preConditions onFail="MARK_RAN"> | ||
<not><viewExists viewName="dw_event_stats"/></not> | ||
</preConditions> | ||
<createView viewName="dw_event_stats"> | ||
SELECT event_type, client_id, count, DATE_TRUNC('day', date), DATE_TRUNC('day', date) as last_modified | ||
FROM event_stats | ||
ORDER BY DATE_TRUNC('day', date_created) DESC; | ||
</createView> | ||
</changeSet> | ||
|
||
<changeSet id="GRANT-READ-TO-DW_USER-TO-DW_EVENT-STATS-VIEW" author="Daniel Palafox" dbms="postgresql"> | ||
<preConditions> | ||
<sqlCheck expectedResult="1">SELECT 1 FROM pg_roles WHERE rolname='dw_user'</sqlCheck> | ||
</preConditions> | ||
<sql>GRANT SELECT ON TABLE dw_event_stats to dw_user;</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
Oops, something went wrong.