[KEYCLOAK-1200]- From and To filter fields in Event viewer in admin app

This commit is contained in:
girirajsharma 2015-05-28 18:05:31 +05:30
parent 92567a070c
commit efe8342965
11 changed files with 124 additions and 149 deletions

View file

@ -1,5 +1,6 @@
package org.keycloak.events; package org.keycloak.events;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -15,9 +16,9 @@ public interface EventQuery {
public EventQuery user(String userId); public EventQuery user(String userId);
public EventQuery fromDate(String fromDate); public EventQuery fromDate(Date fromDate);
public EventQuery toDate(String toDate); public EventQuery toDate(Date toDate);
public EventQuery ipAddress(String ipAddress); public EventQuery ipAddress(String ipAddress);

View file

@ -1,5 +1,6 @@
package org.keycloak.events.admin; package org.keycloak.events.admin;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -71,18 +72,18 @@ public interface AdminEventQuery {
/** /**
* Search by events after the specified time * Search by events after the specified time
* *
* @param fromTime time in millis * @param fromTime from date
* @return <code>this</code> for method chaining * @return <code>this</code> for method chaining
*/ */
AdminEventQuery fromTime(String fromTime); AdminEventQuery fromTime(Date fromTime);
/** /**
* Search by events before the specified time * Search by events before the specified time
* *
* @param toTime time in millis * @param toTime to date
* @return <code>this</code> for method chaining * @return <code>this</code> for method chaining
*/ */
AdminEventQuery toTime(String toTime); AdminEventQuery toTime(Date toTime);
/** /**
* Used for pagination * Used for pagination

View file

@ -3,6 +3,7 @@ package org.keycloak.events.jpa;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -88,28 +89,14 @@ public class JpaAdminEventQuery implements AdminEventQuery {
} }
@Override @Override
public AdminEventQuery fromTime(String fromTime) { public AdminEventQuery fromTime(Date fromTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); predicates.add(cb.greaterThanOrEqualTo(root.<Long>get("time"), fromTime.getTime()));
Long from = null;
try {
from = df.parse(fromTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
predicates.add(cb.greaterThanOrEqualTo(root.<Long>get("time"), from));
return this; return this;
} }
@Override @Override
public AdminEventQuery toTime(String toTime) { public AdminEventQuery toTime(Date toTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); predicates.add(cb.lessThanOrEqualTo(root.<Long>get("time"), toTime.getTime()));
Long to = null;
try {
to = df.parse(toTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
predicates.add(cb.lessThanOrEqualTo(root.<Long>get("time"), to));
return this; return this;
} }

View file

@ -11,9 +11,8 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -68,28 +67,14 @@ public class JpaEventQuery implements EventQuery {
} }
@Override @Override
public EventQuery fromDate(String fromDate) { public EventQuery fromDate(Date fromDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); predicates.add(cb.greaterThanOrEqualTo(root.<Long>get("time"), fromDate.getTime()));
Long from = null;
try {
from = df.parse(fromDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
predicates.add(cb.greaterThanOrEqualTo(root.<Long>get("time"), from));
return this; return this;
} }
@Override @Override
public EventQuery toDate(String toDate) { public EventQuery toDate(Date toDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); predicates.add(cb.lessThanOrEqualTo(root.<Long>get("time"), toDate.getTime()));
Long to = null;
try {
to = df.parse(toDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
predicates.add(cb.lessThanOrEqualTo(root.<Long>get("time"), to));
return this; return this;
} }

View file

@ -2,6 +2,7 @@ package org.keycloak.events.mongo;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -74,28 +75,14 @@ public class MongoAdminEventQuery implements AdminEventQuery{
} }
@Override @Override
public AdminEventQuery fromTime(String fromTime) { public AdminEventQuery fromTime(Date fromTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); query.put("time", BasicDBObjectBuilder.start("$gte", fromTime.getTime()).get());
Long from = null;
try {
from = df.parse(fromTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
query.put("time", BasicDBObjectBuilder.start("$gte", from).get());
return this; return this;
} }
@Override @Override
public AdminEventQuery toTime(String toTime) { public AdminEventQuery toTime(Date toTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); query.put("time", BasicDBObjectBuilder.start("$lte", toTime.getTime()).get());
Long to = null;
try {
to = df.parse(toTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
query.put("time", BasicDBObjectBuilder.start("$lte", to).get());
return this; return this;
} }

View file

@ -4,14 +4,15 @@ import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder; import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBCollection; import com.mongodb.DBCollection;
import com.mongodb.DBCursor; import com.mongodb.DBCursor;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import org.keycloak.events.Event; import org.keycloak.events.Event;
import org.keycloak.events.EventQuery; import org.keycloak.events.EventQuery;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -59,31 +60,17 @@ public class MongoEventQuery implements EventQuery {
} }
@Override @Override
public EventQuery fromDate(String fromDate) { public EventQuery fromDate(Date fromDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long from = null;
try {
from = df.parse(fromDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
BasicDBObject time = query.containsField("time") ? (BasicDBObject) query.get("time") : new BasicDBObject(); BasicDBObject time = query.containsField("time") ? (BasicDBObject) query.get("time") : new BasicDBObject();
time.append("$gte", from); time.append("$gte", fromDate.getTime());
query.put("time", time); query.put("time", time);
return this; return this;
} }
@Override @Override
public EventQuery toDate(String toDate) { public EventQuery toDate(Date toDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long to = null;
try {
to = df.parse(toDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
BasicDBObject time = query.containsField("time") ? (BasicDBObject) query.get("time") : new BasicDBObject(); BasicDBObject time = query.containsField("time") ? (BasicDBObject) query.get("time") : new BasicDBObject();
time.append("$lte", to); time.append("$lte", toDate.getTime());
query.put("time", time); query.put("time", time);
return this; return this;
} }

View file

@ -3,6 +3,7 @@ package org.keycloak.examples.providers.events;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -112,18 +113,10 @@ public class MemAdminEventQuery implements AdminEventQuery {
} }
@Override @Override
public AdminEventQuery fromTime(String fromTime) { public AdminEventQuery fromTime(Date fromTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long from = null;
try {
from = df.parse(fromTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
Iterator<AdminEvent> itr = this.adminEvents.iterator(); Iterator<AdminEvent> itr = this.adminEvents.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
if (!(itr.next().getTime() >= from)) { if (!(itr.next().getTime() >= fromTime.getTime())) {
itr.remove(); itr.remove();
} }
} }
@ -131,18 +124,10 @@ public class MemAdminEventQuery implements AdminEventQuery {
} }
@Override @Override
public AdminEventQuery toTime(String toTime) { public AdminEventQuery toTime(Date toTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long to = null;
try {
to = df.parse(toTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
Iterator<AdminEvent> itr = this.adminEvents.iterator(); Iterator<AdminEvent> itr = this.adminEvents.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
if (!(itr.next().getTime() <= to)) { if (!(itr.next().getTime() <= toTime.getTime())) {
itr.remove(); itr.remove();
} }
} }

View file

@ -7,6 +7,7 @@ import org.keycloak.events.EventType;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -77,18 +78,10 @@ public class MemEventQuery implements EventQuery {
} }
@Override @Override
public EventQuery fromDate(String fromDate) { public EventQuery fromDate(Date fromDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long from = null;
try {
from = df.parse(fromDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
Iterator<Event> itr = this.events.iterator(); Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
if (!(itr.next().getTime() >= from)) { if (!(itr.next().getTime() >= fromDate.getTime())) {
itr.remove(); itr.remove();
} }
} }
@ -96,18 +89,10 @@ public class MemEventQuery implements EventQuery {
} }
@Override @Override
public EventQuery toDate(String toDate) { public EventQuery toDate(Date toDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Long to = null;
try {
to = df.parse(toDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
Iterator<Event> itr = this.events.iterator(); Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
if (!(itr.next().getTime() <= to)) { if (!(itr.next().getTime() <= toDate.getTime())) {
itr.remove(); itr.remove();
} }
} }

View file

@ -35,6 +35,7 @@ import org.keycloak.services.managers.UsersSyncManager;
import org.keycloak.services.ErrorResponse; import org.keycloak.services.ErrorResponse;
import org.keycloak.timer.TimerProvider; import org.keycloak.timer.TimerProvider;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -50,6 +51,10 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -416,10 +421,25 @@ public class RealmAdminResource {
} }
if(dateFrom != null) { if(dateFrom != null) {
query.fromDate(dateFrom); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date from = null;
try {
from = df.parse(dateFrom);
} catch (ParseException e) {
throw new BadRequestException("Invalid value for 'Date(From)', excepted format is yyyy-MM-dd");
}
query.fromDate(from);
} }
if(dateTo != null) { if(dateTo != null) {
query.toDate(dateTo); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date to = null;
try {
to = df.parse(dateTo);
} catch (ParseException e) {
throw new BadRequestException("Invalid value for 'Date(To)', excepted format is yyyy-MM-dd");
}
query.toDate(to);
} }
if (ipAddress != null) { if (ipAddress != null) {
@ -494,10 +514,25 @@ public class RealmAdminResource {
} }
if(dateFrom != null) { if(dateFrom != null) {
query.fromTime(dateFrom); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date from = null;
try {
from = df.parse(dateFrom);
} catch (ParseException e) {
throw new BadRequestException("Invalid value for 'Date(From)', excepted format is yyyy-MM-dd");
}
query.fromTime(from);
} }
if(dateTo != null) { if(dateTo != null) {
query.toTime(dateTo); SimpleDateFormat df = new SimpleDateFormat("Invalid value for 'Date(To)', excepted format is yyyy-MM-dd");
Date to = null;
try {
to = df.parse(dateTo);
} catch (ParseException e) {
throw new BadRequestException("An unexpected server error has occurred");
}
query.toTime(to);
} }
if (firstResult != null) { if (firstResult != null) {

View file

@ -83,14 +83,25 @@ public class AdminEventStoreProviderTest {
String d3 = new String("2015-03-06"); String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07"); String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null; Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try { try {
date1 = formatter.parse(d1); date1 = formatter.parse(d1);
date2 = formatter.parse(d2); date2 = formatter.parse(d2);
date3 = formatter.parse(d3); date3 = formatter.parse(d3);
date4 = formatter.parse(d4); date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -121,22 +132,22 @@ public class AdminEventStoreProviderTest {
Assert.assertEquals(1, eventStore.createAdminQuery().operation(OperationType.DELETE).getResultList().size()); Assert.assertEquals(1, eventStore.createAdminQuery().operation(OperationType.DELETE).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().operation(OperationType.CREATE).getResultList().size()); Assert.assertEquals(4, eventStore.createAdminQuery().operation(OperationType.CREATE).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().fromTime("2015-03-04").getResultList().size()); Assert.assertEquals(8, eventStore.createAdminQuery().fromTime(date1).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().toTime("2015-03-07").getResultList().size()); Assert.assertEquals(8, eventStore.createAdminQuery().toTime(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime("2015-03-06").getResultList().size()); Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date3).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().toTime("2015-03-05").getResultList().size()); Assert.assertEquals(4, eventStore.createAdminQuery().toTime(date2).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime("2015-03-08").getResultList().size()); Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date7).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().toTime("2015-03-03").getResultList().size()); Assert.assertEquals(0, eventStore.createAdminQuery().toTime(date6).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().fromTime("2015-03-04").toTime("2015-03-07").getResultList().size()); Assert.assertEquals(8, eventStore.createAdminQuery().fromTime(date1).toTime(date4).getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().fromTime("2015-03-05").toTime("2015-03-07").getResultList().size()); Assert.assertEquals(6, eventStore.createAdminQuery().fromTime(date2).toTime(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime("2015-03-04").toTime("2015-03-05").getResultList().size()); Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date1).toTime(date2).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime("2015-03-06").toTime("2015-03-07").getResultList().size()); Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date3).toTime(date4).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime("2015-03-01").toTime("2015-03-03").getResultList().size()); Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date5).toTime(date6).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime("2015-03-08").toTime("2015-03-10").getResultList().size()); Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date7).toTime(date8).getResultList().size());
} }

View file

@ -84,14 +84,25 @@ public class EventStoreProviderTest {
String d3 = new String("2015-03-06"); String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07"); String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null; Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try { try {
date1 = formatter.parse(d1); date1 = formatter.parse(d1);
date2 = formatter.parse(d2); date2 = formatter.parse(d2);
date3 = formatter.parse(d3); date3 = formatter.parse(d3);
date4 = formatter.parse(d4); date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -124,22 +135,22 @@ public class EventStoreProviderTest {
Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_PROFILE).getResultList().size()); Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_PROFILE).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_EMAIL).getResultList().size()); Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_EMAIL).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().fromDate("2015-03-04").getResultList().size()); Assert.assertEquals(8, eventStore.createQuery().fromDate(date1).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().toDate("2015-03-07").getResultList().size()); Assert.assertEquals(8, eventStore.createQuery().toDate(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate("2015-03-06").getResultList().size()); Assert.assertEquals(4, eventStore.createQuery().fromDate(date3).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().toDate("2015-03-05").getResultList().size()); Assert.assertEquals(4, eventStore.createQuery().toDate(date2).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate("2015-03-08").getResultList().size()); Assert.assertEquals(0, eventStore.createQuery().fromDate(date7).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().toDate("2015-03-03").getResultList().size()); Assert.assertEquals(0, eventStore.createQuery().toDate(date6).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().fromDate("2015-03-04").toDate("2015-03-07").getResultList().size()); Assert.assertEquals(8, eventStore.createQuery().fromDate(date1).toDate(date4).getResultList().size());
Assert.assertEquals(6, eventStore.createQuery().fromDate("2015-03-05").toDate("2015-03-07").getResultList().size()); Assert.assertEquals(6, eventStore.createQuery().fromDate(date2).toDate(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate("2015-03-04").toDate("2015-03-05").getResultList().size()); Assert.assertEquals(4, eventStore.createQuery().fromDate(date1).toDate(date2).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate("2015-03-06").toDate("2015-03-07").getResultList().size()); Assert.assertEquals(4, eventStore.createQuery().fromDate(date3).toDate(date4).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate("2015-03-01").toDate("2015-03-03").getResultList().size()); Assert.assertEquals(0, eventStore.createQuery().fromDate(date5).toDate(date6).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate("2015-03-08").toDate("2015-03-10").getResultList().size()); Assert.assertEquals(0, eventStore.createQuery().fromDate(date7).toDate(date8).getResultList().size());
} }