Skip to content

Commit efcbcd1

Browse files
committed
feat: allow to deduce the country from the timezone (#18)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 25a1186 commit efcbcd1

File tree

5 files changed

+2762
-1
lines changed

5 files changed

+2762
-1
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2021 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.intellij.telemetry.core.service;
12+
13+
import com.fasterxml.jackson.core.type.TypeReference;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import com.intellij.openapi.diagnostic.Logger;
16+
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.TimeZone;
23+
24+
/**
25+
* A class that provides the country for a given Timezone.
26+
* The mapping that this is based on relies on data provided the "countries-and-timezones" project
27+
* at https://github.com/manuelmhtr/countries-and-timezones
28+
*/
29+
public class Country {
30+
31+
private static final Logger LOGGER = Logger.getInstance(Country.class);
32+
33+
private static final String TIMEZONES = "/timezones.json";
34+
private static final String COUNTRIES = "/countries.json";
35+
private static final String KEY_COUNTRY = "c";
36+
private static final String KEY_ALTERNATIVE = "a";
37+
38+
private static final Country INSTANCE = new Country();
39+
40+
public static Country getInstance() {
41+
return INSTANCE;
42+
}
43+
44+
private Lazy<Map<String, Map<String, String>>> timezones = new Lazy<>(() -> deserialize(TIMEZONES));
45+
private Lazy<Map<String, String>> countries = new Lazy<>(() -> deserialize(COUNTRIES));
46+
47+
protected Country() {
48+
// for testing purposes
49+
}
50+
51+
public String get(TimeZone timeZone) {
52+
if (timeZone == null) {
53+
return null;
54+
}
55+
return get(timeZone.getID());
56+
}
57+
58+
private String get(String timezoneId) {
59+
Map<String, String> timezone = timezones.get().get(timezoneId);
60+
if (timezone == null) {
61+
return timezoneId;
62+
}
63+
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);
70+
}
71+
return getDisplayName(abbreviation);
72+
}
73+
74+
private String getDisplayName(String abbreviation) {
75+
return countries.get().get(abbreviation);
76+
}
77+
78+
private <V> Map<String, V> deserialize(String file) {
79+
try {
80+
ObjectMapper mapper = new ObjectMapper();
81+
InputStream input = getClass().getResourceAsStream(file);
82+
TypeReference<Map<String, V>> typeRef = new TypeReference<Map<String, V>>() {};
83+
return mapper.readValue(input, typeRef);
84+
} catch (IOException e) {
85+
LOGGER.warn("Could not load file " + file, e);
86+
return new HashMap<>();
87+
}
88+
}
89+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Arrays;
1919
import java.util.Locale;
20+
import java.util.TimeZone;
2021

2122
public class Environment {
2223
private final Application plugin;
@@ -144,7 +145,7 @@ private void ensureCountry() {
144145
* Segment won't report countries for incoming requests.
145146
* We thus currently dont have any better solution than use the country in the Locale.
146147
*/
147-
country(Locale.getDefault().getDisplayCountry());
148+
country(Country.getInstance().get(TimeZone.getDefault()));
148149
}
149150
}
150151

