Skip to content

Commit

Permalink
feature: Add ip table to event and event_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Palafox committed Apr 3, 2024
1 parent f64cc2b commit 13e0acc
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface EventManager {

void createEvent(EventType eventType, HttpServletRequest request);

void createPapiEvent(String clientId, boolean anonymous);
void createPapiEvent(String clientId, String ip, boolean anonymous);

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ private String removeAttributesFromUrl(String url) {
}

@Override
public void createPapiEvent(String clientId, boolean anonymous) {
public void createPapiEvent(String clientId, String ip, boolean anonymous) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId(clientId);
eventEntity.setIp(ip);
eventEntity.setLabel(anonymous ? "anonymous" : null);
eventEntity.setDateCreated(new Date());
eventDao.createEvent(eventEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,33 @@ public List<EventStatsEntity> findAll() {
@Transactional
public void createPapiEventStats() {
String query =
"SELECT event_type, client_id, label, count(*), CAST(date_created as date), now(), now() " +
"SELECT event_type, client_id, ip, label, count(*), CAST(date_created as date), now(), now() " +
"FROM event " +
"WHERE event_type = '"+ EventType.PAPI.getValue() + "' " +
"AND CAST(date_created as date) = CAST(now() - (CAST('1' AS INTERVAL DAY) * 1) as date) " +
"GROUP BY event_type, client_id, label, CAST(date_created as date) " +
"GROUP BY event_type, client_id, ip, label, CAST(date_created as date) " +
"ORDER BY CAST(date_created as date) DESC;";

Query queryList = entityManager.createNativeQuery(query);
List<Object[]> eventsList = queryList.getResultList();
List<Object[]> eventsListToRemove = new ArrayList<>();
if (eventsList.size() > 0) {
eventsList.forEach(item -> {
if (item[2] == "anonymous" && item[3] != null && ((BigInteger) item[3]).intValue() < 1000) {
if (item[3] == "anonymous" && item[4] != null && ((BigInteger) item[4]).intValue() < 1000) {
eventsListToRemove.add(item);
}
});
eventsList.removeAll(eventsListToRemove);
eventsList.forEach(item -> {
String insertQuery = "INSERT INTO event_stats (event_type, client_id, count, date, date_created, last_modified) VALUES (:eventType, :clientId, :count, :date, :dateCreated, :lastModified)";
String insertQuery = "INSERT INTO event_stats (event_type, client_id, ip, count, date, date_created, last_modified) VALUES (:eventType, :clientId, :ip, :count, :date, :dateCreated, :lastModified)";
Query insertQueryClients = entityManager.createNativeQuery(insertQuery);
insertQueryClients.setParameter("eventType", item[0]);
insertQueryClients.setParameter("clientId", item[1]);
insertQueryClients.setParameter("count", item[3]);
insertQueryClients.setParameter("date", item[4]);
insertQueryClients.setParameter("dateCreated", item[5]);
insertQueryClients.setParameter("lastModified", item[6]);
insertQueryClients.setParameter("ip", item[2]);
insertQueryClients.setParameter("count", item[4]);
insertQueryClients.setParameter("date", item[5]);
insertQueryClients.setParameter("dateCreated", item[6]);
insertQueryClients.setParameter("lastModified", item[7]);
insertQueryClients.executeUpdate();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EventEntity {
private static final long serialVersionUID = 1L;
private Long id;
private String clientId;
private String ip;
private String eventType;
private String label;
private Date dateCreated;
Expand Down Expand Up @@ -56,6 +57,15 @@ public void setClientId(String client_id) {
this.clientId = client_id;
}

@Column(name = "ip")
public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

@Column(name = "label")
public String getLabel() {
return label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EventStatsEntity extends BaseEntity<Long>{
private Long id;
private String eventType;
private String clientId;
private String ip;
private Integer count;
private Date date;

Expand Down Expand Up @@ -55,6 +56,15 @@ public void setClientId(String client_id) {
this.clientId = client_id;
}

@Column(name = "ip")
public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

@Column(name = "count")
public Integer getCount() {
return count;
Expand Down
3 changes: 2 additions & 1 deletion orcid-persistence/src/main/resources/db-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,6 @@
<include file="/db/updates/dw_event_stats.xml" />
<include file="/db/updates/add_indexes_2024_02_20.xml" />
<include file="/db/updates/dw_views_with_4_months_interval.xml" />
<include file="/db/updates/dw_papi_event_stats.xml" />
<include file="/db/updates/dw_papi_event_stats.xml" />
<include file="/db/updates/add_ip_col_in_events.xml" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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="ADD-FIELD-TO-EVENT" author="Daniel Palafox">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="event" columnName="ip"/>
</not>
</preConditions>
<sql>ALTER TABLE event ADD ip VARCHAR(15);</sql>
</changeSet>

<changeSet id="ADD-FIELD-TO-EVENT_STATS" author="Daniel Palafox">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="event_stats" columnName="ip"/>
</not>
</preConditions>
<sql>ALTER TABLE event_stats ADD ip VARCHAR(15);</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<not><viewExists viewName="dw_papi_event_stats"/></not>
</preConditions>
<createView viewName="dw_papi_event_stats">
SELECT event_type, client_id, count, DATE_TRUNC('day', date), DATE_TRUNC('day', date) as last_modified
SELECT event_type, client_id, ip, count, DATE_TRUNC('day', date), DATE_TRUNC('day', date) as last_modified
FROM event_stats WHERE event_type = 'Public-API'
ORDER BY DATE_TRUNC('day', date_created) DESC;
</createView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

@RunWith(OrcidJUnit4ClassRunner.class)
@ContextConfiguration(inheritInitializers = false, inheritLocations = false, locations = {"classpath:test-orcid-persistence-context.xml"})
Expand Down Expand Up @@ -54,10 +55,21 @@ public void createPapiEventStats() {

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

assertEquals("Client 1", eventStatsEntityList.get(0).getClientId());
assertEquals("105.21.229.72", eventStatsEntityList.get(0).getIp());
assertEquals(Integer.valueOf(10), eventStatsEntityList.get(0).getCount());

assertEquals("Client 2", eventStatsEntityList.get(1).getClientId());
assertEquals("105.21.229.73", eventStatsEntityList.get(1).getIp());
assertEquals(Integer.valueOf(10), eventStatsEntityList.get(1).getCount());

assertNull(eventStatsEntityList.get(2).getClientId());
assertEquals("104.20.228.70", eventStatsEntityList.get(2).getIp());
assertEquals(Integer.valueOf(1100), eventStatsEntityList.get(2).getCount());



eventStatsDao.deleteStatsByType(EventType.PAPI);
}

Expand All @@ -66,6 +78,7 @@ private void createPapiEvents() {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("Client " + (i % 2 == 0 ? 1 : 2));
eventEntity.setIp("105.21.229.7" + (i % 2 == 0 ? 2 : 3));
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
eventEntity.setDateCreated(Date.from(instant));
Expand All @@ -75,7 +88,7 @@ private void createPapiEvents() {
for (int i = 0; i < 10; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("105.21.229.71");
eventEntity.setIp("105.21.229.71");
eventEntity.setLabel("anonymous");
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
Expand All @@ -86,7 +99,7 @@ private void createPapiEvents() {
for (int i = 0; i < 1100; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("104.20.228.70");
eventEntity.setIp("104.20.228.70");
eventEntity.setLabel("anonymous");
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,10 @@ public Response viewResearchResourceSummary(String orcid, Long putCode) {

@Override
public void trackEvents(HttpServletRequest httpRequest) {
boolean anonymous = false;
String clientId = orcidSecurityManager.getClientIdFromAPIRequest();
if (clientId == null) {
clientId = OrcidRequestUtil.getIpAddress(httpRequest);
anonymous = true;
}
eventManager.createPapiEvent(clientId, anonymous);
String ip = OrcidRequestUtil.getIpAddress(httpRequest);

eventManager.createPapiEvent(clientId, ip, clientId == null ? true : false);
}

}

0 comments on commit 13e0acc

Please sign in to comment.