Use metadata used for project generation for stats

Closes gh-810
This commit is contained in:
Madhura Bhave 2019-02-06 11:39:05 -08:00
parent 8c02e4093c
commit 1bd84fe0ce
8 changed files with 83 additions and 60 deletions

View File

@ -57,11 +57,9 @@ class InitializrStatsAutoConfiguration {
@Bean
@ConditionalOnBean(InitializrMetadataProvider.class)
public ProjectGenerationStatPublisher projectRequestStatHandler(
InitializrMetadataProvider provider,
RestTemplateBuilder restTemplateBuilder) {
return new ProjectGenerationStatPublisher(
new ProjectRequestDocumentFactory(provider), this.statsProperties,
restTemplateBuilder, statsRetryTemplate());
return new ProjectGenerationStatPublisher(new ProjectRequestDocumentFactory(),
this.statsProperties, restTemplateBuilder, statsRetryTemplate());
}
@Bean

View File

@ -28,7 +28,6 @@ import io.spring.initializr.generator.ProjectFailedEvent;
import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.generator.ProjectRequestEvent;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.util.Agent;
import io.spring.initializr.util.Version;
@ -41,14 +40,8 @@ import org.springframework.util.StringUtils;
*/
public class ProjectRequestDocumentFactory {
private final InitializrMetadataProvider metadataProvider;
public ProjectRequestDocumentFactory(InitializrMetadataProvider metadataProvider) {
this.metadataProvider = metadataProvider;
}
public ProjectRequestDocument createDocument(ProjectRequestEvent event) {
InitializrMetadata metadata = this.metadataProvider.get();
InitializrMetadata metadata = event.getMetadata();
ProjectRequest request = event.getProjectRequest();
ProjectRequestDocument document = new ProjectRequestDocument();

View File

@ -73,8 +73,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
}
private void configureService(StatsProperties properties) {
ProjectRequestDocumentFactory documentFactory = new ProjectRequestDocumentFactory(
createProvider(getMetadata()));
ProjectRequestDocumentFactory documentFactory = new ProjectRequestDocumentFactory();
this.retryTemplate = new RetryTemplate();
this.statPublisher = new ProjectGenerationStatPublisher(documentFactory,
properties, new RestTemplateBuilder(), this.retryTemplate);
@ -133,7 +132,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.andRespond(withStatus(HttpStatus.CREATED)
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -157,7 +156,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -178,7 +177,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -199,7 +198,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -220,7 +219,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -242,7 +241,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -263,7 +262,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -285,7 +284,7 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
@ -303,10 +302,14 @@ class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
.andExpect(method(HttpMethod.POST))
.andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request));
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
this.mockServer.verify();
}
private ProjectGeneratedEvent createProjectGeneratedEvent(ProjectRequest request) {
return new ProjectGeneratedEvent(request, getMetadata());
}
private static String mockResponse(String id, boolean created) {
return "{\"_index\":\"initializr\",\"_type\":\"request\",\"_id\":\"" + id
+ "\",\"_version\":1,\"_shards\""

View File

@ -32,13 +32,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
private final ProjectRequestDocumentFactory factory = new ProjectRequestDocumentFactory(
createProvider(getMetadata()));
private final ProjectRequestDocumentFactory factory = new ProjectRequestDocumentFactory();
@Test
void createDocumentForSimpleProject() {
ProjectRequest request = createProjectRequest();
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getArtifactId()).isEqualTo("demo");
assertThat(document.getBuildSystem()).isEqualTo("maven");
@ -63,7 +62,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithRequestIp() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("x-forwarded-for", "10.0.0.123");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getIp()).isEqualTo("10.0.0.123");
assertThat(document.getClient().getCountry()).isNull();
@ -73,7 +72,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithRequestIpv6() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("x-forwarded-for", "2001:db8:a0b:12f0::1");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getIp()).isEqualTo("2001:db8:a0b:12f0::1");
assertThat(document.getClient().getCountry()).isNull();
@ -84,7 +83,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
ProjectRequest request = createProjectRequest();
request.getParameters().put("cf-connecting-ip", "10.0.0.123");
request.getParameters().put("cf-ipcountry", "BE");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getIp()).isEqualTo("10.0.0.123");
assertThat(document.getClient().getCountry()).isEqualTo("BE");
@ -94,7 +93,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithCloudFlareIpv6() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("cf-connecting-ip", "2001:db8:a0b:12f0::1");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getIp()).isEqualTo("2001:db8:a0b:12f0::1");
assertThat(document.getClient().getCountry()).isNull();
@ -105,7 +104,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
ProjectRequest request = createProjectRequest();
request.getParameters().put("cf-connecting-ip", "10.0.0.123");
request.getParameters().put("x-forwarded-for", "192.168.1.101");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getIp()).isEqualTo("10.0.0.123");
assertThat(document.getClient().getCountry()).isNull();
@ -115,7 +114,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithCloudFlareCountrySetToXX() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("cf-connecting-ip", "Xx"); // case insensitive
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getCountry()).isNull();
}
@ -124,7 +123,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithUserAgent() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("user-agent", "HTTPie/0.8.0");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getId()).isEqualTo("httpie");
assertThat(document.getClient().getVersion()).isEqualTo("0.8.0");
@ -134,7 +133,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentWithUserAgentNoVersion() {
ProjectRequest request = createProjectRequest();
request.getParameters().put("user-agent", "IntelliJ IDEA");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getClient().getId()).isEqualTo("intellijidea");
assertThat(document.getClient().getVersion()).isNull();
@ -144,7 +143,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentInvalidJavaVersion() {
ProjectRequest request = createProjectRequest();
request.setJavaVersion("1.2");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getJavaVersion()).isEqualTo("1.2");
assertThat(document.getErrorState().isInvalid()).isTrue();
@ -159,7 +158,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentInvalidLanguage() {
ProjectRequest request = createProjectRequest();
request.setLanguage("c++");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getLanguage()).isEqualTo("c++");
assertThat(document.getErrorState().isInvalid()).isTrue();
@ -174,7 +173,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentInvalidPackaging() {
ProjectRequest request = createProjectRequest();
request.setPackaging("ear");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getPackaging()).isEqualTo("ear");
assertThat(document.getErrorState().isInvalid()).isTrue();
@ -189,7 +188,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentInvalidType() {
ProjectRequest request = createProjectRequest();
request.setType("ant-project");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getType()).isEqualTo("ant-project");
assertThat(document.getErrorState().isInvalid()).isTrue();
@ -204,7 +203,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
void createDocumentInvalidDependency() {
ProjectRequest request = createProjectRequest();
request.setDependencies(Arrays.asList("web", "invalid", "data-jpa", "invalid-2"));
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
ProjectGeneratedEvent event = createProjectGeneratedEvent(request);
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getDependencies().getValues()).containsExactly("web",
"data-jpa");
@ -222,7 +221,7 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
@Test
void createDocumentWithProjectFailedEvent() {
ProjectRequest request = createProjectRequest();
ProjectFailedEvent event = new ProjectFailedEvent(request,
ProjectFailedEvent event = new ProjectFailedEvent(request, getMetadata(),
new IllegalStateException("my test message"));
ProjectRequestDocument document = this.factory.createDocument(event);
assertThat(document.getErrorState().isInvalid()).isTrue();
@ -234,4 +233,8 @@ class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
assertThat(document.getErrorState().getMessage()).isEqualTo("my test message");
}
private ProjectGeneratedEvent createProjectGeneratedEvent(ProjectRequest request) {
return new ProjectGeneratedEvent(request, getMetadata());
}
}

