Add count parameter to rules history endpoint.

This commit is contained in:
Bartosz Ługowski 2015-11-16 21:07:53 +01:00
parent 0fb7e4e040
commit b0a444eafc
3 changed files with 142 additions and 20 deletions

View File

@ -189,6 +189,10 @@ Returns all rules for a specified datasource and includes default datasource.
Returns audit history of rules for all datasources. default value of interval can be specified by setting `druid.audit.manager.auditHistoryMillis` (1 week if not configured) in coordinator runtime.properties
* `/druid/coordinator/v1/rules/history?count=<n>`
Returns last <n> entries of audit history of rules for all datasources.
* `/druid/coordinator/v1/rules/{dataSourceName}/history?interval=<interval>`
Returns audit history of rules for a specified datasource. default value of interval can be specified by setting `druid.audit.manager.auditHistoryMillis` (1 week if not configured) in coordinator runtime.properties

View File

@ -20,6 +20,7 @@ package io.druid.server.http;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import io.druid.audit.AuditEntry;
import io.druid.audit.AuditInfo;
import io.druid.audit.AuditManager;
import io.druid.metadata.MetadataRuleManager;
@ -40,7 +41,6 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.List;
@ -116,37 +116,52 @@ public class RulesResource
@QueryParam("count") final Integer count
)
{
Interval theInterval = interval == null ? null : new Interval(interval);
if (theInterval == null && count != null) {
try {
return Response.ok(auditManager.fetchAuditHistory(dataSourceName, "rules", count))
return Response.ok(getRuleHistory(dataSourceName, interval, count))
.build();
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(ImmutableMap.<String, Object>of("error", e.getMessage()))
.build();
}
}
return Response.ok(auditManager.fetchAuditHistory(dataSourceName, "rules", theInterval))
.build();
}
@GET
@Path("/history")
@Produces(MediaType.APPLICATION_JSON)
public Response getDatasourceRuleHistory(
@QueryParam("interval") final String interval
@QueryParam("interval") final String interval,
@QueryParam("count") final Integer count
)
{
try {
Interval theInterval = interval == null ? null : new Interval(interval);
return Response.ok(auditManager.fetchAuditHistory("rules", theInterval))
return Response.ok(getRuleHistory(null, interval, count))
.build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(ImmutableMap.<String, Object>of("error", e.getMessage()))
.build();
}
catch (IllegalArgumentException e) {
return Response.serverError().entity(ImmutableMap.<String, Object>of("error", e.getMessage())).build();
}
}
private List<AuditEntry> getRuleHistory(
final String dataSourceName,
final String interval,
final Integer count
) throws IllegalArgumentException
{
if (interval == null && count != null) {
if (dataSourceName != null) {
return auditManager.fetchAuditHistory(dataSourceName, "rules", count);
}
return auditManager.fetchAuditHistory("rules", count);
}
Interval theInterval = interval == null ? null : new Interval(interval);
if (dataSourceName != null) {
return auditManager.fetchAuditHistory(dataSourceName, "rules", theInterval);
}
return auditManager.fetchAuditHistory("rules", theInterval);
}
}

View File

@ -152,4 +152,107 @@ public class RulesResourceTest
EasyMock.verify(auditManager);
}
@Test
public void testGetAllDatasourcesRuleHistoryWithCount()
{
AuditEntry entry1 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
new DateTime("2013-01-02T00:00:00Z")
);
AuditEntry entry2 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
new DateTime("2013-01-01T00:00:00Z")
);
EasyMock.expect(auditManager.fetchAuditHistory(EasyMock.eq("rules"), EasyMock.eq(2)))
.andReturn(ImmutableList.of(entry1, entry2))
.once();
EasyMock.replay(auditManager);
RulesResource rulesResource = new RulesResource(databaseRuleManager, auditManager);
Response response = rulesResource.getDatasourceRuleHistory(null, 2);
List<AuditEntry> rulesHistory = (List) response.getEntity();
Assert.assertEquals(2, rulesHistory.size());
Assert.assertEquals(entry1, rulesHistory.get(0));
Assert.assertEquals(entry2, rulesHistory.get(1));
EasyMock.verify(auditManager);
}
@Test
public void testGetAllDatasourcesRuleHistoryWithInterval()
{
String interval = "P2D/2013-01-02T00:00:00Z";
Interval theInterval = new Interval(interval);
AuditEntry entry1 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
new DateTime("2013-01-02T00:00:00Z")
);
AuditEntry entry2 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
new DateTime("2013-01-01T00:00:00Z")
);
EasyMock.expect(auditManager.fetchAuditHistory(EasyMock.eq("rules"), EasyMock.eq(theInterval)))
.andReturn(ImmutableList.of(entry1, entry2))
.once();
EasyMock.replay(auditManager);
RulesResource rulesResource = new RulesResource(databaseRuleManager, auditManager);
Response response = rulesResource.getDatasourceRuleHistory(interval, null);
List<AuditEntry> rulesHistory = (List) response.getEntity();
Assert.assertEquals(2, rulesHistory.size());
Assert.assertEquals(entry1, rulesHistory.get(0));
Assert.assertEquals(entry2, rulesHistory.get(1));
EasyMock.verify(auditManager);
}
@Test
public void testGetAllDatasourcesRuleHistoryWithWrongCount()
{
EasyMock.expect(auditManager.fetchAuditHistory(EasyMock.eq("rules"), EasyMock.eq(-1)))
.andThrow(new IllegalArgumentException("Limit must be greater than zero!"))
.once();
EasyMock.replay(auditManager);
RulesResource rulesResource = new RulesResource(databaseRuleManager, auditManager);
Response response = rulesResource.getDatasourceRuleHistory(null, -1);
Map<String, Object> rulesHistory = (Map) response.getEntity();
Assert.assertEquals(400, response.getStatus());
Assert.assertTrue(rulesHistory.containsKey("error"));
Assert.assertEquals("Limit must be greater than zero!", rulesHistory.get("error"));
EasyMock.verify(auditManager);
}
}