diff --git a/src/main/java/com/vodacom/falcon/controller/InsightController.java b/src/main/java/com/vodacom/falcon/controller/InsightController.java index 7a8024e..15f9b36 100644 --- a/src/main/java/com/vodacom/falcon/controller/InsightController.java +++ b/src/main/java/com/vodacom/falcon/controller/InsightController.java @@ -1,5 +1,6 @@ package com.vodacom.falcon.controller; +import com.vodacom.falcon.model.response.HistoricalEconomyInsightResponse; import com.vodacom.falcon.model.response.InsightResponse; import com.vodacom.falcon.service.InsightService; import lombok.RequiredArgsConstructor; @@ -25,4 +26,10 @@ public ResponseEntity getInsight(@RequestParam("city") String c InsightResponse response = falconInsightService.getInsight(city); return new ResponseEntity<>(response, HttpStatus.OK); } + + @GetMapping("/historical") + public ResponseEntity getHistoricalInsights(@RequestParam("city") String city) { + HistoricalEconomyInsightResponse response = falconInsightService.getHistoricalInsights(city); + return new ResponseEntity<>(response, HttpStatus.OK); + } } diff --git a/src/main/java/com/vodacom/falcon/model/response/HistoricalEconomyInsightResponse.java b/src/main/java/com/vodacom/falcon/model/response/HistoricalEconomyInsightResponse.java new file mode 100644 index 0000000..ef5f72f --- /dev/null +++ b/src/main/java/com/vodacom/falcon/model/response/HistoricalEconomyInsightResponse.java @@ -0,0 +1,22 @@ +package com.vodacom.falcon.model.response; + +import com.vodacom.falcon.model.response.workdbank.GDPResponse; +import com.vodacom.falcon.model.response.workdbank.PopulationResponse; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.Set; + + +@Getter +@Setter +@AllArgsConstructor +@Builder +@NoArgsConstructor +public class HistoricalEconomyInsightResponse { + Set gdp; + Set population; +} diff --git a/src/main/java/com/vodacom/falcon/model/response/workdbank/GDPResponse.java b/src/main/java/com/vodacom/falcon/model/response/workdbank/GDPResponse.java new file mode 100644 index 0000000..9f5039d --- /dev/null +++ b/src/main/java/com/vodacom/falcon/model/response/workdbank/GDPResponse.java @@ -0,0 +1,17 @@ +package com.vodacom.falcon.model.response.workdbank; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +import java.math.BigDecimal; + +@Getter +@Setter +@NoArgsConstructor +@ToString +public class GDPResponse { + private Long year; + private BigDecimal value; +} diff --git a/src/main/java/com/vodacom/falcon/model/response/workdbank/PopulationResponse.java b/src/main/java/com/vodacom/falcon/model/response/workdbank/PopulationResponse.java new file mode 100644 index 0000000..e95e521 --- /dev/null +++ b/src/main/java/com/vodacom/falcon/model/response/workdbank/PopulationResponse.java @@ -0,0 +1,15 @@ +package com.vodacom.falcon.model.response.workdbank; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@Getter +@Setter +@NoArgsConstructor +@ToString +public class PopulationResponse { + private Long year; + private Long value; +} diff --git a/src/main/java/com/vodacom/falcon/service/EconomyInsightService.java b/src/main/java/com/vodacom/falcon/service/EconomyInsightService.java index 5720117..51733f8 100644 --- a/src/main/java/com/vodacom/falcon/service/EconomyInsightService.java +++ b/src/main/java/com/vodacom/falcon/service/EconomyInsightService.java @@ -3,6 +3,9 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.vodacom.falcon.client.APICaller; import com.vodacom.falcon.model.response.EconomyInsightResponse; +import com.vodacom.falcon.model.response.HistoricalEconomyInsightResponse; +import com.vodacom.falcon.model.response.workdbank.GDPResponse; +import com.vodacom.falcon.model.response.workdbank.PopulationResponse; import com.vodacom.falcon.model.response.workdbank.WordBankObjectResponse; import com.vodacom.falcon.util.FalconDefaults; import lombok.extern.slf4j.Slf4j; @@ -10,7 +13,15 @@ import java.math.BigDecimal; import java.net.http.HttpResponse; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; import static com.vodacom.falcon.util.FalconDefaults.WB_GDP_INDICATOR_PARAM; import static com.vodacom.falcon.util.FalconDefaults.WB_POPULATION_INDICATOR_PARAM; @@ -52,4 +63,49 @@ public EconomyInsightResponse getEconomyInsight(String countryCode, Integer date } return null; } + + public HistoricalEconomyInsightResponse getHistoricalEconomyInsight(String countryCode, String date) { + String url = String.format("%s/v2/country/%s/indicator/%s;%s/?source=2&date=%s&format=json", FalconDefaults.WORD_BANK_API_BASE_URL, countryCode.toLowerCase(), WB_POPULATION_INDICATOR_PARAM, FalconDefaults.WB_GDP_INDICATOR_PARAM, date); + HttpResponse response = APICaller.getData(url); + if (response != null) { + Object[] object = deserialize(response.body(), Object[].class); + if (object != null) { + List wordBankData = deserializeByTypeReference(serialize(object[1]), new TypeReference<>() { + }); + if (wordBankData != null) { + return extractHistoricalInsights(wordBankData); + } + } + } + return null; + } + + private HistoricalEconomyInsightResponse extractHistoricalInsights(List wordBankData) { + Set gdpResponseList = new TreeSet<>(Comparator.comparing(GDPResponse::getYear)); + Set populationResponseList = new TreeSet<>(Comparator.comparing(PopulationResponse::getYear)); + HistoricalEconomyInsightResponse history = new HistoricalEconomyInsightResponse(); + wordBankData.forEach(f -> { + GDPResponse gdpResponse = new GDPResponse(); + PopulationResponse populationResponse = new PopulationResponse(); + + switch (f.getIndicator().id()) { + case WB_POPULATION_INDICATOR_PARAM -> { + populationResponse.setValue(Long.valueOf(f.getValue())); + populationResponse.setYear(Long.valueOf(f.getDate())); + populationResponseList.add(populationResponse); + } + case WB_GDP_INDICATOR_PARAM -> { + gdpResponse.setYear(Long.valueOf(f.getDate())); + gdpResponse.setValue(new BigDecimal(f.getValue())); + gdpResponseList.add(gdpResponse); + } + default -> log.info("Unrecognized indicator {}", f.getIndicator()); + } + } + ); + + history.setPopulation(populationResponseList); + history.setGdp(gdpResponseList); + return history; + } } diff --git a/src/main/java/com/vodacom/falcon/service/InsightService.java b/src/main/java/com/vodacom/falcon/service/InsightService.java index e6b4a46..2030e8a 100644 --- a/src/main/java/com/vodacom/falcon/service/InsightService.java +++ b/src/main/java/com/vodacom/falcon/service/InsightService.java @@ -2,6 +2,7 @@ import com.vodacom.falcon.model.response.EconomyInsightResponse; import com.vodacom.falcon.model.response.ExchangeRateResponse; +import com.vodacom.falcon.model.response.HistoricalEconomyInsightResponse; import com.vodacom.falcon.model.response.InsightResponse; import com.vodacom.falcon.model.response.MetadataResponse; import com.vodacom.falcon.model.response.WeatherForecastResponse; @@ -15,9 +16,11 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Objects; import static com.vodacom.falcon.util.FalconDefaults.WB_FILTER_DATE; +import static com.vodacom.falcon.util.FalconDefaults.WB_RANGE_FILTER_DATE; @Service @Slf4j @@ -73,4 +76,17 @@ public InsightResponse getInsight(String city) { .exchangeRate(exchangeRateResponse) .build(); } + + + public HistoricalEconomyInsightResponse getHistoricalInsights(String city) { + log.info("Getting insights for {}", city); + + String encodedCity = URLEncoder.encode(city, StandardCharsets.UTF_8); + String countryCode = countryMetadataService.getCountryCode(encodedCity); + + if (Objects.nonNull(countryCode)) { + return economyInsightService.getHistoricalEconomyInsight(countryCode, WB_RANGE_FILTER_DATE); + } + return null; + } } diff --git a/src/main/java/com/vodacom/falcon/util/FalconDefaults.java b/src/main/java/com/vodacom/falcon/util/FalconDefaults.java index 28809a4..2281bcc 100644 --- a/src/main/java/com/vodacom/falcon/util/FalconDefaults.java +++ b/src/main/java/com/vodacom/falcon/util/FalconDefaults.java @@ -4,6 +4,7 @@ public class FalconDefaults { public static final String WB_POPULATION_INDICATOR_PARAM = "SP.POP.TOTL"; public static final String WB_GDP_INDICATOR_PARAM = "NY.GDP.MKTP.CD"; public static final Integer WB_FILTER_DATE = 2022; // Last year of stable data + public static final String WB_RANGE_FILTER_DATE = "2012:2022"; // Last year of stable data public static final String WORD_BANK_API_BASE_URL = "http://api.worldbank.org"; public static final String OPEN_WEATHER_API_BASE_URL = "https://api.openweathermap.org"; public static final String MAIN_EXCHANGE_RATE_API_BASE_URL = "http://api.exchangeratesapi.io";