Skip to content

Commit

Permalink
Use DateTime instead of Date and Calendar in FakeHttpRawMessageGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Schalanda committed Jan 28, 2016
1 parent 5c1f919 commit 6803144
Showing 1 changed file with 56 additions and 85 deletions.
Expand Up @@ -16,69 +16,55 @@
*/ */
package org.graylog2.inputs.random.generators; package org.graylog2.inputs.random.generators;


import com.google.common.base.Function;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.graylog2.plugin.Message; import org.graylog2.plugin.Message;
import org.graylog2.plugin.Tools; import org.graylog2.plugin.Tools;
import org.joda.time.DateTime;


import javax.annotation.Nullable;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Calendar;


import static java.util.Objects.requireNonNull;
import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.DELETE; import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.DELETE;
import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.GET; import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.GET;
import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.POST; import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.POST;
import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.PUT; import static org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator.GeneratorState.Method.PUT;


public class FakeHttpRawMessageGenerator { public class FakeHttpRawMessageGenerator {

private static final Random rand = new Random();
private static final int MAX_WEIGHT = 50; private static final int MAX_WEIGHT = 50;


private final String source;

private final Random rand = new Random(System.currentTimeMillis());


private static final List<Resource> GET_RESOURCES = ImmutableList.of( private static final List<Resource> GET_RESOURCES = ImmutableList.of(
new Resource("/login", "LoginController", "login", 10), new Resource("/login", "LoginController", "login", 10),
new Resource("/users", "UsersController", "index", 2), new Resource("/users", "UsersController", "index", 2),
new Resource("/posts", "PostsController", "index", 40), new Resource("/posts", "PostsController", "index", 40),
new Resource("/posts/45326", "PostsController", "show", 12), new Resource("/posts/45326", "PostsController", "show", 12),
new Resource("/posts/45326/edit", "PostsController", "edit", 1)); new Resource("/posts/45326/edit", "PostsController", "edit", 1));


private static final ImmutableMap<String, Resource> RESOURCE_MAP = Maps.uniqueIndex(GET_RESOURCES, new Function<Resource, String>() { private static final Map<String, Resource> RESOURCE_MAP = Maps.uniqueIndex(GET_RESOURCES, input -> requireNonNull(input).getResource());
@Nullable
@Override
public String apply(@Nullable Resource input) {
if (input == null) {
throw new IllegalStateException();
}
return input.getResource();
}
});


private final List<UserId> USER_IDS = ImmutableList.of( private static final List<UserId> USER_IDS = ImmutableList.of(
new UserId(9001, 10), new UserId(9001, 10),
new UserId(54351, 1), new UserId(54351, 1),
new UserId(74422, 5), new UserId(74422, 5),
new UserId(6476752, 12), new UserId(6476752, 12),
new UserId(6469981, 40)); new UserId(6469981, 40));


private final String source;

public FakeHttpRawMessageGenerator(String source) { public FakeHttpRawMessageGenerator(String source) {
this.source = source; this.source = requireNonNull(source);
} }


public static int rateDeviation(int val, int maxDeviation, Random rand) { public static int rateDeviation(int val, int maxDeviation, Random rand) {
int deviationPercent = rand.nextInt(maxDeviation); final int deviationPercent = rand.nextInt(maxDeviation);

final double x = val / 100.0 * deviationPercent;
double x = val / 100.0 * deviationPercent;


// Add or substract? // Add or substract?
double result = 0; final double result;
if (rand.nextBoolean()) { if (rand.nextBoolean()) {
result = val - x; result = val - x;
} else { } else {
Expand All @@ -92,12 +78,11 @@ public static int rateDeviation(int val, int maxDeviation, Random rand) {
} }
} }



public GeneratorState generateState() { public GeneratorState generateState() {
final GeneratorState generatorState = new GeneratorState(); final GeneratorState generatorState = new GeneratorState();


int methodProb = rand.nextInt(100); final int methodProb = rand.nextInt(100);
int successProb = rand.nextInt(100); final int successProb = rand.nextInt(100);


generatorState.source = source; generatorState.source = source;
generatorState.isSuccessful = (successProb < 98); generatorState.isSuccessful = (successProb < 98);
Expand Down Expand Up @@ -141,8 +126,8 @@ public static Message generateMessage(GeneratorState state) {
return msg; return msg;
} }


private static String shortMessage(Date ingesttime, String method, String resource, int code, int tookMs) { private static String shortMessage(DateTime ingestTime, String method, String resource, int code, int tookMs) {
return ingesttime + " " + method + " " + resource + " [" + code + "]" + " " + tookMs + "ms"; return ingestTime + " " + method + " " + resource + " [" + code + "]" + " " + tookMs + "ms";
} }


private Weighted getWeighted(List<? extends Weighted> list) { private Weighted getWeighted(List<? extends Weighted> list) {
Expand All @@ -156,17 +141,33 @@ private Weighted getWeighted(List<? extends Weighted> list) {
} }
} }


public static Message get(GeneratorState state, private static Map<String, Object> ingestTimeFields(DateTime ingestTime) {
Random rand) { return ImmutableMap.<String, Object>builder()
.put("ingest_time", ingestTime.toString())
.put("ingest_time_epoch", ingestTime.getMillis())
.put("second", ingestTime.getSecondOfMinute())
.put("minute", ingestTime.getMinuteOfHour())
.put("hour", ingestTime.getHourOfDay())
.put("day", ingestTime.getDayOfMonth())
.put("month", ingestTime.getMonthOfYear())
.put("year", ingestTime.getYear())
.build();
}

private static Map<String, Object> resourceFields(Resource resource) {
return ImmutableMap.<String, Object>builder()
.put("resource", resource.getResource())
.put("controller", resource.getController())
.put("action", resource.getAction())
.build();
}

public static Message get(GeneratorState state, Random rand) {
final boolean isSuccessful = state.isSuccessful; final boolean isSuccessful = state.isSuccessful;


Message msg;
int msBase = 100; int msBase = 100;
int deviation = 30; int deviation = 30;
int code = isSuccessful ? 200 : 500; int code = isSuccessful ? 200 : 500;
Date ingestTime = new Date();
Calendar ingestCalendar = Calendar.getInstance();
ingestCalendar.setTime(ingestTime);
if (!isSuccessful && state.isTimeout) { if (!isSuccessful && state.isTimeout) {
// Simulate an internal API timeout from time to time. // Simulate an internal API timeout from time to time.
msBase = 5000; msBase = 5000;
Expand All @@ -177,28 +178,18 @@ public static Message get(GeneratorState state,
msBase = 400; msBase = 400;
} }


int tookMs = rateDeviation(msBase, deviation, rand); final DateTime ingestTime = Tools.iso8601();

msg = new Message(shortMessage(ingestTime, "GET", state.resource, code, tookMs), state.source, Tools.iso8601());
msg.addField("ingest_time", ingestTime.toString());
msg.addField("ingest_time_epoch", ingestTime.getTime());
msg.addField("second", ingestCalendar.get(Calendar.SECOND));
msg.addField("minute", ingestCalendar.get(Calendar.MINUTE));
msg.addField("hour", ingestCalendar.get(Calendar.HOUR));
msg.addField("day", ingestCalendar.get(Calendar.DAY_OF_MONTH));
msg.addField("month", ingestCalendar.get(Calendar.MONTH));
msg.addField("year", ingestCalendar.get(Calendar.YEAR));
msg.addField("http_method", "GET");
msg.addField("http_response_code", code);


final Resource resource = RESOURCE_MAP.get(state.resource); final Resource resource = RESOURCE_MAP.get(state.resource);
msg.addField("resource", resource.getResource()); final int tookMs = rateDeviation(msBase, deviation, rand);
msg.addField("controller", resource.getController());
msg.addField("action", resource.getAction());


final Message msg = new Message(shortMessage(ingestTime, "GET", state.resource, code, tookMs), state.source, Tools.iso8601());
msg.addFields(ingestTimeFields(ingestTime));
msg.addFields(resourceFields(resource));
msg.addField("http_method", "GET");
msg.addField("http_response_code", code);
msg.addField("user_id", state.userId); msg.addField("user_id", state.userId);
msg.addField("took_ms", tookMs); msg.addField("took_ms", tookMs);

return msg; return msg;
} }


Expand All @@ -214,17 +205,12 @@ private static Message successfulPUT(String source) {
return new Message("successful PUT", source, Tools.iso8601()); return new Message("successful PUT", source, Tools.iso8601());
} }


private static Message failedPUT(GeneratorState state, private static Message failedPUT(GeneratorState state, Random rand) {
Random rand) {
final boolean isSuccessful = state.isSuccessful; final boolean isSuccessful = state.isSuccessful;


Message msg;
int msBase = 100; int msBase = 100;
int deviation = 15; int deviation = 15;
int code = isSuccessful ? 200 : 500; int code = isSuccessful ? 200 : 500;
Date ingestTime = new Date();
Calendar ingestCalendar = Calendar.getInstance();
ingestCalendar.setTime(ingestTime);
if (!isSuccessful && state.isTimeout) { if (!isSuccessful && state.isTimeout) {
// Simulate an internal API timeout from time to time. // Simulate an internal API timeout from time to time.
msBase = 5000; msBase = 5000;
Expand All @@ -235,35 +221,21 @@ private static Message failedPUT(GeneratorState state,
msBase = 400; msBase = 400;
} }


int tookMs = rateDeviation(msBase, deviation, rand); final DateTime ingestTime = Tools.iso8601();

msg = new Message(shortMessage(ingestTime, "PUT", state.resource, code, tookMs), state.source, Tools.iso8601());
msg.addField("ingest_time", ingestTime.toString());
msg.addField("ingest_time_epoch", ingestTime.getTime());
msg.addField("second", ingestCalendar.get(Calendar.SECOND));
msg.addField("minute", ingestCalendar.get(Calendar.MINUTE));
msg.addField("hour", ingestCalendar.get(Calendar.HOUR));
msg.addField("day", ingestCalendar.get(Calendar.DAY_OF_MONTH));
msg.addField("month", ingestCalendar.get(Calendar.MONTH));
msg.addField("year", ingestCalendar.get(Calendar.YEAR));
msg.addField("http_method", "PUT");
msg.addField("http_response_code", code);


final Resource resource = RESOURCE_MAP.get(state.resource); final Resource resource = RESOURCE_MAP.get(state.resource);
msg.addField("resource", resource.getResource()); final int tookMs = rateDeviation(msBase, deviation, rand);
msg.addField("controller", resource.getController());
msg.addField("action", resource.getAction());


final Message msg = new Message(shortMessage(ingestTime, "PUT", state.resource, code, tookMs), state.source, Tools.iso8601());
msg.addFields(ingestTimeFields(ingestTime));
msg.addFields(resourceFields(resource));
msg.addField("http_method", "PUT");
msg.addField("http_response_code", code);
msg.addField("user_id", state.userId); msg.addField("user_id", state.userId);
msg.addField("took_ms", tookMs); msg.addField("took_ms", tookMs);

return msg; return msg;
} }


/* private static Message failedPUT(String source) {
return new Message("failed PUT", source, Tools.iso8601());
}
*/
private static Message successfulDELETE(String source) { private static Message successfulDELETE(String source) {
return new Message("successful DELETE", source, Tools.iso8601()); return new Message("successful DELETE", source, Tools.iso8601());
} }
Expand Down Expand Up @@ -345,5 +317,4 @@ public enum Method {
GET, POST, DELETE, PUT GET, POST, DELETE, PUT
} }
} }
} }

0 comments on commit 6803144

Please sign in to comment.