From f9993196664fe687bb8050711cc1e5acf891f1aa Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sat, 9 Jul 2016 17:20:57 -0400 Subject: [PATCH] Remove metrics, change GET /ipLog/:id to accept both ips and user uuids --- src/main/java/net/frozenorb/apiv3/APIv3.java | 6 +-- .../apiv3/handler/MetricsHandler.java | 43 ------------------- .../net/frozenorb/apiv3/model/IpLogEntry.java | 19 +++++--- .../net/frozenorb/apiv3/route/GETMetrics.java | 18 -------- .../apiv3/route/ipLog/GETIpLogId.java | 42 +++++++++++------- 5 files changed, 40 insertions(+), 88 deletions(-) delete mode 100644 src/main/java/net/frozenorb/apiv3/handler/MetricsHandler.java delete mode 100644 src/main/java/net/frozenorb/apiv3/route/GETMetrics.java diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index c631ac8..a2cd62b 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -34,10 +34,8 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.handler.ActorAttributeHandler; import net.frozenorb.apiv3.handler.AuthorizationHandler; -import net.frozenorb.apiv3.handler.MetricsHandler; import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.route.GETDumpsType; -import net.frozenorb.apiv3.route.GETMetrics; import net.frozenorb.apiv3.route.GETWhoAmI; import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId; import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens; @@ -259,7 +257,6 @@ public final class APIv3 extends AbstractVerticle { }); http.route().method(HttpMethod.PUT).method(HttpMethod.POST).method(HttpMethod.DELETE).handler(BodyHandler.create()); http.route().handler(new ActorAttributeHandler()); - http.route().handler(new MetricsHandler()); http.route().handler(new AuthorizationHandler()); http.exceptionHandler(Throwable::printStackTrace); @@ -306,7 +303,7 @@ public final class APIv3 extends AbstractVerticle { http.get("/ipIntel/:userIp").handler(new GETIpInteld()); - http.get("/ipLog/:userId").handler(new GETIpLogId()); + http.get("/ipLog/:id").handler(new GETIpLogId()); http.get("/notificationTemplates/:notificationTemplateId").handler(new GETNotificationTemplatesId()); http.get("/notificationTemplates").handler(new GETNotificationTemplates()); @@ -359,7 +356,6 @@ public final class APIv3 extends AbstractVerticle { http.post("/users/:userId/verifyTotp").handler(new POSTUsersIdVerifyTotp()); http.get("/dumps/:dumpType").handler(new GETDumpsType()); - http.get("/metrics").handler(new GETMetrics()); http.get("/whoami").handler(new GETWhoAmI()); int port = Integer.parseInt(config.getProperty("http.port")); diff --git a/src/main/java/net/frozenorb/apiv3/handler/MetricsHandler.java b/src/main/java/net/frozenorb/apiv3/handler/MetricsHandler.java deleted file mode 100644 index 73fe81b..0000000 --- a/src/main/java/net/frozenorb/apiv3/handler/MetricsHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.frozenorb.apiv3.handler; - -import io.vertx.core.Handler; -import io.vertx.ext.web.RoutingContext; -import io.vertx.redis.RedisClient; -import io.vertx.redis.RedisOptions; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.actor.Actor; - -public final class MetricsHandler implements Handler { - - private static final RedisClient redisClient = RedisClient.create(APIv3.getVertxInstance(), - new RedisOptions() - .setAddress(APIv3.getConfig().getProperty("redis.address")) - .setPort(Integer.parseInt(APIv3.getConfig().getProperty("redis.port"))) - ); - - @Override - public void handle(RoutingContext ctx) { - redisClient.incr("apiv3:requests:total", (totalUpdateResult) -> { - if (totalUpdateResult.failed()) { - totalUpdateResult.cause().printStackTrace(); - return; - } - - redisClient.incr("apiv3:requests:method:" + ctx.request().method(), (methodUpdateResult) -> { - if (methodUpdateResult.failed()) { - methodUpdateResult.cause().printStackTrace(); - return; - } - - redisClient.incr("apiv3:requests:actor:" + ((Actor) ctx.get("actor")).getType(), (actorUpdateResult) -> { - if (actorUpdateResult.failed()) { - actorUpdateResult.cause().printStackTrace(); - } - }); - }); - }); - - ctx.next(); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java index 9737717..93dd53e 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java @@ -8,6 +8,7 @@ import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.AllArgsConstructor; import lombok.Getter; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -20,7 +21,7 @@ import java.util.UUID; @AllArgsConstructor public final class IpLogEntry { - private static final MongoCollection ipLogCollection = APIv3.getDatabase().getCollection("ipLog", IpLogEntry.class); + private static final MongoCollection ipLogCollection = APIv3.getDatabase().getCollection("ipLog", IpLogEntry.class); @Getter @Id private String id; @Getter private UUID user; @@ -30,11 +31,11 @@ public final class IpLogEntry { @Getter private int uses; public static void findAll(SingleResultCallback> callback) { - ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback); + ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); } public static void findById(String id, SingleResultCallback callback) { - ipLogCollection.find(new Document("_id", id)).first(callback); + ipLogCollection.find(new Document("_id", id)).first(SyncUtils.vertxWrap(callback)); } public static void findByUser(User user, SingleResultCallback> callback) { @@ -42,7 +43,11 @@ public final class IpLogEntry { } public static void findByUser(UUID user, SingleResultCallback> callback) { - ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback); + ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); + } + + public static void findByIp(String userIp, SingleResultCallback> callback) { + ipLogCollection.find(new Document("userIp", userIp)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); } public static void findByUserAndIp(User user, String userIp, SingleResultCallback callback) { @@ -50,7 +55,7 @@ public final class IpLogEntry { } public static void findByUserAndIp(UUID user, String userIp, SingleResultCallback callback) { - ipLogCollection.find(new Document("user", user).append("userIp", userIp)).first(callback); + ipLogCollection.find(new Document("user", user).append("userIp", userIp)).first(SyncUtils.vertxWrap(callback)); } private IpLogEntry() {} // For Jackson @@ -70,11 +75,11 @@ public final class IpLogEntry { } public void insert(SingleResultCallback callback) { - ipLogCollection.insertOne(this, callback); + ipLogCollection.insertOne(this, SyncUtils.vertxWrap(callback)); } public void save(SingleResultCallback callback) { - ipLogCollection.replaceOne(new Document("_id", id), this, callback); + ipLogCollection.replaceOne(new Document("_id", id), this, SyncUtils.vertxWrap(callback)); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/GETMetrics.java b/src/main/java/net/frozenorb/apiv3/route/GETMetrics.java deleted file mode 100644 index 8bbbacf..0000000 --- a/src/main/java/net/frozenorb/apiv3/route/GETMetrics.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.route; - -import io.vertx.core.Handler; -import io.vertx.core.json.JsonObject; -import io.vertx.ext.dropwizard.MetricsService; -import io.vertx.ext.web.RoutingContext; -import net.frozenorb.apiv3.APIv3; - -public final class GETMetrics implements Handler { - - public void handle(RoutingContext ctx) { - MetricsService metricsService = MetricsService.create(APIv3.getVertxInstance()); - JsonObject metrics = metricsService.getMetricsSnapshot(APIv3.getVertxInstance()); - - APIv3.respondJson(ctx, 200, metrics); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java index 4ef87c7..8d4278d 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETIpLogId.java @@ -10,21 +10,33 @@ import net.frozenorb.apiv3.util.ErrorUtils; public final class GETIpLogId implements Handler { public void handle(RoutingContext ctx) { - User.findById(ctx.request().getParam("userId"), (user, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - } else if (user == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); - } else { - IpLogEntry.findByUser(user, (ipLog, error2) -> { - if (error2 != null) { - ErrorUtils.respondInternalError(ctx, error2); - } else { - APIv3.respondJson(ctx, 200, ipLog); - } - }); - } - }); + String search = ctx.request().getParam("id"); + + if (search.length() >= 32) { + User.findById(search, (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else if (user == null) { + ErrorUtils.respondNotFound(ctx, "User", search); + } else { + IpLogEntry.findByUser(user, (ipLog, error2) -> { + if (error2 != null) { + ErrorUtils.respondInternalError(ctx, error2); + } else { + APIv3.respondJson(ctx, 200, ipLog); + } + }); + } + }); + } else { + IpLogEntry.findByIp(search, (ipLogs, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, ipLogs); + } + }); + } } } \ No newline at end of file