@Override public Alert createAlert(String checkId, Alert alert) { alert.setId(ObjectId.get().toString()); alert.setCheckId(checkId); getAlertsCollection().insert(mapper.alertToDBObject(alert)); return alert; }
@Override public Check getCheck(String checkId) { DBObject dbo = getChecksCollection().findOne(object("_id", checkId)); if (dbo == null) { return null; } return mapper.checkFrom(dbo); }
@Override public void updateSubscription(String checkId, Subscription subscription) { DBObject subscriptionObject = mapper.subscriptionToDBObject(subscription); DBObject subscriptionFindObject = forId(subscription.getId()); DBObject checkFindObject = forId(checkId).with("subscriptions", object("$elemMatch", subscriptionFindObject)); DBObject updateObject = object("$set", object("subscriptions.$", subscriptionObject)); getChecksCollection().update(checkFindObject, updateObject); }
@Override public Subscription createSubscription(String checkId, Subscription subscription) { subscription.setId(ObjectId.get().toString()); DBObject check = forId(checkId); DBObject query = object("$push", object("subscriptions", mapper.subscriptionToDBObject(subscription))); getChecksCollection().update(check, query); return subscription; }
@Override public Alert getLastAlertForTargetOfCheck(String target, String checkId) { DBObject query = object("checkId", checkId).with("target", target); DBCursor cursor = getAlertsCollection().find(query).sort(object("timestamp", -1)).limit(1); try { while (cursor.hasNext()) { return mapper.alertFrom(cursor.next()); } } finally { cursor.close(); } return null; }
@Override 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()) { checks.add(mapper.checkFrom(dbc.next())); } return new SeyrenResponse<Check>().withValues(checks).withTotal(dbc.count()); }
@Override public SeyrenResponse<Alert> getAlerts(int start, int items) { DBCursor dbc = getAlertsCollection().find().sort(object("timestamp", -1)).skip(start).limit(items); List<Alert> alerts = new ArrayList<Alert>(); while (dbc.hasNext()) { alerts.add(mapper.alertFrom(dbc.next())); } dbc.close(); return new SeyrenResponse<Alert>() .withValues(alerts) .withItems(items) .withStart(start) .withTotal(dbc.count()); }
@Override 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()) { checks.add(mapper.checkFrom(dbc.next())); } dbc.close(); return new SeyrenResponse<Check>().withValues(checks).withTotal(dbc.count()); }
@Override public Check createCheck(Check check) { check.setId(ObjectId.get().toString()); getChecksCollection().insert(mapper.checkToDBObject(check)); return check; }