package com.atlassian.bitbucket.experimental.event.repository;

import com.atlassian.analytics.api.annotations.EventName;
import com.atlassian.bitbucket.event.repository.RepositoryCloneEvent;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.request.RequestContext;
import com.atlassian.event.api.AsynchronousPreferred;

import javax.annotation.Nonnull;
import java.time.Duration;

import static com.google.common.base.Preconditions.checkNotNull;

@AsynchronousPreferred
@EventName("stash.repository.cloned")
public class AnalyticsRepositoryCloneEvent extends RepositoryCloneEvent {

    private final long bytesRead;
    private final long bytesWritten;
    private final long duration;

    public AnalyticsRepositoryCloneEvent(@Nonnull Object source, @Nonnull Repository repository,
                                         @Nonnull RequestContext requestContext) {
        super(source, repository);
        checkNotNull(requestContext, "requestContext");

        bytesRead = requestContext.getBytesRead();
        bytesWritten = requestContext.getBytesWritten();
        duration = requestContext.getDuration().map(Duration::toMillis).orElse(0L);
    }

    public long getBytesRead() {
        return bytesRead;
    }

    public long getBytesWritten() {
        return bytesWritten;
    }

    public long getDuration() {
        return duration;
    }
}
