com.mercateo.eventstore.reader.EventStatisticsCollector Maven / Gradle / Ivy
/**
* Copyright © 2018 Mercateo AG (http://www.mercateo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mercateo.eventstore.reader;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicLong;
import com.github.msemys.esjc.RecordedEvent;
import com.mercateo.eventstore.connection.EventStream;
import com.mercateo.eventstore.domain.EventStreamId;
import io.vavr.control.Option;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class EventStatisticsCollector {
private final EventStreamState state;
private final Option client;
private final EventStreamId eventStreamId;
private long startTime = System.currentTimeMillis();
private AtomicLong eventCount = new AtomicLong();
private AtomicLong totalSize = new AtomicLong();
private boolean inInitialReplay = true;
public EventStatisticsCollector(EventStream eventStream, Option client) {
eventStreamId = eventStream.getEventStreamId();
this.state = new EventStreamState(eventStreamId);
this.client = client;
log.info("use metrics client: {}", this.client.map(ignore -> "YES").getOrElse(() -> "NO"));
}
public void onLiveProcessingStarted() {
if (inInitialReplay) {
log.info("replayed {} events with {} bytes in {} s", eventCount.get(), totalSize.get(),
getElapsedTimeInSeconds());
client.forEach(c -> c.onReplayFinished(eventStreamId, getEventCount(), getEventRate(), getEventDataRate()));
inInitialReplay = false;
}
}
public void onEvent(RecordedEvent event) {
if (event == null) {
log.warn("onEvent() received null event");
return;
}
final long eventCount = this.eventCount.incrementAndGet();
final String renderedEvent = new String(event.data, Charset.defaultCharset());
if (!inInitialReplay) {
log.info("event {} received with type {}: {}", eventCount, event.eventType, renderedEvent);
} else {
log.trace("event {} received with type {}: {}", eventCount, event.eventType, renderedEvent);
}
totalSize.addAndGet(event.data.length + event.metadata.length);
client.forEach(c -> c.onEvent(eventStreamId));
}
protected long getEventCount() {
return eventCount.get();
}
protected long getTotalSize() {
return totalSize.get();
}
private double getElapsedTimeInSeconds() {
return (System.currentTimeMillis() - startTime) / 1000.0;
}
protected double getEventRate() {
double elapsedTimeInSeconds = getElapsedTimeInSeconds();
return getEventCount() / elapsedTimeInSeconds;
}
protected double getEventDataRate() {
double elapsedTimeInSeconds = getElapsedTimeInSeconds();
return getTotalSize() / elapsedTimeInSeconds;
}
public EventStreamState getStreamState() {
return state;
}
public EventStreamId getEventStreamId() {
return state.getStreamId();
}
}