Navigation Menu

Skip to content

Commit

Permalink
added country based analytics to main forecast api
Browse files Browse the repository at this point in the history
  • Loading branch information
prabusb committed May 29, 2016
1 parent 4a2c591 commit 1438e3e
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/main/java/edu/hackathon/controller/AnalyticsController.java
Expand Up @@ -3,7 +3,10 @@
*/
package edu.hackathon.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
Expand All @@ -17,7 +20,10 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import edu.hackathon.rest.domain.AnalyticsWrapper;
import edu.hackathon.rest.domain.BookingAnalytics;
import edu.hackathon.rest.domain.Country;
import edu.hackathon.rest.domain.CountryAnalytics;
import edu.hackathon.service.AnalyticsService;

/**
Expand Down Expand Up @@ -46,10 +52,35 @@ public String healthcheck() {
*/
@RequestMapping("/forecast/{from}/{to}/{type}")
@ResponseBody
public HttpEntity<List<BookingAnalytics>> bookingForecast(@PathVariable String from, @PathVariable String to,
public HttpEntity<AnalyticsWrapper> bookingForecast(@PathVariable String from, @PathVariable String to,
@PathVariable String type) {
return new ResponseEntity<>(
analyticsService.forecastBookingCost(getDateFromString(from), getDateFromString(to)), HttpStatus.OK);

AnalyticsWrapper analyticsWrapper = new AnalyticsWrapper();
List<BookingAnalytics> bookingAnalyticsList = analyticsService.forecastBookingCost(getDateFromString(from), getDateFromString(to));

Map<String, CountryAnalytics> countryDetailsMap = new HashMap<>();
for (BookingAnalytics bookingAnalytics : bookingAnalyticsList) {
if (bookingAnalytics.getCountries() != null) {
for (Country country : bookingAnalytics.getCountries()) {
if (countryDetailsMap.get(country.getCode()) != null) {
CountryAnalytics countryAnalytics = countryDetailsMap.get(country.getCode());
countryAnalytics.setCode(country.getCode());
countryAnalytics.setName(country.getName());
countryAnalytics.updateNewDetails(country);
countryDetailsMap.put(country.getCode(), countryAnalytics);
} else {
CountryAnalytics countryAnalytics = new CountryAnalytics(country.getAncillaryCount(),
country.getBookingCount(), country.getAncillaryPrice(), country.getBookingPrice(), country.getCode(), country.getName());
countryDetailsMap.put(country.getCode(), countryAnalytics);
}

}
}
}

analyticsWrapper.setBookingAnalyticsList(bookingAnalyticsList);
analyticsWrapper.setCountryAnalyticsList(new ArrayList<>(countryDetailsMap.values()));//.toArray());
return new ResponseEntity<>(analyticsWrapper, HttpStatus.OK);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/hackathon/rest/domain/AbstractAnalyticsItem.java
Expand Up @@ -48,6 +48,18 @@ public void setAncillaryCount(BigInteger ancillaryCount) {
this.ancillaryCount = ancillaryCount;
}

public void addAncillaryCount(BigInteger ancillaryCount) {
this.ancillaryCount.add(ancillaryCount);
}

public void updateBookingPrice(Price bookingPrice) {
this.bookingPrice.setAmount(this.bookingPrice.getAmount() + bookingPrice.getAmount());
}

public void updateAncillaryPrice(Price ancillaryPrice) {
this.ancillaryPrice.setAmount(this.ancillaryPrice.getAmount() + ancillaryPrice.getAmount());
}

public Price getAncillaryPrice() {
return ancillaryPrice;
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/edu/hackathon/rest/domain/AnalyticsWrapper.java
@@ -0,0 +1,40 @@
package edu.hackathon.rest.domain;

import java.util.List;

public class AnalyticsWrapper {

private List<BookingAnalytics> bookingAnalyticsList;

private List<CountryAnalytics> countryAnalyticsList;

/**
* @return the bookingAnalyticsList
*/
public List<BookingAnalytics> getBookingAnalyticsList() {
return bookingAnalyticsList;
}

/**
* @param bookingAnalyticsList the bookingAnalyticsList to set
*/
public void setBookingAnalyticsList(List<BookingAnalytics> bookingAnalyticsList) {
this.bookingAnalyticsList = bookingAnalyticsList;
}

/**
* @return the countryAnalyticsList
*/
public List<CountryAnalytics> getCountryAnalyticsList() {
return countryAnalyticsList;
}

/**
* @param countryAnalyticsList the countryAnalyticsList to set
*/
public void setCountryAnalyticsList(List<CountryAnalytics> countryAnalyticsList) {
this.countryAnalyticsList = countryAnalyticsList;
}


}
47 changes: 47 additions & 0 deletions src/main/java/edu/hackathon/rest/domain/CountryAnalytics.java
@@ -0,0 +1,47 @@
package edu.hackathon.rest.domain;

import java.math.BigInteger;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CountryAnalytics extends AbstractAnalyticsItem {

@JsonProperty
private String name;

@JsonProperty
private String code;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public CountryAnalytics(BigInteger ancillaryCount, BigInteger bookingCount, Price ancillaryPrice, Price bookingPrice, String code, String name) {
this.setAncillaryCount(ancillaryCount);
this.setBookingCount(bookingCount);
this.setBookingPrice(bookingPrice);
this.setAncillaryPrice(ancillaryPrice);
this.code = code;
this.name = name;
}

public void updateNewDetails(Country country) {
this.addBookingCount(country.getBookingCount());
this.addAncillaryCount(country.getAncillaryCount());
this.updateAncillaryPrice(country.getAncillaryPrice());
this.updateBookingPrice(country.getBookingPrice());
}

}

0 comments on commit 1438e3e

Please sign in to comment.