diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java index b2added5546..544c1e3302d 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java @@ -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); } diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java index b842ca4c792..ef94b970d14 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java @@ -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); diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventStatsDaoImpl.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventStatsDaoImpl.java index b427bd0045d..5245acc6e8e 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventStatsDaoImpl.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventStatsDaoImpl.java @@ -48,11 +48,11 @@ public List 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); @@ -60,20 +60,21 @@ public void createPapiEventStats() { List 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(); }); } diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java index c656f6fd44b..bce397fbd89 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java @@ -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; @@ -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; diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventStatsEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventStatsEntity.java index 9134bdaa496..2e0dd7cdd11 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventStatsEntity.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventStatsEntity.java @@ -22,6 +22,7 @@ public class EventStatsEntity extends BaseEntity{ private Long id; private String eventType; private String clientId; + private String ip; private Integer count; private Date date; @@ -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; diff --git a/orcid-persistence/src/main/resources/db-master.xml b/orcid-persistence/src/main/resources/db-master.xml index 7a97b36f35e..fda8f31a661 100644 --- a/orcid-persistence/src/main/resources/db-master.xml +++ b/orcid-persistence/src/main/resources/db-master.xml @@ -385,5 +385,6 @@ - + + diff --git a/orcid-persistence/src/main/resources/db/updates/add_ip_col_in_events.xml b/orcid-persistence/src/main/resources/db/updates/add_ip_col_in_events.xml new file mode 100644 index 00000000000..c0346ae2467 --- /dev/null +++ b/orcid-persistence/src/main/resources/db/updates/add_ip_col_in_events.xml @@ -0,0 +1,23 @@ + + + + + + + + + ALTER TABLE event ADD ip VARCHAR(15); + + + + + + + + + ALTER TABLE event_stats ADD ip VARCHAR(15); + + + \ No newline at end of file diff --git a/orcid-persistence/src/main/resources/db/updates/dw_papi_event_stats.xml b/orcid-persistence/src/main/resources/db/updates/dw_papi_event_stats.xml index 921b7d03fc4..6be18bfd3ea 100644 --- a/orcid-persistence/src/main/resources/db/updates/dw_papi_event_stats.xml +++ b/orcid-persistence/src/main/resources/db/updates/dw_papi_event_stats.xml @@ -29,7 +29,7 @@ - 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; diff --git a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java index 3f9966fc0b6..42d0b7a76c4 100644 --- a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java +++ b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java @@ -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"}) @@ -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); } @@ -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)); @@ -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(); @@ -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(); diff --git a/orcid-pub-web/src/main/java/org/orcid/api/publicV3/server/delegator/impl/PublicV3ApiServiceDelegatorImpl.java b/orcid-pub-web/src/main/java/org/orcid/api/publicV3/server/delegator/impl/PublicV3ApiServiceDelegatorImpl.java index ec686740cdc..f714009c6c3 100644 --- a/orcid-pub-web/src/main/java/org/orcid/api/publicV3/server/delegator/impl/PublicV3ApiServiceDelegatorImpl.java +++ b/orcid-pub-web/src/main/java/org/orcid/api/publicV3/server/delegator/impl/PublicV3ApiServiceDelegatorImpl.java @@ -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); } }