com.fluxtion.runtime.dataflow.helpers.Peekers Maven / Gradle / Ivy
package com.fluxtion.runtime.dataflow.helpers;
import com.fluxtion.runtime.annotations.Initialise;
import com.fluxtion.runtime.annotations.NoTriggerReference;
import com.fluxtion.runtime.annotations.Start;
import com.fluxtion.runtime.annotations.builder.Inject;
import com.fluxtion.runtime.partition.LambdaReflection;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.runtime.time.Clock;
public interface Peekers {
/**
* logs the contents of a streamed node to console:
*
* - {} is replaced the to string of the node being peeked
* - %e is replaced with millisecond event time stamp
* - %t is replaced with millisecond wall clock time stamp
* - %p is replaced with millisecond process time stamp
* - %de is replaced with millisecond event time stamp delta from start
* - %dt is replaced with millisecond wall clock time stamp delta from start
* - %dp is replaced with millisecond process time stamp delta from start
*
*/
static LambdaReflection.SerializableConsumer console(String message) {
return new TemplateMessage<>(message, null)::templateAndLogToConsole;
}
static LambdaReflection.SerializableConsumer console(String message, SerializableFunction transform) {
return new TemplateMessage<>(message, transform)::templateAndLogToConsole;
}
static void println(Object message) {
System.out.println(message);
}
class TemplateMessage {
@Inject
@NoTriggerReference
public Clock clock;
private final String message;
private final SerializableFunction transformFunction;
private transient long initialTime;
public TemplateMessage(String message, SerializableFunction transformFunction) {
this.message = message;
this.transformFunction = transformFunction;
}
public TemplateMessage(String message) {
this.message = message;
this.transformFunction = null;
}
@Initialise
public void initialise() {
initialTime = clock.getWallClockTime();
}
@Start
public void start() {
initialTime = clock.getWallClockTime();
}
public void templateAndLogToConsole(T input) {
if (initialTime > clock.getWallClockTime()) {
initialTime = clock.getWallClockTime();
}
String output = transformFunction == null ? input.toString() : transformFunction.apply(input).toString();
System.out.println(
message.replace("{}", output).replace("%e", "" + clock.getEventTime())
.replace("%t", "" + clock.getWallClockTime())
.replace("%p", "" + clock.getProcessTime())
.replace("%de", "" + (clock.getEventTime() - initialTime))
.replace("%dt", "" + (clock.getWallClockTime() - initialTime))
.replace("%dp", "" + (clock.getProcessTime() - initialTime))
);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy