Skip to content

Commit c03d6dd

Browse files
committed
fix: send 2-letter country abbreviation, not display name (#30)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent a6c56e2 commit c03d6dd

File tree

5 files changed

+150
-280
lines changed

5 files changed

+150
-280
lines changed

src/main/java/com/redhat/devtools/intellij/telemetry/core/service/Country.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class Country {
3131
private static final Logger LOGGER = Logger.getInstance(Country.class);
3232

3333
private static final String TIMEZONES = "/timezones.json";
34-
private static final String COUNTRIES = "/countries.json";
3534
private static final String KEY_COUNTRY = "c";
3635
private static final String KEY_ALTERNATIVE = "a";
3736

@@ -41,8 +40,7 @@ public static Country getInstance() {
4140
return INSTANCE;
4241
}
4342

44-
private Lazy<Map<String, Map<String, String>>> timezones = new Lazy<>(() -> deserialize(TIMEZONES));
45-
private Lazy<Map<String, String>> countries = new Lazy<>(() -> deserialize(COUNTRIES));
43+
private final Lazy<Map<String, Map<String, String>>> timezones = new Lazy<>(() -> deserialize(TIMEZONES));
4644

4745
protected Country() {
4846
// for testing purposes
@@ -55,24 +53,20 @@ public String get(TimeZone timeZone) {
5553
return get(timeZone.getID());
5654
}
5755

58-
private String get(String timezoneId) {
56+
public String get(String timezoneId) {
5957
Map<String, String> timezone = timezones.get().get(timezoneId);
6058
if (timezone == null) {
61-
return timezoneId;
59+
return null;
6260
}
6361
String abbreviation = timezone.get(KEY_COUNTRY);
64-
if (abbreviation == null) {
65-
String alternative = timezone.get(KEY_ALTERNATIVE);
66-
if (alternative == null) {
67-
return timezoneId;
68-
}
69-
return get(alternative);
62+
if (abbreviation != null) {
63+
return abbreviation;
7064
}
71-
return getDisplayName(abbreviation);
72-
}
73-
74-
private String getDisplayName(String abbreviation) {
75-
return countries.get().get(abbreviation);
65+
String alternative = timezone.get(KEY_ALTERNATIVE);
66+
if (alternative == null) {
67+
return null;
68+
}
69+
return get(alternative);
7670
}
7771

7872
private <V> Map<String, V> deserialize(String file) {

src/main/java/com/redhat/devtools/intellij/telemetry/core/service/Environment.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.TimeZone;
2121

2222
public class Environment {
23+
24+
public static final String UNKNOWN_COUNTRY = "ZZ";
25+
2326
private final Application plugin;
2427
private final Application application;
2528
private final Platform platform;
@@ -118,7 +121,7 @@ public Builder timezone(String timezone) {
118121

119122
private void ensureTimezone() {
120123
if (timezone == null) {
121-
timezone(System.getProperty("user.timezone", ""));
124+
timezone(TimeZone.getDefault().getID());
122125
}
123126
}
124127

@@ -145,7 +148,12 @@ private void ensureCountry() {
145148
* Segment won't report countries for incoming requests.
146149
* We thus currently dont have any better solution than use the country in the Locale.
147150
*/
148-
country(Country.getInstance().get(TimeZone.getDefault()));
151+
ensureTimezone();
152+
String country = Country.getInstance().get(timezone);
153+
if (country == null) {
154+
country = UNKNOWN_COUNTRY;
155+
}
156+
country(country);
149157
}
150158
}
151159

@@ -179,7 +187,6 @@ public Environment build() {
179187
ensurePlatform();
180188
ensureCountry();
181189
ensureLocale();
182-
ensurePlatform();
183190
ensureTimezone();
184191
return new Environment(plugin, application, platform, timezone, locale, country);
185192
}

src/main/resources/countries.json

Lines changed: 0 additions & 251 deletions
This file was deleted.

src/test/java/com/redhat/devtools/intellij/telemetry/core/service/CountryTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ void get_should_return_country_for_timezoneId() {
2424
// when
2525
String country = Country.getInstance().get(TimeZone.getTimeZone("Europe/Zurich"));
2626
// then
27-
assertThat(country).isEqualTo("Switzerland");
27+
assertThat(country).isEqualTo("CH");
2828
}
2929

3030
@Test
3131
void get_should_return_null_for_null_timezoneId() {
3232
// given
3333
// when
34-
String country = Country.getInstance().get(null);
34+
String country = Country.getInstance().get((String) null);
3535
// then
3636
assertThat(country).isEqualTo(null);
3737
}
@@ -44,7 +44,7 @@ void get_should_return_timezoneId_for_unknown_timezoneId() {
4444
timeZone.setID("Aldreean/Organa Major");
4545
String country = Country.getInstance().get(timeZone);
4646
// then
47-
assertThat(country).isEqualTo("Aldreean/Organa Major");
47+
assertThat(country).isNull();
4848
}
4949

5050
@Test
@@ -53,16 +53,16 @@ void get_should_return_country_for_timezoneId_with_alternative() {
5353
// when "America/Argentina/ComodRivadavia" -> "a: America/Argentina/Catamarca" -> "c: AR" -> "Argentina"
5454
String country = Country.getInstance().get(TimeZone.getTimeZone("America/Argentina/ComodRivadavia"));
5555
// then
56-
assertThat(country).isEqualTo("Argentina");
56+
assertThat(country).isEqualTo("AR");
5757
}
5858

5959
@Test
60-
void get_should_return_timezoneId_for_timezoneId_without_country_nor_alternative() {
60+
void get_should_return_null_for_timezoneId_without_country_nor_alternative() {
6161
// given
6262
// when
6363
String country = Country.getInstance().get(TimeZone.getTimeZone("CET"));
6464
// then
65-
assertThat(country).isEqualTo("CET");
65+
assertThat(country).isNull();
6666
}
6767

6868
}

0 commit comments

Comments
 (0)