Skip to content

Commit

Permalink
- Feature : Adding historical data for population and gdp
Browse files Browse the repository at this point in the history
  • Loading branch information
FarukBraimo committed Jun 18, 2024
1 parent 4ea1c06 commit 07eeec6
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,4 +26,10 @@ public ResponseEntity<InsightResponse> getInsight(@RequestParam("city") String c
InsightResponse response = falconInsightService.getInsight(city);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("/historical")
public ResponseEntity<HistoricalEconomyInsightResponse> getHistoricalInsights(@RequestParam("city") String city) {
HistoricalEconomyInsightResponse response = falconInsightService.getHistoricalInsights(city);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -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<GDPResponse> gdp;
Set<PopulationResponse> population;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
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;
import org.springframework.stereotype.Service;

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;
Expand Down Expand Up @@ -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<String> response = APICaller.getData(url);
if (response != null) {
Object[] object = deserialize(response.body(), Object[].class);
if (object != null) {
List<WordBankObjectResponse> wordBankData = deserializeByTypeReference(serialize(object[1]), new TypeReference<>() {
});
if (wordBankData != null) {
return extractHistoricalInsights(wordBankData);
}
}
}
return null;
}

private HistoricalEconomyInsightResponse extractHistoricalInsights(List<WordBankObjectResponse> wordBankData) {
Set<GDPResponse> gdpResponseList = new TreeSet<>(Comparator.comparing(GDPResponse::getYear));
Set<PopulationResponse> 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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/vodacom/falcon/service/InsightService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/vodacom/falcon/util/FalconDefaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 07eeec6

Please sign in to comment.