Skip to content

Commit

Permalink
Merge pull request #84 from Mogztter/dissociate-builder-client
Browse files Browse the repository at this point in the history
Adds an EventBuilder to dissociate the DSL/builder from the client
  • Loading branch information
jamtur01 authored Nov 20, 2017
2 parents b5f02ff + aaa7cfc commit 57d47af
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package io.riemann.riemann.client;

import io.riemann.riemann.Proto;
import io.riemann.riemann.Proto.Attribute;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventBuilder {
private final Proto.Event.Builder builder;
private final Map<String, String> attributes = new HashMap<String, String>();

public EventBuilder() {
this.builder = Proto.Event.newBuilder();
}

public EventBuilder host(String host) {
if (null == host) {
builder.clearHost();
} else {
builder.setHost(host);
}
return this;
}

public EventBuilder service(String service) {
if (null == service) {
builder.clearService();
} else {
builder.setService(service);
}
return this;
}

public EventBuilder state(String state) {
if (null == state) {
builder.clearState();
} else {
builder.setState(state);
}
return this;
}

public EventBuilder description(String description) {
if (null == description) {
builder.clearDescription();
} else {
builder.setDescription(description);
}
return this;
}

public EventBuilder time() {
builder.clearTime();
builder.clearTimeMicros();
return this;
}

public EventBuilder time(float time) {
builder.setTime((long) time);
builder.setTimeMicros((long) (time * 1000000));
return this;
}

public EventBuilder time(double time) {
builder.setTime((long) time);
builder.setTimeMicros((long) (time * 1000000));
return this;
}

public EventBuilder time(long time) {
builder.setTime(time);
return this;
}

public EventBuilder metric() {
builder.clearMetricF();
builder.clearMetricD();
builder.clearMetricSint64();
return this;
}

public EventBuilder metric(byte metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
return this;
}

public EventBuilder metric(short metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
return this;
}

public EventBuilder metric(int metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
return this;
}

public EventBuilder metric(long metric) {
builder.setMetricSint64(metric);
builder.setMetricF((float) metric);
return this;
}

public EventBuilder metric(float metric) {
builder.setMetricF(metric);
return this;
}

public EventBuilder metric(double metric) {
builder.setMetricD(metric);
builder.setMetricF((float) metric);
return this;
}

public EventBuilder tag(String tag) {
builder.addTags(tag);
return this;
}

public EventBuilder tags(List<String> tags) {
builder.addAllTags(tags);
return this;
}

public EventBuilder tags(String... tags) {
builder.addAllTags(Arrays.asList(tags));
return this;
}

public EventBuilder ttl() {
builder.clearTtl();
return this;
}

public EventBuilder ttl(float ttl) {
builder.setTtl(ttl);
return this;
}

public EventBuilder attribute(String name, String value) {
attributes.put(name, value);
return this;
}

public EventBuilder attributes(Map<String, String> attributes) {
this.attributes.putAll(attributes);
return this;
}

// Returns the compiled Protobuf event for this DSL. Merges in the custom
// attributes map. Can only be called safely once.
public Proto.Event build() {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
Attribute.Builder attribBuilder = Attribute.newBuilder();
attribBuilder.setKey(entry.getKey());
attribBuilder.setValue(entry.getValue());
builder.addAttributes(attribBuilder);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,164 +11,132 @@

public class EventDSL {
public final IRiemannClient client;
public final Event.Builder builder;
public final Map<String, String> attributes = new HashMap<String, String>();
public final EventBuilder event;

public EventDSL(IRiemannClient client) {
this.client = client;
this.builder = Event.newBuilder();
this.event = new EventBuilder();
try {
this.builder.setHost(java.net.InetAddress.getLocalHost().getHostName());
this.event.host(java.net.InetAddress.getLocalHost().getHostName());
} catch (java.net.UnknownHostException e) {
// If we can't get the local host, a null host is perfectly
// acceptable. Caller will know soon enough. :)
}
}

public EventDSL host(String host) {
if (null == host) {
builder.clearHost();
} else {
builder.setHost(host);
}
this.event.host(host);
return this;
}

public EventDSL service(String service) {
if (null == service) {
builder.clearService();
} else {
builder.setService(service);
}
this.event.service(service);
return this;
}

public EventDSL state(String state) {
if (null == state) {
builder.clearState();
} else {
builder.setState(state);
}
this.event.state(state);
return this;
}

public EventDSL description(String description) {
if (null == description) {
builder.clearDescription();
} else {
builder.setDescription(description);
}
this.event.description(description);
return this;
}

public EventDSL time(Null n) {
builder.clearMetricF();
this.event.time();
return this;
}

public EventDSL time(float time) {
builder.setTime((long) time);
builder.setTimeMicros((long) (time * 1000000));
this.event.time(time);
return this;
}

public EventDSL time(double time) {
builder.setTime((long) time);
builder.setTimeMicros((long) (time * 1000000));
this.event.time(time);
return this;
}
public EventDSL time(long time) {
builder.setTime(time);
this.event.time(time);
return this;
}

public EventDSL metric(Null n) {
builder.clearMetricF();
builder.clearMetricD();
builder.clearMetricSint64();
this.event.metric();
return this;
}

public EventDSL metric(byte metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
return this;
this.event.metric(metric);
return this;
}

public EventDSL metric(short metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
return this;
this.event.metric(metric);
return this;
}

public EventDSL metric(int metric) {
builder.setMetricSint64((long) metric);
builder.setMetricF((float) metric);
this.event.metric(metric);
return this;
}

public EventDSL metric(long metric) {
builder.setMetricSint64(metric);
builder.setMetricF((float) metric);
this.event.metric(metric);
return this;
}

public EventDSL metric(float metric) {
builder.setMetricF(metric);
return this;
this.event.metric(metric);
return this;
}

public EventDSL metric(double metric) {
builder.setMetricD(metric);
builder.setMetricF((float) metric);
this.event.metric(metric);
return this;
}

public EventDSL tag(String tag) {
builder.addTags(tag);
this.event.tag(tag);
return this;
}

public EventDSL tags(List<String> tags) {
builder.addAllTags(tags);
this.event.tags(tags);
return this;
}

public EventDSL tags(String... tags) {
builder.addAllTags(Arrays.asList(tags));
this.event.tags(tags);
return this;
}

public EventDSL ttl(Null n) {
builder.clearTtl();
this.event.ttl();
return this;
}

public EventDSL ttl(float ttl) {
builder.setTtl(ttl);
this.event.ttl(ttl);
return this;
}

public EventDSL attribute(String name, String value) {
attributes.put(name, value);
return this;
this.event.attribute(name, value);
return this;
}

public EventDSL attributes(Map<String, String> attributes) {
this.attributes.putAll(attributes);
return this;
this.event.attributes(attributes);
return this;
}

// Returns the compiled Protobuf event for this DSL. Merges in the custom
// attributes map. Can only be called safely once.
public Event build() {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
Attribute.Builder attribBuilder = Attribute.newBuilder();
attribBuilder.setKey(entry.getKey());
attribBuilder.setValue(entry.getValue());
builder.addAttributes(attribBuilder);
}
return builder.build();
return this.event.build();
}

public IPromise<Msg> send() {
Expand Down
Loading

0 comments on commit 57d47af

Please sign in to comment.