Skip to content

Commit

Permalink
Issue scobal#48 - GET /checks now using SeyrenResponse. No paging as …
Browse files Browse the repository at this point in the history
…not yet needed on any of the pages
  • Loading branch information
Mark Pope committed Aug 9, 2012
1 parent 42e460c commit 36efd74
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class ChecksAT {
public void testGetChecksReturnsOk() {
Response response = get(checks());
assertThat(response, hasStatusCode(200));
assertThat(response.asJson(), hasJsonPath("$.", hasSize(0)));
assertThat(response.asJson(), hasJsonPath("$.values", hasSize(0)));
}

@Test
public void testGetChecksReturnsResultsOk() {
Response createResponse = createCheck("{ }");
Response response = get(checks());
assertThat(response, hasStatusCode(200));
assertThat(response.asJson(), hasJsonPath("$.", hasSize(1)));
assertThat(response.asJson(), hasJsonPath("$.values", hasSize(1)));
deleteLocation(createResponse.getHeader("Location").getValue());
}

Expand All @@ -46,7 +46,7 @@ public void testGetChecksByErrorStateReturnsOk() {
Response createResponse = createCheck("{ \"state\" : \"ERROR\" }");
Response response = get(checks().withParam("state", "ERROR"));
assertThat(response, hasStatusCode(200));
assertThat(response.asJson(), hasJsonPath("$.", hasSize(1)));
assertThat(response.asJson(), hasJsonPath("$.values", hasSize(1)));
deleteLocation(createResponse.getHeader("Location").getValue());
}

Expand All @@ -55,7 +55,7 @@ public void testGetChecksByWarnStateReturnsOk() {
Response createResponse = createCheck("{ \"state\" : \"WARN\" }");
Response response = get(checks().withParam("state", "WARN"));
assertThat(response, hasStatusCode(200));
assertThat(response.asJson(), hasJsonPath("$.", hasSize(1)));
assertThat(response.asJson(), hasJsonPath("$.values", hasSize(1)));
deleteLocation(createResponse.getHeader("Location").getValue());
}

Expand Down
23 changes: 4 additions & 19 deletions seyren-api/src/main/java/com/seyren/api/bean/ChecksBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import com.seyren.api.jaxrs.ChecksResource;
import com.seyren.core.domain.AlertType;
import com.seyren.core.domain.Check;
import com.seyren.core.domain.SeyrenResponse;
import com.seyren.core.store.ChecksStore;

@Named
Expand All @@ -43,14 +40,11 @@ public ChecksBean(ChecksStore checksStore) {

@Override
public Response getChecks(Set<String> states, Boolean enabled) {
List<Check> checks;
SeyrenResponse<Check> checks;
if (states != null && !states.isEmpty()) {
checks = checksStore.getChecksByState(states);
checks = checksStore.getChecksByState(states, enabled);
} else {
checks = checksStore.getChecks();
}
if (enabled != null) {
filterByEnabled(checks, enabled);
checks = checksStore.getChecks(enabled);
}
return Response.ok(checks).build();
}
Expand Down Expand Up @@ -89,15 +83,6 @@ public Response deleteCheck(String checkId) {
return Response.noContent().build();
}

private void filterByEnabled(final List<Check> checks, final boolean enabled) {
CollectionUtils.filter(checks, new Predicate() {
@Override
public boolean evaluate(Object object) {
return ((Check)object).isEnabled() == enabled;
}
});
}

private URI uri(String checkId) {
try {
return new URI("checks/" + checkId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CheckScheduler(ChecksStore checksStore, AlertsStore alertsStore, Notifica

@Scheduled(fixedRate = 60000)
public void performChecks() {
List<Check> checks = checksStore.getChecks();
List<Check> checks = checksStore.getChecks(true).getValues();
for (final Check check : checks) {
executor.execute(new CheckRunner(check));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
*/
package com.seyren.core.store;

import java.util.List;
import java.util.Set;

import com.seyren.core.domain.Check;
import com.seyren.core.domain.SeyrenResponse;

public interface ChecksStore {

List<Check> getChecks();
SeyrenResponse<Check> getChecks(Boolean enabled);

List<Check> getChecksByState(Set<String> states);
SeyrenResponse<Check> getChecksByState(Set<String> states, Boolean enabled);

Check getCheck(String checkId);

Expand Down
37 changes: 27 additions & 10 deletions seyren-mongo/src/main/java/com/seyren/mongo/MongoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,41 @@ private DBCollection getAlertsCollection() {
}

@Override
public List<Check> getChecks() {
List<Check> result = new ArrayList<Check>();
DBCursor dbc = getChecksCollection().find();
public SeyrenResponse<Check> getChecks(Boolean enabled) {
List<Check> checks = new ArrayList<Check>();
DBCursor dbc;
if (enabled != null) {
dbc = getChecksCollection().find(object("enabled", enabled));
} else {
dbc = getChecksCollection().find();
}
while (dbc.hasNext()) {
result.add(mapper.checkFrom(dbc.next()));
checks.add(mapper.checkFrom(dbc.next()));
}
return result;
return new SeyrenResponse<Check>()
.withValues(checks)
.withTotal(dbc.count());
}

@Override
public List<Check> getChecksByState(Set<String> states) {
List<Check> result = new ArrayList<Check>();
DBCursor dbc = getChecksCollection().find(object("state", object("$in", states.toArray())));
public SeyrenResponse<Check> getChecksByState(Set<String> states, Boolean enabled) {
List<Check> checks = new ArrayList<Check>();

DBObject query = new BasicDBObject();
query.put("state", object("$in", states.toArray()));
if (enabled != null) {
query.put("enabled", enabled);
}
DBCursor dbc = getChecksCollection().find(query);

while (dbc.hasNext()) {
result.add(mapper.checkFrom(dbc.next()));
checks.add(mapper.checkFrom(dbc.next()));
}
dbc.close();
return result;

return new SeyrenResponse<Check>()
.withValues(checks)
.withTotal(dbc.count());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion seyren-web/src/main/webapp/html/check.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h2>
<li><a ng:click="loadNewerAlerts()">Newer &rarr;</a></li>
</ul>
</div>
<table class="table table-bordered table-striped" ng:show="alerts">
<table class="table table-bordered table-striped" ng:show="alerts.values">
<thead>
<tr>
<th>Timestamp</th>
Expand Down
6 changes: 3 additions & 3 deletions seyren-web/src/main/webapp/html/checks.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>Checks</h1>
</div>
<div class="row-fluid">
<div class="span12">
<table class="table table-bordered table-striped" ng:show="checks" ng:init="predicate='name'">
<table class="table table-bordered table-striped" ng:show="checks.values.length > 0" ng:init="predicate='name'">
<thead>
<tr>
<th><a href="" ng:click="predicate = 'name'; reverse=!reverse">Name</a></th>
Expand All @@ -27,7 +27,7 @@ <h1>Checks</h1>
</tr>
</thead>
<tbody>
<tr ng:repeat="check in checks.$orderBy(predicate, reverse).$filter(filter)" ng:click="selectCheck(check.id)" style="cursor: pointer;">
<tr ng:repeat="check in checks.values.$orderBy(predicate, reverse).$filter(filter)" ng:click="selectCheck(check.id)" style="cursor: pointer;">
<td>{{ check.name }}</td>
<td>{{ check.target }}</td>
<td>
Expand All @@ -43,7 +43,7 @@ <h1>Checks</h1>
</tr>
</tbody>
</table>
<p ng:hide="checks">We've got no checks. Why not create one?</p>
<p ng:hide="checks.values.length > 0">We've got no checks. Why not create one?</p>
</div>

<div class="modal hide" id="createCheckModal">
Expand Down
6 changes: 3 additions & 3 deletions seyren-web/src/main/webapp/html/home.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row-fluid" ng:init="loadUnhealthyChecks()" ng:controller="HomeController">
<h2>Checks in an unhealthy state</h2>
<table class="table table-bordered table-striped" ng:show="unhealthyChecks">
<table class="table table-bordered table-striped" ng:show="unhealthyChecks.values.length > 0">
<thead>
<tr>
<th>Name</th>
Expand All @@ -12,7 +12,7 @@ <h2>Checks in an unhealthy state</h2>
</tr>
</thead>
<tbody>
<tr ng:repeat="check in unhealthyChecks" ng:click="selectCheck(check.id)" style="cursor: pointer;">
<tr ng:repeat="check in unhealthyChecks.values" ng:click="selectCheck(check.id)" style="cursor: pointer;">
<td>{{ check.name }}</td>
<td>{{ check.target }}</td>
<td>
Expand All @@ -28,7 +28,7 @@ <h2>Checks in an unhealthy state</h2>
</tr>
</tbody>
</table>
<p ng:hide="unhealthyChecks">No checks in an unhealthy state</p>
<p ng:hide="unhealthyChecks.values.length > 0">No checks in an unhealthy state</p>
</div>

<div class="row-fluid" ng:init="loadAlertStream()" ng:controller="HomeController">
Expand Down
2 changes: 1 addition & 1 deletion seyren-web/src/main/webapp/js/checks-controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global console,$ */
/*global console,$,angular */

function ChecksController() {
this.$xhr.defaults.headers.post['Content-Type'] = 'application/json';
Expand Down

0 comments on commit 36efd74

Please sign in to comment.