This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
make use of new quote service.
- Loading branch information
JaredGordon
committed
Sep 9, 2015
1 parent
663fe78
commit a724282
Showing
38 changed files
with
698 additions
and
833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...otrader-data/src/main/java/org/springframework/nanotrader/data/cloud/QuoteRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.springframework.nanotrader.data.cloud; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.nanotrader.data.domain.MarketSummary; | ||
| import org.springframework.nanotrader.data.domain.Quote; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import feign.Param; | ||
| import feign.RequestLine; | ||
|
|
||
| @Repository | ||
| public interface QuoteRepository { | ||
|
|
||
| @RequestLine("GET /{symbol}") | ||
| public Quote findBySymbol(@Param(value = "symbol") String symbol); | ||
|
|
||
| @RequestLine("GET /") | ||
| public List<Quote> findAll(); | ||
|
|
||
| @RequestLine("GET /symbols") | ||
| List<String> symbols(); | ||
|
|
||
| @RequestLine("GET /marketSummary") | ||
| MarketSummary marketSummary(); | ||
|
|
||
| @RequestLine("GET /topGainers") | ||
| List<Quote> topGainers(); | ||
|
|
||
| @RequestLine("GET /topLosers") | ||
| List<Quote> topLosers(); | ||
| } |
31 changes: 31 additions & 0 deletions
31
...main/java/org/springframework/nanotrader/data/cloud/QuoteRepositoryConnectionCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package org.springframework.nanotrader.data.cloud; | ||
|
|
||
| import java.net.URL; | ||
|
|
||
| import org.springframework.cloud.service.AbstractServiceConnectorCreator; | ||
| import org.springframework.cloud.service.ServiceConnectorConfig; | ||
|
|
||
| import feign.Feign; | ||
| import feign.gson.GsonEncoder; | ||
|
|
||
| public class QuoteRepositoryConnectionCreator extends | ||
| AbstractServiceConnectorCreator<QuoteRepository, WebServiceInfo> { | ||
|
|
||
| @Override | ||
| public QuoteRepository create(WebServiceInfo serviceInfo, | ||
| ServiceConnectorConfig serviceConnectorConfig) { | ||
| return createRepository(serviceInfo.getUri()); | ||
| } | ||
|
|
||
| public QuoteRepository createRepository(String url) { | ||
| QuoteRepository qr = Feign.builder().encoder(new GsonEncoder()) | ||
| .decoder(new RealTimeQuoteDecoder()) | ||
| .target(QuoteRepository.class, url); | ||
|
|
||
| return qr; | ||
| } | ||
|
|
||
| public QuoteRepository createRepository(URL url) { | ||
| return createRepository(url.toString()); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...a/src/main/java/org/springframework/nanotrader/data/cloud/QuoteWebServiceInfoCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.springframework.nanotrader.data.cloud; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator; | ||
| import org.springframework.cloud.cloudfoundry.Tags; | ||
|
|
||
| public class QuoteWebServiceInfoCreator extends | ||
| CloudFoundryServiceInfoCreator<WebServiceInfo> { | ||
|
|
||
| public static final String QUOTES_PREFIX = "quoteService"; | ||
|
|
||
| public QuoteWebServiceInfoCreator() { | ||
| super(new Tags(), QUOTES_PREFIX); | ||
| } | ||
|
|
||
| @Override | ||
| public WebServiceInfo createServiceInfo(Map<String, Object> serviceData) { | ||
| String id = (String) serviceData.get("name"); | ||
|
|
||
| Map<String, Object> credentials = getCredentials(serviceData); | ||
| String uri = getUriFromCredentials(credentials); | ||
|
|
||
| return new WebServiceInfo(id, uri); | ||
| } | ||
| } |
139 changes: 139 additions & 0 deletions
139
...er-data/src/main/java/org/springframework/nanotrader/data/cloud/RealTimeQuoteDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package org.springframework.nanotrader.data.cloud; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Type; | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import net.minidev.json.JSONArray; | ||
|
|
||
| import org.springframework.nanotrader.data.domain.MarketSummary; | ||
| import org.springframework.nanotrader.data.domain.Quote; | ||
| import org.springframework.util.NumberUtils; | ||
|
|
||
| import com.google.gson.reflect.TypeToken; | ||
| import com.jayway.jsonpath.JsonPath; | ||
| import com.jayway.jsonpath.ReadContext; | ||
|
|
||
| import feign.FeignException; | ||
| import feign.Response; | ||
| import feign.codec.DecodeException; | ||
| import feign.gson.GsonDecoder; | ||
|
|
||
| public class RealTimeQuoteDecoder extends GsonDecoder { | ||
|
|
||
| @Override | ||
| public Object decode(Response response, Type type) throws IOException, | ||
| DecodeException, FeignException { | ||
|
|
||
| Response.Body body = response.body(); | ||
| if (body == null) { | ||
| return null; | ||
| } | ||
|
|
||
| // System.out.println(Util.toString(body.asReader())); | ||
|
|
||
| Type typeOfListOfQuote = new TypeToken<List<Quote>>() { | ||
| }.getType(); | ||
| if (Quote.class.equals(type) || typeOfListOfQuote.equals(type)) { | ||
| return processQuoteBody(body); | ||
| } | ||
|
|
||
| if (MarketSummary.class.equals(type)) { | ||
| return processMarketSummaryBody(body); | ||
| } | ||
|
|
||
| return super.decode(response, type); | ||
| } | ||
|
|
||
| private BigDecimal getBigDecimal(ReadContext ctx, String path) { | ||
| Object o = ctx.read(path); | ||
| if (o == null) { | ||
| return new BigDecimal(0); | ||
| } | ||
| return NumberUtils.parseNumber(o.toString(), BigDecimal.class); | ||
| } | ||
|
|
||
| private Object processMarketSummaryBody(Response.Body body) | ||
| throws IOException { | ||
| ReadContext ctx = JsonPath.parse(body.asInputStream()); | ||
|
|
||
| MarketSummary m = new MarketSummary(); | ||
| m.setChange(getBigDecimal(ctx, "$.change")); | ||
| m.setSummaryDate(new Date()); | ||
| m.setTradeStockIndexAverage(getBigDecimal(ctx, "$.average")); | ||
| m.setTradeStockIndexOpenAverage(getBigDecimal(ctx, "$.open")); | ||
| m.setTradeStockIndexVolume(getBigDecimal(ctx, "$.volume")); | ||
|
|
||
| return m; | ||
| } | ||
|
|
||
| private Object processQuoteBody(Response.Body body) throws IOException { | ||
| ReadContext ctx = JsonPath.parse(body.asInputStream()); | ||
|
|
||
| // is this a single quote, or a collection? | ||
| if (ctx.read("$.").getClass().equals(JSONArray.class)) { | ||
| return quotesFromJson(ctx); | ||
| } | ||
|
|
||
| return quoteFromJson(ctx); | ||
| } | ||
|
|
||
| private Quote quoteFromJson(ReadContext ctx) { | ||
| Quote q = new Quote(); | ||
| q.setChange1(getBigDecimal(ctx, "$.Change")); | ||
| q.setCompanyname(ctx.read("$.Name").toString()); | ||
| q.setHigh(getBigDecimal(ctx, "$.DaysHigh")); | ||
| q.setLow(getBigDecimal(ctx, "$.DaysLow")); | ||
| q.setOpen1(getBigDecimal(ctx, "$.Open")); | ||
| q.setPrice(getBigDecimal(ctx, "$.Price")); | ||
| q.setSymbol(ctx.read("$.Symbol").toString()); | ||
| q.setVolume(getBigDecimal(ctx, "$.Volume")); | ||
|
|
||
| return q; | ||
| } | ||
|
|
||
| private List<Quote> quotesFromJson(ReadContext ctx) { | ||
| ArrayList<Quote> quotes = new ArrayList<Quote>(); | ||
|
|
||
| JSONArray qs = ctx.read("$.."); | ||
| for (int i = 0; i < qs.size(); i++) { | ||
| Quote q = new Quote(); | ||
| q.setChange1(getBigDecimal(ctx, "$..[" + i + "].Change")); | ||
| q.setCompanyname(ctx.read("$..[" + i + "].Name").toString()); | ||
| q.setHigh(getBigDecimal(ctx, "$..[" + i + "].DaysHigh")); | ||
| q.setLow(getBigDecimal(ctx, "$..[" + i + "].DaysLow")); | ||
| q.setOpen1(getBigDecimal(ctx, "$..[" + i + "].PreviousClose")); | ||
| q.setPrice(getBigDecimal(ctx, "$..[" + i + "].Price")); | ||
| q.setSymbol(ctx.read("$..[" + i + "].Symbol").toString()); | ||
| q.setVolume(getBigDecimal(ctx, "$..[" + i + "].Volume")); | ||
|
|
||
| quotes.add(q); | ||
| } | ||
|
|
||
| return quotes; | ||
| } | ||
|
|
||
| public static String formatSymbols(Set<String> symbols) { | ||
| if (symbols == null || symbols.size() < 1) { | ||
| return "()"; | ||
| } | ||
|
|
||
| Object[] o = symbols.toArray(); | ||
|
|
||
| StringBuffer sb = new StringBuffer("("); | ||
| for (int i = 0; i < o.length; i++) { | ||
| sb.append("\'"); | ||
| sb.append(o[i]); | ||
| sb.append("\'"); | ||
| if (i < o.length - 1) { | ||
| sb.append(","); | ||
| } | ||
| } | ||
| sb.append(")"); | ||
| return sb.toString(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...notrader-data/src/main/java/org/springframework/nanotrader/data/cloud/WebServiceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.springframework.nanotrader.data.cloud; | ||
|
|
||
| import org.springframework.cloud.service.UriBasedServiceInfo; | ||
|
|
||
| public class WebServiceInfo extends UriBasedServiceInfo { | ||
| public WebServiceInfo(String id, String url) { | ||
| super(id, url); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.