Skip to content

Commit c18ee21

Browse files
authored
feat: MAU: track Hilla version in the usage statistics file (#17013)
1 parent a6a22c1 commit c18ee21

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/ServerInfo.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.vaadin.base.devserver;
1717

1818
import java.io.Serializable;
19+
import java.util.LinkedHashSet;
20+
import java.util.Set;
1921

2022
import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
2123
import com.vaadin.flow.server.Platform;
@@ -31,6 +33,7 @@ public class ServerInfo implements Serializable {
3133
private final String javaVersion;
3234
private final String osVersion;
3335
private final String productName;
36+
private final String hillaVersion;
3437

3538
/**
3639
* Creates a new instance.
@@ -41,6 +44,7 @@ public ServerInfo() {
4144
this.javaVersion = fetchJavaVersion();
4245
this.osVersion = fetchOperatingSystem();
4346
this.productName = fetchProductName();
47+
this.hillaVersion = fetchHillaVersion();
4448
}
4549

4650
private String fetchJavaVersion() {
@@ -59,11 +63,25 @@ private String fetchOperatingSystem() {
5963
}
6064

6165
private String fetchVaadinVersion() {
62-
return Platform.getVaadinVersion().orElse("?");
66+
return isVaadinAvailable() ? Platform.getVaadinVersion().orElse("?")
67+
: "-";
68+
}
69+
70+
private String fetchHillaVersion() {
71+
return EndpointRequestUtil.isHillaAvailable()
72+
? Platform.getHillaVersion().orElse("?")
73+
: "-";
6374
}
6475

6576
private String fetchProductName() {
66-
return EndpointRequestUtil.isHillaAvailable() ? "Hilla" : "Vaadin";
77+
final Set<String> result = new LinkedHashSet<>();
78+
if (isVaadinAvailable()) {
79+
result.add("Vaadin");
80+
}
81+
if (EndpointRequestUtil.isHillaAvailable()) {
82+
result.add("Hilla");
83+
}
84+
return String.join(",", result);
6785
}
6886

6987
public String getFlowVersion() {
@@ -74,6 +92,10 @@ public String getVaadinVersion() {
7492
return vaadinVersion;
7593
}
7694

95+
public String getHillaVersion() {
96+
return hillaVersion;
97+
}
98+
7799
public String getJavaVersion() {
78100
return javaVersion;
79101
}
@@ -82,7 +104,17 @@ public String getOsVersion() {
82104
return osVersion;
83105
}
84106

107+
/**
108+
* Returns the product name.
109+
*
110+
* @return the product name, one of "Vaadin", "Hilla" or "Vaadin,Hilla".
111+
*/
85112
public String getProductName() {
86113
return productName;
87114
}
115+
116+
private static boolean isVaadinAvailable() {
117+
return Thread.currentThread().getContextClassLoader().getResource(
118+
"META-INF/maven/com.vaadin/vaadin-core/pom.properties") != null;
119+
}
88120
}

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/stats/DevModeUsageStatistics.java

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ private void trackGlobalData() {
120120
// Update basic project statistics and save
121121
projectData.setValue(StatisticsConstants.FIELD_FLOW_VERSION,
122122
Version.getFullVersion());
123+
projectData.setValue(StatisticsConstants.FIELD_VAADIN_VERSION,
124+
serverInfo.getVaadinVersion());
125+
projectData.setValue(StatisticsConstants.FIELD_HILLA_VERSION,
126+
serverInfo.getHillaVersion());
123127
projectData.setValue(StatisticsConstants.FIELD_SOURCE_ID,
124128
ProjectHelpers.getProjectSource(projectFolder));
125129
projectData.increment(

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/stats/StatisticsConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class StatisticsConstants {
5151
static final String FIELD_OPERATING_SYSTEM = "os";
5252
static final String FIELD_JVM = "jvm";
5353
static final String FIELD_FLOW_VERSION = "flowVersion";
54+
static final String FIELD_VAADIN_VERSION = "vaadinVersion";
55+
static final String FIELD_HILLA_VERSION = "hillaVersion";
5456
static final String FIELD_SOURCE_ID = "sourceId";
5557
static final String FIELD_PROKEY = "proKey";
5658
static final String FIELD_USER_KEY = "userKey";
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,126 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
116
package com.vaadin.base.devserver;
217

18+
import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Rule;
322
import org.junit.Test;
23+
import org.junit.rules.TemporaryFolder;
24+
import org.mockito.MockedStatic;
25+
import org.mockito.Mockito;
426

5-
import java.util.List;
27+
import java.io.IOException;
28+
import java.net.URL;
29+
import java.net.URLClassLoader;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.util.LinkedList;
633

7-
import static org.junit.Assert.*;
34+
import static org.junit.Assert.assertEquals;
835

936
public class ServerInfoTest {
37+
private ClassLoader oldContextClassLoader;
38+
39+
@Rule
40+
public TemporaryFolder temporary = new TemporaryFolder();
41+
42+
private MockedStatic<EndpointRequestUtil> endpointRequestUtilMockedStatic;
43+
44+
@Before
45+
public void rememberContextClassLoader() throws Exception {
46+
oldContextClassLoader = Thread.currentThread().getContextClassLoader();
47+
fakePlatform(false, false);
48+
}
49+
50+
@After
51+
public void restoreContextClassLoader() {
52+
Thread.currentThread().setContextClassLoader(oldContextClassLoader);
53+
if (endpointRequestUtilMockedStatic != null) {
54+
endpointRequestUtilMockedStatic.close();
55+
endpointRequestUtilMockedStatic = null;
56+
}
57+
}
58+
59+
private void fakePlatform(boolean vaadin, boolean hilla)
60+
throws IOException {
61+
if (endpointRequestUtilMockedStatic != null) {
62+
endpointRequestUtilMockedStatic.close();
63+
endpointRequestUtilMockedStatic = null;
64+
}
65+
66+
final LinkedList<URL> classpath = new LinkedList<>();
67+
if (vaadin) {
68+
final Path vaadinJar = temporary.newFolder().toPath();
69+
final Path pomProperties = vaadinJar.resolve(
70+
"META-INF/maven/com.vaadin/vaadin-core/pom.properties");
71+
Files.createDirectories(pomProperties.getParent());
72+
Files.writeString(pomProperties, "version=24.1.0");
73+
classpath.add(vaadinJar.toUri().toURL());
74+
}
75+
final ClassLoader classLoader = new URLClassLoader(
76+
classpath.toArray(new URL[0]), null);
77+
if (hilla) {
78+
endpointRequestUtilMockedStatic = Mockito
79+
.mockStatic(EndpointRequestUtil.class);
80+
endpointRequestUtilMockedStatic
81+
.when(EndpointRequestUtil::isHillaAvailable)
82+
.thenReturn(true);
83+
}
84+
Thread.currentThread().setContextClassLoader(classLoader);
85+
}
1086

1187
@Test
12-
public void testGetProductName() {
88+
public void testGetProductNameWithNoProducts() {
1389
ServerInfo serverInfo = new ServerInfo();
14-
var productNames = List.of("Vaadin", "Hilla");
15-
// This test is more to prevent regressions
16-
assertTrue("Product name should be there by default",
17-
productNames.contains(serverInfo.getProductName()));
90+
assertEquals("", serverInfo.getProductName());
91+
}
92+
93+
@Test
94+
public void testGetProductNameWithVaadinOnClasspath() throws Exception {
95+
fakePlatform(true, false);
96+
ServerInfo serverInfo = new ServerInfo();
97+
assertEquals("Vaadin", serverInfo.getProductName());
98+
}
99+
100+
@Test
101+
public void testGetProductNameWithHillaOnClasspath() throws Exception {
102+
fakePlatform(false, true);
103+
ServerInfo serverInfo = new ServerInfo();
104+
assertEquals("Hilla", serverInfo.getProductName());
105+
}
106+
107+
@Test
108+
public void testGetProductNameWithBothVaadinAndHillaOnClasspath()
109+
throws Exception {
110+
fakePlatform(true, true);
111+
ServerInfo serverInfo = new ServerInfo();
112+
assertEquals("Vaadin,Hilla", serverInfo.getProductName());
113+
}
114+
115+
@Test
116+
public void hillaVersionIsDashWhenNoHillaOnClasspath() {
117+
final ServerInfo serverInfo = new ServerInfo();
118+
assertEquals("-", serverInfo.getHillaVersion());
119+
}
120+
121+
@Test
122+
public void vaadinVersionIsDashWhenNoVaadinOnClasspath() {
123+
final ServerInfo serverInfo = new ServerInfo();
124+
assertEquals("-", serverInfo.getVaadinVersion());
18125
}
19126
}

0 commit comments

Comments
 (0)