View File

@ -16,6 +16,8 @@
package io.spring.initializr.generator;
import io.spring.initializr.metadata.InitializrMetadata;
/**
* Event published when an error occurred trying to generate a project.
*
@ -25,8 +27,9 @@ public class ProjectFailedEvent extends ProjectRequestEvent {
private final Exception cause;
public ProjectFailedEvent(ProjectRequest projectRequest, Exception cause) {
super(projectRequest);
public ProjectFailedEvent(ProjectRequest projectRequest, InitializrMetadata metadata,
Exception cause) {
super(projectRequest, metadata);
this.cause = cause;
}

View File

@ -16,6 +16,8 @@
package io.spring.initializr.generator;
import io.spring.initializr.metadata.InitializrMetadata;
/**
* Event published when a new project has been generated successfully.
*
@ -23,8 +25,9 @@ package io.spring.initializr.generator;
*/
public class ProjectGeneratedEvent extends ProjectRequestEvent {
public ProjectGeneratedEvent(ProjectRequest projectRequest) {
super(projectRequest);
public ProjectGeneratedEvent(ProjectRequest projectRequest,
InitializrMetadata metadata) {
super(projectRequest, metadata);
}
}

View File

@ -133,18 +133,19 @@ public class ProjectGenerator {
* @return the Maven POM
*/
public byte[] generateMavenPom(ProjectRequest request) {
InitializrMetadata metadata = this.metadataProvider.get();
try {
Map<String, Object> model = resolveModel(request);
Map<String, Object> model = resolveModel(request, metadata);
if (!isMavenBuild(request)) {
throw new InvalidProjectRequestException("Could not generate Maven pom, "
+ "invalid project type " + request.getType());
}
byte[] content = doGenerateMavenPom(model);
publishProjectGeneratedEvent(request);
publishProjectGeneratedEvent(request, metadata);
return content;
}
catch (InitializrException ex) {
publishProjectFailedEvent(request, ex);
publishProjectFailedEvent(request, metadata, ex);
throw ex;
}
}
@ -155,19 +156,20 @@ public class ProjectGenerator {
* @return the gradle build
*/
public byte[] generateGradleBuild(ProjectRequest request) {
InitializrMetadata metadata = this.metadataProvider.get();
try {
Map<String, Object> model = resolveModel(request);
Map<String, Object> model = resolveModel(request, metadata);
if (!isGradleBuild(request)) {
throw new InvalidProjectRequestException(
"Could not generate Gradle build, " + "invalid project type "
+ request.getType());
}
byte[] content = doGenerateGradleBuild(model);
publishProjectGeneratedEvent(request);
publishProjectGeneratedEvent(request, metadata);
return content;
}
catch (InitializrException ex) {
publishProjectFailedEvent(request, ex);
publishProjectFailedEvent(request, metadata, ex);
throw ex;
}
}
@ -179,14 +181,15 @@ public class ProjectGenerator {
* @return the generated project structure
*/
public File generateProjectStructure(ProjectRequest request) {
InitializrMetadata metadata = this.metadataProvider.get();
try {
Map<String, Object> model = resolveModel(request);
Map<String, Object> model = resolveModel(request, metadata);
File rootDir = generateProjectStructure(request, model);
publishProjectGeneratedEvent(request);
publishProjectGeneratedEvent(request, metadata);
return rootDir;
}
catch (InitializrException ex) {
publishProjectFailedEvent(request, ex);
publishProjectFailedEvent(request, metadata, ex);
throw ex;
}
}
@ -302,13 +305,15 @@ public class ProjectGenerator {
}
}
private void publishProjectGeneratedEvent(ProjectRequest request) {
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request);
private void publishProjectGeneratedEvent(ProjectRequest request,
InitializrMetadata metadata) {
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, metadata);
this.eventPublisher.publishEvent(event);
}
private void publishProjectFailedEvent(ProjectRequest request, Exception cause) {
ProjectFailedEvent event = new ProjectFailedEvent(request, cause);
private void publishProjectFailedEvent(ProjectRequest request,
InitializrMetadata metadata, Exception cause) {
ProjectFailedEvent event = new ProjectFailedEvent(request, metadata, cause);
this.eventPublisher.publishEvent(event);
}
@ -333,12 +338,13 @@ public class ProjectGenerator {
* Resolve the specified {@link ProjectRequest} and return the model to use to
* generate the project.
* @param originalRequest the request to handle
* @param metadata the initializr metadata
* @return a model for that request
*/
protected Map<String, Object> resolveModel(ProjectRequest originalRequest) {
protected Map<String, Object> resolveModel(ProjectRequest originalRequest,
InitializrMetadata metadata) {
Assert.notNull(originalRequest.getBootVersion(), "boot version must not be null");
Map<String, Object> model = new LinkedHashMap<>();
InitializrMetadata metadata = this.metadataProvider.get();
ProjectRequest request = this.requestResolver.resolve(originalRequest, metadata);

View File

@ -16,6 +16,8 @@
package io.spring.initializr.generator;
import io.spring.initializr.metadata.InitializrMetadata;
/**
* Event published when a {@link ProjectRequest} has been processed.
*
@ -27,10 +29,14 @@ public abstract class ProjectRequestEvent {
private final ProjectRequest projectRequest;
private final InitializrMetadata metadata;
private final long timestamp;
protected ProjectRequestEvent(ProjectRequest projectRequest) {
protected ProjectRequestEvent(ProjectRequest projectRequest,
InitializrMetadata metadata) {
this.projectRequest = projectRequest;
this.metadata = metadata;
this.timestamp = System.currentTimeMillis();
}
@ -50,4 +56,12 @@ public abstract class ProjectRequestEvent {
return this.timestamp;
}
/**
* Return the metadata that was used to generate the project.
* @return the metadata
*/
public InitializrMetadata getMetadata() {
return this.metadata;
}
}