src/main/resources/countries.json

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
{
2+
"AD": "Andorra",
3+
"AE": "United Arab Emirates",
4+
"AF": "Afghanistan",
5+
"AG": "Antigua and Barbuda",
6+
"AI": "Anguilla",
7+
"AL": "Albania",
8+
"AM": "Armenia",
9+
"AO": "Angola",
10+
"AQ": "Antarctica",
11+
"AR": "Argentina",
12+
"AS": "American Samoa",
13+
"AT": "Austria",
14+
"AU": "Australia",
15+
"AW": "Aruba",
16+
"AX": "Åland Islands",
17+
"AZ": "Azerbaijan",
18+
"BA": "Bosnia and Herzegovina",
19+
"BB": "Barbados",
20+
"BD": "Bangladesh",
21+
"BE": "Belgium",
22+
"BF": "Burkina Faso",
23+
"BG": "Bulgaria",
24+
"BH": "Bahrain",
25+
"BI": "Burundi",
26+
"BJ": "Benin",
27+
"BL": "Saint Barthélemy",
28+
"BM": "Bermuda",
29+
"BN": "Brunei",
30+
"BO": "Bolivia",
31+
"BQ": "Caribbean Netherlands",
32+
"BR": "Brazil",
33+
"BS": "Bahamas",
34+
"BT": "Bhutan",
35+
"BV": "Bouvet Island",
36+
"BW": "Botswana",
37+
"BY": "Belarus",
38+
"BZ": "Belize",
39+
"CA": "Canada",
40+
"CC": "Cocos Islands",
41+
"CD": "Democratic Republic of the Congo",
42+
"CF": "Central African Republic",
43+
"CG": "Republic of the Congo",
44+
"CH": "Switzerland",
45+
"CI": "Ivory Coast",
46+
"CK": "Cook Islands",
47+
"CL": "Chile",
48+
"CM": "Cameroon",
49+
"CN": "China",
50+
"CO": "Colombia",
51+
"CR": "Costa Rica",
52+
"CU": "Cuba",
53+
"CV": "Cabo Verde",
54+
"CW": "Curaçao",
55+
"CX": "Christmas Island",
56+
"CY": "Cyprus",
57+
"CZ": "Czechia",
58+
"DE": "Germany",
59+
"DJ": "Djibouti",
60+
"DK": "Denmark",
61+
"DM": "Dominica",
62+
"DO": "Dominican Republic",
63+
"DZ": "Algeria",
64+
"EC": "Ecuador",
65+
"EE": "Estonia",
66+
"EG": "Egypt",
67+
"EH": "Western Sahara",
68+
"ER": "Eritrea",
69+
"ES": "Spain",
70+
"ET": "Ethiopia",
71+
"FI": "Finland",
72+
"FJ": "Fiji",
73+
"FK": "Falkland Islands",
74+
"FM": "Micronesia",
75+
"FO": "Faroe Islands",
76+
"FR": "France",
77+
"GA": "Gabon",
78+
"GB": "United Kingdom",
79+
"GD": "Grenada",
80+
"GE": "Georgia",
81+
"GF": "French Guiana",
82+
"GG": "Guernsey",
83+
"GH": "Ghana",
84+
"GI": "Gibraltar",
85+
"GL": "Greenland",
86+
"GM": "Gambia",
87+
"GN": "Guinea",
88+
"GP": "Guadeloupe",
89+
"GQ": "Equatorial Guinea",
90+
"GR": "Greece",
91+
"GS": "South Georgia and the South Sandwich Islands",
92+
"GT": "Guatemala",
93+
"GU": "Guam",
94+
"GW": "Guinea-Bissau",
95+
"GY": "Guyana",
96+
"HK": "Hong Kong",
97+
"HM": "Heard Island and McDonald Islands",
98+
"HN": "Honduras",
99+
"HR": "Croatia",
100+
"HT": "Haiti",
101+
"HU": "Hungary",
102+
"ID": "Indonesia",
103+
"IE": "Ireland",
104+
"IL": "Israel",
105+
"IM": "Isle of Man",
106+
"IN": "India",
107+
"IO": "British Indian Ocean Territory",
108+
"IQ": "Iraq",
109+
"IR": "Iran",
110+
"IS": "Iceland",
111+
"IT": "Italy",
112+
"JE": "Jersey",
113+
"JM": "Jamaica",
114+
"JO": "Jordan",
115+
"JP": "Japan",
116+
"KE": "Kenya",
117+
"KG": "Kyrgyzstan",
118+
"KH": "Cambodia",
119+
"KI": "Kiribati",
120+
"KM": "Comoros",
121+
"KN": "Saint Kitts and Nevis",
122+
"KP": "North Korea",
123+
"KR": "South Korea",
124+
"KW": "Kuwait",
125+
"KY": "Cayman Islands",
126+
"KZ": "Kazakhstan",
127+
"LA": "Laos",
128+
"LB": "Lebanon",
129+
"LC": "Saint Lucia",
130+
"LI": "Liechtenstein",
131+
"LK": "Sri Lanka",
132+
"LR": "Liberia",
133+
"LS": "Lesotho",
134+
"LT": "Lithuania",
135+
"LU": "Luxembourg",
136+
"LV": "Latvia",
137+
"LY": "Libya",
138+
"MA": "Morocco",
139+
"MC": "Monaco",
140+
"MD": "Moldova",
141+
"ME": "Montenegro",
142+
"MF": "Saint Martin",
143+
"MG": "Madagascar",
144+
"MH": "Marshall Islands",
145+
"MK": "North Macedonia",
146+
"ML": "Mali",
147+
"MM": "Myanmar",
148+
"MN": "Mongolia",
149+
"MO": "Macao",
150+
"MP": "Northern Mariana Islands",
151+
"MQ": "Martinique",
152+
"MR": "Mauritania",
153+
"MS": "Montserrat",
154+
"MT": "Malta",
155+
"MU": "Mauritius",
156+
"MV": "Maldives",
157+
"MW": "Malawi",
158+
"MX": "Mexico",
159+
"MY": "Malaysia",
160+
"MZ": "Mozambique",
161+
"NA": "Namibia",
162+
"NC": "New Caledonia",
163+
"NE": "Niger",
164+
"NF": "Norfolk Island",
165+
"NG": "Nigeria",
166+
"NI": "Nicaragua",
167+
"NL": "Netherlands",
168+
"NO": "Norway",
169+
"NP": "Nepal",
170+
"NR": "Nauru",
171+
"NU": "Niue",
172+
"NZ": "New Zealand",
173+
"OM": "Oman",
174+
"PA": "Panama",
175+
"PE": "Peru",
176+
"PF": "French Polynesia",
177+
"PG": "Papua New Guinea",
178+
"PH": "Philippines",
179+
"PK": "Pakistan",
180+
"PL": "Poland",
181+
"PM": "Saint Pierre and Miquelon",
182+
"PN": "Pitcairn",
183+
"PR": "Puerto Rico",
184+
"PS": "Palestine",
185+
"PT": "Portugal",
186+
"PW": "Palau",
187+
"PY": "Paraguay",
188+
"QA": "Qatar",
189+
"RE": "Réunion",
190+
"RO": "Romania",
191+
"RS": "Serbia",
192+
"RU": "Russia",
193+
"RW": "Rwanda",
194+
"SA": "Saudi Arabia",
195+
"SB": "Solomon Islands",
196+
"SC": "Seychelles",
197+
"SD": "Sudan",
198+
"SE": "Sweden",
199+
"SG": "Singapore",
200+
"SH": "Saint Helena, Ascension and Tristan da Cunha",
201+
"SI": "Slovenia",
202+
"SJ": "Svalbard and Jan Mayen",
203+
"SK": "Slovakia",
204+
"SL": "Sierra Leone",
205+
"SM": "San Marino",
206+
"SN": "Senegal",
207+
"SO": "Somalia",
208+
"SR": "Suriname",
209+
"SS": "South Sudan",
210+
"ST": "Sao Tome and Principe",
211+
"SV": "El Salvador",
212+
"SX": "Sint Maarten",
213+
"SY": "Syria",
214+
"SZ": "Eswatini",
215+
"TC": "Turks and Caicos Islands",
216+
"TD": "Chad",
217+
"TF": "French Southern Territories",
218+
"TG": "Togo",
219+
"TH": "Thailand",
220+
"TJ": "Tajikistan",
221+
"TK": "Tokelau",
222+
"TL": "Timor-Leste",
223+
"TM": "Turkmenistan",
224+
"TN": "Tunisia",
225+
"TO": "Tonga",
226+
"TR": "Turkey",
227+
"TT": "Trinidad and Tobago",
228+
"TV": "Tuvalu",
229+
"TW": "Taiwan",
230+
"TZ": "Tanzania",
231+
"UA": "Ukraine",
232+
"UG": "Uganda",
233+
"UM": "United States Minor Outlying Islands",
234+
"US": "United States of America",
235+
"UY": "Uruguay",
236+
"UZ": "Uzbekistan",
237+
"VA": "Holy See",
238+
"VC": "Saint Vincent and the Grenadines",
239+
"VE": "Venezuela",
240+
"VG": "Virgin Islands (UK)",
241+
"VI": "Virgin Islands (US)",
242+
"VN": "Vietnam",
243+
"VU": "Vanuatu",
244+
"WF": "Wallis and Futuna",
245+
"WS": "Samoa",
246+
"YE": "Yemen",
247+
"YT": "Mayotte",
248+
"ZA": "South Africa",
249+
"ZM": "Zambia",
250+
"ZW": "Zimbabwe"
251+
}

0 commit comments

Comments
 (0)