<commit_before>package de.retest.recheck.persistence;

import static de.retest.recheck.XmlTransformerUtil.getXmlTransformer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;

import de.retest.recheck.Properties;
import de.retest.recheck.auth.RetestAuthentication;
import de.retest.recheck.persistence.bin.KryoPersistence;
import de.retest.recheck.persistence.xml.XmlFolderPersistence;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CloudPersistence<T extends Persistable> implements Persistence<T> {
	private static final String SERVICE_ENDPOINT = "https://marvin.prod.cloud.retest.org/api/report";
	private final KryoPersistence<T> kryoPersistence = new KryoPersistence<>();
	private final XmlFolderPersistence<T> folderPersistence = new XmlFolderPersistence<>( getXmlTransformer() );

	public static final String RECHECK_API_KEY = "RECHECK_API_KEY";

	@Override
	public void save( final URI identifier, final T element ) throws IOException {
		kryoPersistence.save( identifier, element );
		if ( identifier.getPath().endsWith( Properties.AGGREGATED_TEST_REPORT_FILE_NAME ) ) {
			saveToCloud( identifier );
		}
	}

	private void saveToCloud( final URI identifier ) throws IOException {
		final HttpResponse<String> uploadUrlResponse = getUploadUrl();

		if ( uploadUrlResponse.isSuccess() ) {
			uploadReport( identifier, uploadUrlResponse.getBody() );
		}
	}

	private void uploadReport( final URI identifier, final String url ) throws IOException {
		final HttpResponse<?> uploadResponse = Unirest.put( url ) //
				.header( "x-amz-meta-report-name", Paths.get( identifier ).getFileName().toString() )
				.field( "upload", new File( identifier ) ) //
				.asEmpty();

		if ( uploadResponse.isSuccess() ) {
			log.info( "Successfully uploaded report to rehub" );
		}
	}

	private HttpResponse<String> getUploadUrl() {
		final RetestAuthentication auth = RetestAuthentication.getInstance();
		final String token = String.format( "Bearer %s", auth.getAccessTokenString() );

		return Unirest.post( SERVICE_ENDPOINT ) //
				.header( "Authorization", token )//
				.asString();
	}

	@Override
	public T load( final URI identifier ) throws IOException {
		if ( Paths.get( identifier ).toFile().isDirectory() ) {
			return folderPersistence.load( identifier );
		}
		return kryoPersistence.load( identifier );
	}

}
<commit_msg>Use test class(es) as report name<commit_after>package de.retest.recheck.persistence;

import static de.retest.recheck.XmlTransformerUtil.getXmlTransformer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import de.retest.recheck.Properties;
import de.retest.recheck.auth.RetestAuthentication;
import de.retest.recheck.persistence.bin.KryoPersistence;
import de.retest.recheck.persistence.xml.XmlFolderPersistence;
import de.retest.recheck.report.SuiteReplayResult;
import de.retest.recheck.report.TestReport;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CloudPersistence<T extends Persistable> implements Persistence<T> {
	private static final String SERVICE_ENDPOINT = "https://marvin.prod.cloud.retest.org/api/report";
	private final KryoPersistence<T> kryoPersistence = new KryoPersistence<>();
	private final XmlFolderPersistence<T> folderPersistence = new XmlFolderPersistence<>( getXmlTransformer() );

	public static final String RECHECK_API_KEY = "RECHECK_API_KEY";

	@Override
	public void save( final URI identifier, final T element ) throws IOException {
		kryoPersistence.save( identifier, element );

		if ( isAggregatedReport( identifier ) && element instanceof TestReport ) {
			saveToCloud( identifier, (TestReport) element );
		}
	}

	private boolean isAggregatedReport( final URI identifier ) {
		return identifier.getPath().endsWith( Properties.AGGREGATED_TEST_REPORT_FILE_NAME );
		}

	private List<String> getTestClasses( final TestReport report ) {
		return report.getSuiteReplayResults().stream() //
				.map( SuiteReplayResult::getName ) //
				.collect( Collectors.toList() );
	}

	private void saveToCloud( final URI identifier, final TestReport report ) throws IOException {
		final HttpResponse<String> uploadUrlResponse = getUploadUrl();

		final ReportUploadMetadata metadata = ReportUploadMetadata.builder() //
				.location( identifier ) //
				.uploadUrl( uploadUrlResponse.getBody() ) //
				.testClasses( getTestClasses( report ) ) //
				.build();

		if ( uploadUrlResponse.isSuccess() ) {
			uploadReport( metadata );
		}
	}

	private void uploadReport( final ReportUploadMetadata metadata ) throws IOException {
		final String reportName = metadata.getTestClasses() //
				.stream() //
				.collect( Collectors.joining( ", " ) );

		final HttpResponse<?> uploadResponse = Unirest.put( metadata.getUploadUrl() ) //
				.header( "x-amz-meta-report-name", reportName ) //
				.field( "upload", new File( metadata.getLocation() ) ) //
				.asEmpty();

		if ( uploadResponse.isSuccess() ) {
			log.info( "Successfully uploaded report to rehub" );
		}
	}

	private HttpResponse<String> getUploadUrl() {
		final RetestAuthentication auth = RetestAuthentication.getInstance();
		final String token = String.format( "Bearer %s", auth.getAccessTokenString() );

		return Unirest.post( SERVICE_ENDPOINT ) //
				.header( "Authorization", token )//
				.asString();
	}

	@Override
	public T load( final URI identifier ) throws IOException {
		if ( Paths.get( identifier ).toFile().isDirectory() ) {
			return folderPersistence.load( identifier );
		}
		return kryoPersistence.load( identifier );
	}

}

{'fraction_non_alphanumeric': 0.11300121506682867, 'fraction_numerical': 0.0010414858531504947, 'mean_word_length': 4.7909547738693465, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '937671', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1737, 'n_words': 399}